From ecf4789cc441571bb5007983f8b0bcd08724a074 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 26 May 2019 11:20:03 +0000 Subject: [PATCH] Process incoming tag add requests --- src/Protocol/ActivityPub/Processor.php | 37 ++++++++++++++++++++++++++ src/Protocol/ActivityPub/Receiver.php | 13 +++++++++ 2 files changed, 50 insertions(+) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 045c48a4c1..a5f6607edc 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -198,6 +198,43 @@ class Processor Item::delete(['uri' => $activity['object_id'], 'owner-id' => $owner]); } + /** + * Prepare the item array for an activity + * + * @param array $activity Activity array + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function addTag($activity) + { + if (empty($activity['object_content']) || empty($activity['object_id'])) { + return; + } + + foreach ($activity['receiver'] as $receiver) { + $item = Item::selectFirst(['id', 'tag', 'origin', 'author-link'], ['uri' => $activity['target_id'], 'uid' => $receiver]); + if (!DBA::isResult($item)) { + // We don't fetch missing content for this purpose + continue; + } + + if (($item['author-link'] != $activity['actor']) && !$item['origin']) { + Logger::info('Not origin, not from the author, skipping update', ['id' => $item['id'], 'author' => $item['author-link'], 'actor' => $activity['actor']]); + continue; + } + + // To-Do: + // - Check if "blocktag" is set + // - Check if actor is a contact + + if (!stristr($item['tag'], trim($activity['object_content']))) { + $tag = $item['tag'] . (strlen($item['tag']) ? ',' : '') . '#[url=' . $activity['object_id'] . ']'. $activity['object_content'] . '[/url]'; + Item::update(['tag' => $tag], ['id' => $item['id']]); + Logger::info('Tagged item', ['id' => $item['id'], 'tag' => $activity['object_content'], 'uri' => $activity['target_id'], 'actor' => $activity['actor']]); + } + } + } + /** * Prepare the item array for an activity * diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 74d9392752..97bd7dc7e7 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -226,6 +226,13 @@ class Receiver $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id'); $object_data['object_id'] = $object_id; $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type + } elseif (in_array($type, ['as:Add'])) { + $object_data = []; + $object_data['id'] = JsonLD::fetchElement($activity, '@id'); + $object_data['target_id'] = JsonLD::fetchElement($activity, 'as:target', '@id'); + $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id'); + $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type'); + $object_data['object_content'] = JsonLD::fetchElement($activity['as:object'], 'as:content', '@type'); } else { $object_data = []; $object_data['id'] = JsonLD::fetchElement($activity, '@id'); @@ -366,6 +373,12 @@ class Receiver } break; + case 'as:Add': + if ($object_data['object_type'] == 'as:tag') { + ActivityPub\Processor::addTag($object_data); + } + break; + case 'as:Announce': if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { $profile = APContact::getByURL($object_data['actor']); -- 2.39.5