[PLUGIN][ActivityPub] Support and federate scopes

This commit is contained in:
Diogo Peralta Cordeiro 2021-12-26 05:51:59 +00:00
parent 21f585ef7e
commit 91f8c86efa
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 85 additions and 19 deletions

View File

@ -172,8 +172,10 @@ class Activity extends Model
]; ];
$attr['object'] = ($attr['type'] === 'Create') ? self::jsonToType(Model::toJson($object->getObject())) : ActivityPub::getUriByObject($object->getObject()); $attr['object'] = ($attr['type'] === 'Create') ? self::jsonToType(Model::toJson($object->getObject())) : ActivityPub::getUriByObject($object->getObject());
$attr['to'] = array_unique(array_merge($attr['to'], $attr['object']->get('to'))); if (!is_string($attr['object'])) {
$attr['cc'] = array_unique(array_merge($attr['cc'], $attr['object']->get('cc'))); $attr['to'] = array_unique(array_merge($attr['to'], $attr['object']->get('to')));
$attr['cc'] = array_unique(array_merge($attr['cc'], $attr['object']->get('cc')));
}
$type = self::jsonToType($attr); $type = self::jsonToType($attr);
Event::handle('ActivityPubAddActivityStreamsTwoData', [$type->get('type'), &$type]); Event::handle('ActivityPubAddActivityStreamsTwoData', [$type->get('type'), &$type]);

View File

