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;
210 $object_data['directmessage'] = JsonLD::fetchElement($activity, 'litepub:directMessage');
212 // We had been able to retrieve the object data - so we can trust the source
213 $trust_source = true;
214 } elseif (in_array($type, ['as:Like', 'as:Dislike']) ||
215 (($type == 'as:Follow') && in_array($object_type, self::CONTENT_TYPES))) {
216 // Create a mostly empty array out of the activity data (instead of the object).
217 // This way we later don't have to check for the existence of ech individual array element.
218 $object_data = self::processObject($activity);
219 $object_data['name'] = $type;
220 $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
221 $object_data['object_id'] = $object_id;
222 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
225 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
226 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
227 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
228 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
229 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
231 // An Undo is done on the object of an object, so we need that type as well
232 if ($type == 'as:Undo') {
233 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $uid);
237 $object_data = self::addActivityFields($object_data, $activity);
239 if (empty($object_data['object_type'])) {
240 $object_data['object_type'] = $object_type;
243 $object_data['type'] = $type;
244 $object_data['actor'] = $actor;
245 $object_data['item_receiver'] = $receivers;
246 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
248 Logger::log('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], Logger::DEBUG);
254 * Fetches the first user id from the receiver array
256 * @param array $receivers Array with receivers
257 * @return integer user id;
259 public static function getFirstUserFromReceivers($receivers)
261 foreach ($receivers as $receiver) {
262 if (!empty($receiver)) {
270 * Store the unprocessed data into the conversation table
271 * This has to be done outside the regular function,
272 * since we store everything - not only item posts.
274 * @param array $activity Array with activity data
275 * @param string $body The raw message
278 private static function storeConversation($activity, $body)
280 if (empty($body) || empty($activity['id'])) {
285 'protocol' => Conversation::PARCEL_ACTIVITYPUB,
286 'item-uri' => $activity['id'],
287 'reply-to-uri' => defaults($activity, 'reply-to-id', ''),
288 'conversation-href' => defaults($activity, 'context', ''),
289 'conversation-uri' => defaults($activity, 'conversation', ''),
291 'received' => DateTimeFormat::utcNow()];
293 DBA::insert('conversation', $conversation, true);
297 * Processes the activity object
299 * @param array $activity Array with activity data
300 * @param string $body
301 * @param integer $uid User ID
302 * @param boolean $trust_source Do we trust the source?
305 public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
307 $type = JsonLD::fetchElement($activity, '@type');
309 Logger::log('Empty type', Logger::DEBUG);
313 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
314 Logger::log('Empty object', Logger::DEBUG);
318 if (!JsonLD::fetchElement($activity, 'as:actor', '@id')) {
319 Logger::log('Empty actor', Logger::DEBUG);
324 // Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
325 if ($trust_source && ($type == 'as:Create') && is_array($activity['as:object'])) {
326 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
327 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
328 $trust_source = ($actor == $attributed_to);
329 if (!$trust_source) {
330 Logger::log('Not trusting actor: ' . $actor . '. It differs from attributedTo: ' . $attributed_to, Logger::DEBUG);
334 // $trust_source is called by reference and is set to true if the content was retrieved successfully
335 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
336 if (empty($object_data)) {
337 Logger::log('No object data found', Logger::DEBUG);
341 if (!$trust_source) {
342 Logger::log('No trust for activity type "' . $type . '", so we quit now.', Logger::DEBUG);
346 // Only store content related stuff - and no announces, since they possibly overwrite the original content
347 if (in_array($object_data['object_type'], self::CONTENT_TYPES) && ($type != 'as:Announce')) {
348 self::storeConversation($object_data, $body);
351 // Internal flag for thread completion. See Processor.php
352 if (!empty($activity['thread-completion'])) {
353 $object_data['thread-completion'] = $activity['thread-completion'];
358 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
359 ActivityPub\Processor::createItem($object_data);
364 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
365 $profile = APContact::getByURL($object_data['actor']);
366 if ($profile['type'] == 'Person') {
367 // Reshared posts from persons appear as summary at the bottom
368 // If this isn't set, then a single reshare appears on top. This is used for groups.
369 $object_data['thread-completion'] = true;
371 ActivityPub\Processor::createItem($object_data);
373 // Add the bottom reshare information only for persons
374 if ($profile['type'] == 'Person') {
375 $announce_object_data = self::processObject($activity);
376 $announce_object_data['name'] = $type;
377 $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
378 $announce_object_data['object_id'] = $object_data['object_id'];
379 $announce_object_data['object_type'] = $object_data['object_type'];
381 ActivityPub\Processor::createActivity($announce_object_data, ACTIVITY2_ANNOUNCE);
387 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
388 ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE);
393 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
394 ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE);
398 case 'as:TentativeAccept':
399 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
400 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE);
405 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
406 ActivityPub\Processor::updateItem($object_data);
407 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
408 ActivityPub\Processor::updatePerson($object_data);
413 if ($object_data['object_type'] == 'as:Tombstone') {
414 ActivityPub\Processor::deleteItem($object_data);
415 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
416 ActivityPub\Processor::deletePerson($object_data);
421 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
422 ActivityPub\Processor::followUser($object_data);
423 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
424 $object_data['reply-to-id'] = $object_data['object_id'];
425 ActivityPub\Processor::createActivity($object_data, ACTIVITY_FOLLOW);
430 if ($object_data['object_type'] == 'as:Follow') {
431 ActivityPub\Processor::acceptFollowUser($object_data);
432 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
433 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND);
438 if ($object_data['object_type'] == 'as:Follow') {
439 ActivityPub\Processor::rejectFollowUser($object_data);
440 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
441 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO);
446 if (($object_data['object_type'] == 'as:Follow') &&
447 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
448 ActivityPub\Processor::undoFollowUser($object_data);
449 } elseif (($object_data['object_type'] == 'as:Accept') &&
450 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
451 ActivityPub\Processor::rejectFollowUser($object_data);
452 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
453 in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
454 ActivityPub\Processor::undoActivity($object_data);
459 Logger::log('Unknown activity: ' . $type . ' ' . $object_data['object_type'], Logger::DEBUG);
465 * Fetch the receiver list from an activity array
467 * @param array $activity
468 * @param string $actor
471 * @return array with receivers (user id)
474 private static function getReceivers($activity, $actor, $tags = [])
478 // When it is an answer, we inherite the receivers from the parent
479 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo', '@id');
480 if (!empty($replyto)) {
481 $parents = Item::select(['uid'], ['uri' => $replyto]);
482 while ($parent = Item::fetch($parents)) {
483 $receivers['uid:' . $parent['uid']] = $parent['uid'];
487 if (!empty($actor)) {
488 $profile = APContact::getByURL($actor);
489 $followers = defaults($profile, 'followers', '');
491 Logger::log('Actor: ' . $actor . ' - Followers: ' . $followers, Logger::DEBUG);
493 Logger::log('Empty actor', Logger::DEBUG);
497 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
498 $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
499 if (empty($receiver_list)) {
503 foreach ($receiver_list as $receiver) {
504 if ($receiver == self::PUBLIC_COLLECTION) {
505 $receivers['uid:0'] = 0;
508 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
509 // This will most likely catch all OStatus connections to Mastodon
510 $condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
511 , 'archive' => false, 'pending' => false];
512 $contacts = DBA::select('contact', ['uid'], $condition);
513 while ($contact = DBA::fetch($contacts)) {
514 if ($contact['uid'] != 0) {
515 $receivers['uid:' . $contact['uid']] = $contact['uid'];
518 DBA::close($contacts);
521 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
522 $receivers = array_merge($receivers, self::getReceiverForActor($actor, $tags));
526 // Fetching all directly addressed receivers
527 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
528 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
529 if (!DBA::isResult($contact)) {
533 // Check if the potential receiver is following the actor
534 // Exception: The receiver is targetted via "to" or this is a comment
535 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
536 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
537 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
538 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
540 // Forum posts are only accepted from forum contacts
541 if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
542 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
545 if (!DBA::exists('contact', $condition)) {
550 $receivers['uid:' . $contact['uid']] = $contact['uid'];
554 self::switchContacts($receivers, $actor);
560 * Fetch the receiver list of a given actor
562 * @param string $actor
565 * @return array with receivers (user id)
568 public static function getReceiverForActor($actor, $tags)
571 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
572 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
573 'network' => $networks, 'archive' => false, 'pending' => false];
574 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
575 while ($contact = DBA::fetch($contacts)) {
576 if (self::isValidReceiverForActor($contact, $actor, $tags)) {
577 $receivers['uid:' . $contact['uid']] = $contact['uid'];
580 DBA::close($contacts);
585 * Tests if the contact is a valid receiver for this actor
587 * @param array $contact
588 * @param string $actor
591 * @return bool with receivers (user id)
594 private static function isValidReceiverForActor($contact, $actor, $tags)
596 // Public contacts are no valid receiver
597 if ($contact['uid'] == 0) {
601 // Are we following the contact? Then this is a valid receiver
602 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
606 // When the possible receiver isn't a community, then it is no valid receiver
607 $owner = User::getOwnerDataById($contact['uid']);
608 if (empty($owner) || ($owner['contact-type'] != Contact::TYPE_COMMUNITY)) {
612 // Is the community account tagged?
613 foreach ($tags as $tag) {
614 if ($tag['type'] != 'Mention') {
618 if ($tag['href'] == $owner['url']) {
627 * Switches existing contacts to ActivityPub
629 * @param integer $cid Contact ID
630 * @param integer $uid User ID
631 * @param string $url Profile URL
632 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
633 * @throws \ImagickException
635 public static function switchContact($cid, $uid, $url)
637 $profile = ActivityPub::probeProfile($url);
638 if (empty($profile)) {
642 Logger::log('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' to ActivityPub');
644 $photo = defaults($profile, 'photo', null);
645 unset($profile['photo']);
646 unset($profile['baseurl']);
647 unset($profile['guid']);
649 $profile['nurl'] = Strings::normaliseLink($profile['url']);
650 DBA::update('contact', $profile, ['id' => $cid]);
652 Contact::updateAvatar($photo, $uid, $cid);
654 // Send a new follow request to be sure that the connection still exists
655 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND]])) {
656 ActivityPub\Transmitter::sendActivity('Follow', $profile['url'], $uid);
657 Logger::log('Send a new follow request to ' . $profile['url'] . ' for user ' . $uid, Logger::DEBUG);
666 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
667 * @throws \ImagickException
669 private static function switchContacts($receivers, $actor)
675 foreach ($receivers as $receiver) {
676 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
677 if (DBA::isResult($contact)) {
678 self::switchContact($contact['id'], $receiver, $actor);
681 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
682 if (DBA::isResult($contact)) {
683 self::switchContact($contact['id'], $receiver, $actor);
691 * @param $object_data
692 * @param array $activity
696 private static function addActivityFields($object_data, $activity)
698 if (!empty($activity['published']) && empty($object_data['published'])) {
699 $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
702 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
703 $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid', '@value');
706 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
707 $object_data['service'] = JsonLD::fetchElement($object_data, 'service', '@value');
713 * Fetches the object data from external ressources if needed
715 * @param string $object_id Object ID of the the provided object
716 * @param array $object The provided object array
717 * @param boolean $trust_source Do we trust the provided object?
718 * @param integer $uid User ID for the signature that we use to fetch data
720 * @return array with trusted and valid object data
721 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
722 * @throws \ImagickException
724 private static function fetchObject($object_id, $object = [], $trust_source = false, $uid = 0)
726 // By fetching the type we check if the object is complete.
727 $type = JsonLD::fetchElement($object, '@type');
729 if (!$trust_source || empty($type)) {
730 $data = ActivityPub::fetchContent($object_id, $uid);
732 $object = JsonLD::compact($data);
733 Logger::log('Fetched content for ' . $object_id, Logger::DEBUG);
735 Logger::log('Empty content for ' . $object_id . ', check if content is available locally.', Logger::DEBUG);
737 $item = Item::selectFirst([], ['uri' => $object_id]);
738 if (!DBA::isResult($item)) {
739 Logger::log('Object with url ' . $object_id . ' was not found locally.', Logger::DEBUG);
742 Logger::log('Using already stored item for url ' . $object_id, Logger::DEBUG);
743 $data = ActivityPub\Transmitter::createNote($item);
744 $object = JsonLD::compact($data);
747 Logger::log('Using original object for url ' . $object_id, Logger::DEBUG);
750 $type = JsonLD::fetchElement($object, '@type');
753 Logger::log('Empty type', Logger::DEBUG);
757 if (in_array($type, self::CONTENT_TYPES)) {
758 return self::processObject($object);
761 if ($type == 'as:Announce') {
762 $object_id = JsonLD::fetchElement($object, 'object', '@id');
763 if (empty($object_id)) {
766 return self::fetchObject($object_id, [], false, $uid);
769 Logger::log('Unhandled object type: ' . $type, Logger::DEBUG);
773 * Convert tags from JSON-LD format into a simplified format
775 * @param array $tags Tags in JSON-LD format
777 * @return array with tags in a simplified format
779 private static function processTags($tags)
787 foreach ($tags as $tag) {
792 $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
793 'href' => JsonLD::fetchElement($tag, 'as:href', '@id'),
794 'name' => JsonLD::fetchElement($tag, 'as:name', '@value')];
796 if (empty($element['type'])) {
800 $taglist[] = $element;
806 * Convert emojis from JSON-LD format into a simplified format
809 * @return array with emojis in a simplified format
811 private static function processEmojis($emojis)
815 if (empty($emojis)) {
819 foreach ($emojis as $emoji) {
820 if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
824 $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id');
825 $element = ['name' => JsonLD::fetchElement($emoji, 'as:name', '@value'),
828 $emojilist[] = $element;
834 * Convert attachments from JSON-LD format into a simplified format
836 * @param array $attachments Attachments in JSON-LD format
838 * @return array with attachmants in a simplified format
840 private static function processAttachments($attachments)
844 if (empty($attachments)) {
848 foreach ($attachments as $attachment) {
849 if (empty($attachment)) {
853 $attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
854 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
855 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
856 'url' => JsonLD::fetchElement($attachment, 'as:url', '@id')];
862 * Fetches data from the object part of an activity
864 * @param array $object
869 private static function processObject($object)
871 if (!JsonLD::fetchElement($object, '@id')) {
876 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
877 $object_data['id'] = JsonLD::fetchElement($object, '@id');
878 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id');
880 // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
881 if (empty($object_data['reply-to-id']) || ($object_data['reply-to-id'] == './')) {
882 $object_data['reply-to-id'] = $object_data['id'];
885 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
886 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
888 if (empty($object_data['updated'])) {
889 $object_data['updated'] = $object_data['published'];
892 if (empty($object_data['published']) && !empty($object_data['updated'])) {
893 $object_data['published'] = $object_data['updated'];
896 $actor = JsonLD::fetchElement($object, 'as:attributedTo', '@id');
898 $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
901 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
902 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
903 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
904 $object_data['actor'] = $object_data['author'] = $actor;
905 $object_data['context'] = JsonLD::fetchElement($object, 'as:context', '@id');
906 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation', '@id');
907 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
908 $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
909 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
910 $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
911 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
912 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
913 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
914 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
915 $object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
916 $object_data['location'] = JsonLD::fetchElement($object_data, 'location', '@value');
917 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
918 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
919 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
920 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
921 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
922 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
923 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
924 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
925 $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
926 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
928 // Special treatment for Hubzilla links
929 if (is_array($object_data['alternate-url'])) {
930 $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href', '@id');
932 if (!is_string($object_data['alternate-url'])) {
933 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href', '@id');
937 $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags']);
939 // Test if it is an answer to a mail
940 if (DBA::exists('mail', ['uri' => $object_data['reply-to-id']])) {
941 $object_data['directmessage'] = true;
944 // Common object data:
947 // @context, type, actor, signature, mediaType, duration, replies, icon
949 // Also missing: (Defined in the standard, but currently unused)
950 // audience, preview, endTime, startTime, image
955 // contentMap, announcement_count, announcements, context_id, likes, like_count
956 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
961 // category, licence, language, commentsEnabled
964 // views, waitTranscoding, state, support, subtitleLanguage
965 // likes, dislikes, shares, comments