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(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
50 stristr(defaults($_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 $object_type = self::fetchObjectType($activity, $object_id, $uid);
199 // Fetch the content only on activities where this matters
200 if (in_array($type, ['as:Create', 'as:Update', 'as:Announce'])) {
201 if ($type == 'as:Announce') {
202 $trust_source = false;
204 $object_data = self::fetchObject($object_id, $activity['as:object'], $trust_source, $uid);
205 if (empty($object_data)) {
206 Logger::log("Object data couldn't be processed", Logger::DEBUG);
209 $object_data['object_id'] = $object_id;
211 // We had been able to retrieve the object data - so we can trust the source
212 $trust_source = true;
213 } elseif (in_array($type, ['as:Like', 'as:Dislike']) ||
214 (($type == 'as:Follow') && in_array($object_type, self::CONTENT_TYPES))) {
215 // Create a mostly empty array out of the activity data (instead of the object).
216 // This way we later don't have to check for the existence of ech individual array element.
217 $object_data = self::processObject($activity);
218 $object_data['name'] = $type;
219 $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
220 $object_data['object_id'] = $object_id;
221 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
224 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
225 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
226 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
227 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
228 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
230 // An Undo is done on the object of an object, so we need that type as well
231 if ($type == 'as:Undo') {
232 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $uid);
236 $object_data = self::addActivityFields($object_data, $activity);
238 if (empty($object_data['object_type'])) {
239 $object_data['object_type'] = $object_type;
242 $object_data['type'] = $type;
243 $object_data['actor'] = $actor;
244 $object_data['item_receiver'] = $receivers;
245 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
247 Logger::log('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], Logger::DEBUG);
253 * Fetches the first user id from the receiver array
255 * @param array $receivers Array with receivers
256 * @return integer user id;
258 public static function getFirstUserFromReceivers($receivers)
260 foreach ($receivers as $receiver) {
261 if (!empty($receiver)) {
269 * Store the unprocessed data into the conversation table
270 * This has to be done outside the regular function,
271 * since we store everything - not only item posts.
273 * @param array $activity Array with activity data
274 * @param string $body The raw message
277 private static function storeConversation($activity, $body)
279 if (empty($body) || empty($activity['id'])) {
284 'protocol' => Conversation::PARCEL_ACTIVITYPUB,
285 'item-uri' => $activity['id'],
286 'reply-to-uri' => defaults($activity, 'reply-to-id', ''),
287 'conversation-href' => defaults($activity, 'context', ''),
288 'conversation-uri' => defaults($activity, 'conversation', ''),
290 'received' => DateTimeFormat::utcNow()];
292 DBA::insert('conversation', $conversation, true);
296 * Processes the activity object
298 * @param array $activity Array with activity data
299 * @param string $body
300 * @param integer $uid User ID
301 * @param boolean $trust_source Do we trust the source?
304 public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
306 $type = JsonLD::fetchElement($activity, '@type');
308 Logger::log('Empty type', Logger::DEBUG);
312 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
313 Logger::log('Empty object', Logger::DEBUG);
317 if (!JsonLD::fetchElement($activity, 'as:actor', '@id')) {
318 Logger::log('Empty actor', Logger::DEBUG);
323 // Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
324 if ($trust_source && ($type == 'as:Create') && is_array($activity['as:object'])) {
325 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
326 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
327 $trust_source = ($actor == $attributed_to);
328 if (!$trust_source) {
329 Logger::log('Not trusting actor: ' . $actor . '. It differs from attributedTo: ' . $attributed_to, Logger::DEBUG);
333 // $trust_source is called by reference and is set to true if the content was retrieved successfully
334 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
335 if (empty($object_data)) {
336 Logger::log('No object data found', Logger::DEBUG);
340 if (!$trust_source) {
341 Logger::log('No trust for activity type "' . $type . '", so we quit now.', Logger::DEBUG);
345 // Only store content related stuff - and no announces, since they possibly overwrite the original content
346 if (in_array($object_data['object_type'], self::CONTENT_TYPES) && ($type != 'as:Announce')) {
347 self::storeConversation($object_data, $body);
350 // Internal flag for thread completion. See Processor.php
351 if (!empty($activity['thread-completion'])) {
352 $object_data['thread-completion'] = $activity['thread-completion'];
357 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
358 ActivityPub\Processor::createItem($object_data);
363 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
364 $profile = APContact::getByURL($object_data['actor']);
365 if ($profile['type'] == 'Person') {
366 // Reshared posts from persons appear as summary at the bottom
367 // If this isn't set, then a single reshare appears on top. This is used for groups.
368 $object_data['thread-completion'] = true;
370 ActivityPub\Processor::createItem($object_data);
372 // Add the bottom reshare information only for persons
373 if ($profile['type'] == 'Person') {
374 $announce_object_data = self::processObject($activity);
375 $announce_object_data['name'] = $type;
376 $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
377 $announce_object_data['object_id'] = $object_data['object_id'];
378 $announce_object_data['object_type'] = $object_data['object_type'];
380 ActivityPub\Processor::createActivity($announce_object_data, ACTIVITY2_ANNOUNCE);
386 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
387 ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE);
392 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
393 ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE);
397 case 'as:TentativeAccept':
398 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
399 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE);
404 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
405 ActivityPub\Processor::updateItem($object_data);
406 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
407 ActivityPub\Processor::updatePerson($object_data);
412 if ($object_data['object_type'] == 'as:Tombstone') {
413 ActivityPub\Processor::deleteItem($object_data);
414 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
415 ActivityPub\Processor::deletePerson($object_data);
420 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
421 ActivityPub\Processor::followUser($object_data);
422 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
423 $object_data['reply-to-id'] = $object_data['object_id'];
424 ActivityPub\Processor::createActivity($object_data, ACTIVITY_FOLLOW);
429 if ($object_data['object_type'] == 'as:Follow') {
430 ActivityPub\Processor::acceptFollowUser($object_data);
431 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
432 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND);
437 if ($object_data['object_type'] == 'as:Follow') {
438 ActivityPub\Processor::rejectFollowUser($object_data);
439 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
440 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO);
445 if (($object_data['object_type'] == 'as:Follow') &&
446 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
447 ActivityPub\Processor::undoFollowUser($object_data);
448 } elseif (($object_data['object_type'] == 'as:Accept') &&
449 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
450 ActivityPub\Processor::rejectFollowUser($object_data);
451 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
452 in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
453 ActivityPub\Processor::undoActivity($object_data);
458 Logger::log('Unknown activity: ' . $type . ' ' . $object_data['object_type'], Logger::DEBUG);
464 * Fetch the receiver list from an activity array
466 * @param array $activity
467 * @param string $actor
470 * @return array with receivers (user id)
473 private static function getReceivers($activity, $actor, $tags = [])
477 // When it is an answer, we inherite the receivers from the parent
478 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo', '@id');
479 if (!empty($replyto)) {
480 $parents = Item::select(['uid'], ['uri' => $replyto]);
481 while ($parent = Item::fetch($parents)) {
482 $receivers['uid:' . $parent['uid']] = $parent['uid'];
486 if (!empty($actor)) {
487 $profile = APContact::getByURL($actor);
488 $followers = defaults($profile, 'followers', '');
490 Logger::log('Actor: ' . $actor . ' - Followers: ' . $followers, Logger::DEBUG);
492 Logger::log('Empty actor', Logger::DEBUG);
496 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
497 $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
498 if (empty($receiver_list)) {
502 foreach ($receiver_list as $receiver) {
503 if ($receiver == self::PUBLIC_COLLECTION) {
504 $receivers['uid:0'] = 0;
507 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
508 // This will most likely catch all OStatus connections to Mastodon
509 $condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
510 , 'archive' => false, 'pending' => false];
511 $contacts = DBA::select('contact', ['uid'], $condition);
512 while ($contact = DBA::fetch($contacts)) {
513 if ($contact['uid'] != 0) {
514 $receivers['uid:' . $contact['uid']] = $contact['uid'];
517 DBA::close($contacts);
520 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
521 $receivers = array_merge($receivers, self::getReceiverForActor($actor, $tags));
525 // Fetching all directly addressed receivers
526 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
527 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
528 if (!DBA::isResult($contact)) {
532 // Check if the potential receiver is following the actor
533 // Exception: The receiver is targetted via "to" or this is a comment
534 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
535 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
536 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
537 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
539 // Forum posts are only accepted from forum contacts
540 if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
541 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
544 if (!DBA::exists('contact', $condition)) {
549 $receivers['uid:' . $contact['uid']] = $contact['uid'];
553 self::switchContacts($receivers, $actor);
559 * Fetch the receiver list of a given actor
561 * @param string $actor
564 * @return array with receivers (user id)
567 public static function getReceiverForActor($actor, $tags)
570 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
571 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
572 'network' => $networks, 'archive' => false, 'pending' => false];
573 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
574 while ($contact = DBA::fetch($contacts)) {
575 if (self::isValidReceiverForActor($contact, $actor, $tags)) {
576 $receivers['uid:' . $contact['uid']] = $contact['uid'];
579 DBA::close($contacts);
584 * Tests if the contact is a valid receiver for this actor
586 * @param array $contact
587 * @param string $actor
590 * @return bool with receivers (user id)
593 private static function isValidReceiverForActor($contact, $actor, $tags)
595 // Public contacts are no valid receiver
596 if ($contact['uid'] == 0) {
600 // Are we following the contact? Then this is a valid receiver
601 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
605 // When the possible receiver isn't a community, then it is no valid receiver
606 $owner = User::getOwnerDataById($contact['uid']);
607 if (empty($owner) || ($owner['contact-type'] != Contact::TYPE_COMMUNITY)) {
611 // Is the community account tagged?
612 foreach ($tags as $tag) {
613 if ($tag['type'] != 'Mention') {
617 if ($tag['href'] == $owner['url']) {
626 * Switches existing contacts to ActivityPub
628 * @param integer $cid Contact ID
629 * @param integer $uid User ID
630 * @param string $url Profile URL
631 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
632 * @throws \ImagickException
634 public static function switchContact($cid, $uid, $url)
636 $profile = ActivityPub::probeProfile($url);
637 if (empty($profile)) {
641 Logger::log('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' to ActivityPub');
643 $photo = defaults($profile, 'photo', null);
644 unset($profile['photo']);
645 unset($profile['baseurl']);
646 unset($profile['guid']);
648 $profile['nurl'] = Strings::normaliseLink($profile['url']);
649 DBA::update('contact', $profile, ['id' => $cid]);
651 Contact::updateAvatar($photo, $uid, $cid);
653 // Send a new follow request to be sure that the connection still exists
654 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND]])) {
655 ActivityPub\Transmitter::sendActivity('Follow', $profile['url'], $uid);
656 Logger::log('Send a new follow request to ' . $profile['url'] . ' for user ' . $uid, Logger::DEBUG);
665 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
666 * @throws \ImagickException
668 private static function switchContacts($receivers, $actor)
674 foreach ($receivers as $receiver) {
675 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
676 if (DBA::isResult($contact)) {
677 self::switchContact($contact['id'], $receiver, $actor);
680 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
681 if (DBA::isResult($contact)) {
682 self::switchContact($contact['id'], $receiver, $actor);
690 * @param $object_data
691 * @param array $activity
695 private static function addActivityFields($object_data, $activity)
697 if (!empty($activity['published']) && empty($object_data['published'])) {
698 $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
701 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
702 $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid', '@value');
705 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
706 $object_data['service'] = JsonLD::fetchElement($object_data, 'service', '@value');
712 * Fetches the object data from external ressources if needed
714 * @param string $object_id Object ID of the the provided object
715 * @param array $object The provided object array
716 * @param boolean $trust_source Do we trust the provided object?
717 * @param integer $uid User ID for the signature that we use to fetch data
719 * @return array with trusted and valid object data
720 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
721 * @throws \ImagickException
723 private static function fetchObject($object_id, $object = [], $trust_source = false, $uid = 0)
725 // By fetching the type we check if the object is complete.
726 $type = JsonLD::fetchElement($object, '@type');
728 if (!$trust_source || empty($type)) {
729 $data = ActivityPub::fetchContent($object_id, $uid);
731 $object = JsonLD::compact($data);
732 Logger::log('Fetched content for ' . $object_id, Logger::DEBUG);
734 Logger::log('Empty content for ' . $object_id . ', check if content is available locally.', Logger::DEBUG);
736 $item = Item::selectFirst([], ['uri' => $object_id]);
737 if (!DBA::isResult($item)) {
738 Logger::log('Object with url ' . $object_id . ' was not found locally.', Logger::DEBUG);
741 Logger::log('Using already stored item for url ' . $object_id, Logger::DEBUG);
742 $data = ActivityPub\Transmitter::createNote($item);
743 $object = JsonLD::compact($data);
746 Logger::log('Using original object for url ' . $object_id, Logger::DEBUG);
749 $type = JsonLD::fetchElement($object, '@type');
752 Logger::log('Empty type', Logger::DEBUG);
756 if (in_array($type, self::CONTENT_TYPES)) {
757 return self::processObject($object);
760 if ($type == 'as:Announce') {
761 $object_id = JsonLD::fetchElement($object, 'object', '@id');
762 if (empty($object_id)) {
765 return self::fetchObject($object_id, [], false, $uid);
768 Logger::log('Unhandled object type: ' . $type, Logger::DEBUG);
772 * Convert tags from JSON-LD format into a simplified format
774 * @param array $tags Tags in JSON-LD format
776 * @return array with tags in a simplified format
778 private static function processTags($tags)
786 foreach ($tags as $tag) {
791 $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
792 'href' => JsonLD::fetchElement($tag, 'as:href', '@id'),
793 'name' => JsonLD::fetchElement($tag, 'as:name', '@value')];
795 if (empty($element['type'])) {
799 $taglist[] = $element;
805 * Convert emojis from JSON-LD format into a simplified format
808 * @return array with emojis in a simplified format
810 private static function processEmojis($emojis)
814 if (empty($emojis)) {
818 foreach ($emojis as $emoji) {
819 if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
823 $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id');
824 $element = ['name' => JsonLD::fetchElement($emoji, 'as:name', '@value'),
827 $emojilist[] = $element;
833 * Convert attachments from JSON-LD format into a simplified format
835 * @param array $attachments Attachments in JSON-LD format
837 * @return array with attachmants in a simplified format
839 private static function processAttachments($attachments)
843 if (empty($attachments)) {
847 foreach ($attachments as $attachment) {
848 if (empty($attachment)) {
852 $attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
853 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
854 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
855 'url' => JsonLD::fetchElement($attachment, 'as:url', '@id')];
861 * Fetches data from the object part of an activity
863 * @param array $object
868 private static function processObject($object)
870 if (!JsonLD::fetchElement($object, '@id')) {
875 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
876 $object_data['id'] = JsonLD::fetchElement($object, '@id');
877 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id');
879 // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
880 if (empty($object_data['reply-to-id']) || ($object_data['reply-to-id'] == './')) {
881 $object_data['reply-to-id'] = $object_data['id'];
884 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
885 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
887 if (empty($object_data['updated'])) {
888 $object_data['updated'] = $object_data['published'];
891 if (empty($object_data['published']) && !empty($object_data['updated'])) {
892 $object_data['published'] = $object_data['updated'];
895 $actor = JsonLD::fetchElement($object, 'as:attributedTo', '@id');
897 $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
900 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
901 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
902 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
903 $object_data['actor'] = $object_data['author'] = $actor;
904 $object_data['context'] = JsonLD::fetchElement($object, 'as:context', '@id');
905 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation', '@id');
906 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
907 $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
908 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
909 $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
910 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
911 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
912 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
913 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
914 $object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
915 $object_data['location'] = JsonLD::fetchElement($object_data, 'location', '@value');
916 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
917 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
918 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
919 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
920 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
921 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
922 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
923 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
924 $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
925 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
927 // Special treatment for Hubzilla links
928 if (is_array($object_data['alternate-url'])) {
929 $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href', '@id');
931 if (!is_string($object_data['alternate-url'])) {
932 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href', '@id');
936 $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags']);
938 // Common object data:
941 // @context, type, actor, signature, mediaType, duration, replies, icon
943 // Also missing: (Defined in the standard, but currently unused)
944 // audience, preview, endTime, startTime, image
949 // contentMap, announcement_count, announcements, context_id, likes, like_count
950 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
955 // category, licence, language, commentsEnabled
958 // views, waitTranscoding, state, support, subtitleLanguage
959 // likes, dislikes, shares, comments