3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Protocol\ActivityPub;
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Database\DBA;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Content\Text\Markdown;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Model\Contact;
31 use Friendica\Model\APContact;
32 use Friendica\Model\Item;
33 use Friendica\Model\Post;
34 use Friendica\Model\User;
35 use Friendica\Protocol\Activity;
36 use Friendica\Protocol\ActivityPub;
37 use Friendica\Util\HTTPSignature;
38 use Friendica\Util\JsonLD;
39 use Friendica\Util\LDSignature;
40 use Friendica\Util\Strings;
43 * ActivityPub Receiver Protocol class
48 * Check what this is meant to do:
57 const PUBLIC_COLLECTION = 'as:Public';
58 const ACCOUNT_TYPES = ['as:Person', 'as:Organization', 'as:Service', 'as:Group', 'as:Application'];
59 const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event', 'as:Audio', 'as:Page', 'as:Question'];
60 const ACTIVITY_TYPES = ['as:Like', 'as:Dislike', 'as:Accept', 'as:Reject', 'as:TentativeAccept'];
62 const TARGET_UNKNOWN = 0;
67 const TARGET_FOLLOWER = 5;
68 const TARGET_ANSWER = 6;
69 const TARGET_GLOBAL = 7;
72 * Checks incoming message from the inbox
76 * @param integer $uid User ID
79 public static function processInbox($body, $header, $uid)
81 $activity = json_decode($body, true);
82 if (empty($activity)) {
83 Logger::warning('Invalid body.');
87 $ldactivity = JsonLD::compact($activity);
89 $actor = JsonLD::fetchElement($ldactivity, 'as:actor', '@id');
91 $apcontact = APContact::getByURL($actor);
92 if (empty($apcontact)) {
93 Logger::notice('Unable to retrieve AP contact for actor - message is discarded', ['actor' => $actor]);
95 } elseif ($apcontact['type'] == 'Application' && $apcontact['nick'] == 'relay') {
96 self::processRelayPost($ldactivity, $actor);
99 APContact::unmarkForArchival($apcontact);
102 $http_signer = HTTPSignature::getSigner($body, $header);
103 if ($http_signer === false) {
104 Logger::warning('Invalid HTTP signature, message will be discarded.');
106 } elseif (empty($http_signer)) {
107 Logger::info('Signer is a tombstone. The message will be discarded, the signer account is deleted.');
110 Logger::info('Valid HTTP signature', ['signer' => $http_signer]);
113 $signer = [$http_signer];
115 Logger::info('Message for user ' . $uid . ' is from actor ' . $actor);
117 if (LDSignature::isSigned($activity)) {
118 $ld_signer = LDSignature::getSigner($activity);
119 if (empty($ld_signer)) {
120 Logger::info('Invalid JSON-LD signature from ' . $actor);
121 } elseif ($ld_signer != $http_signer) {
122 $signer[] = $ld_signer;
124 if (!empty($ld_signer && ($actor == $http_signer))) {
125 Logger::info('The HTTP and the JSON-LD signature belong to ' . $ld_signer);
126 $trust_source = true;
127 } elseif (!empty($ld_signer)) {
128 Logger::info('JSON-LD signature is signed by ' . $ld_signer);
129 $trust_source = true;
130 } elseif ($actor == $http_signer) {
131 Logger::info('Bad JSON-LD signature, but HTTP signer fits the actor.');
132 $trust_source = true;
134 Logger::info('Invalid JSON-LD signature and the HTTP signer is different.');
135 $trust_source = false;
137 } elseif ($actor == $http_signer) {
138 Logger::info('Trusting post without JSON-LD signature, The actor fits the HTTP signer.');
139 $trust_source = true;
141 Logger::info('No JSON-LD signature, different actor.');
142 $trust_source = false;
145 self::processActivity($ldactivity, $body, $uid, $trust_source, true, $signer);
149 * Process incoming posts from relays
151 * @param array $activity
152 * @param string $actor
155 private static function processRelayPost(array $activity, string $actor)
157 $type = JsonLD::fetchElement($activity, '@type');
159 Logger::info('Empty type', ['activity' => $activity]);
163 if ($type != 'as:Announce') {
164 Logger::info('Not an announcement', ['activity' => $activity]);
168 $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
169 if (empty($object_id)) {
170 Logger::info('No object id found', ['activity' => $activity]);
174 $contact = Contact::getByURL($actor);
175 if (empty($contact)) {
176 Logger::info('Relay contact not found', ['actor' => $actor]);
180 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
181 Logger::notice('Relay is no sharer', ['actor' => $actor]);
185 Logger::info('Got relayed message id', ['id' => $object_id]);
187 $item_id = Item::searchByLink($object_id);
189 Logger::info('Relayed message already exists', ['id' => $object_id, 'item' => $item_id]);
193 $id = Processor::fetchMissingActivity($object_id, [], $actor);
195 Logger::notice('Relayed message had not been fetched', ['id' => $object_id]);
199 $item_id = Item::searchByLink($object_id);
201 Logger::info('Relayed message had been fetched and stored', ['id' => $object_id, 'item' => $item_id]);
203 Logger::notice('Relayed message had not been stored', ['id' => $object_id]);
208 * Fetches the object type for a given object id
210 * @param array $activity
211 * @param string $object_id Object ID of the the provided object
212 * @param integer $uid User ID
214 * @return string with object type
215 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
216 * @throws \ImagickException
218 private static function fetchObjectType($activity, $object_id, $uid = 0)
220 if (!empty($activity['as:object'])) {
221 $object_type = JsonLD::fetchElement($activity['as:object'], '@type');
222 if (!empty($object_type)) {
227 if (Post::exists(['uri' => $object_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]])) {
228 // We just assume "note" since it doesn't make a difference for the further processing
232 $profile = APContact::getByURL($object_id);
233 if (!empty($profile['type'])) {
234 APContact::unmarkForArchival($profile);
235 return 'as:' . $profile['type'];
238 $data = ActivityPub::fetchContent($object_id, $uid);
240 $object = JsonLD::compact($data);
241 $type = JsonLD::fetchElement($object, '@type');
251 * Prepare the object array
253 * @param array $activity Array with activity data
254 * @param integer $uid User ID
255 * @param boolean $push Message had been pushed to our system
256 * @param boolean $trust_source Do we trust the source?
258 * @return array with object data
259 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
260 * @throws \ImagickException
262 public static function prepareObjectData($activity, $uid, $push, &$trust_source)
264 $id = JsonLD::fetchElement($activity, '@id');
265 if (!empty($id) && !$trust_source) {
267 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
272 // Fetch a user out of the receivers of the message.
273 $fetch_uid = Receiver::getBestUserForActivity($activity, $actor);
278 $fetched_activity = ActivityPub::fetchContent($id, $fetch_uid);
279 if (!empty($fetched_activity)) {
280 $object = JsonLD::compact($fetched_activity);
281 $fetched_id = JsonLD::fetchElement($object, '@id');
282 if ($fetched_id == $id) {
283 Logger::info('Activity had been fetched successfully', ['id' => $id]);
284 $trust_source = true;
287 Logger::info('Activity id is not equal', ['id' => $id, 'fetched' => $fetched_id]);
290 Logger::info('Activity could not been fetched', ['id' => $id]);
294 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
296 Logger::info('Empty actor', ['activity' => $activity]);
300 $type = JsonLD::fetchElement($activity, '@type');
302 // Fetch all receivers from to, cc, bto and bcc
303 $receiverdata = self::getReceivers($activity, $actor);
304 $receivers = $reception_types = [];
305 foreach ($receiverdata as $key => $data) {
306 $receivers[$key] = $data['uid'];
307 $reception_types[$data['uid']] = $data['type'] ?? self::TARGET_UNKNOWN;
310 // When it is a delivery to a personal inbox we add that user to the receivers
312 $additional = [$uid => $uid];
313 $receivers = array_replace($receivers, $additional);
314 if (empty($activity['thread-completion']) && (empty($reception_types[$uid]) || in_array($reception_types[$uid], [self::TARGET_UNKNOWN, self::TARGET_FOLLOWER, self::TARGET_ANSWER, self::TARGET_GLOBAL]))) {
315 $reception_types[$uid] = self::TARGET_BCC;
318 // We possibly need some user to fetch private content,
319 // so we fetch the first out ot the list.
320 $uid = self::getFirstUserFromReceivers($receivers);
323 $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
324 if (empty($object_id)) {
325 Logger::info('No object found');
329 if (!is_string($object_id)) {
330 Logger::info('Invalid object id', ['object' => $object_id]);
334 $object_type = self::fetchObjectType($activity, $object_id, $uid);
336 // Fetch the activity on Lemmy "Announce" messages (announces of activities)
337 if (($type == 'as:Announce') && in_array($object_type, array_merge(self::ACTIVITY_TYPES, ['as:Delete', 'as:Undo', 'as:Update']))) {
338 $data = ActivityPub::fetchContent($object_id, $uid);
340 $type = $object_type;
341 $activity = JsonLD::compact($data);
343 // Some variables need to be refetched since the activity changed
344 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
345 $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
346 $object_type = self::fetchObjectType($activity, $object_id, $uid);
350 // Any activities on account types must not be altered
351 if (in_array($object_type, self::ACCOUNT_TYPES)) {
353 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
354 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
355 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
356 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
357 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
358 $object_data['push'] = $push;
359 } elseif (in_array($type, ['as:Create', 'as:Update', 'as:Announce']) || strpos($type, '#emojiReaction')) {
360 // Fetch the content only on activities where this matters
361 // We can receive "#emojiReaction" when fetching content from Hubzilla systems
362 // Always fetch on "Announce"
363 $object_data = self::fetchObject($object_id, $activity['as:object'], $trust_source && ($type != 'as:Announce'), $uid);
364 if (empty($object_data)) {
365 Logger::info("Object data couldn't be processed");
369 $object_data['object_id'] = $object_id;
371 if ($type == 'as:Announce') {
372 $object_data['push'] = false;
374 $object_data['push'] = $push;
377 // Test if it is an answer to a mail
378 if (DBA::exists('mail', ['uri' => $object_data['reply-to-id']])) {
379 $object_data['directmessage'] = true;
381 $object_data['directmessage'] = JsonLD::fetchElement($activity, 'litepub:directMessage');
383 } elseif (in_array($type, array_merge(self::ACTIVITY_TYPES, ['as:Follow'])) && in_array($object_type, self::CONTENT_TYPES)) {
384 // Create a mostly empty array out of the activity data (instead of the object).
385 // This way we later don't have to check for the existence of each individual array element.
386 $object_data = self::processObject($activity);
387 $object_data['name'] = $type;
388 $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
389 $object_data['object_id'] = $object_id;
390 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
391 $object_data['push'] = $push;
392 } elseif (in_array($type, ['as:Add'])) {
394 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
395 $object_data['target_id'] = JsonLD::fetchElement($activity, 'as:target', '@id');
396 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
397 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
398 $object_data['object_content'] = JsonLD::fetchElement($activity['as:object'], 'as:content', '@type');
399 $object_data['push'] = $push;
402 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
403 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
404 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
405 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
406 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
407 $object_data['push'] = $push;
409 // An Undo is done on the object of an object, so we need that type as well
410 if (($type == 'as:Undo') && !empty($object_data['object_object'])) {
411 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $uid);
415 $object_data = self::addActivityFields($object_data, $activity);
417 if (empty($object_data['object_type'])) {
418 $object_data['object_type'] = $object_type;
421 $object_data['type'] = $type;
422 $object_data['actor'] = $actor;
423 $object_data['item_receiver'] = $receivers;
424 $object_data['receiver'] = array_replace($object_data['receiver'] ?? [], $receivers);
425 $object_data['reception_type'] = array_replace($object_data['reception_type'] ?? [], $reception_types);
427 $author = $object_data['author'] ?? $actor;
428 if (!empty($author) && !empty($object_data['id'])) {
429 $author_host = parse_url($author, PHP_URL_HOST);
430 $id_host = parse_url($object_data['id'], PHP_URL_HOST);
431 if ($author_host == $id_host) {
432 Logger::info('Valid hosts', ['type' => $type, 'host' => $id_host]);
434 Logger::notice('Differing hosts on author and id', ['type' => $type, 'author' => $author_host, 'id' => $id_host]);
435 $trust_source = false;
439 Logger::info('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id']);
445 * Fetches the first user id from the receiver array
447 * @param array $receivers Array with receivers
448 * @return integer user id;
450 public static function getFirstUserFromReceivers($receivers)
452 foreach ($receivers as $receiver) {
453 if (!empty($receiver)) {
461 * Processes the activity object
463 * @param array $activity Array with activity data
464 * @param string $body
465 * @param integer $uid User ID
466 * @param boolean $trust_source Do we trust the source?
467 * @param boolean $push Message had been pushed to our system
470 public static function processActivity($activity, string $body = '', int $uid = null, bool $trust_source = false, bool $push = false, array $signer = [])
472 $type = JsonLD::fetchElement($activity, '@type');
474 Logger::info('Empty type', ['activity' => $activity]);
478 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
479 Logger::info('Empty object', ['activity' => $activity]);
483 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
485 Logger::info('Empty actor', ['activity' => $activity]);
489 if (is_array($activity['as:object'])) {
490 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
495 // Test the provided signatures against the actor and "attributedTo"
497 if (!empty($attributed_to) && !empty($actor)) {
498 $trust_source = (in_array($actor, $signer) && in_array($attributed_to, $signer));
500 $trust_source = in_array($actor, $signer);
504 // $trust_source is called by reference and is set to true if the content was retrieved successfully
505 $object_data = self::prepareObjectData($activity, $uid, $push, $trust_source);
506 if (empty($object_data)) {
507 Logger::info('No object data found', ['activity' => $activity]);
511 // Lemmy is announcing activities.
512 // We are changing the announces into regular activities.
513 if (($type == 'as:Announce') && in_array($object_data['type'] ?? '', array_merge(self::ACTIVITY_TYPES, ['as:Delete', 'as:Undo', 'as:Update']))) {
514 $type = $object_data['type'];
517 if (!$trust_source) {
518 Logger::info('Activity trust could not be achieved.', ['id' => $object_data['object_id'], 'type' => $type, 'signer' => $signer, 'actor' => $actor, 'attributedTo' => $attributed_to]);
522 if (!empty($body) && empty($object_data['raw'])) {
523 $object_data['raw'] = $body;
526 // Internal flag for thread completion. See Processor.php
527 if (!empty($activity['thread-completion'])) {
528 $object_data['thread-completion'] = $activity['thread-completion'];
530 if (!empty($activity['thread-children-type'])) {
531 $object_data['thread-children-type'] = $activity['thread-children-type'];
534 // Internal flag for posts that arrived via relay
535 if (!empty($activity['from-relay'])) {
536 $object_data['from-relay'] = $activity['from-relay'];
541 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
542 $item = ActivityPub\Processor::createItem($object_data);
543 ActivityPub\Processor::postItem($object_data, $item);
548 if ($object_data['object_type'] == 'as:tag') {
549 ActivityPub\Processor::addTag($object_data);
554 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
555 $object_data['thread-completion'] = Contact::getIdForURL($actor);
557 $item = ActivityPub\Processor::createItem($object_data);
562 $item['post-reason'] = Item::PR_ANNOUNCEMENT;
563 ActivityPub\Processor::postItem($object_data, $item);
565 $announce_object_data = self::processObject($activity);
566 $announce_object_data['name'] = $type;
567 $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
568 $announce_object_data['object_id'] = $object_data['object_id'];
569 $announce_object_data['object_type'] = $object_data['object_type'];
570 $announce_object_data['push'] = $push;
573 $announce_object_data['raw'] = $body;
576 ActivityPub\Processor::createActivity($announce_object_data, Activity::ANNOUNCE);
581 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
582 ActivityPub\Processor::createActivity($object_data, Activity::LIKE);
587 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
588 ActivityPub\Processor::createActivity($object_data, Activity::DISLIKE);
592 case 'as:TentativeAccept':
593 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
594 ActivityPub\Processor::createActivity($object_data, Activity::ATTENDMAYBE);
599 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
600 ActivityPub\Processor::updateItem($object_data);
601 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
602 ActivityPub\Processor::updatePerson($object_data);
607 if ($object_data['object_type'] == 'as:Tombstone') {
608 ActivityPub\Processor::deleteItem($object_data);
609 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
610 ActivityPub\Processor::deletePerson($object_data);
615 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
616 ActivityPub\Processor::followUser($object_data);
617 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
618 $object_data['reply-to-id'] = $object_data['object_id'];
619 ActivityPub\Processor::createActivity($object_data, Activity::FOLLOW);
624 if ($object_data['object_type'] == 'as:Follow') {
625 ActivityPub\Processor::acceptFollowUser($object_data);
626 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
627 ActivityPub\Processor::createActivity($object_data, Activity::ATTEND);
632 if ($object_data['object_type'] == 'as:Follow') {
633 ActivityPub\Processor::rejectFollowUser($object_data);
634 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
635 ActivityPub\Processor::createActivity($object_data, Activity::ATTENDNO);
640 if (($object_data['object_type'] == 'as:Follow') &&
641 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
642 ActivityPub\Processor::undoFollowUser($object_data);
643 } elseif (($object_data['object_type'] == 'as:Accept') &&
644 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
645 ActivityPub\Processor::rejectFollowUser($object_data);
646 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
647 in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
648 ActivityPub\Processor::undoActivity($object_data);
653 Logger::info('Unknown activity: ' . $type . ' ' . $object_data['object_type']);
659 * Fetch a user id from an activity array
661 * @param array $activity
662 * @param string $actor
664 * @return int user id
666 private static function getBestUserForActivity(array $activity, string $actor)
669 $receivers = self::getReceivers($activity, $actor);
670 foreach ($receivers as $receiver) {
671 if ($receiver['type'] == self::TARGET_GLOBAL) {
674 if (empty($uid) || ($receiver['type'] == self::TARGET_TO)) {
675 $uid = $receiver['uid'];
682 * Fetch the receiver list from an activity array
684 * @param array $activity
685 * @param string $actor
687 * @param boolean $fetch_unlisted
689 * @return array with receivers (user id)
692 private static function getReceivers($activity, $actor, $tags = [], $fetch_unlisted = false)
694 $reply = $receivers = [];
696 // When it is an answer, we inherite the receivers from the parent
697 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo', '@id');
698 if (!empty($replyto)) {
701 // Fix possibly wrong item URI (could be an answer to a plink uri)
702 $fixedReplyTo = Item::getURIByLink($replyto);
703 if (!empty($fixedReplyTo)) {
704 $reply[] = $fixedReplyTo;
708 // Fetch all posts that refer to the object id
709 $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
710 if (!empty($object_id)) {
711 $reply[] = $object_id;
714 if (!empty($reply)) {
715 $parents = Post::select(['uid'], ['uri' => $reply]);
716 while ($parent = Post::fetch($parents)) {
717 $receivers[$parent['uid']] = ['uid' => $parent['uid'], 'type' => self::TARGET_ANSWER];
719 DBA::close($parents);
722 if (!empty($actor)) {
723 $profile = APContact::getByURL($actor);
724 $followers = $profile['followers'] ?? '';
725 $is_forum = ($actor['type'] ?? '') == 'Group';
726 Logger::info('Got actor and followers', ['actor' => $actor, 'followers' => $followers]);
728 Logger::info('Empty actor', ['activity' => $activity]);
733 // We have to prevent false follower assumptions upon thread completions
734 $follower_target = empty($activity['thread-completion']) ? self::TARGET_FOLLOWER : self::TARGET_UNKNOWN;
736 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
737 $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
738 if (empty($receiver_list)) {
742 foreach ($receiver_list as $receiver) {
743 if ($receiver == self::PUBLIC_COLLECTION) {
744 $receivers[0] = ['uid' => 0, 'type' => self::TARGET_GLOBAL];
747 // Add receiver "-1" for unlisted posts
748 if ($fetch_unlisted && ($receiver == self::PUBLIC_COLLECTION) && ($element == 'as:cc')) {
749 $receivers[-1] = ['uid' => -1, 'type' => self::TARGET_GLOBAL];
752 // Fetch the receivers for the public and the followers collection
753 if ((($receiver == $followers) || (($receiver == self::PUBLIC_COLLECTION) && !$is_forum)) && !empty($actor)) {
754 $receivers = self::getReceiverForActor($actor, $tags, $receivers, $follower_target);
758 // Fetching all directly addressed receivers
759 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
760 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
761 if (!DBA::isResult($contact)) {
765 // Check if the potential receiver is following the actor
766 // Exception: The receiver is targetted via "to" or this is a comment
767 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
768 $networks = Protocol::FEDERATED;
769 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
770 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
772 // Forum posts are only accepted from forum contacts
773 if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
774 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
777 if (!DBA::exists('contact', $condition)) {
782 $type = $receivers[$contact['uid']]['type'] ?? self::TARGET_UNKNOWN;
783 if (in_array($type, [self::TARGET_UNKNOWN, self::TARGET_FOLLOWER, self::TARGET_ANSWER, self::TARGET_GLOBAL])) {
786 $type = self::TARGET_TO;
789 $type = self::TARGET_CC;
792 $type = self::TARGET_BTO;
795 $type = self::TARGET_BCC;
799 $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $type];
804 self::switchContacts($receivers, $actor);
810 * Fetch the receiver list of a given actor
812 * @param string $actor
814 * @param array $receivers
815 * @param integer $target_type
817 * @return array with receivers (user id)
820 private static function getReceiverForActor($actor, $tags, $receivers, $target_type)
822 $basecondition = ['rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
823 'network' => Protocol::FEDERATED, 'archive' => false, 'pending' => false];
825 $condition = DBA::mergeConditions($basecondition, ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($actor), 0]);
826 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
827 while ($contact = DBA::fetch($contacts)) {
828 if (empty($receivers[$contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) {
829 $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $target_type];
832 DBA::close($contacts);
834 // The queries are split because of performance issues
835 $condition = DBA::mergeConditions($basecondition, ["`alias` IN (?, ?) AND `uid` != ?", Strings::normaliseLink($actor), $actor, 0]);
836 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
837 while ($contact = DBA::fetch($contacts)) {
838 if (empty($receivers[$contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) {
839 $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $target_type];
842 DBA::close($contacts);
847 * Tests if the contact is a valid receiver for this actor
849 * @param array $contact
850 * @param string $actor
853 * @return bool with receivers (user id)
856 private static function isValidReceiverForActor($contact, $tags)
858 // Are we following the contact? Then this is a valid receiver
859 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
863 // When the possible receiver isn't a community, then it is no valid receiver
864 $owner = User::getOwnerDataById($contact['uid']);
865 if (empty($owner) || ($owner['contact-type'] != Contact::TYPE_COMMUNITY)) {
869 // Is the community account tagged?
870 foreach ($tags as $tag) {
871 if ($tag['type'] != 'Mention') {
875 if (Strings::compareLink($tag['href'], $owner['url'])) {
884 * Switches existing contacts to ActivityPub
886 * @param integer $cid Contact ID
887 * @param integer $uid User ID
888 * @param string $url Profile URL
889 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
890 * @throws \ImagickException
892 public static function switchContact($cid, $uid, $url)
894 if (DBA::exists('contact', ['id' => $cid, 'network' => Protocol::ACTIVITYPUB])) {
895 Logger::info('Contact is already ActivityPub', ['id' => $cid, 'uid' => $uid, 'url' => $url]);
899 if (Contact::updateFromProbe($cid)) {
900 Logger::info('Update was successful', ['id' => $cid, 'uid' => $uid, 'url' => $url]);
903 // Send a new follow request to be sure that the connection still exists
904 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB])) {
905 Logger::info('Contact had been switched to ActivityPub. Sending a new follow request.', ['uid' => $uid, 'url' => $url]);
906 ActivityPub\Transmitter::sendActivity('Follow', $url, $uid);
915 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
916 * @throws \ImagickException
918 private static function switchContacts($receivers, $actor)
924 foreach ($receivers as $receiver) {
925 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver['uid'], 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
926 if (DBA::isResult($contact)) {
927 self::switchContact($contact['id'], $receiver['uid'], $actor);
930 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver['uid'], 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
931 if (DBA::isResult($contact)) {
932 self::switchContact($contact['id'], $receiver['uid'], $actor);
940 * @param $object_data
941 * @param array $activity
945 private static function addActivityFields($object_data, $activity)
947 if (!empty($activity['published']) && empty($object_data['published'])) {
948 $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
951 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
952 $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid', '@value');
955 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
956 $object_data['service'] = JsonLD::fetchElement($object_data, 'service', '@value');
958 if (!empty($object_data['object_id'])) {
959 // Some systems (e.g. GNU Social) don't reply to the "id" field but the "uri" field.
960 $objectId = Item::getURIByLink($object_data['object_id']);
961 if (!empty($objectId) && ($object_data['object_id'] != $objectId)) {
962 Logger::notice('Fix wrong object-id', ['received' => $object_data['object_id'], 'correct' => $objectId]);
963 $object_data['object_id'] = $objectId;
971 * Fetches the object data from external ressources if needed
973 * @param string $object_id Object ID of the the provided object
974 * @param array $object The provided object array
975 * @param boolean $trust_source Do we trust the provided object?
976 * @param integer $uid User ID for the signature that we use to fetch data
978 * @return array|false with trusted and valid object data
979 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
980 * @throws \ImagickException
982 private static function fetchObject(string $object_id, array $object = [], bool $trust_source = false, int $uid = 0)
984 // By fetching the type we check if the object is complete.
985 $type = JsonLD::fetchElement($object, '@type');
987 if (!$trust_source || empty($type)) {
988 $data = ActivityPub::fetchContent($object_id, $uid);
990 $object = JsonLD::compact($data);
991 Logger::info('Fetched content for ' . $object_id);
993 Logger::info('Empty content for ' . $object_id . ', check if content is available locally.');
995 $item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['uri' => $object_id]);
996 if (!DBA::isResult($item)) {
997 Logger::info('Object with url ' . $object_id . ' was not found locally.');
1000 Logger::info('Using already stored item for url ' . $object_id);
1001 $data = ActivityPub\Transmitter::createNote($item);
1002 $object = JsonLD::compact($data);
1005 $id = JsonLD::fetchElement($object, '@id');
1007 Logger::info('Empty id');
1011 if ($id != $object_id) {
1012 Logger::info('Fetched id differs from provided id', ['provided' => $object_id, 'fetched' => $id]);
1016 Logger::info('Using original object for url ' . $object_id);
1019 $type = JsonLD::fetchElement($object, '@type');
1021 Logger::info('Empty type');
1025 // Lemmy is resharing "create" activities instead of content
1026 // We fetch the content from the activity.
1027 if (in_array($type, ['as:Create'])) {
1028 $object = $object['as:object'];
1029 $type = JsonLD::fetchElement($object, '@type');
1031 Logger::info('Empty type');
1034 $object_data = self::processObject($object);
1037 // We currently don't handle 'pt:CacheFile', but with this step we avoid logging
1038 if (in_array($type, self::CONTENT_TYPES) || ($type == 'pt:CacheFile')) {
1039 $object_data = self::processObject($object);
1041 if (!empty($data)) {
1042 $object_data['raw'] = json_encode($data);
1044 return $object_data;
1047 if ($type == 'as:Announce') {
1048 $object_id = JsonLD::fetchElement($object, 'object', '@id');
1049 if (empty($object_id) || !is_string($object_id)) {
1052 return self::fetchObject($object_id, [], false, $uid);
1055 Logger::info('Unhandled object type: ' . $type);
1060 * Converts the language element (Used by Peertube)
1062 * @param array $languages
1063 * @return array Languages
1065 public static function processLanguages(array $languages)
1067 if (empty($languages)) {
1071 $language_list = [];
1073 foreach ($languages as $language) {
1074 if (!empty($language['_:identifier']) && !empty($language['as:name'])) {
1075 $language_list[$language['_:identifier']] = $language['as:name'];
1078 return $language_list;
1082 * Convert tags from JSON-LD format into a simplified format
1084 * @param array $tags Tags in JSON-LD format
1086 * @return array with tags in a simplified format
1088 public static function processTags(array $tags)
1092 foreach ($tags as $tag) {
1097 $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
1098 'href' => JsonLD::fetchElement($tag, 'as:href', '@id'),
1099 'name' => JsonLD::fetchElement($tag, 'as:name', '@value')];
1101 if (empty($element['type'])) {
1105 if (empty($element['href'])) {
1106 $element['href'] = $element['name'];
1109 $taglist[] = $element;
1115 * Convert emojis from JSON-LD format into a simplified format
1117 * @param array $emojis
1118 * @return array with emojis in a simplified format
1120 private static function processEmojis(array $emojis)
1124 foreach ($emojis as $emoji) {
1125 if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
1129 $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id');
1130 $element = ['name' => JsonLD::fetchElement($emoji, 'as:name', '@value'),
1133 $emojilist[] = $element;
1140 * Convert attachments from JSON-LD format into a simplified format
1142 * @param array $attachments Attachments in JSON-LD format
1144 * @return array Attachments in a simplified format
1146 private static function processAttachments(array $attachments)
1150 // Removes empty values
1151 $attachments = array_filter($attachments);
1153 foreach ($attachments as $attachment) {
1154 switch (JsonLD::fetchElement($attachment, '@type')) {
1159 $urls = JsonLD::fetchElementArray($attachment, 'as:url');
1160 foreach ($urls as $url) {
1161 // Single scalar URL case
1162 if (is_string($url)) {
1167 $href = JsonLD::fetchElement($url, 'as:href', '@id');
1168 $mediaType = JsonLD::fetchElement($url, 'as:mediaType', '@value');
1169 if (Strings::startsWith($mediaType, 'image')) {
1178 'title' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
1179 'desc' => JsonLD::fetchElement($attachment, 'as:summary', '@value'),
1181 'image' => $pageImage,
1185 $mediaType = JsonLD::fetchElement($attachment, 'as:mediaType', '@value');
1186 $imageFullUrl = JsonLD::fetchElement($attachment, 'as:url', '@id');
1187 $imagePreviewUrl = null;
1189 if (!$imageFullUrl && ($urls = JsonLD::fetchElementArray($attachment, 'as:url'))) {
1190 $imageVariants = [];
1191 $previewVariants = [];
1192 foreach ($urls as $url) {
1193 // Scalar URL, no discrimination possible
1194 if (is_string($url)) {
1195 $imageFullUrl = $url;
1199 // Not sure what to do with a different Link media type than the base Image, we skip
1200 if ($mediaType != JsonLD::fetchElement($url, 'as:mediaType', '@value')) {
1204 $href = JsonLD::fetchElement($url, 'as:href', '@id');
1206 // Default URL choice if no discriminating width is provided
1207 $imageFullUrl = $href ?? $imageFullUrl;
1209 $width = intval(JsonLD::fetchElement($url, 'as:width', '@value') ?? 1);
1211 if ($href && $width) {
1212 $imageVariants[$width] = $href;
1213 // 632 is the ideal width for full screen frio posts, we compute the absolute distance to it
1214 $previewVariants[abs(632 - $width)] = $href;
1218 if ($imageVariants) {
1219 // Taking the maximum size image
1220 ksort($imageVariants);
1221 $imageFullUrl = array_pop($imageVariants);
1223 // Taking the minimum number distance to the target distance
1224 ksort($previewVariants);
1225 $imagePreviewUrl = array_shift($previewVariants);
1228 unset($imageVariants);
1229 unset($previewVariants);
1233 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
1234 'mediaType' => $mediaType,
1235 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
1236 'url' => $imageFullUrl,
1237 'image' => $imagePreviewUrl !== $imageFullUrl ? $imagePreviewUrl : null,
1242 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
1243 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
1244 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
1245 'url' => JsonLD::fetchElement($attachment, 'as:url', '@id'),
1246 'height' => JsonLD::fetchElement($attachment, 'as:height', '@value'),
1247 'width' => JsonLD::fetchElement($attachment, 'as:width', '@value'),
1248 'image' => JsonLD::fetchElement($attachment, 'as:image', '@id')
1257 * Fetch the original source or content with the "language" Markdown or HTML
1259 * @param array $object
1260 * @param array $object_data
1263 * @throws \Exception
1265 private static function getSource($object, $object_data)
1267 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
1268 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
1269 if (!empty($object_data['source'])) {
1270 return $object_data;
1273 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/markdown');
1274 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
1275 if (!empty($object_data['source'])) {
1276 $object_data['source'] = Markdown::toBBCode($object_data['source']);
1277 return $object_data;
1280 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/html');
1281 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
1282 if (!empty($object_data['source'])) {
1283 $object_data['source'] = HTML::toBBCode($object_data['source']);
1284 return $object_data;
1287 return $object_data;
1291 * Extracts a potential alternate URL from a list of additional URL elements
1293 * @param array $urls
1296 private static function extractAlternateUrl(array $urls): string
1299 foreach ($urls as $key => $url) {
1300 // Not a list but a single URL element
1301 if (!is_numeric($key)) {
1305 if (empty($url['@type']) || ($url['@type'] != 'as:Link')) {
1309 $href = JsonLD::fetchElement($url, 'as:href', '@id');
1314 $mediatype = JsonLD::fetchElement($url, 'as:mediaType');
1315 if (empty($mediatype)) {
1319 if ($mediatype == 'text/html') {
1320 $alternateUrl = $href;
1324 return $alternateUrl;
1328 * Check if the "as:url" element is an array with multiple links
1329 * This is the case with audio and video posts.
1330 * Then the links are added as attachments
1332 * @param array $urls The object URL list
1333 * @return array an array of attachments
1335 private static function processAttachmentUrls(array $urls): array
1338 foreach ($urls as $key => $url) {
1339 // Not a list but a single URL element
1340 if (!is_numeric($key)) {
1344 if (empty($url['@type']) || ($url['@type'] != 'as:Link')) {
1348 $href = JsonLD::fetchElement($url, 'as:href', '@id');
1353 $mediatype = JsonLD::fetchElement($url, 'as:mediaType');
1354 if (empty($mediatype)) {
1358 $filetype = strtolower(substr($mediatype, 0, strpos($mediatype, '/')));
1360 if ($filetype == 'audio') {
1361 $attachments[] = ['type' => $filetype, 'mediaType' => $mediatype, 'url' => $href, 'height' => null, 'size' => null, 'name' => ''];
1362 } elseif ($filetype == 'video') {
1363 $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
1364 // PeerTube audio-only track
1365 if ($height === 0) {
1369 $size = (int)JsonLD::fetchElement($url, 'pt:size', '@value');
1370 $attachments[] = ['type' => $filetype, 'mediaType' => $mediatype, 'url' => $href, 'height' => $height, 'size' => $size, 'name' => ''];
1371 } elseif (in_array($mediatype, ['application/x-bittorrent', 'application/x-bittorrent;x-scheme-handler/magnet'])) {
1372 $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
1374 // For Torrent links we always store the highest resolution
1375 if (!empty($attachments[$mediatype]['height']) && ($height < $attachments[$mediatype]['height'])) {
1379 $attachments[$mediatype] = ['type' => $mediatype, 'mediaType' => $mediatype, 'url' => $href, 'height' => $height, 'size' => null, 'name' => ''];
1380 } elseif ($mediatype == 'application/x-mpegURL') {
1381 // PeerTube exception, actual video link is in the tags of this URL element
1382 $attachments = array_merge($attachments, self::processAttachmentUrls($url['as:tag']));
1386 return array_values($attachments);
1390 * Fetches data from the object part of an activity
1392 * @param array $object
1395 * @throws \Exception
1397 private static function processObject($object)
1399 if (!JsonLD::fetchElement($object, '@id')) {
1404 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
1405 $object_data['id'] = JsonLD::fetchElement($object, '@id');
1406 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id');
1408 // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
1409 if (empty($object_data['reply-to-id']) || ($object_data['reply-to-id'] == './')) {
1410 $object_data['reply-to-id'] = $object_data['id'];
1412 // On activities the "reply to" is the id of the object it refers to
1413 if (in_array($object_data['object_type'], self::ACTIVITY_TYPES)) {
1414 $object_id = JsonLD::fetchElement($object, 'as:object', '@id');
1415 if (!empty($object_id)) {
1416 $object_data['reply-to-id'] = $object_id;
1420 // Some systems (e.g. GNU Social) don't reply to the "id" field but the "uri" field.
1421 $replyToId = Item::getURIByLink($object_data['reply-to-id']);
1422 if (!empty($replyToId) && ($object_data['reply-to-id'] != $replyToId)) {
1423 Logger::notice('Fix wrong reply-to', ['received' => $object_data['reply-to-id'], 'correct' => $replyToId]);
1424 $object_data['reply-to-id'] = $replyToId;
1428 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
1429 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
1431 if (empty($object_data['updated'])) {
1432 $object_data['updated'] = $object_data['published'];
1435 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1436 $object_data['published'] = $object_data['updated'];
1439 $actor = JsonLD::fetchElement($object, 'as:attributedTo', '@id');
1440 if (empty($actor)) {
1441 $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
1444 $location = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
1445 $location = JsonLD::fetchElement($location, 'location', '@value');
1447 // Some AP software allow formatted text in post location, so we run all the text converters we have to boil
1448 // down to HTML and then finally format to plaintext.
1449 $location = Markdown::convert($location);
1450 $location = BBCode::toPlaintext($location);
1453 $object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value');
1454 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
1455 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
1456 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
1457 $object_data['actor'] = $object_data['author'] = $actor;
1458 $object_data['context'] = JsonLD::fetchElement($object, 'as:context', '@id');
1459 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation', '@id');
1460 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
1461 $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
1462 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
1463 $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
1464 $object_data['mediatype'] = JsonLD::fetchElement($object, 'as:mediaType', '@value');
1465 $object_data = self::getSource($object, $object_data);
1466 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
1467 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
1468 $object_data['location'] = $location;
1469 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
1470 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
1471 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
1472 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
1473 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []);
1474 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []);
1475 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', null, '@type', 'toot:Emoji') ?? []);
1476 $object_data['languages'] = self::processLanguages(JsonLD::fetchElementArray($object, 'sc:inLanguage') ?? []);
1477 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
1478 $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
1479 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
1481 // Special treatment for Hubzilla links
1482 if (is_array($object_data['alternate-url'])) {
1483 $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href', '@id');
1485 if (!is_string($object_data['alternate-url'])) {
1486 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href', '@id');
1490 if (in_array($object_data['object_type'], ['as:Audio', 'as:Video'])) {
1491 $object_data['alternate-url'] = self::extractAlternateUrl($object['as:url'] ?? []) ?: $object_data['alternate-url'];
1492 $object_data['attachments'] = array_merge($object_data['attachments'], self::processAttachmentUrls($object['as:url'] ?? []));
1495 // For page types we expect that the alternate url posts to some page.
1496 // So we add this to the attachments if it differs from the id.
1497 // Currently only Lemmy is using the page type.
1498 if (($object_data['object_type'] == 'as:Page') && !empty($object_data['alternate-url']) && !Strings::compareLink($object_data['alternate-url'], $object_data['id'])) {
1499 $object_data['attachments'][] = ['url' => $object_data['alternate-url']];
1500 $object_data['alternate-url'] = null;
1503 $receiverdata = self::getReceivers($object, $object_data['actor'], $object_data['tags'], true);
1504 $receivers = $reception_types = [];
1505 foreach ($receiverdata as $key => $data) {
1506 $receivers[$key] = $data['uid'];
1507 $reception_types[$data['uid']] = $data['type'] ?? 0;
1510 $object_data['receiver'] = $receivers;
1511 $object_data['reception_type'] = $reception_types;
1513 $object_data['unlisted'] = in_array(-1, $object_data['receiver']);
1514 unset($object_data['receiver'][-1]);
1515 unset($object_data['reception_type'][-1]);
1517 // Common object data:
1520 // @context, type, actor, signature, mediaType, duration, replies, icon
1522 // Also missing: (Defined in the standard, but currently unused)
1523 // audience, preview, endTime, startTime, image
1528 // contentMap, announcement_count, announcements, context_id, likes, like_count
1529 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1534 // category, licence, language, commentsEnabled
1537 // views, waitTranscoding, state, support, subtitleLanguage
1538 // likes, dislikes, shares, comments
1540 return $object_data;