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 // Test if it is an answer to a mail
212 if (DBA::exists('mail', ['uri' => $object_data['reply-to-id']])) {
213 $object_data['directmessage'] = true;
215 $object_data['directmessage'] = JsonLD::fetchElement($activity, 'litepub:directMessage');
218 // We had been able to retrieve the object data - so we can trust the source
219 $trust_source = true;
220 } elseif (in_array($type, ['as:Like', 'as:Dislike']) ||
221 (($type == 'as:Follow') && in_array($object_type, self::CONTENT_TYPES))) {
222 // Create a mostly empty array out of the activity data (instead of the object).
223 // This way we later don't have to check for the existence of ech individual array element.
224 $object_data = self::processObject($activity);
225 $object_data['name'] = $type;
226 $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
227 $object_data['object_id'] = $object_id;
228 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
229 } elseif (in_array($type, ['as:Add'])) {
231 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
232 $object_data['target_id'] = JsonLD::fetchElement($activity, 'as:target', '@id');
233 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
234 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
235 $object_data['object_content'] = JsonLD::fetchElement($activity['as:object'], 'as:content', '@type');
238 $object_data['id'] = JsonLD::fetchElement($activity, '@id');
239 $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
240 $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
241 $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
242 $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
244 // An Undo is done on the object of an object, so we need that type as well
245 if ($type == 'as:Undo') {
246 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $uid);
250 $object_data = self::addActivityFields($object_data, $activity);
252 if (empty($object_data['object_type'])) {
253 $object_data['object_type'] = $object_type;
256 $object_data['type'] = $type;
257 $object_data['actor'] = $actor;
258 $object_data['item_receiver'] = $receivers;
259 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
261 Logger::log('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], Logger::DEBUG);
267 * Fetches the first user id from the receiver array
269 * @param array $receivers Array with receivers
270 * @return integer user id;
272 public static function getFirstUserFromReceivers($receivers)
274 foreach ($receivers as $receiver) {
275 if (!empty($receiver)) {
283 * Store the unprocessed data into the conversation table
284 * This has to be done outside the regular function,
285 * since we store everything - not only item posts.
287 * @param array $activity Array with activity data
288 * @param string $body The raw message
291 private static function storeConversation($activity, $body)
293 if (empty($body) || empty($activity['id'])) {
298 'protocol' => Conversation::PARCEL_ACTIVITYPUB,
299 'item-uri' => $activity['id'],
300 'reply-to-uri' => defaults($activity, 'reply-to-id', ''),
301 'conversation-href' => defaults($activity, 'context', ''),
302 'conversation-uri' => defaults($activity, 'conversation', ''),
304 'received' => DateTimeFormat::utcNow()];
306 DBA::insert('conversation', $conversation, true);
310 * Processes the activity object
312 * @param array $activity Array with activity data
313 * @param string $body
314 * @param integer $uid User ID
315 * @param boolean $trust_source Do we trust the source?
318 public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
320 $type = JsonLD::fetchElement($activity, '@type');
322 Logger::log('Empty type', Logger::DEBUG);
326 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
327 Logger::log('Empty object', Logger::DEBUG);
331 if (!JsonLD::fetchElement($activity, 'as:actor', '@id')) {
332 Logger::log('Empty actor', Logger::DEBUG);
337 // Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
338 if ($trust_source && ($type == 'as:Create') && is_array($activity['as:object'])) {
339 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
340 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
341 $trust_source = ($actor == $attributed_to);
342 if (!$trust_source) {
343 Logger::log('Not trusting actor: ' . $actor . '. It differs from attributedTo: ' . $attributed_to, Logger::DEBUG);
347 // $trust_source is called by reference and is set to true if the content was retrieved successfully
348 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
349 if (empty($object_data)) {
350 Logger::log('No object data found', Logger::DEBUG);
354 if (!$trust_source) {
355 Logger::log('No trust for activity type "' . $type . '", so we quit now.', Logger::DEBUG);
359 // Only store content related stuff - and no announces, since they possibly overwrite the original content
360 if (in_array($object_data['object_type'], self::CONTENT_TYPES) && ($type != 'as:Announce')) {
361 self::storeConversation($object_data, $body);
364 // Internal flag for thread completion. See Processor.php
365 if (!empty($activity['thread-completion'])) {
366 $object_data['thread-completion'] = $activity['thread-completion'];
371 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
372 ActivityPub\Processor::createItem($object_data);
377 if ($object_data['object_type'] == 'as:tag') {
378 ActivityPub\Processor::addTag($object_data);
383 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
384 $profile = APContact::getByURL($object_data['actor']);
385 if ($profile['type'] == 'Person') {
386 // Reshared posts from persons appear as summary at the bottom
387 // If this isn't set, then a single reshare appears on top. This is used for groups.
388 $object_data['thread-completion'] = true;
390 ActivityPub\Processor::createItem($object_data);
392 // Add the bottom reshare information only for persons
393 if ($profile['type'] == 'Person') {
394 $announce_object_data = self::processObject($activity);
395 $announce_object_data['name'] = $type;
396 $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
397 $announce_object_data['object_id'] = $object_data['object_id'];
398 $announce_object_data['object_type'] = $object_data['object_type'];
400 ActivityPub\Processor::createActivity($announce_object_data, ACTIVITY2_ANNOUNCE);
406 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
407 ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE);
412 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
413 ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE);
417 case 'as:TentativeAccept':
418 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
419 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE);
424 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
425 ActivityPub\Processor::updateItem($object_data);
426 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
427 ActivityPub\Processor::updatePerson($object_data);
432 if ($object_data['object_type'] == 'as:Tombstone') {
433 ActivityPub\Processor::deleteItem($object_data);
434 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
435 ActivityPub\Processor::deletePerson($object_data);
440 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
441 ActivityPub\Processor::followUser($object_data);
442 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
443 $object_data['reply-to-id'] = $object_data['object_id'];
444 ActivityPub\Processor::createActivity($object_data, ACTIVITY_FOLLOW);
449 if ($object_data['object_type'] == 'as:Follow') {
450 ActivityPub\Processor::acceptFollowUser($object_data);
451 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
452 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND);
457 if ($object_data['object_type'] == 'as:Follow') {
458 ActivityPub\Processor::rejectFollowUser($object_data);
459 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
460 ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO);
465 if (($object_data['object_type'] == 'as:Follow') &&
466 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
467 ActivityPub\Processor::undoFollowUser($object_data);
468 } elseif (($object_data['object_type'] == 'as:Accept') &&
469 in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
470 ActivityPub\Processor::rejectFollowUser($object_data);
471 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
472 in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
473 ActivityPub\Processor::undoActivity($object_data);
478 Logger::log('Unknown activity: ' . $type . ' ' . $object_data['object_type'], Logger::DEBUG);
484 * Fetch the receiver list from an activity array
486 * @param array $activity
487 * @param string $actor
490 * @return array with receivers (user id)
493 private static function getReceivers($activity, $actor, $tags = [])
497 // When it is an answer, we inherite the receivers from the parent
498 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo', '@id');
499 if (!empty($replyto)) {
500 $parents = Item::select(['uid'], ['uri' => $replyto]);
501 while ($parent = Item::fetch($parents)) {
502 $receivers['uid:' . $parent['uid']] = $parent['uid'];
506 if (!empty($actor)) {
507 $profile = APContact::getByURL($actor);
508 $followers = defaults($profile, 'followers', '');
510 Logger::log('Actor: ' . $actor . ' - Followers: ' . $followers, Logger::DEBUG);
512 Logger::log('Empty actor', Logger::DEBUG);
516 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
517 $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
518 if (empty($receiver_list)) {
522 foreach ($receiver_list as $receiver) {
523 if ($receiver == self::PUBLIC_COLLECTION) {
524 $receivers['uid:0'] = 0;
527 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
528 // This will most likely catch all OStatus connections to Mastodon
529 $condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
530 , 'archive' => false, 'pending' => false];
531 $contacts = DBA::select('contact', ['uid'], $condition);
532 while ($contact = DBA::fetch($contacts)) {
533 if ($contact['uid'] != 0) {
534 $receivers['uid:' . $contact['uid']] = $contact['uid'];
537 DBA::close($contacts);
540 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
541 $receivers = array_merge($receivers, self::getReceiverForActor($actor, $tags));
545 // Fetching all directly addressed receivers
546 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
547 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
548 if (!DBA::isResult($contact)) {
552 // Check if the potential receiver is following the actor
553 // Exception: The receiver is targetted via "to" or this is a comment
554 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
555 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
556 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
557 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
559 // Forum posts are only accepted from forum contacts
560 if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
561 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
564 if (!DBA::exists('contact', $condition)) {
569 $receivers['uid:' . $contact['uid']] = $contact['uid'];
573 self::switchContacts($receivers, $actor);
579 * Fetch the receiver list of a given actor
581 * @param string $actor
584 * @return array with receivers (user id)
587 public static function getReceiverForActor($actor, $tags)
590 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
591 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
592 'network' => $networks, 'archive' => false, 'pending' => false];
593 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
594 while ($contact = DBA::fetch($contacts)) {
595 if (self::isValidReceiverForActor($contact, $actor, $tags)) {
596 $receivers['uid:' . $contact['uid']] = $contact['uid'];
599 DBA::close($contacts);
604 * Tests if the contact is a valid receiver for this actor
606 * @param array $contact
607 * @param string $actor
610 * @return bool with receivers (user id)
613 private static function isValidReceiverForActor($contact, $actor, $tags)
615 // Public contacts are no valid receiver
616 if ($contact['uid'] == 0) {
620 // Are we following the contact? Then this is a valid receiver
621 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
625 // When the possible receiver isn't a community, then it is no valid receiver
626 $owner = User::getOwnerDataById($contact['uid']);
627 if (empty($owner) || ($owner['contact-type'] != Contact::TYPE_COMMUNITY)) {
631 // Is the community account tagged?
632 foreach ($tags as $tag) {
633 if ($tag['type'] != 'Mention') {
637 if ($tag['href'] == $owner['url']) {
646 * Switches existing contacts to ActivityPub
648 * @param integer $cid Contact ID
649 * @param integer $uid User ID
650 * @param string $url Profile URL
651 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
652 * @throws \ImagickException
654 public static function switchContact($cid, $uid, $url)
656 $profile = ActivityPub::probeProfile($url);
657 if (empty($profile)) {
661 Logger::log('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' to ActivityPub');
663 $photo = defaults($profile, 'photo', null);
664 unset($profile['photo']);
665 unset($profile['baseurl']);
666 unset($profile['guid']);
668 $profile['nurl'] = Strings::normaliseLink($profile['url']);
669 DBA::update('contact', $profile, ['id' => $cid]);
671 Contact::updateAvatar($photo, $uid, $cid);
673 // Send a new follow request to be sure that the connection still exists
674 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND]])) {
675 ActivityPub\Transmitter::sendActivity('Follow', $profile['url'], $uid);
676 Logger::log('Send a new follow request to ' . $profile['url'] . ' for user ' . $uid, Logger::DEBUG);
685 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
686 * @throws \ImagickException
688 private static function switchContacts($receivers, $actor)
694 foreach ($receivers as $receiver) {
695 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
696 if (DBA::isResult($contact)) {
697 self::switchContact($contact['id'], $receiver, $actor);
700 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
701 if (DBA::isResult($contact)) {
702 self::switchContact($contact['id'], $receiver, $actor);
710 * @param $object_data
711 * @param array $activity
715 private static function addActivityFields($object_data, $activity)
717 if (!empty($activity['published']) && empty($object_data['published'])) {
718 $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
721 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
722 $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid', '@value');
725 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
726 $object_data['service'] = JsonLD::fetchElement($object_data, 'service', '@value');
732 * Fetches the object data from external ressources if needed
734 * @param string $object_id Object ID of the the provided object
735 * @param array $object The provided object array
736 * @param boolean $trust_source Do we trust the provided object?
737 * @param integer $uid User ID for the signature that we use to fetch data
739 * @return array with trusted and valid object data
740 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
741 * @throws \ImagickException
743 private static function fetchObject($object_id, $object = [], $trust_source = false, $uid = 0)
745 // By fetching the type we check if the object is complete.
746 $type = JsonLD::fetchElement($object, '@type');
748 if (!$trust_source || empty($type)) {
749 $data = ActivityPub::fetchContent($object_id, $uid);
751 $object = JsonLD::compact($data);
752 Logger::log('Fetched content for ' . $object_id, Logger::DEBUG);
754 Logger::log('Empty content for ' . $object_id . ', check if content is available locally.', Logger::DEBUG);
756 $item = Item::selectFirst([], ['uri' => $object_id]);
757 if (!DBA::isResult($item)) {
758 Logger::log('Object with url ' . $object_id . ' was not found locally.', Logger::DEBUG);
761 Logger::log('Using already stored item for url ' . $object_id, Logger::DEBUG);
762 $data = ActivityPub\Transmitter::createNote($item);
763 $object = JsonLD::compact($data);
766 Logger::log('Using original object for url ' . $object_id, Logger::DEBUG);
769 $type = JsonLD::fetchElement($object, '@type');
772 Logger::log('Empty type', Logger::DEBUG);
776 if (in_array($type, self::CONTENT_TYPES)) {
777 return self::processObject($object);
780 if ($type == 'as:Announce') {
781 $object_id = JsonLD::fetchElement($object, 'object', '@id');
782 if (empty($object_id)) {
785 return self::fetchObject($object_id, [], false, $uid);
788 Logger::log('Unhandled object type: ' . $type, Logger::DEBUG);
792 * Convert tags from JSON-LD format into a simplified format
794 * @param array $tags Tags in JSON-LD format
796 * @return array with tags in a simplified format
798 private static function processTags($tags)
806 foreach ($tags as $tag) {
811 $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
812 'href' => JsonLD::fetchElement($tag, 'as:href', '@id'),
813 'name' => JsonLD::fetchElement($tag, 'as:name', '@value')];
815 if (empty($element['type'])) {
819 $taglist[] = $element;
825 * Convert emojis from JSON-LD format into a simplified format
828 * @return array with emojis in a simplified format
830 private static function processEmojis($emojis)
834 if (empty($emojis)) {
838 foreach ($emojis as $emoji) {
839 if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
843 $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id');
844 $element = ['name' => JsonLD::fetchElement($emoji, 'as:name', '@value'),
847 $emojilist[] = $element;
853 * Convert attachments from JSON-LD format into a simplified format
855 * @param array $attachments Attachments in JSON-LD format
857 * @return array with attachmants in a simplified format
859 private static function processAttachments($attachments)
863 if (empty($attachments)) {
867 foreach ($attachments as $attachment) {
868 if (empty($attachment)) {
872 $attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
873 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
874 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
875 'url' => JsonLD::fetchElement($attachment, 'as:url', '@id')];
881 * Fetches data from the object part of an activity
883 * @param array $object
888 private static function processObject($object)
890 if (!JsonLD::fetchElement($object, '@id')) {
895 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
896 $object_data['id'] = JsonLD::fetchElement($object, '@id');
897 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id');
899 // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
900 if (empty($object_data['reply-to-id']) || ($object_data['reply-to-id'] == './')) {
901 $object_data['reply-to-id'] = $object_data['id'];
904 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
905 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
907 if (empty($object_data['updated'])) {
908 $object_data['updated'] = $object_data['published'];
911 if (empty($object_data['published']) && !empty($object_data['updated'])) {
912 $object_data['published'] = $object_data['updated'];
915 $actor = JsonLD::fetchElement($object, 'as:attributedTo', '@id');
917 $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
920 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
921 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
922 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
923 $object_data['actor'] = $object_data['author'] = $actor;
924 $object_data['context'] = JsonLD::fetchElement($object, 'as:context', '@id');
925 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation', '@id');
926 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
927 $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
928 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
929 $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
930 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
931 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
932 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
933 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
934 $object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
935 $object_data['location'] = JsonLD::fetchElement($object_data, 'location', '@value');
936 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
937 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
938 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
939 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
940 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
941 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
942 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
943 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
944 $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
945 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
947 // Special treatment for Hubzilla links
948 if (is_array($object_data['alternate-url'])) {
949 $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href', '@id');
951 if (!is_string($object_data['alternate-url'])) {
952 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href', '@id');
956 $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags']);
958 // Common object data:
961 // @context, type, actor, signature, mediaType, duration, replies, icon
963 // Also missing: (Defined in the standard, but currently unused)
964 // audience, preview, endTime, startTime, image
969 // contentMap, announcement_count, announcements, context_id, likes, like_count
970 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
975 // category, licence, language, commentsEnabled
978 // views, waitTranscoding, state, support, subtitleLanguage
979 // likes, dislikes, shares, comments