3 * @file src/Protocol/ActivityPub/Receiver.php
5 namespace Friendica\Protocol\ActivityPub;
7 use Friendica\Database\DBA;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Protocol;
10 use Friendica\Model\Contact;
11 use Friendica\Model\APContact;
12 use Friendica\Model\Conversation;
13 use Friendica\Model\Item;
14 use Friendica\Model\User;
15 use Friendica\Protocol\ActivityPub;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\HTTPSignature;
18 use Friendica\Util\JsonLD;
19 use Friendica\Util\LDSignature;
20 use Friendica\Util\Strings;
23 * @brief ActivityPub Receiver Protocol class
28 * Check what this is meant to do:
37 const PUBLIC_COLLECTION = 'as:Public';
38 const ACCOUNT_TYPES = ['as:Person', 'as:Organization', 'as:Service', 'as:Group', 'as:Application'];
39 const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event'];
40 const ACTIVITY_TYPES = ['as:Like', 'as:Dislike', 'as:Accept', 'as:Reject', 'as:TentativeAccept'];
43 * Checks if the web request is done for the AP protocol
45 * @return bool is it AP?
47 public static function isRequest()
49 return stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
50 stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
54 * Checks incoming message from the inbox
58 * @param integer $uid User ID
61 public static function processInbox($body, $header, $uid)
63 $http_signer = HTTPSignature::getSigner($body, $header);
64 if (empty($http_signer)) {
65 Logger::warning('Invalid HTTP signature, message will be discarded.');
68 Logger::info('Valid HTTP signature', ['signer' => $http_signer]);
71 $activity = json_decode($body, true);
73 if (empty($activity)) {
74 Logger::warning('Invalid body.');
78 $ldactivity = JsonLD::compact($activity);
80 $actor = JsonLD::fetchElement($ldactivity, 'as:actor', '@id');
82 Logger::info('Message for user ' . $uid . ' is from actor ' . $actor);
84 if (LDSignature::isSigned($activity)) {
85 $ld_signer = LDSignature::getSigner($activity);
86 if (empty($ld_signer)) {
87 Logger::log('Invalid JSON-LD signature from ' . $actor, Logger::DEBUG);
89 if (!empty($ld_signer && ($actor == $http_signer))) {
90 Logger::log('The HTTP and the JSON-LD signature belong to ' . $ld_signer, Logger::DEBUG);
92 } elseif (!empty($ld_signer)) {
93 Logger::log('JSON-LD signature is signed by ' . $ld_signer, Logger::DEBUG);
95 } elseif ($actor == $http_signer) {
96 Logger::log('Bad JSON-LD signature, but HTTP signer fits the actor.', Logger::DEBUG);
99 Logger::log('Invalid JSON-LD signature and the HTTP signer is different.', Logger::DEBUG);
100 $trust_source = false;
102 } elseif ($actor == $http_signer) {
103 Logger::log('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', Logger::DEBUG);
104 $trust_source = true;
106 Logger::log('No JSON-LD signature, different actor.', Logger::DEBUG);
107 $trust_source = false;
110 self::processActivity($ldactivity, $body, $uid, $trust_source);
114 * Fetches the object type for a given object id
116 * @param array $activity
117 * @param string $object_id Object ID of the the provided object
118 * @param integer $uid User ID
120 * @return string with object type
121 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
122 * @throws \ImagickException
124 private static function fetchObjectType($activity, $object_id, $uid = 0)
126 if (!empty($activity['as:object'])) {
127 $object_type = JsonLD::fetchElement($activity['as:object'], '@type');
128 if (!empty($object_type)) {
133 if (Item::exists(['uri' => $object_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]])) {
134 // We just assume "note" since it doesn't make a difference for the further processing
138 $profile = APContact::getByURL($object_id);
139 if (!empty($profile['type'])) {
140 return 'as:' . $profile['type'];
143 $data = ActivityPub::fetchContent($object_id, $uid);
145 $object = JsonLD::compact($data);
146 $type = JsonLD::fetchElement($object, '@type');
156 * Prepare the object array
158 * @param array $activity
159 * @param integer $uid User ID
160 * @param $trust_source
162 * @return array with object data
163 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
164 * @throws \ImagickException
166 private static function prepareObjectData($activity, $uid, &$trust_source)
168 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
170 Logger::log('Empty actor', Logger::DEBUG);
174 $type = JsonLD::fetchElement($activity, '@type');
176 // Fetch all receivers from to, cc, bto and bcc
177 $receivers = self::getReceivers($activity, $actor);
179 // When it is a delivery to a personal inbox we add that user to the receivers
181 $additional = ['uid:' . $uid => $uid];
182 $receivers = array_merge($receivers, $additional);
184 // We possibly need some user to fetch private content,
185 // so we fetch the first out ot the list.
186 $uid = self::getFirstUserFromReceivers($receivers);
189 Logger::log('Receivers: ' . $uid . ' - ' . json_encode($receivers), Logger::DEBUG);
191 $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
192 if (empty($object_id)) {
193 Logger::log('No object found', Logger::DEBUG);
197 if (!is_string($object_id)) {
198 Logger::info('Invalid object id', ['object' => $object_id]);
202 $object_type = self::fetchObjectType($activity, $object_id, $uid);
204 // Fetch the content only on activities where this matters
205 if (in_array($type, ['as:Create', 'as:Update', 'as:Announce'])) {
206 if ($type == 'as:Announce') {
207 $trust_source = false;
209 $object_data = self::fetchObject($object_id, $activity['as:object'], $trust_source, $uid);
210 if (empty($object_data)) {
211 Logger::log("Object data couldn't be processed", Logger::DEBUG);
214 $object_data['object_id'] = $object_id;
216 // Test if it is an answer to a mail
217 if (DBA::exists('mail', ['uri' => $object_data['reply-to-id']])) {
218 $object_data['directmessage'] = true;
220 $object_data['directmessage'] = JsonLD::fetchElement($activity, 'litepub:directMessage');
223 // We had been able to retrieve the object data - so we can trust the source
224 $trust_source = true;
225 } elseif (in_array($type, array_merge(self::ACTIVITY_TYPES, ['as:Follow'])) && in_array($object_type, self::CONTENT_TYPES)) {
226 // Create a mostly empty array out of the activity data (instead of the object).
227 // This way we later don't have to check for the existence of ech individual array element.
228 $object_data = self::processObject($activity);
229 $object_data['name'] = $type;
230 $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
231 $object_data['object_id'] = $object_id;
232 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
233 } elseif (in_array($type, ['as:Add'])) {
235 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
236 $object_data['target_id'] = JsonLD::fetchElement($activity, 'as:target', '@id');
237 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
238 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
239 $object_data['object_content'] = JsonLD::fetchElement($activity['as:object'], 'as:content', '@type');
242 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
243 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
244 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
245 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
246 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
248 // An Undo is done on the object of an object, so we need that type as well
249 if ($type == 'as:Undo') {
250 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $uid);
254 $object_data = self::addActivityFields($object_data, $activity);
256 if (empty($object_data['object_type'])) {
257 $object_data['object_type'] = $object_type;
260 $object_data['type'] = $type;
261 $object_data['actor'] = $actor;
262 $object_data['item_receiver'] = $receivers;
263 $object_data['receiver'] = array_merge($object_data['receiver'] ?? [], $receivers);
265 Logger::log('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], Logger::DEBUG);
271 * Fetches the first user id from the receiver array
273 * @param array $receivers Array with receivers
274 * @return integer user id;
276 public static function getFirstUserFromReceivers($receivers)
278 foreach ($receivers as $receiver) {
279 if (!empty($receiver)) {
287 * Store the unprocessed data into the conversation table
288 * This has to be done outside the regular function,
289 * since we store everything - not only item posts.
291 * @param array $activity Array with activity data
292 * @param string $body The raw message
295 private static function storeConversation($activity, $body)
297 if (empty($body) || empty($activity['id'])) {
302 'protocol' => Conversation::PARCEL_ACTIVITYPUB,
303 'item-uri' => $activity['id'],
304 'reply-to-uri' => $activity['reply-to-id'] ?? '',
305 'conversation-href' => $activity['context'] ?? '',
306 'conversation-uri' => $activity['conversation'] ?? '',
308 'received' => DateTimeFormat::utcNow()];
310 DBA::insert('conversation', $conversation, true);
314 * Processes the activity object
316 * @param array $activity Array with activity data
317 * @param string $body
318 * @param integer $uid User ID
319 * @param boolean $trust_source Do we trust the source?
322 public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
324 $type = JsonLD::fetchElement($activity, '@type');
326 Logger::log('Empty type', Logger::DEBUG);
330 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
331 Logger::log('Empty object', Logger::DEBUG);
335 if (!JsonLD::fetchElement($activity, 'as:actor', '@id')) {
336 Logger::log('Empty actor', Logger::DEBUG);
341 // Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
342 if ($trust_source && ($type == 'as:Create') && is_array($activity['as:object'])) {
343 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
344 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
345 $trust_source = ($actor == $attributed_to);
346 if (!$trust_source) {
347 Logger::log('Not trusting actor: ' . $actor . '. It differs from attributedTo: ' . $attributed_to, Logger::DEBUG);
351 // $trust_source is called by reference and is set to true if the content was retrieved successfully
352 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
353 if (empty($object_data)) {
354 Logger::log('No object data found', Logger::DEBUG);
358 if (!$trust_source) {
359 Logger::log('No trust for activity type "' . $type . '", so we quit now.', Logger::DEBUG);
363 // Only store content related stuff - and no announces, since they possibly overwrite the original content
364 if (in_array($object_data['object_type'], self::CONTENT_TYPES) && ($type != 'as:Announce')) {
365 self::storeConversation($object_data, $body);
368 // Internal flag for thread completion. See Processor.php
369 if (!empty($activity['thread-completion'])) {
370 $object_data['thread-completion'] = $activity['thread-completion'];
375 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
376 ActivityPub\Processor::createItem($object_data);
381 if ($object_data['object_type'] == 'as:tag') {
382 ActivityPub\Processor::addTag($object_data);
387 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
388 $profile = APContact::getByURL($object_data['actor']);
389 // Reshared posts from persons appear as summary at the bottom
390 // If this isn't set, then a single reshare appears on top. This is used for groups.
391 $object_data['thread-completion'] = ($profile['type'] != 'Group');
393 ActivityPub\Processor::createItem($object_data);
395 // Add the bottom reshare information only for persons
396 if ($profile['type'] != 'Group') {
397 $announce_object_data = self::processObject($activity);
398 $announce_object_data['name'] = $type;
399 $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
400 $announce_object_data['object_id'] = $object_data['object_id'];
401 $announce_object_data['object_type'] = $object_data['object_type'];
403 ActivityPub\Processor::createActivity($announce_object_data, ACTIVITY2_ANNOUNCE);
409 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
410 ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE);
415 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
416 ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE);
420 case 'as:TentativeAccept':
421 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
422 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE);
427 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
428 ActivityPub\Processor::updateItem($object_data);
429 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
430 ActivityPub\Processor::updatePerson($object_data);
435 if ($object_data['object_type'] == 'as:Tombstone') {
436 ActivityPub\Processor::deleteItem($object_data);
437 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
438 ActivityPub\Processor::deletePerson($object_data);
443 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
444 ActivityPub\Processor::followUser($object_data);
445 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
446 $object_data['reply-to-id'] = $object_data['object_id'];
447 ActivityPub\Processor::createActivity($object_data, ACTIVITY_FOLLOW);
452 if ($object_data['object_type'] == 'as:Follow') {
453 ActivityPub\Processor::acceptFollowUser($object_data);
454 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
455 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND);
460 if ($object_data['object_type'] == 'as:Follow') {
461 ActivityPub\Processor::rejectFollowUser($object_data);
462 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
463 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO);
468 if (($object_data['object_type'] == 'as:Follow') &&
469 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
470 ActivityPub\Processor::undoFollowUser($object_data);
471 } elseif (($object_data['object_type'] == 'as:Accept') &&
472 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
473 ActivityPub\Processor::rejectFollowUser($object_data);
474 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
475 in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
476 ActivityPub\Processor::undoActivity($object_data);
481 Logger::log('Unknown activity: ' . $type . ' ' . $object_data['object_type'], Logger::DEBUG);
487 * Fetch the receiver list from an activity array
489 * @param array $activity
490 * @param string $actor
493 * @return array with receivers (user id)
496 private static function getReceivers($activity, $actor, $tags = [])
500 // When it is an answer, we inherite the receivers from the parent
501 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo', '@id');
502 if (!empty($replyto)) {
503 $parents = Item::select(['uid'], ['uri' => $replyto]);
504 while ($parent = Item::fetch($parents)) {
505 $receivers['uid:' . $parent['uid']] = $parent['uid'];
509 if (!empty($actor)) {
510 $profile = APContact::getByURL($actor);
511 $followers = $profile['followers'] ?? '';
513 Logger::log('Actor: ' . $actor . ' - Followers: ' . $followers, Logger::DEBUG);
515 Logger::log('Empty actor', Logger::DEBUG);
519 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
520 $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
521 if (empty($receiver_list)) {
525 foreach ($receiver_list as $receiver) {
526 if ($receiver == self::PUBLIC_COLLECTION) {
527 $receivers['uid:0'] = 0;
530 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
531 // This will most likely catch all OStatus connections to Mastodon
532 $condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
533 , 'archive' => false, 'pending' => false];
534 $contacts = DBA::select('contact', ['uid'], $condition);
535 while ($contact = DBA::fetch($contacts)) {
536 if ($contact['uid'] != 0) {
537 $receivers['uid:' . $contact['uid']] = $contact['uid'];
540 DBA::close($contacts);
543 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
544 $receivers = array_merge($receivers, self::getReceiverForActor($actor, $tags));
548 // Fetching all directly addressed receivers
549 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
550 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
551 if (!DBA::isResult($contact)) {
555 // Check if the potential receiver is following the actor
556 // Exception: The receiver is targetted via "to" or this is a comment
557 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
558 $networks = Protocol::FEDERATED;
559 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
560 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
562 // Forum posts are only accepted from forum contacts
563 if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
564 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
567 if (!DBA::exists('contact', $condition)) {
572 $receivers['uid:' . $contact['uid']] = $contact['uid'];
576 self::switchContacts($receivers, $actor);
582 * Fetch the receiver list of a given actor
584 * @param string $actor
587 * @return array with receivers (user id)
590 public static function getReceiverForActor($actor, $tags)
593 $networks = Protocol::FEDERATED;
594 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
595 'network' => $networks, 'archive' => false, 'pending' => false];
596 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
597 while ($contact = DBA::fetch($contacts)) {
598 if (self::isValidReceiverForActor($contact, $actor, $tags)) {
599 $receivers['uid:' . $contact['uid']] = $contact['uid'];
602 DBA::close($contacts);
607 * Tests if the contact is a valid receiver for this actor
609 * @param array $contact
610 * @param string $actor
613 * @return bool with receivers (user id)
616 private static function isValidReceiverForActor($contact, $actor, $tags)
618 // Public contacts are no valid receiver
619 if ($contact['uid'] == 0) {
623 // Are we following the contact? Then this is a valid receiver
624 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
628 // When the possible receiver isn't a community, then it is no valid receiver
629 $owner = User::getOwnerDataById($contact['uid']);
630 if (empty($owner) || ($owner['contact-type'] != Contact::TYPE_COMMUNITY)) {
634 // Is the community account tagged?
635 foreach ($tags as $tag) {
636 if ($tag['type'] != 'Mention') {
640 if ($tag['href'] == $owner['url']) {
649 * Switches existing contacts to ActivityPub
651 * @param integer $cid Contact ID
652 * @param integer $uid User ID
653 * @param string $url Profile URL
654 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
655 * @throws \ImagickException
657 public static function switchContact($cid, $uid, $url)
659 if (DBA::exists('contact', ['id' => $cid, 'network' => Protocol::ACTIVITYPUB])) {
660 Logger::info('Contact is already ActivityPub', ['id' => $cid, 'uid' => $uid, 'url' => $url]);
664 if (Contact::updateFromProbe($cid, '', true)) {
665 Logger::info('Update was successful', ['id' => $cid, 'uid' => $uid, 'url' => $url]);
668 // Send a new follow request to be sure that the connection still exists
669 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB])) {
670 Logger::info('Contact had been switched to ActivityPub. Sending a new follow request.', ['uid' => $uid, 'url' => $url]);
671 ActivityPub\Transmitter::sendActivity('Follow', $url, $uid);
680 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
681 * @throws \ImagickException
683 private static function switchContacts($receivers, $actor)
689 foreach ($receivers as $receiver) {
690 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
691 if (DBA::isResult($contact)) {
692 self::switchContact($contact['id'], $receiver, $actor);
695 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
696 if (DBA::isResult($contact)) {
697 self::switchContact($contact['id'], $receiver, $actor);
705 * @param $object_data
706 * @param array $activity
710 private static function addActivityFields($object_data, $activity)
712 if (!empty($activity['published']) && empty($object_data['published'])) {
713 $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
716 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
717 $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid', '@value');
720 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
721 $object_data['service'] = JsonLD::fetchElement($object_data, 'service', '@value');
727 * Fetches the object data from external ressources if needed
729 * @param string $object_id Object ID of the the provided object
730 * @param array $object The provided object array
731 * @param boolean $trust_source Do we trust the provided object?
732 * @param integer $uid User ID for the signature that we use to fetch data
734 * @return array|false with trusted and valid object data
735 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
736 * @throws \ImagickException
738 private static function fetchObject(string $object_id, array $object = [], bool $trust_source = false, int $uid = 0)
740 // By fetching the type we check if the object is complete.
741 $type = JsonLD::fetchElement($object, '@type');
743 if (!$trust_source || empty($type)) {
744 $data = ActivityPub::fetchContent($object_id, $uid);
746 $object = JsonLD::compact($data);
747 Logger::log('Fetched content for ' . $object_id, Logger::DEBUG);
749 Logger::log('Empty content for ' . $object_id . ', check if content is available locally.', Logger::DEBUG);
751 $item = Item::selectFirst([], ['uri' => $object_id]);
752 if (!DBA::isResult($item)) {
753 Logger::log('Object with url ' . $object_id . ' was not found locally.', Logger::DEBUG);
756 Logger::log('Using already stored item for url ' . $object_id, Logger::DEBUG);
757 $data = ActivityPub\Transmitter::createNote($item);
758 $object = JsonLD::compact($data);
761 Logger::log('Using original object for url ' . $object_id, Logger::DEBUG);
764 $type = JsonLD::fetchElement($object, '@type');
767 Logger::log('Empty type', Logger::DEBUG);
771 if (in_array($type, self::CONTENT_TYPES)) {
772 return self::processObject($object);
775 if ($type == 'as:Announce') {
776 $object_id = JsonLD::fetchElement($object, 'object', '@id');
777 if (empty($object_id) || !is_string($object_id)) {
780 return self::fetchObject($object_id, [], false, $uid);
783 Logger::log('Unhandled object type: ' . $type, Logger::DEBUG);
788 * Convert tags from JSON-LD format into a simplified format
790 * @param array $tags Tags in JSON-LD format
792 * @return array with tags in a simplified format
794 private static function processTags($tags)
802 foreach ($tags as $tag) {
807 $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
808 'href' => JsonLD::fetchElement($tag, 'as:href', '@id'),
809 'name' => JsonLD::fetchElement($tag, 'as:name', '@value')];
811 if (empty($element['type'])) {
815 $taglist[] = $element;
821 * Convert emojis from JSON-LD format into a simplified format
824 * @return array with emojis in a simplified format
826 private static function processEmojis($emojis)
830 if (empty($emojis)) {
834 foreach ($emojis as $emoji) {
835 if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
839 $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id');
840 $element = ['name' => JsonLD::fetchElement($emoji, 'as:name', '@value'),
843 $emojilist[] = $element;
849 * Convert attachments from JSON-LD format into a simplified format
851 * @param array $attachments Attachments in JSON-LD format
853 * @return array with attachmants in a simplified format
855 private static function processAttachments($attachments)
859 if (empty($attachments)) {
863 foreach ($attachments as $attachment) {
864 if (empty($attachment)) {
868 $attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
869 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
870 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
871 'url' => JsonLD::fetchElement($attachment, 'as:url', '@id')];
877 * Fetches data from the object part of an activity
879 * @param array $object
884 private static function processObject($object)
886 if (!JsonLD::fetchElement($object, '@id')) {
891 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
892 $object_data['id'] = JsonLD::fetchElement($object, '@id');
893 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id');
895 // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
896 if (empty($object_data['reply-to-id']) || ($object_data['reply-to-id'] == './')) {
897 $object_data['reply-to-id'] = $object_data['id'];
900 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
901 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
903 if (empty($object_data['updated'])) {
904 $object_data['updated'] = $object_data['published'];
907 if (empty($object_data['published']) && !empty($object_data['updated'])) {
908 $object_data['published'] = $object_data['updated'];
911 $actor = JsonLD::fetchElement($object, 'as:attributedTo', '@id');
913 $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
916 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
917 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
918 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
919 $object_data['actor'] = $object_data['author'] = $actor;
920 $object_data['context'] = JsonLD::fetchElement($object, 'as:context', '@id');
921 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation', '@id');
922 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
923 $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
924 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
925 $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
926 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
927 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
928 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
929 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
930 $object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
931 $object_data['location'] = JsonLD::fetchElement($object_data, 'location', '@value');
932 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
933 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
934 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
935 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
936 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
937 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
938 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
939 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
940 $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
941 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
943 // Special treatment for Hubzilla links
944 if (is_array($object_data['alternate-url'])) {
945 $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href', '@id');
947 if (!is_string($object_data['alternate-url'])) {
948 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href', '@id');
952 $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags']);
954 // Common object data:
957 // @context, type, actor, signature, mediaType, duration, replies, icon
959 // Also missing: (Defined in the standard, but currently unused)
960 // audience, preview, endTime, startTime, image
965 // contentMap, announcement_count, announcements, context_id, likes, like_count
966 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
971 // category, licence, language, commentsEnabled
974 // views, waitTranscoding, state, support, subtitleLanguage
975 // likes, dislikes, shares, comments