@ -39,6 +39,7 @@ use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\GSFile; use App\Core\GSFile;
use App\Core\HTTPClient; use App\Core\HTTPClient;
use App\Core\VisibilityScope;
use Component\Language\Entity\Language; use Component\Language\Entity\Language;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
@ -67,6 +68,10 @@ use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use function array_key_exists;
use function is_null;
use function is_string;
use const PHP_URL_HOST;
/** /**
* This class handles translation between JSON and GSNotes * This class handles translation between JSON and GSNotes
@ -93,8 +98,8 @@ class Note extends Model
{ {
$handleInReplyTo = function (AbstractObject|string $type_note): ?int { $handleInReplyTo = function (AbstractObject|string $type_note): ?int {
try { try {
$parent_note = \is_null($type_note->get('inReplyTo')) ? null : ActivityPub::getObjectByUri($type_note->get('inReplyTo'), try_online: true); $parent_note = is_null($type_note->get('inReplyTo')) ? null : ActivityPub::getObjectByUri($type_note->get('inReplyTo'), try_online: true);
if ($parent_note instanceof \App\Entity\Note) { if ($parent_note instanceof GSNote) {
return $parent_note->getId(); return $parent_note->getId();
} elseif ($parent_note instanceof Type\AbstractObject && $parent_note->get('type') === 'Note') { } elseif ($parent_note instanceof Type\AbstractObject && $parent_note->get('type') === 'Note') {
return self::fromJson($parent_note)->getId(); return self::fromJson($parent_note)->getId();
@ -111,13 +116,13 @@ class Note extends Model
}; };
$source = $options['source'] ?? 'ActivityPub'; $source = $options['source'] ?? 'ActivityPub';
$type_note = \is_string($json) ? self::jsonToType($json) : $json; $type_note = is_string($json) ? self::jsonToType($json) : $json;
$actor = null; $actor = null;
$actor_id = null; $actor_id = null;
if ($json instanceof AbstractObject if ($json instanceof AbstractObject
&& \array_key_exists('test_authority', $options) && array_key_exists('test_authority', $options)
&& $options['test_authority'] && $options['test_authority']
&& \array_key_exists('actor_uri', $options) && array_key_exists('actor_uri', $options)
) { ) {
$actor_uri = $options['actor_uri']; $actor_uri = $options['actor_uri'];
if ($actor_uri !== $type_note->get('attributedTo')) { if ($actor_uri !== $type_note->get('attributedTo')) {
@ -130,7 +135,7 @@ class Note extends Model
} }
} }
if (\is_null($actor_id)) { if (is_null($actor_id)) {
$actor = ActivityPub::getActorByUri($type_note->get('attributedTo')); $actor = ActivityPub::getActorByUri($type_note->get('attributedTo'));
$actor_id = $actor->getId(); $actor_id = $actor->getId();
} }
@ -159,14 +164,49 @@ class Note extends Model
]); ]);
} }
$obj = new GSNote(); if (!is_null($map['language_id'])) {
if (!\is_null($map['language_id'])) {
$map['language_id'] = Language::getByLocale($map['language_id'])->getId(); $map['language_id'] = Language::getByLocale($map['language_id'])->getId();
} else { } else {
$map['language_id'] = null; $map['language_id'] = null;
} }
// Scope
if (in_array('https://www.w3.org/ns/activitystreams#Public', $type_note->get('to'))) {
// Public: Visible for all, shown in public feeds
$map['scope'] = VisibilityScope::PUBLIC;
} elseif (\in_array('https://www.w3.org/ns/activitystreams#Public', $type_note->get('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::PUBLIC;
} else {
// Either Followers-only or Direct
if ($type_note->get('directMessage') ?? false // Is DM explicitly?
|| (empty($type_note->get('cc')))) { // Only has TO targets
$map['scope'] = VisibilityScope::MESSAGE;
} else { // Then is collection
$map['scope'] = VisibilityScope::COLLECTION;
}
}
$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]);
}
}
}
$obj = new GSNote();
foreach ($map as $prop => $val) { foreach ($map as $prop => $val) {
$set = Formatting::snakeCaseToCamelCase("set_{$prop}"); $set = Formatting::snakeCaseToCamelCase("set_{$prop}");
$obj->{$set}($val); $obj->{$set}($val);
@ -203,7 +243,6 @@ class Note extends Model
// Assign conversation to this note // Assign conversation to this note
Conversation::assignLocalConversation($obj, $reply_to); Conversation::assignLocalConversation($obj, $reply_to);
$object_mentions_ids = [];
foreach ($type_note->get('tag') as $ap_tag) { foreach ($type_note->get('tag') as $ap_tag) {
switch ($ap_tag->get('type')) { switch ($ap_tag->get('type')) {
case 'Mention': case 'Mention':
@ -219,7 +258,7 @@ class Note extends Model
case 'Hashtag': case 'Hashtag':
$match = ltrim($ap_tag->get('name'), '#'); $match = ltrim($ap_tag->get('name'), '#');
$tag = Tag::ensureValid($match); $tag = Tag::ensureValid($match);
$canonical_tag = $ap_tag->get('canonical') ?? Tag::canonicalTag($tag, \is_null($lang_id = $obj->getLanguageId()) ? null : Language::getById($lang_id)->getLocale()); $canonical_tag = $ap_tag->get('canonical') ?? Tag::canonicalTag($tag, is_null($lang_id = $obj->getLanguageId()) ? null : Language::getById($lang_id)->getLocale());
DB::persist(NoteTag::create([ DB::persist(NoteTag::create([
'tag' => $tag, 'tag' => $tag,
'canonical' => $canonical_tag, 'canonical' => $canonical_tag,
@ -234,7 +273,7 @@ class Note extends Model
break; break;
} }
} }
$obj->setObjectMentionsIds($object_mentions_ids); $obj->setObjectMentionsIds(array_unique($object_mentions_ids));
// The content would be non-sanitized text/html // 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]]);
@ -281,23 +320,48 @@ class Note extends Model
'id' => $object->getUrl(), 'id' => $object->getUrl(),
'published' => $object->getCreated()->format(DateTimeInterface::RFC3339), 'published' => $object->getCreated()->format(DateTimeInterface::RFC3339),
'attributedTo' => $object->getActor()->getUri(Router::ABSOLUTE_URL), 'attributedTo' => $object->getActor()->getUri(Router::ABSOLUTE_URL),
'to' => ['https://www.w3.org/ns/activitystreams#Public'], // TODO: implement proper scope address
'cc' => ['https://www.w3.org/ns/activitystreams#Public'],
'content' => $object->getRendered(), 'content' => $object->getRendered(),
'attachment' => [], 'attachment' => [],
'tag' => [], 'tag' => [],
'inReplyTo' => $object->getReplyTo() === null ? null : ActivityPub::getUriByObject(GSNote::getById($object->getReplyTo())),
'inConversation' => $object->getConversationUri(), 'inConversation' => $object->getConversationUri(),
'directMessage' => false, // TODO: implement proper scope address 'directMessage' => $object->getScope() === VisibilityScope::MESSAGE,
]; ];
// Target scope
switch ($object->getScope()) {
case VisibilityScope::PUBLIC:
$attr['to'] = ['https://www.w3.org/ns/activitystreams#Public'];
$attr['cc'] = [Router::url('actor_subscribers_id', ['id' => $object->getActor()->getId()], Router::ABSOLUTE_URL)];
break;
case VisibilityScope::LOCAL:
throw new ClientException('This note was not federated.', 403);
case VisibilityScope::ADDRESSEE:
case VisibilityScope::MESSAGE:
$attr['to'] = []; // Will be filled later
$attr['cc'] = [];
break;
case VisibilityScope::GROUP: // Will have the group in the To
case VisibilityScope::COLLECTION:
// Since we don't support sending unlisted/followers-only
// notices, arriving here means we're instead answering to that type
// of posts. In this situation, it's safer to always send answers of type unlisted.
$attr['to'] = [];
$attr['cc'] = ['https://www.w3.org/ns/activitystreams#Public'];
break;
default:
Log::error('ActivityPub->Note->toJson: Found an unknown visibility scope.');
throw new ServerException('Found an unknown visibility scope which cannot federate.');
}
// Mentions // Mentions
foreach ($object->getNotificationTargets() as $mention) { foreach ($object->getNotificationTargets() as $mention) {
$attr['tag'][] = [ $attr['tag'][] = [
'type' => 'Mention', 'type' => 'Mention',
'href' => ($href = $mention->getUri()), 'href' => ($href = $mention->getUri()),
'name' => '@' . $mention->getNickname() . '@' . parse_url($href, \PHP_URL_HOST), 'name' => '@' . $mention->getNickname() . '@' . parse_url($href, PHP_URL_HOST),
]; ];
$attr['cc'][] = $href; $attr['to'][] = $href;
} }
// Hashtags // Hashtags