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::log('Invalid HTTP signature, message will be discarded.', Logger::DEBUG);
68 Logger::log('HTTP signature is signed by ' . $http_signer, Logger::DEBUG);
71 $activity = json_decode($body, true);
73 if (empty($activity)) {
74 Logger::log('Invalid body.', Logger::DEBUG);
78 $ldactivity = JsonLD::compact($activity);
80 $actor = JsonLD::fetchElement($ldactivity, 'as:actor');
82 Logger::log('Message for user ' . $uid . ' is from actor ' . $actor, Logger::DEBUG);
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');
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');
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 // We had been able to retrieve the object data - so we can trust the source
210 $trust_source = true;
211 } elseif (in_array($type, ['as:Like', 'as:Dislike']) ||
212 (($type == 'as:Follow') && in_array($object_type, self::CONTENT_TYPES))) {
213 // Create a mostly empty array out of the activity data (instead of the object).
214 // This way we later don't have to check for the existence of ech individual array element.
215 $object_data = self::processObject($activity);
216 $object_data['name'] = $type;
217 $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor');
218 $object_data['object_id'] = $object_id;
219 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
222 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
223 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object');
224 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor');
225 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
226 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
228 // An Undo is done on the object of an object, so we need that type as well
229 if ($type == 'as:Undo') {
230 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $uid);
234 $object_data = self::addActivityFields($object_data, $activity);
236 if (empty($object_data['object_type'])) {
237 $object_data['object_type'] = $object_type;
240 $object_data['type'] = $type;
241 $object_data['actor'] = $actor;
242 $object_data['item_receiver'] = $receivers;
243 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
245 Logger::log('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], Logger::DEBUG);
251 * Fetches the first user id from the receiver array
253 * @param array $receivers Array with receivers
254 * @return integer user id;
256 public static function getFirstUserFromReceivers($receivers)
258 foreach ($receivers as $receiver) {
259 if (!empty($receiver)) {
267 * Store the unprocessed data into the conversation table
268 * This has to be done outside the regular function,
269 * since we store everything - not only item posts.
271 * @param array $activity Array with activity data
272 * @param string $body The raw message
275 private static function storeConversation($activity, $body)
277 if (empty($body) || empty($activity['id'])) {
282 'protocol' => Conversation::PARCEL_ACTIVITYPUB,
283 'item-uri' => $activity['id'],
284 'reply-to-uri' => defaults($activity, 'reply-to-id', ''),
285 'conversation-href' => defaults($activity, 'context', ''),
286 'conversation-uri' => defaults($activity, 'conversation', ''),
288 'received' => DateTimeFormat::utcNow()];
290 DBA::insert('conversation', $conversation, true);
294 * Processes the activity object
296 * @param array $activity Array with activity data
297 * @param string $body
298 * @param integer $uid User ID
299 * @param boolean $trust_source Do we trust the source?
302 public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
304 $type = JsonLD::fetchElement($activity, '@type');
306 Logger::log('Empty type', Logger::DEBUG);
310 if (!JsonLD::fetchElement($activity, 'as:object')) {
311 Logger::log('Empty object', Logger::DEBUG);
315 if (!JsonLD::fetchElement($activity, 'as:actor')) {
316 Logger::log('Empty actor', Logger::DEBUG);
321 // Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
322 if ($trust_source && ($type == 'as:Create') && is_array($activity['as:object'])) {
323 $actor = JsonLD::fetchElement($activity, 'as:actor');
324 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo');
325 $trust_source = ($actor == $attributed_to);
326 if (!$trust_source) {
327 Logger::log('Not trusting actor: ' . $actor . '. It differs from attributedTo: ' . $attributed_to, Logger::DEBUG);
331 // $trust_source is called by reference and is set to true if the content was retrieved successfully
332 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
333 if (empty($object_data)) {
334 Logger::log('No object data found', Logger::DEBUG);
338 if (!$trust_source) {
339 Logger::log('No trust for activity type "' . $type . '", so we quit now.', Logger::DEBUG);
343 // Only store content related stuff - and no announces, since they possibly overwrite the original content
344 if (in_array($object_data['object_type'], self::CONTENT_TYPES) && ($type != 'as:Announce')) {
345 self::storeConversation($object_data, $body);
348 // Internal flag for thread completion. See Processor.php
349 if (!empty($activity['thread-completion'])) {
350 $object_data['thread-completion'] = $activity['thread-completion'];
356 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
357 ActivityPub\Processor::createItem($object_data);
362 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
363 ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE);
368 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
369 ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE);
373 case 'as:TentativeAccept':
374 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
375 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE);
380 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
381 ActivityPub\Processor::updateItem($object_data);
382 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
383 ActivityPub\Processor::updatePerson($object_data);
388 if ($object_data['object_type'] == 'as:Tombstone') {
389 ActivityPub\Processor::deleteItem($object_data);
390 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
391 ActivityPub\Processor::deletePerson($object_data);
396 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
397 ActivityPub\Processor::followUser($object_data);
398 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
399 $object_data['reply-to-id'] = $object_data['object_id'];
400 ActivityPub\Processor::createActivity($object_data, ACTIVITY_FOLLOW);
405 if ($object_data['object_type'] == 'as:Follow') {
406 ActivityPub\Processor::acceptFollowUser($object_data);
407 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
408 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND);
413 if ($object_data['object_type'] == 'as:Follow') {
414 ActivityPub\Processor::rejectFollowUser($object_data);
415 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
416 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO);
421 if (($object_data['object_type'] == 'as:Follow') &&
422 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
423 ActivityPub\Processor::undoFollowUser($object_data);
424 } elseif (($object_data['object_type'] == 'as:Accept') &&
425 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
426 ActivityPub\Processor::rejectFollowUser($object_data);
427 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
428 in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
429 ActivityPub\Processor::undoActivity($object_data);
434 Logger::log('Unknown activity: ' . $type . ' ' . $object_data['object_type'], Logger::DEBUG);
440 * Fetch the receiver list from an activity array
442 * @param array $activity
443 * @param string $actor
446 * @return array with receivers (user id)
449 private static function getReceivers($activity, $actor, $tags = [])
453 // When it is an answer, we inherite the receivers from the parent
454 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo');
455 if (!empty($replyto)) {
456 $parents = Item::select(['uid'], ['uri' => $replyto]);
457 while ($parent = Item::fetch($parents)) {
458 $receivers['uid:' . $parent['uid']] = $parent['uid'];
462 if (!empty($actor)) {
463 $profile = APContact::getByURL($actor);
464 $followers = defaults($profile, 'followers', '');
466 Logger::log('Actor: ' . $actor . ' - Followers: ' . $followers, Logger::DEBUG);
468 Logger::log('Empty actor', Logger::DEBUG);
472 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
473 $receiver_list = JsonLD::fetchElementArray($activity, $element);
474 if (empty($receiver_list)) {
478 foreach ($receiver_list as $receiver) {
479 if ($receiver == self::PUBLIC_COLLECTION) {
480 $receivers['uid:0'] = 0;
483 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
484 // This will most likely catch all OStatus connections to Mastodon
485 $condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
486 , 'archive' => false, 'pending' => false];
487 $contacts = DBA::select('contact', ['uid'], $condition);
488 while ($contact = DBA::fetch($contacts)) {
489 if ($contact['uid'] != 0) {
490 $receivers['uid:' . $contact['uid']] = $contact['uid'];
493 DBA::close($contacts);
496 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
497 $receivers = array_merge($receivers, self::getReceiverForActor($actor, $tags));
501 // Fetching all directly addressed receivers
502 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
503 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
504 if (!DBA::isResult($contact)) {
508 // Check if the potential receiver is following the actor
509 // Exception: The receiver is targetted via "to" or this is a comment
510 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
511 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
512 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
513 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
515 // Forum posts are only accepted from forum contacts
516 if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
517 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
520 if (!DBA::exists('contact', $condition)) {
525 $receivers['uid:' . $contact['uid']] = $contact['uid'];
529 self::switchContacts($receivers, $actor);
535 * Fetch the receiver list of a given actor
537 * @param string $actor
540 * @return array with receivers (user id)
543 public static function getReceiverForActor($actor, $tags)
546 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
547 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
548 'network' => $networks, 'archive' => false, 'pending' => false];
549 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
550 while ($contact = DBA::fetch($contacts)) {
551 if (self::isValidReceiverForActor($contact, $actor, $tags)) {
552 $receivers['uid:' . $contact['uid']] = $contact['uid'];
555 DBA::close($contacts);
560 * Tests if the contact is a valid receiver for this actor
562 * @param array $contact
563 * @param string $actor
566 * @return bool with receivers (user id)
569 private static function isValidReceiverForActor($contact, $actor, $tags)
571 // Public contacts are no valid receiver
572 if ($contact['uid'] == 0) {
576 // Are we following the contact? Then this is a valid receiver
577 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
581 // When the possible receiver isn't a community, then it is no valid receiver
582 $owner = User::getOwnerDataById($contact['uid']);
583 if (empty($owner) || ($owner['contact-type'] != Contact::TYPE_COMMUNITY)) {
587 // Is the community account tagged?
588 foreach ($tags as $tag) {
589 if ($tag['type'] != 'Mention') {
593 if ($tag['href'] == $owner['url']) {
602 * Switches existing contacts to ActivityPub
604 * @param integer $cid Contact ID
605 * @param integer $uid User ID
606 * @param string $url Profile URL
607 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
608 * @throws \ImagickException
610 public static function switchContact($cid, $uid, $url)
612 $profile = ActivityPub::probeProfile($url);
613 if (empty($profile)) {
617 Logger::log('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' to ActivityPub');
619 $photo = defaults($profile, 'photo', null);
620 unset($profile['photo']);
621 unset($profile['baseurl']);
622 unset($profile['guid']);
624 $profile['nurl'] = Strings::normaliseLink($profile['url']);
625 DBA::update('contact', $profile, ['id' => $cid]);
627 Contact::updateAvatar($photo, $uid, $cid);
629 // Send a new follow request to be sure that the connection still exists
630 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND]])) {
631 ActivityPub\Transmitter::sendActivity('Follow', $profile['url'], $uid);
632 Logger::log('Send a new follow request to ' . $profile['url'] . ' for user ' . $uid, Logger::DEBUG);
641 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
642 * @throws \ImagickException
644 private static function switchContacts($receivers, $actor)
650 foreach ($receivers as $receiver) {
651 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
652 if (DBA::isResult($contact)) {
653 self::switchContact($contact['id'], $receiver, $actor);
656 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
657 if (DBA::isResult($contact)) {
658 self::switchContact($contact['id'], $receiver, $actor);
666 * @param $object_data
667 * @param array $activity
671 private static function addActivityFields($object_data, $activity)
673 if (!empty($activity['published']) && empty($object_data['published'])) {
674 $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
677 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
678 $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid');
681 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
687 * Fetches the object data from external ressources if needed
689 * @param string $object_id Object ID of the the provided object
690 * @param array $object The provided object array
691 * @param boolean $trust_source Do we trust the provided object?
692 * @param integer $uid User ID for the signature that we use to fetch data
694 * @return array with trusted and valid object data
695 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
696 * @throws \ImagickException
698 private static function fetchObject($object_id, $object = [], $trust_source = false, $uid = 0)
700 // By fetching the type we check if the object is complete.
701 $type = JsonLD::fetchElement($object, '@type');
703 if (!$trust_source || empty($type)) {
704 $data = ActivityPub::fetchContent($object_id, $uid);
706 $object = JsonLD::compact($data);
707 Logger::log('Fetched content for ' . $object_id, Logger::DEBUG);
709 Logger::log('Empty content for ' . $object_id . ', check if content is available locally.', Logger::DEBUG);
711 $item = Item::selectFirst([], ['uri' => $object_id]);
712 if (!DBA::isResult($item)) {
713 Logger::log('Object with url ' . $object_id . ' was not found locally.', Logger::DEBUG);
716 Logger::log('Using already stored item for url ' . $object_id, Logger::DEBUG);
717 $data = ActivityPub\Transmitter::createNote($item);
718 $object = JsonLD::compact($data);
721 Logger::log('Using original object for url ' . $object_id, Logger::DEBUG);
724 $type = JsonLD::fetchElement($object, '@type');
727 Logger::log('Empty type', Logger::DEBUG);
731 if (in_array($type, self::CONTENT_TYPES)) {
732 return self::processObject($object);
735 if ($type == 'as:Announce') {
736 $object_id = JsonLD::fetchElement($object, 'object');
737 if (empty($object_id)) {
740 return self::fetchObject($object_id, [], false, $uid);
743 Logger::log('Unhandled object type: ' . $type, Logger::DEBUG);
747 * Convert tags from JSON-LD format into a simplified format
749 * @param array $tags Tags in JSON-LD format
751 * @return array with tags in a simplified format
753 private static function processTags($tags)
761 foreach ($tags as $tag) {
766 $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
767 'href' => JsonLD::fetchElement($tag, 'as:href'),
768 'name' => JsonLD::fetchElement($tag, 'as:name')];
770 if (empty($element['type'])) {
774 $taglist[] = $element;
780 * Convert emojis from JSON-LD format into a simplified format
783 * @return array with emojis in a simplified format
785 private static function processEmojis($emojis)
789 if (empty($emojis)) {
793 foreach ($emojis as $emoji) {
794 if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
798 $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url');
799 $element = ['name' => JsonLD::fetchElement($emoji, 'as:name'),
802 $emojilist[] = $element;
808 * Convert attachments from JSON-LD format into a simplified format
810 * @param array $attachments Attachments in JSON-LD format
812 * @return array with attachmants in a simplified format
814 private static function processAttachments($attachments)
818 if (empty($attachments)) {
822 foreach ($attachments as $attachment) {
823 if (empty($attachment)) {
827 $attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
828 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType'),
829 'name' => JsonLD::fetchElement($attachment, 'as:name'),
830 'url' => JsonLD::fetchElement($attachment, 'as:url')];
836 * Fetches data from the object part of an activity
838 * @param array $object
843 private static function processObject($object)
845 if (!JsonLD::fetchElement($object, '@id')) {
850 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
851 $object_data['id'] = JsonLD::fetchElement($object, '@id');
852 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo');
854 // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
855 if (empty($object_data['reply-to-id']) || ($object_data['reply-to-id'] == './')) {
856 $object_data['reply-to-id'] = $object_data['id'];
859 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
860 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
862 if (empty($object_data['updated'])) {
863 $object_data['updated'] = $object_data['published'];
866 if (empty($object_data['published']) && !empty($object_data['updated'])) {
867 $object_data['published'] = $object_data['updated'];
870 $actor = JsonLD::fetchElement($object, 'as:attributedTo');
872 $actor = JsonLD::fetchElement($object, 'as:actor');
875 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid');
876 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment');
877 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like');
878 $object_data['actor'] = $object_data['author'] = $actor;
879 $object_data['context'] = JsonLD::fetchElement($object, 'as:context');
880 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation');
881 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
882 $object_data['name'] = JsonLD::fetchElement($object, 'as:name');
883 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary');
884 $object_data['content'] = JsonLD::fetchElement($object, 'as:content');
885 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
886 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
887 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
888 $object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
889 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
890 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
891 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
892 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
893 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
894 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
895 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
896 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
897 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url');
899 // Special treatment for Hubzilla links
900 if (is_array($object_data['alternate-url'])) {
901 $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href');
903 if (!is_string($object_data['alternate-url'])) {
904 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href');
908 $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags']);
910 // Common object data:
913 // @context, type, actor, signature, mediaType, duration, replies, icon
915 // Also missing: (Defined in the standard, but currently unused)
916 // audience, preview, endTime, startTime, image
921 // contentMap, announcement_count, announcements, context_id, likes, like_count
922 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
927 // category, licence, language, commentsEnabled
930 // views, waitTranscoding, state, support, subtitleLanguage
931 // likes, dislikes, shares, comments