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 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, ['as:Like', 'as:Dislike']) ||
226 (($type == 'as:Follow') && in_array($object_type, self::CONTENT_TYPES))) {
227 // Create a mostly empty array out of the activity data (instead of the object).
228 // This way we later don't have to check for the existence of ech individual array element.
229 $object_data = self::processObject($activity);
230 $object_data['name'] = $type;
231 $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
232 $object_data['object_id'] = $object_id;
233 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
234 } elseif (in_array($type, ['as:Add'])) {
236 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
237 $object_data['target_id'] = JsonLD::fetchElement($activity, 'as:target', '@id');
238 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
239 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
240 $object_data['object_content'] = JsonLD::fetchElement($activity['as:object'], 'as:content', '@type');
243 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
244 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
245 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
246 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
247 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
249 // An Undo is done on the object of an object, so we need that type as well
250 if ($type == 'as:Undo') {
251 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $uid);
255 $object_data = self::addActivityFields($object_data, $activity);
257 if (empty($object_data['object_type'])) {
258 $object_data['object_type'] = $object_type;
261 $object_data['type'] = $type;
262 $object_data['actor'] = $actor;
263 $object_data['item_receiver'] = $receivers;
264 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
266 Logger::log('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], Logger::DEBUG);
272 * Fetches the first user id from the receiver array
274 * @param array $receivers Array with receivers
275 * @return integer user id;
277 public static function getFirstUserFromReceivers($receivers)
279 foreach ($receivers as $receiver) {
280 if (!empty($receiver)) {
288 * Store the unprocessed data into the conversation table
289 * This has to be done outside the regular function,
290 * since we store everything - not only item posts.
292 * @param array $activity Array with activity data
293 * @param string $body The raw message
296 private static function storeConversation($activity, $body)
298 if (empty($body) || empty($activity['id'])) {
303 'protocol' => Conversation::PARCEL_ACTIVITYPUB,
304 'item-uri' => $activity['id'],
305 'reply-to-uri' => defaults($activity, 'reply-to-id', ''),
306 'conversation-href' => defaults($activity, 'context', ''),
307 'conversation-uri' => defaults($activity, 'conversation', ''),
309 'received' => DateTimeFormat::utcNow()];
311 DBA::insert('conversation', $conversation, true);
315 * Processes the activity object
317 * @param array $activity Array with activity data
318 * @param string $body
319 * @param integer $uid User ID
320 * @param boolean $trust_source Do we trust the source?
323 public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
325 $type = JsonLD::fetchElement($activity, '@type');
327 Logger::log('Empty type', Logger::DEBUG);
331 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
332 Logger::log('Empty object', Logger::DEBUG);
336 if (!JsonLD::fetchElement($activity, 'as:actor', '@id')) {
337 Logger::log('Empty actor', Logger::DEBUG);
342 // Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
343 if ($trust_source && ($type == 'as:Create') && is_array($activity['as:object'])) {
344 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
345 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
346 $trust_source = ($actor == $attributed_to);
347 if (!$trust_source) {
348 Logger::log('Not trusting actor: ' . $actor . '. It differs from attributedTo: ' . $attributed_to, Logger::DEBUG);
352 // $trust_source is called by reference and is set to true if the content was retrieved successfully
353 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
354 if (empty($object_data)) {
355 Logger::log('No object data found', Logger::DEBUG);
359 if (!$trust_source) {
360 Logger::log('No trust for activity type "' . $type . '", so we quit now.', Logger::DEBUG);
364 // Only store content related stuff - and no announces, since they possibly overwrite the original content
365 if (in_array($object_data['object_type'], self::CONTENT_TYPES) && ($type != 'as:Announce')) {
366 self::storeConversation($object_data, $body);
369 // Internal flag for thread completion. See Processor.php
370 if (!empty($activity['thread-completion'])) {
371 $object_data['thread-completion'] = $activity['thread-completion'];
376 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
377 ActivityPub\Processor::createItem($object_data);
382 if ($object_data['object_type'] == 'as:tag') {
383 ActivityPub\Processor::addTag($object_data);
388 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
389 $profile = APContact::getByURL($object_data['actor']);
390 if ($profile['type'] == 'Person') {
391 // Reshared posts from persons appear as summary at the bottom
392 // If this isn't set, then a single reshare appears on top. This is used for groups.
393 $object_data['thread-completion'] = true;
395 ActivityPub\Processor::createItem($object_data);
397 // Add the bottom reshare information only for persons
398 if ($profile['type'] == 'Person') {
399 $announce_object_data = self::processObject($activity);
400 $announce_object_data['name'] = $type;
401 $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
402 $announce_object_data['object_id'] = $object_data['object_id'];
403 $announce_object_data['object_type'] = $object_data['object_type'];
405 ActivityPub\Processor::createActivity($announce_object_data, ACTIVITY2_ANNOUNCE);
411 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
412 ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE);
417 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
418 ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE);
422 case 'as:TentativeAccept':
423 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
424 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE);
429 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
430 ActivityPub\Processor::updateItem($object_data);
431 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
432 ActivityPub\Processor::updatePerson($object_data);
437 if ($object_data['object_type'] == 'as:Tombstone') {
438 ActivityPub\Processor::deleteItem($object_data);
439 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
440 ActivityPub\Processor::deletePerson($object_data);
445 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
446 ActivityPub\Processor::followUser($object_data);
447 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
448 $object_data['reply-to-id'] = $object_data['object_id'];
449 ActivityPub\Processor::createActivity($object_data, ACTIVITY_FOLLOW);
454 if ($object_data['object_type'] == 'as:Follow') {
455 ActivityPub\Processor::acceptFollowUser($object_data);
456 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
457 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND);
462 if ($object_data['object_type'] == 'as:Follow') {
463 ActivityPub\Processor::rejectFollowUser($object_data);
464 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
465 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO);
470 if (($object_data['object_type'] == 'as:Follow') &&
471 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
472 ActivityPub\Processor::undoFollowUser($object_data);
473 } elseif (($object_data['object_type'] == 'as:Accept') &&
474 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
475 ActivityPub\Processor::rejectFollowUser($object_data);
476 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
477 in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
478 ActivityPub\Processor::undoActivity($object_data);
483 Logger::log('Unknown activity: ' . $type . ' ' . $object_data['object_type'], Logger::DEBUG);
489 * Fetch the receiver list from an activity array
491 * @param array $activity
492 * @param string $actor
495 * @return array with receivers (user id)
498 private static function getReceivers($activity, $actor, $tags = [])
502 // When it is an answer, we inherite the receivers from the parent
503 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo', '@id');
504 if (!empty($replyto)) {
505 $parents = Item::select(['uid'], ['uri' => $replyto]);
506 while ($parent = Item::fetch($parents)) {
507 $receivers['uid:' . $parent['uid']] = $parent['uid'];
511 if (!empty($actor)) {
512 $profile = APContact::getByURL($actor);
513 $followers = defaults($profile, 'followers', '');
515 Logger::log('Actor: ' . $actor . ' - Followers: ' . $followers, Logger::DEBUG);
517 Logger::log('Empty actor', Logger::DEBUG);
521 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
522 $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
523 if (empty($receiver_list)) {
527 foreach ($receiver_list as $receiver) {
528 if ($receiver == self::PUBLIC_COLLECTION) {
529 $receivers['uid:0'] = 0;
532 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
533 // This will most likely catch all OStatus connections to Mastodon
534 $condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
535 , 'archive' => false, 'pending' => false];
536 $contacts = DBA::select('contact', ['uid'], $condition);
537 while ($contact = DBA::fetch($contacts)) {
538 if ($contact['uid'] != 0) {
539 $receivers['uid:' . $contact['uid']] = $contact['uid'];
542 DBA::close($contacts);
545 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
546 $receivers = array_merge($receivers, self::getReceiverForActor($actor, $tags));
550 // Fetching all directly addressed receivers
551 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
552 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
553 if (!DBA::isResult($contact)) {
557 // Check if the potential receiver is following the actor
558 // Exception: The receiver is targetted via "to" or this is a comment
559 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
560 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
561 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
562 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
564 // Forum posts are only accepted from forum contacts
565 if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
566 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
569 if (!DBA::exists('contact', $condition)) {
574 $receivers['uid:' . $contact['uid']] = $contact['uid'];
578 self::switchContacts($receivers, $actor);
584 * Fetch the receiver list of a given actor
586 * @param string $actor
589 * @return array with receivers (user id)
592 public static function getReceiverForActor($actor, $tags)
595 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
596 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
597 'network' => $networks, 'archive' => false, 'pending' => false];
598 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
599 while ($contact = DBA::fetch($contacts)) {
600 if (self::isValidReceiverForActor($contact, $actor, $tags)) {
601 $receivers['uid:' . $contact['uid']] = $contact['uid'];
604 DBA::close($contacts);
609 * Tests if the contact is a valid receiver for this actor
611 * @param array $contact
612 * @param string $actor
615 * @return bool with receivers (user id)
618 private static function isValidReceiverForActor($contact, $actor, $tags)
620 // Public contacts are no valid receiver
621 if ($contact['uid'] == 0) {
625 // Are we following the contact? Then this is a valid receiver
626 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
630 // When the possible receiver isn't a community, then it is no valid receiver
631 $owner = User::getOwnerDataById($contact['uid']);
632 if (empty($owner) || ($owner['contact-type'] != Contact::TYPE_COMMUNITY)) {
636 // Is the community account tagged?
637 foreach ($tags as $tag) {
638 if ($tag['type'] != 'Mention') {
642 if ($tag['href'] == $owner['url']) {
651 * Switches existing contacts to ActivityPub
653 * @param integer $cid Contact ID
654 * @param integer $uid User ID
655 * @param string $url Profile URL
656 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
657 * @throws \ImagickException
659 public static function switchContact($cid, $uid, $url)
661 $profile = ActivityPub::probeProfile($url);
662 if (empty($profile)) {
666 Logger::log('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' to ActivityPub');
668 $photo = defaults($profile, 'photo', null);
669 unset($profile['photo']);
670 unset($profile['baseurl']);
671 unset($profile['guid']);
673 $profile['nurl'] = Strings::normaliseLink($profile['url']);
674 DBA::update('contact', $profile, ['id' => $cid]);
676 Contact::updateAvatar($photo, $uid, $cid);
678 // Send a new follow request to be sure that the connection still exists
679 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND]])) {
680 ActivityPub\Transmitter::sendActivity('Follow', $profile['url'], $uid);
681 Logger::log('Send a new follow request to ' . $profile['url'] . ' for user ' . $uid, Logger::DEBUG);
690 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
691 * @throws \ImagickException
693 private static function switchContacts($receivers, $actor)
699 foreach ($receivers as $receiver) {
700 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
701 if (DBA::isResult($contact)) {
702 self::switchContact($contact['id'], $receiver, $actor);
705 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
706 if (DBA::isResult($contact)) {
707 self::switchContact($contact['id'], $receiver, $actor);
715 * @param $object_data
716 * @param array $activity
720 private static function addActivityFields($object_data, $activity)
722 if (!empty($activity['published']) && empty($object_data['published'])) {
723 $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
726 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
727 $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid', '@value');
730 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
731 $object_data['service'] = JsonLD::fetchElement($object_data, 'service', '@value');
737 * Fetches the object data from external ressources if needed
739 * @param string $object_id Object ID of the the provided object
740 * @param array $object The provided object array
741 * @param boolean $trust_source Do we trust the provided object?
742 * @param integer $uid User ID for the signature that we use to fetch data
744 * @return array with trusted and valid object data
745 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
746 * @throws \ImagickException
748 private static function fetchObject($object_id, $object = [], $trust_source = false, $uid = 0)
750 // By fetching the type we check if the object is complete.
751 $type = JsonLD::fetchElement($object, '@type');
753 if (!$trust_source || empty($type)) {
754 $data = ActivityPub::fetchContent($object_id, $uid);
756 $object = JsonLD::compact($data);
757 Logger::log('Fetched content for ' . $object_id, Logger::DEBUG);
759 Logger::log('Empty content for ' . $object_id . ', check if content is available locally.', Logger::DEBUG);
761 $item = Item::selectFirst([], ['uri' => $object_id]);
762 if (!DBA::isResult($item)) {
763 Logger::log('Object with url ' . $object_id . ' was not found locally.', Logger::DEBUG);
766 Logger::log('Using already stored item for url ' . $object_id, Logger::DEBUG);
767 $data = ActivityPub\Transmitter::createNote($item);
768 $object = JsonLD::compact($data);
771 Logger::log('Using original object for url ' . $object_id, Logger::DEBUG);
774 $type = JsonLD::fetchElement($object, '@type');
777 Logger::log('Empty type', Logger::DEBUG);
781 if (in_array($type, self::CONTENT_TYPES)) {
782 return self::processObject($object);
785 if ($type == 'as:Announce') {
786 $object_id = JsonLD::fetchElement($object, 'object', '@id');
787 if (empty($object_id) || !is_string($object_id)) {
790 return self::fetchObject($object_id, [], false, $uid);
793 Logger::log('Unhandled object type: ' . $type, Logger::DEBUG);
797 * Convert tags from JSON-LD format into a simplified format
799 * @param array $tags Tags in JSON-LD format
801 * @return array with tags in a simplified format
803 private static function processTags($tags)
811 foreach ($tags as $tag) {
816 $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
817 'href' => JsonLD::fetchElement($tag, 'as:href', '@id'),
818 'name' => JsonLD::fetchElement($tag, 'as:name', '@value')];
820 if (empty($element['type'])) {
824 $taglist[] = $element;
830 * Convert emojis from JSON-LD format into a simplified format
833 * @return array with emojis in a simplified format
835 private static function processEmojis($emojis)
839 if (empty($emojis)) {
843 foreach ($emojis as $emoji) {
844 if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
848 $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id');
849 $element = ['name' => JsonLD::fetchElement($emoji, 'as:name', '@value'),
852 $emojilist[] = $element;
858 * Convert attachments from JSON-LD format into a simplified format
860 * @param array $attachments Attachments in JSON-LD format
862 * @return array with attachmants in a simplified format
864 private static function processAttachments($attachments)
868 if (empty($attachments)) {
872 foreach ($attachments as $attachment) {
873 if (empty($attachment)) {
877 $attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
878 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
879 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
880 'url' => JsonLD::fetchElement($attachment, 'as:url', '@id')];
886 * Fetches data from the object part of an activity
888 * @param array $object
893 private static function processObject($object)
895 if (!JsonLD::fetchElement($object, '@id')) {
900 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
901 $object_data['id'] = JsonLD::fetchElement($object, '@id');
902 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id');
904 // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
905 if (empty($object_data['reply-to-id']) || ($object_data['reply-to-id'] == './')) {
906 $object_data['reply-to-id'] = $object_data['id'];
909 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
910 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
912 if (empty($object_data['updated'])) {
913 $object_data['updated'] = $object_data['published'];
916 if (empty($object_data['published']) && !empty($object_data['updated'])) {
917 $object_data['published'] = $object_data['updated'];
920 $actor = JsonLD::fetchElement($object, 'as:attributedTo', '@id');
922 $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
925 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
926 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
927 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
928 $object_data['actor'] = $object_data['author'] = $actor;
929 $object_data['context'] = JsonLD::fetchElement($object, 'as:context', '@id');
930 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation', '@id');
931 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
932 $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
933 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
934 $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
935 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
936 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
937 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
938 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
939 $object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
940 $object_data['location'] = JsonLD::fetchElement($object_data, 'location', '@value');
941 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
942 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
943 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
944 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
945 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
946 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
947 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
948 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
949 $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
950 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
952 // Special treatment for Hubzilla links
953 if (is_array($object_data['alternate-url'])) {
954 $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href', '@id');
956 if (!is_string($object_data['alternate-url'])) {
957 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href', '@id');
961 $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags']);
963 // Common object data:
966 // @context, type, actor, signature, mediaType, duration, replies, icon
968 // Also missing: (Defined in the standard, but currently unused)
969 // audience, preview, endTime, startTime, image
974 // contentMap, announcement_count, announcements, context_id, likes, like_count
975 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
980 // category, licence, language, commentsEnabled
983 // views, waitTranscoding, state, support, subtitleLanguage
984 // likes, dislikes, shares, comments