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) {
266 $fetch_uid = $uid ?: self::getBestUserForActivity($activity);
268 $fetched_activity = ActivityPub::fetchContent($id, $fetch_uid);
269 if (!empty($fetched_activity)) {
270 $object = JsonLD::compact($fetched_activity);
271 $fetched_id = JsonLD::fetchElement($object, '@id');
272 if ($fetched_id == $id) {
273 Logger::info('Activity had been fetched successfully', ['id' => $id]);
274 $trust_source = true;
277 Logger::info('Activity id is not equal', ['id' => $id, 'fetched' => $fetched_id]);
280 Logger::info('Activity could not been fetched', ['id' => $id]);
284 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
286 Logger::info('Empty actor', ['activity' => $activity]);
290 $type = JsonLD::fetchElement($activity, '@type');
292 // Fetch all receivers from to, cc, bto and bcc
293 $receiverdata = self::getReceivers($activity, $actor);
294 $receivers = $reception_types = [];
295 foreach ($receiverdata as $key => $data) {
296 $receivers[$key] = $data['uid'];
297 $reception_types[$data['uid']] = $data['type'] ?? self::TARGET_UNKNOWN;
300 // When it is a delivery to a personal inbox we add that user to the receivers
302 $additional = [$uid => $uid];
303 $receivers = array_replace($receivers, $additional);
304 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]))) {
305 $reception_types[$uid] = self::TARGET_BCC;
309 // We possibly need some user to fetch private content,
310 // so we fetch one out of the receivers if no uid is provided.
311 $fetch_uid = $uid ?: self::getBestUserForActivity($activity);
313 $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
314 if (empty($object_id)) {
315 Logger::info('No object found');
319 if (!is_string($object_id)) {
320 Logger::info('Invalid object id', ['object' => $object_id]);
324 $object_type = self::fetchObjectType($activity, $object_id, $fetch_uid);
326 // Fetch the activity on Lemmy "Announce" messages (announces of activities)
327 if (($type == 'as:Announce') && in_array($object_type, array_merge(self::ACTIVITY_TYPES, ['as:Delete', 'as:Undo', 'as:Update']))) {
328 $data = ActivityPub::fetchContent($object_id, $fetch_uid);
330 $type = $object_type;
331 $activity = JsonLD::compact($data);
333 // Some variables need to be refetched since the activity changed
334 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
335 $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
336 $object_type = self::fetchObjectType($activity, $object_id, $fetch_uid);
340 // Any activities on account types must not be altered
341 if (in_array($object_type, self::ACCOUNT_TYPES)) {
343 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
344 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
345 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
346 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
347 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
348 $object_data['push'] = $push;
349 } elseif (in_array($type, ['as:Create', 'as:Update', 'as:Announce']) || strpos($type, '#emojiReaction')) {
350 // Fetch the content only on activities where this matters
351 // We can receive "#emojiReaction" when fetching content from Hubzilla systems
352 // Always fetch on "Announce"
353 $object_data = self::fetchObject($object_id, $activity['as:object'], $trust_source && ($type != 'as:Announce'), $fetch_uid);
354 if (empty($object_data)) {
355 Logger::info("Object data couldn't be processed");
359 $object_data['object_id'] = $object_id;
361 if ($type == 'as:Announce') {
362 $object_data['push'] = false;
364 $object_data['push'] = $push;
367 // Test if it is an answer to a mail
368 if (DBA::exists('mail', ['uri' => $object_data['reply-to-id']])) {
369 $object_data['directmessage'] = true;
371 $object_data['directmessage'] = JsonLD::fetchElement($activity, 'litepub:directMessage');
373 } elseif (in_array($type, array_merge(self::ACTIVITY_TYPES, ['as:Follow'])) && in_array($object_type, self::CONTENT_TYPES)) {
374 // Create a mostly empty array out of the activity data (instead of the object).
375 // This way we later don't have to check for the existence of each individual array element.
376 $object_data = self::processObject($activity);
377 $object_data['name'] = $type;
378 $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
379 $object_data['object_id'] = $object_id;
380 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
381 $object_data['push'] = $push;
382 } elseif (in_array($type, ['as:Add'])) {
384 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
385 $object_data['target_id'] = JsonLD::fetchElement($activity, 'as:target', '@id');
386 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
387 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
388 $object_data['object_content'] = JsonLD::fetchElement($activity['as:object'], 'as:content', '@type');
389 $object_data['push'] = $push;
392 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
393 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
394 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
395 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
396 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
397 $object_data['push'] = $push;
399 // An Undo is done on the object of an object, so we need that type as well
400 if (($type == 'as:Undo') && !empty($object_data['object_object'])) {
401 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $fetch_uid);
405 $object_data = self::addActivityFields($object_data, $activity);
407 if (empty($object_data['object_type'])) {
408 $object_data['object_type'] = $object_type;
411 $object_data['type'] = $type;
412 $object_data['actor'] = $actor;
413 $object_data['item_receiver'] = $receivers;
414 $object_data['receiver'] = array_replace($object_data['receiver'] ?? [], $receivers);
415 $object_data['reception_type'] = array_replace($object_data['reception_type'] ?? [], $reception_types);
417 $author = $object_data['author'] ?? $actor;
418 if (!empty($author) && !empty($object_data['id'])) {
419 $author_host = parse_url($author, PHP_URL_HOST);
420 $id_host = parse_url($object_data['id'], PHP_URL_HOST);
421 if ($author_host == $id_host) {
422 Logger::info('Valid hosts', ['type' => $type, 'host' => $id_host]);
424 Logger::notice('Differing hosts on author and id', ['type' => $type, 'author' => $author_host, 'id' => $id_host]);
425 $trust_source = false;
429 Logger::info('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id']);
435 * Fetches the first user id from the receiver array
437 * @param array $receivers Array with receivers
438 * @return integer user id;
440 public static function getFirstUserFromReceivers($receivers)
442 foreach ($receivers as $receiver) {
443 if (!empty($receiver)) {
451 * Processes the activity object
453 * @param array $activity Array with activity data
454 * @param string $body
455 * @param integer $uid User ID
456 * @param boolean $trust_source Do we trust the source?
457 * @param boolean $push Message had been pushed to our system
460 public static function processActivity($activity, string $body = '', int $uid = null, bool $trust_source = false, bool $push = false, array $signer = [])
462 $type = JsonLD::fetchElement($activity, '@type');
464 Logger::info('Empty type', ['activity' => $activity]);
468 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
469 Logger::info('Empty object', ['activity' => $activity]);
473 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
475 Logger::info('Empty actor', ['activity' => $activity]);
479 if (is_array($activity['as:object'])) {
480 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
485 // Test the provided signatures against the actor and "attributedTo"
487 if (!empty($attributed_to) && !empty($actor)) {
488 $trust_source = (in_array($actor, $signer) && in_array($attributed_to, $signer));
490 $trust_source = in_array($actor, $signer);
494 // $trust_source is called by reference and is set to true if the content was retrieved successfully
495 $object_data = self::prepareObjectData($activity, $uid, $push, $trust_source);
496 if (empty($object_data)) {
497 Logger::info('No object data found', ['activity' => $activity]);
501 // Lemmy is announcing activities.
502 // We are changing the announces into regular activities.
503 if (($type == 'as:Announce') && in_array($object_data['type'] ?? '', array_merge(self::ACTIVITY_TYPES, ['as:Delete', 'as:Undo', 'as:Update']))) {
504 $type = $object_data['type'];
507 if (!$trust_source) {
508 Logger::info('Activity trust could not be achieved.', ['id' => $object_data['object_id'], 'type' => $type, 'signer' => $signer, 'actor' => $actor, 'attributedTo' => $attributed_to]);
512 if (!empty($body) && empty($object_data['raw'])) {
513 $object_data['raw'] = $body;
516 // Internal flag for thread completion. See Processor.php
517 if (!empty($activity['thread-completion'])) {
518 $object_data['thread-completion'] = $activity['thread-completion'];
520 if (!empty($activity['thread-children-type'])) {
521 $object_data['thread-children-type'] = $activity['thread-children-type'];
524 // Internal flag for posts that arrived via relay
525 if (!empty($activity['from-relay'])) {
526 $object_data['from-relay'] = $activity['from-relay'];
531 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
532 $item = ActivityPub\Processor::createItem($object_data);
533 ActivityPub\Processor::postItem($object_data, $item);
538 if ($object_data['object_type'] == 'as:tag') {
539 ActivityPub\Processor::addTag($object_data);
544 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
545 $object_data['thread-completion'] = Contact::getIdForURL($actor);
547 $item = ActivityPub\Processor::createItem($object_data);
552 $item['post-reason'] = Item::PR_ANNOUNCEMENT;
553 ActivityPub\Processor::postItem($object_data, $item);
555 $announce_object_data = self::processObject($activity);
556 $announce_object_data['name'] = $type;
557 $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
558 $announce_object_data['object_id'] = $object_data['object_id'];
559 $announce_object_data['object_type'] = $object_data['object_type'];
560 $announce_object_data['push'] = $push;
563 $announce_object_data['raw'] = $body;
566 ActivityPub\Processor::createActivity($announce_object_data, Activity::ANNOUNCE);
571 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
572 ActivityPub\Processor::createActivity($object_data, Activity::LIKE);
577 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
578 ActivityPub\Processor::createActivity($object_data, Activity::DISLIKE);
582 case 'as:TentativeAccept':
583 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
584 ActivityPub\Processor::createActivity($object_data, Activity::ATTENDMAYBE);
589 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
590 ActivityPub\Processor::updateItem($object_data);
591 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
592 ActivityPub\Processor::updatePerson($object_data);
597 if ($object_data['object_type'] == 'as:Tombstone') {
598 ActivityPub\Processor::deleteItem($object_data);
599 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
600 ActivityPub\Processor::deletePerson($object_data);
605 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
606 ActivityPub\Processor::followUser($object_data);
607 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
608 $object_data['reply-to-id'] = $object_data['object_id'];
609 ActivityPub\Processor::createActivity($object_data, Activity::FOLLOW);
614 if ($object_data['object_type'] == 'as:Follow') {
615 ActivityPub\Processor::acceptFollowUser($object_data);
616 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
617 ActivityPub\Processor::createActivity($object_data, Activity::ATTEND);
622 if ($object_data['object_type'] == 'as:Follow') {
623 ActivityPub\Processor::rejectFollowUser($object_data);
624 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
625 ActivityPub\Processor::createActivity($object_data, Activity::ATTENDNO);
630 if (($object_data['object_type'] == 'as:Follow') &&
631 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
632 ActivityPub\Processor::undoFollowUser($object_data);
633 } elseif (($object_data['object_type'] == 'as:Accept') &&
634 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
635 ActivityPub\Processor::rejectFollowUser($object_data);
636 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
637 in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
638 ActivityPub\Processor::undoActivity($object_data);
643 Logger::info('Unknown activity: ' . $type . ' ' . $object_data['object_type']);
649 * Fetch a user id from an activity array
651 * @param array $activity
652 * @param string $actor
654 * @return int user id
656 public static function getBestUserForActivity(array $activity)
659 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id') ?? '';
661 $receivers = self::getReceivers($activity, $actor);
662 foreach ($receivers as $receiver) {
663 if ($receiver['type'] == self::TARGET_GLOBAL) {
666 if (empty($uid) || ($receiver['type'] == self::TARGET_TO)) {
667 $uid = $receiver['uid'];
674 * Fetch the receiver list from an activity array
676 * @param array $activity
677 * @param string $actor
679 * @param boolean $fetch_unlisted
681 * @return array with receivers (user id)
684 private static function getReceivers($activity, $actor, $tags = [], $fetch_unlisted = false)
686 $reply = $receivers = [];
688 // When it is an answer, we inherite the receivers from the parent
689 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo', '@id');
690 if (!empty($replyto)) {
693 // Fix possibly wrong item URI (could be an answer to a plink uri)
694 $fixedReplyTo = Item::getURIByLink($replyto);
695 if (!empty($fixedReplyTo)) {
696 $reply[] = $fixedReplyTo;
700 // Fetch all posts that refer to the object id
701 $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
702 if (!empty($object_id)) {
703 $reply[] = $object_id;
706 if (!empty($reply)) {
707 $parents = Post::select(['uid'], ['uri' => $reply]);
708 while ($parent = Post::fetch($parents)) {
709 $receivers[$parent['uid']] = ['uid' => $parent['uid'], 'type' => self::TARGET_ANSWER];
711 DBA::close($parents);
714 if (!empty($actor)) {
715 $profile = APContact::getByURL($actor);
716 $followers = $profile['followers'] ?? '';
717 $is_forum = ($actor['type'] ?? '') == 'Group';
718 Logger::info('Got actor and followers', ['actor' => $actor, 'followers' => $followers]);
720 Logger::info('Empty actor', ['activity' => $activity]);
725 // We have to prevent false follower assumptions upon thread completions
726 $follower_target = empty($activity['thread-completion']) ? self::TARGET_FOLLOWER : self::TARGET_UNKNOWN;
728 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
729 $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
730 if (empty($receiver_list)) {
734 foreach ($receiver_list as $receiver) {
735 if ($receiver == self::PUBLIC_COLLECTION) {
736 $receivers[0] = ['uid' => 0, 'type' => self::TARGET_GLOBAL];
739 // Add receiver "-1" for unlisted posts
740 if ($fetch_unlisted && ($receiver == self::PUBLIC_COLLECTION) && ($element == 'as:cc')) {
741 $receivers[-1] = ['uid' => -1, 'type' => self::TARGET_GLOBAL];
744 // Fetch the receivers for the public and the followers collection
745 if ((($receiver == $followers) || (($receiver == self::PUBLIC_COLLECTION) && !$is_forum)) && !empty($actor)) {
746 $receivers = self::getReceiverForActor($actor, $tags, $receivers, $follower_target);
750 // Fetching all directly addressed receivers
751 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
752 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
753 if (!DBA::isResult($contact)) {
757 // Check if the potential receiver is following the actor
758 // Exception: The receiver is targetted via "to" or this is a comment
759 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
760 $networks = Protocol::FEDERATED;
761 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
762 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
764 // Forum posts are only accepted from forum contacts
765 if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
766 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
769 if (!DBA::exists('contact', $condition)) {
774 $type = $receivers[$contact['uid']]['type'] ?? self::TARGET_UNKNOWN;
775 if (in_array($type, [self::TARGET_UNKNOWN, self::TARGET_FOLLOWER, self::TARGET_ANSWER, self::TARGET_GLOBAL])) {
778 $type = self::TARGET_TO;
781 $type = self::TARGET_CC;
784 $type = self::TARGET_BTO;
787 $type = self::TARGET_BCC;
791 $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $type];
796 self::switchContacts($receivers, $actor);
802 * Fetch the receiver list of a given actor
804 * @param string $actor
806 * @param array $receivers
807 * @param integer $target_type
809 * @return array with receivers (user id)
812 private static function getReceiverForActor($actor, $tags, $receivers, $target_type)
814 $basecondition = ['rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
815 'network' => Protocol::FEDERATED, 'archive' => false, 'pending' => false];
817 $condition = DBA::mergeConditions($basecondition, ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($actor), 0]);
818 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
819 while ($contact = DBA::fetch($contacts)) {
820 if (empty($receivers[$contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) {
821 $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $target_type];
824 DBA::close($contacts);
826 // The queries are split because of performance issues
827 $condition = DBA::mergeConditions($basecondition, ["`alias` IN (?, ?) AND `uid` != ?", Strings::normaliseLink($actor), $actor, 0]);
828 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
829 while ($contact = DBA::fetch($contacts)) {
830 if (empty($receivers[$contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) {
831 $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $target_type];
834 DBA::close($contacts);
839 * Tests if the contact is a valid receiver for this actor
841 * @param array $contact
842 * @param string $actor
845 * @return bool with receivers (user id)
848 private static function isValidReceiverForActor($contact, $tags)
850 // Are we following the contact? Then this is a valid receiver
851 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
855 // When the possible receiver isn't a community, then it is no valid receiver
856 $owner = User::getOwnerDataById($contact['uid']);
857 if (empty($owner) || ($owner['contact-type'] != Contact::TYPE_COMMUNITY)) {
861 // Is the community account tagged?
862 foreach ($tags as $tag) {
863 if ($tag['type'] != 'Mention') {
867 if (Strings::compareLink($tag['href'], $owner['url'])) {
876 * Switches existing contacts to ActivityPub
878 * @param integer $cid Contact ID
879 * @param integer $uid User ID
880 * @param string $url Profile URL
881 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
882 * @throws \ImagickException
884 public static function switchContact($cid, $uid, $url)
886 if (DBA::exists('contact', ['id' => $cid, 'network' => Protocol::ACTIVITYPUB])) {
887 Logger::info('Contact is already ActivityPub', ['id' => $cid, 'uid' => $uid, 'url' => $url]);
891 if (Contact::updateFromProbe($cid)) {
892 Logger::info('Update was successful', ['id' => $cid, 'uid' => $uid, 'url' => $url]);
895 // Send a new follow request to be sure that the connection still exists
896 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB])) {
897 Logger::info('Contact had been switched to ActivityPub. Sending a new follow request.', ['uid' => $uid, 'url' => $url]);
898 ActivityPub\Transmitter::sendActivity('Follow', $url, $uid);
907 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
908 * @throws \ImagickException
910 private static function switchContacts($receivers, $actor)
916 foreach ($receivers as $receiver) {
917 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver['uid'], 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
918 if (DBA::isResult($contact)) {
919 self::switchContact($contact['id'], $receiver['uid'], $actor);
922 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver['uid'], 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
923 if (DBA::isResult($contact)) {
924 self::switchContact($contact['id'], $receiver['uid'], $actor);
932 * @param $object_data
933 * @param array $activity
937 private static function addActivityFields($object_data, $activity)
939 if (!empty($activity['published']) && empty($object_data['published'])) {
940 $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
943 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
944 $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid', '@value');
947 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
948 $object_data['service'] = JsonLD::fetchElement($object_data, 'service', '@value');
950 if (!empty($object_data['object_id'])) {
951 // Some systems (e.g. GNU Social) don't reply to the "id" field but the "uri" field.
952 $objectId = Item::getURIByLink($object_data['object_id']);
953 if (!empty($objectId) && ($object_data['object_id'] != $objectId)) {
954 Logger::notice('Fix wrong object-id', ['received' => $object_data['object_id'], 'correct' => $objectId]);
955 $object_data['object_id'] = $objectId;
963 * Fetches the object data from external ressources if needed
965 * @param string $object_id Object ID of the the provided object
966 * @param array $object The provided object array
967 * @param boolean $trust_source Do we trust the provided object?
968 * @param integer $uid User ID for the signature that we use to fetch data
970 * @return array|false with trusted and valid object data
971 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
972 * @throws \ImagickException
974 private static function fetchObject(string $object_id, array $object = [], bool $trust_source = false, int $uid = 0)
976 // By fetching the type we check if the object is complete.
977 $type = JsonLD::fetchElement($object, '@type');
979 if (!$trust_source || empty($type)) {
980 $data = ActivityPub::fetchContent($object_id, $uid);
982 $object = JsonLD::compact($data);
983 Logger::info('Fetched content for ' . $object_id);
985 Logger::info('Empty content for ' . $object_id . ', check if content is available locally.');
987 $item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['uri' => $object_id]);
988 if (!DBA::isResult($item)) {
989 Logger::info('Object with url ' . $object_id . ' was not found locally.');
992 Logger::info('Using already stored item for url ' . $object_id);
993 $data = ActivityPub\Transmitter::createNote($item);
994 $object = JsonLD::compact($data);
997 $id = JsonLD::fetchElement($object, '@id');
999 Logger::info('Empty id');
1003 if ($id != $object_id) {
1004 Logger::info('Fetched id differs from provided id', ['provided' => $object_id, 'fetched' => $id]);
1008 Logger::info('Using original object for url ' . $object_id);
1011 $type = JsonLD::fetchElement($object, '@type');
1013 Logger::info('Empty type');
1017 // Lemmy is resharing "create" activities instead of content
1018 // We fetch the content from the activity.
1019 if (in_array($type, ['as:Create'])) {
1020 $object = $object['as:object'];
1021 $type = JsonLD::fetchElement($object, '@type');
1023 Logger::info('Empty type');
1026 $object_data = self::processObject($object);
1029 // We currently don't handle 'pt:CacheFile', but with this step we avoid logging
1030 if (in_array($type, self::CONTENT_TYPES) || ($type == 'pt:CacheFile')) {
1031 $object_data = self::processObject($object);
1033 if (!empty($data)) {
1034 $object_data['raw'] = json_encode($data);
1036 return $object_data;
1039 if ($type == 'as:Announce') {
1040 $object_id = JsonLD::fetchElement($object, 'object', '@id');
1041 if (empty($object_id) || !is_string($object_id)) {
1044 return self::fetchObject($object_id, [], false, $uid);
1047 Logger::info('Unhandled object type: ' . $type);
1052 * Converts the language element (Used by Peertube)
1054 * @param array $languages
1055 * @return array Languages
1057 public static function processLanguages(array $languages)
1059 if (empty($languages)) {
1063 $language_list = [];
1065 foreach ($languages as $language) {
1066 if (!empty($language['_:identifier']) && !empty($language['as:name'])) {
1067 $language_list[$language['_:identifier']] = $language['as:name'];
1070 return $language_list;
1074 * Convert tags from JSON-LD format into a simplified format
1076 * @param array $tags Tags in JSON-LD format
1078 * @return array with tags in a simplified format
1080 public static function processTags(array $tags)
1084 foreach ($tags as $tag) {
1089 $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
1090 'href' => JsonLD::fetchElement($tag, 'as:href', '@id'),
1091 'name' => JsonLD::fetchElement($tag, 'as:name', '@value')];
1093 if (empty($element['type'])) {
1097 if (empty($element['href'])) {
1098 $element['href'] = $element['name'];
1101 $taglist[] = $element;
1107 * Convert emojis from JSON-LD format into a simplified format
1109 * @param array $emojis
1110 * @return array with emojis in a simplified format
1112 private static function processEmojis(array $emojis)
1116 foreach ($emojis as $emoji) {
1117 if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
1121 $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id');
1122 $element = ['name' => JsonLD::fetchElement($emoji, 'as:name', '@value'),
1125 $emojilist[] = $element;
1132 * Convert attachments from JSON-LD format into a simplified format
1134 * @param array $attachments Attachments in JSON-LD format
1136 * @return array Attachments in a simplified format
1138 private static function processAttachments(array $attachments)
1142 // Removes empty values
1143 $attachments = array_filter($attachments);
1145 foreach ($attachments as $attachment) {
1146 switch (JsonLD::fetchElement($attachment, '@type')) {
1151 $urls = JsonLD::fetchElementArray($attachment, 'as:url');
1152 foreach ($urls as $url) {
1153 // Single scalar URL case
1154 if (is_string($url)) {
1159 $href = JsonLD::fetchElement($url, 'as:href', '@id');
1160 $mediaType = JsonLD::fetchElement($url, 'as:mediaType', '@value');
1161 if (Strings::startsWith($mediaType, 'image')) {
1170 'title' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
1171 'desc' => JsonLD::fetchElement($attachment, 'as:summary', '@value'),
1173 'image' => $pageImage,
1177 $mediaType = JsonLD::fetchElement($attachment, 'as:mediaType', '@value');
1178 $imageFullUrl = JsonLD::fetchElement($attachment, 'as:url', '@id');
1179 $imagePreviewUrl = null;
1181 if (!$imageFullUrl && ($urls = JsonLD::fetchElementArray($attachment, 'as:url'))) {
1182 $imageVariants = [];
1183 $previewVariants = [];
1184 foreach ($urls as $url) {
1185 // Scalar URL, no discrimination possible
1186 if (is_string($url)) {
1187 $imageFullUrl = $url;
1191 // Not sure what to do with a different Link media type than the base Image, we skip
1192 if ($mediaType != JsonLD::fetchElement($url, 'as:mediaType', '@value')) {
1196 $href = JsonLD::fetchElement($url, 'as:href', '@id');
1198 // Default URL choice if no discriminating width is provided
1199 $imageFullUrl = $href ?? $imageFullUrl;
1201 $width = intval(JsonLD::fetchElement($url, 'as:width', '@value') ?? 1);
1203 if ($href && $width) {
1204 $imageVariants[$width] = $href;
1205 // 632 is the ideal width for full screen frio posts, we compute the absolute distance to it
1206 $previewVariants[abs(632 - $width)] = $href;
1210 if ($imageVariants) {
1211 // Taking the maximum size image
1212 ksort($imageVariants);
1213 $imageFullUrl = array_pop($imageVariants);
1215 // Taking the minimum number distance to the target distance
1216 ksort($previewVariants);
1217 $imagePreviewUrl = array_shift($previewVariants);
1220 unset($imageVariants);
1221 unset($previewVariants);
1225 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
1226 'mediaType' => $mediaType,
1227 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
1228 'url' => $imageFullUrl,
1229 'image' => $imagePreviewUrl !== $imageFullUrl ? $imagePreviewUrl : null,
1234 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
1235 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
1236 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
1237 'url' => JsonLD::fetchElement($attachment, 'as:url', '@id'),
1238 'height' => JsonLD::fetchElement($attachment, 'as:height', '@value'),
1239 'width' => JsonLD::fetchElement($attachment, 'as:width', '@value'),
1240 'image' => JsonLD::fetchElement($attachment, 'as:image', '@id')
1249 * Fetch the original source or content with the "language" Markdown or HTML
1251 * @param array $object
1252 * @param array $object_data
1255 * @throws \Exception
1257 private static function getSource($object, $object_data)
1259 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
1260 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
1261 if (!empty($object_data['source'])) {
1262 return $object_data;
1265 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/markdown');
1266 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
1267 if (!empty($object_data['source'])) {
1268 $object_data['source'] = Markdown::toBBCode($object_data['source']);
1269 return $object_data;
1272 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/html');
1273 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
1274 if (!empty($object_data['source'])) {
1275 $object_data['source'] = HTML::toBBCode($object_data['source']);
1276 return $object_data;
1279 return $object_data;
1283 * Extracts a potential alternate URL from a list of additional URL elements
1285 * @param array $urls
1288 private static function extractAlternateUrl(array $urls): string
1291 foreach ($urls as $key => $url) {
1292 // Not a list but a single URL element
1293 if (!is_numeric($key)) {
1297 if (empty($url['@type']) || ($url['@type'] != 'as:Link')) {
1301 $href = JsonLD::fetchElement($url, 'as:href', '@id');
1306 $mediatype = JsonLD::fetchElement($url, 'as:mediaType');
1307 if (empty($mediatype)) {
1311 if ($mediatype == 'text/html') {
1312 $alternateUrl = $href;
1316 return $alternateUrl;
1320 * Check if the "as:url" element is an array with multiple links
1321 * This is the case with audio and video posts.
1322 * Then the links are added as attachments
1324 * @param array $urls The object URL list
1325 * @return array an array of attachments
1327 private static function processAttachmentUrls(array $urls): array
1330 foreach ($urls as $key => $url) {
1331 // Not a list but a single URL element
1332 if (!is_numeric($key)) {
1336 if (empty($url['@type']) || ($url['@type'] != 'as:Link')) {
1340 $href = JsonLD::fetchElement($url, 'as:href', '@id');
1345 $mediatype = JsonLD::fetchElement($url, 'as:mediaType');
1346 if (empty($mediatype)) {
1350 $filetype = strtolower(substr($mediatype, 0, strpos($mediatype, '/')));
1352 if ($filetype == 'audio') {
1353 $attachments[] = ['type' => $filetype, 'mediaType' => $mediatype, 'url' => $href, 'height' => null, 'size' => null, 'name' => ''];
1354 } elseif ($filetype == 'video') {
1355 $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
1356 // PeerTube audio-only track
1357 if ($height === 0) {
1361 $size = (int)JsonLD::fetchElement($url, 'pt:size', '@value');
1362 $attachments[] = ['type' => $filetype, 'mediaType' => $mediatype, 'url' => $href, 'height' => $height, 'size' => $size, 'name' => ''];
1363 } elseif (in_array($mediatype, ['application/x-bittorrent', 'application/x-bittorrent;x-scheme-handler/magnet'])) {
1364 $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
1366 // For Torrent links we always store the highest resolution
1367 if (!empty($attachments[$mediatype]['height']) && ($height < $attachments[$mediatype]['height'])) {
1371 $attachments[$mediatype] = ['type' => $mediatype, 'mediaType' => $mediatype, 'url' => $href, 'height' => $height, 'size' => null, 'name' => ''];
1372 } elseif ($mediatype == 'application/x-mpegURL') {
1373 // PeerTube exception, actual video link is in the tags of this URL element
1374 $attachments = array_merge($attachments, self::processAttachmentUrls($url['as:tag']));
1378 return array_values($attachments);
1382 * Fetches data from the object part of an activity
1384 * @param array $object
1387 * @throws \Exception
1389 private static function processObject($object)
1391 if (!JsonLD::fetchElement($object, '@id')) {
1396 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
1397 $object_data['id'] = JsonLD::fetchElement($object, '@id');
1398 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id');
1400 // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
1401 if (empty($object_data['reply-to-id']) || ($object_data['reply-to-id'] == './')) {
1402 $object_data['reply-to-id'] = $object_data['id'];
1404 // On activities the "reply to" is the id of the object it refers to
1405 if (in_array($object_data['object_type'], self::ACTIVITY_TYPES)) {
1406 $object_id = JsonLD::fetchElement($object, 'as:object', '@id');
1407 if (!empty($object_id)) {
1408 $object_data['reply-to-id'] = $object_id;
1412 // Some systems (e.g. GNU Social) don't reply to the "id" field but the "uri" field.
1413 $replyToId = Item::getURIByLink($object_data['reply-to-id']);
1414 if (!empty($replyToId) && ($object_data['reply-to-id'] != $replyToId)) {
1415 Logger::notice('Fix wrong reply-to', ['received' => $object_data['reply-to-id'], 'correct' => $replyToId]);
1416 $object_data['reply-to-id'] = $replyToId;
1420 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
1421 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
1423 if (empty($object_data['updated'])) {
1424 $object_data['updated'] = $object_data['published'];
1427 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1428 $object_data['published'] = $object_data['updated'];
1431 $actor = JsonLD::fetchElement($object, 'as:attributedTo', '@id');
1432 if (empty($actor)) {
1433 $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
1436 $location = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
1437 $location = JsonLD::fetchElement($location, 'location', '@value');
1439 // Some AP software allow formatted text in post location, so we run all the text converters we have to boil
1440 // down to HTML and then finally format to plaintext.
1441 $location = Markdown::convert($location);
1442 $location = BBCode::toPlaintext($location);
1445 $object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value');
1446 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
1447 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
1448 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
1449 $object_data['actor'] = $object_data['author'] = $actor;
1450 $object_data['context'] = JsonLD::fetchElement($object, 'as:context', '@id');
1451 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation', '@id');
1452 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
1453 $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
1454 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
1455 $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
1456 $object_data['mediatype'] = JsonLD::fetchElement($object, 'as:mediaType', '@value');
1457 $object_data = self::getSource($object, $object_data);
1458 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
1459 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
1460 $object_data['location'] = $location;
1461 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
1462 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
1463 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
1464 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
1465 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []);
1466 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []);
1467 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', null, '@type', 'toot:Emoji') ?? []);
1468 $object_data['languages'] = self::processLanguages(JsonLD::fetchElementArray($object, 'sc:inLanguage') ?? []);
1469 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
1470 $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
1471 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
1473 // Special treatment for Hubzilla links
1474 if (is_array($object_data['alternate-url'])) {
1475 $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href', '@id');
1477 if (!is_string($object_data['alternate-url'])) {
1478 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href', '@id');
1482 if (in_array($object_data['object_type'], ['as:Audio', 'as:Video'])) {
1483 $object_data['alternate-url'] = self::extractAlternateUrl($object['as:url'] ?? []) ?: $object_data['alternate-url'];
1484 $object_data['attachments'] = array_merge($object_data['attachments'], self::processAttachmentUrls($object['as:url'] ?? []));
1487 // For page types we expect that the alternate url posts to some page.
1488 // So we add this to the attachments if it differs from the id.
1489 // Currently only Lemmy is using the page type.
1490 if (($object_data['object_type'] == 'as:Page') && !empty($object_data['alternate-url']) && !Strings::compareLink($object_data['alternate-url'], $object_data['id'])) {
1491 $object_data['attachments'][] = ['url' => $object_data['alternate-url']];
1492 $object_data['alternate-url'] = null;
1495 $receiverdata = self::getReceivers($object, $object_data['actor'], $object_data['tags'], true);
1496 $receivers = $reception_types = [];
1497 foreach ($receiverdata as $key => $data) {
1498 $receivers[$key] = $data['uid'];
1499 $reception_types[$data['uid']] = $data['type'] ?? 0;
1502 $object_data['receiver'] = $receivers;
1503 $object_data['reception_type'] = $reception_types;
1505 $object_data['unlisted'] = in_array(-1, $object_data['receiver']);
1506 unset($object_data['receiver'][-1]);
1507 unset($object_data['reception_type'][-1]);
1509 // Common object data:
1512 // @context, type, actor, signature, mediaType, duration, replies, icon
1514 // Also missing: (Defined in the standard, but currently unused)
1515 // audience, preview, endTime, startTime, image
1520 // contentMap, announcement_count, announcements, context_id, likes, like_count
1521 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1526 // category, licence, language, commentsEnabled
1529 // views, waitTranscoding, state, support, subtitleLanguage
1530 // likes, dislikes, shares, comments
1532 return $object_data;