[PLUGIN][ActivityPub] Fix several issues with target and notifications inserted by AP

This commit is contained in:
2022-02-13 22:55:54 +00:00
parent 56c884026f
commit 66323c5a73
9 changed files with 114 additions and 52 deletions

View File

@@ -1,6 +1,6 @@
<?php
declare(strict_types = 1);
declare(strict_types=1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
@@ -32,16 +32,17 @@ declare(strict_types = 1);
namespace Plugin\ActivityPub\Util\Model;
use _PHPStan_76800bfb5\Nette\NotImplementedException;
use ActivityPhp\Type\AbstractObject;
use App\Core\DB\DB;
use App\Entity\Activity as GSActivity;
use App\Util\Exception\NotImplementedException;
use DateTime;
use InvalidArgumentException;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActivity;
/**
* This class handles translation between JSON and ActivityPub Activities
* This class handles translation between JSON and ActivityPub Creates
*
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
@@ -52,6 +53,20 @@ class ActivityCreate extends Activity
{
if ($type_object instanceof AbstractObject) {
if ($type_object->get('type') === 'Note') {
$actual_to = array_flip(is_string($type_object->get('to')) ? [$type_object->get('to')] : $type_object->get('to'));
$actual_cc = array_flip(is_string($type_object->get('cc')) ? [$type_object->get('cc')] : $type_object->get('cc'));
foreach (is_string($type_activity->get('to')) ? [$type_activity->get('to')] : $type_activity->get('to') as $to) {
if ($to !== 'https://www.w3.org/ns/activitystreams#Public') {
$actual_to[$to] = true;
}
}
foreach (is_string($type_activity->get('cc')) ? [$type_activity->get('cc')] : $type_activity->get('cc') as $cc) {
if ($cc !== 'https://www.w3.org/ns/activitystreams#Public') {
$actual_cc[$cc] = true;
}
}
$type_object->set('to', array_keys($actual_to));
$type_object->set('cc', array_keys($actual_cc));
$note = Note::fromJson($type_object, ['test_authority' => true, 'actor_uri' => $type_activity->get('actor'), 'actor' => $actor, 'actor_id' => $actor->getId()]);
} else {
throw new NotImplementedException('ActivityPub plugin can only handle Create with objects of type Note.');
@@ -59,26 +74,27 @@ class ActivityCreate extends Activity
} elseif ($type_object instanceof \App\Entity\Note) {
$note = $type_object;
} else {
throw new \http\Exception\InvalidArgumentException('Create{:Object} should be either an AbstractObject or a Note.');
throw new InvalidArgumentException('Create{:Object} should be either an AbstractObject or a Note.');
}
// Store Activity
$act = GSActivity::create([
'actor_id' => $actor->getId(),
'verb' => 'create',
'actor_id' => $actor->getId(),
'verb' => 'create',
'object_type' => 'note',
'object_id' => $note->getId(),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'source' => 'ActivityPub',
'object_id' => $note->getId(),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'source' => 'ActivityPub',
]);
DB::persist($act);
// Store ActivityPub Activity
$ap_act = ActivitypubActivity::create([
'activity_id' => $act->getId(),
'activity_id' => $act->getId(),
'activity_uri' => $type_activity->get('id'),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'modified' => new DateTime(),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'modified' => new DateTime(),
]);
DB::persist($ap_act);
$ap_act->setObjectMentionIds($note->_object_mentions_ids);
return $ap_act;
}
}

View File

@@ -42,7 +42,7 @@ use InvalidArgumentException;
use Plugin\ActivityPub\Entity\ActivitypubActivity;
/**
* This class handles translation between JSON and ActivityPub Activities
* This class handles translation between JSON and ActivityPub Follows
*
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later

View File

@@ -39,6 +39,8 @@ use App\Core\DB\DB;
use App\Core\Event;
use App\Core\GSFile;
use App\Core\HTTPClient;
use App\Util\HTML;
use Plugin\ActivityPub\Util\Explorer;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Router\Router;
@@ -114,8 +116,10 @@ class Note extends Model
$source = $options['source'] ?? 'ActivityPub';
$type_note = \is_string($json) ? self::jsonToType($json) : $json;
$actor = null;
$actor_id = null;
$actor = null;
$to = $type_note->has('to') ? (is_string($type_note->get('to')) ? [$type_note->get('to')] : $type_note->get('to')) : [];
$cc = $type_note->has('cc') ? (is_string($type_note->get('cc')) ? [$type_note->get('cc')] : $type_note->get('cc')) : [];
if ($json instanceof AbstractObject
&& \array_key_exists('test_authority', $options)
&& $options['test_authority']
@@ -140,7 +144,7 @@ class Note extends Model
'is_local' => false,
'created' => new DateTime($type_note->get('published') ?? 'now'),
'content' => $type_note->get('content') ?? null,
'rendered' => null,
'rendered' => $type_note->has('content') ? HTML::sanitize($type_note->get('content')) : null,
'content_type' => 'text/html',
'language_id' => $type_note->get('contentLang') ?? null,
'url' => $type_note->get('url') ?? $type_note->get('id'),
@@ -149,17 +153,6 @@ class Note extends Model
'modified' => new DateTime(),
'source' => $source,
];
if ($map['content'] !== null) {
$mentions = [];
Event::handle('RenderNoteContent', [
$map['content'],
$map['content_type'],
&$map['rendered'],
$actor,
$map['language_id'],
&$mentions,
]);
}
if (!\is_null($map['language_id'])) {
$map['language_id'] = Language::getByLocale($map['language_id'])->getId();
@@ -168,10 +161,10 @@ class Note extends Model
}
// Scope
if (\in_array('https://www.w3.org/ns/activitystreams#Public', $type_note->get('to'))) {
if (\in_array('https://www.w3.org/ns/activitystreams#Public', $to)) {
// Public: Visible for all, shown in public feeds
$map['scope'] = VisibilityScope::EVERYWHERE;
} elseif (\in_array('https://www.w3.org/ns/activitystreams#Public', $type_note->get('cc'))) {
} elseif (\in_array('https://www.w3.org/ns/activitystreams#Public', $cc)) {
// Unlisted: Visible for all but not shown in public feeds
// It isn't the note that dictates what feed is shown in but the feed, it only dictates who can access it.
$map['scope'] = VisibilityScope::EVERYWHERE;
@@ -186,20 +179,30 @@ class Note extends Model
}
$object_mentions_ids = [];
foreach ([$type_note->get('to'), $type_note->get('cc')] as $target) {
foreach ($target as $to) {
if ($to === 'https://www.w3.org/ns/activitystreams#Public') {
continue;
}
try {
$actor = ActivityPub::getActorByUri($to);
if ($actor->getIsLocal()) {
$object_mentions_ids[] = $actor->getId();
}
// TODO: If group, set note's scope as Group
} catch (Exception $e) {
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
foreach ($to as $target) {
if ($target === 'https://www.w3.org/ns/activitystreams#Public') {
continue;
}
try {
$actor = ActivityPub::getActorByUri($target);
$object_mentions_ids[$actor->getId()] = $target;
// If $to is a group, set note's scope as Group
if ($actor->isGroup()) {
$map['scope'] = VisibilityScope::GROUP;
}
} catch (Exception $e) {
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
}
}
foreach ($cc as $target) {
if ($target === 'https://www.w3.org/ns/activitystreams#Public') {
continue;
}
try {
$actor = ActivityPub::getActorByUri($target);
$object_mentions_ids[$actor->getId()] = $target;
} catch (Exception $e) {
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
}
}
@@ -243,10 +246,20 @@ class Note extends Model
foreach ($type_note->get('tag') as $ap_tag) {
switch ($ap_tag->get('type')) {
case 'Mention':
case 'Group':
try {
$actor = ActivityPub::getActorByUri($ap_tag->get('href'));
if ($actor->getIsLocal()) {
$object_mentions_ids[] = $actor->getId();
$object_mentions_ids[$actor->getId()] = $ap_tag->get('href');
} catch (Exception $e) {
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
}
break;
case 'Collection':
$explorer = new Explorer();
try {
$actors = $explorer->lookup($ap_tag->get('href'));
foreach($actors as $actor) {
$object_mentions_ids[$actor->getId()] = $ap_tag->get('href');
}
} catch (Exception $e) {
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
@@ -270,9 +283,12 @@ class Note extends Model
break;
}
}
$obj->setObjectMentionsIds(array_unique($object_mentions_ids));
// The content would be non-sanitized text/html
Event::handle('ProcessNoteContent', [$obj, $obj->getRendered(), $obj->getContentType(), $process_note_content_extra_args = ['TagProcessed' => true]]);
Event::handle('ProcessNoteContent', [$obj, $obj->getRendered(), $obj->getContentType(), $process_note_content_extra_args = ['TagProcessed' => true, 'ignoreLinks' => $object_mentions_ids]]);
$object_mentions_ids = array_keys($object_mentions_ids);
$obj->setObjectMentionsIds($object_mentions_ids);
if ($processed_attachments !== []) {
foreach ($processed_attachments as [$a, $fname]) {
@@ -338,7 +354,9 @@ class Note extends Model
$attr['to'] = []; // Will be filled later
$attr['cc'] = [];
break;
case VisibilityScope::GROUP: // Will have the group in the To
case VisibilityScope::GROUP:
// Will have the group in the To
// no break
case VisibilityScope::COLLECTION:
// Since we don't support sending unlisted/followers-only
// notices, arriving here means we're instead answering to that type