]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Receiver.php
686ac8be327ecbe8f1235bf706e475c9a55fce10
[friendica.git] / src / Protocol / ActivityPub / Receiver.php
1 <?php
2 /**
3  * @file src/Protocol/ActivityPub/Receiver.php
4  */
5 namespace Friendica\Protocol\ActivityPub;
6
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;
21
22 /**
23  * @brief ActivityPub Receiver Protocol class
24  *
25  * To-Do:
26  * - Undo Announce
27  *
28  * Check what this is meant to do:
29  * - Add
30  * - Block
31  * - Flag
32  * - Remove
33  * - Undo Block
34  */
35 class Receiver
36 {
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'];
41
42         /**
43          * Checks if the web request is done for the AP protocol
44          *
45          * @return is it AP?
46          */
47         public static function isRequest()
48         {
49                 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
50                         stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
51         }
52
53         /**
54          * Checks incoming message from the inbox
55          *
56          * @param $body
57          * @param $header
58          * @param integer $uid User ID
59          */
60         public static function processInbox($body, $header, $uid)
61         {
62                 $http_signer = HTTPSignature::getSigner($body, $header);
63                 if (empty($http_signer)) {
64                         Logger::log('Invalid HTTP signature, message will be discarded.', Logger::DEBUG);
65                         return;
66                 } else {
67                         Logger::log('HTTP signature is signed by ' . $http_signer, Logger::DEBUG);
68                 }
69
70                 $activity = json_decode($body, true);
71
72                 if (empty($activity)) {
73                         Logger::log('Invalid body.', Logger::DEBUG);
74                         return;
75                 }
76
77                 $ldactivity = JsonLD::compact($activity);
78
79                 $actor = JsonLD::fetchElement($ldactivity, 'as:actor');
80
81                 Logger::log('Message for user ' . $uid . ' is from actor ' . $actor, Logger::DEBUG);
82
83                 if (LDSignature::isSigned($activity)) {
84                         $ld_signer = LDSignature::getSigner($activity);
85                         if (empty($ld_signer)) {
86                                 Logger::log('Invalid JSON-LD signature from ' . $actor, Logger::DEBUG);
87                         }
88                         if (!empty($ld_signer && ($actor == $http_signer))) {
89                                 Logger::log('The HTTP and the JSON-LD signature belong to ' . $ld_signer, Logger::DEBUG);
90                                 $trust_source = true;
91                         } elseif (!empty($ld_signer)) {
92                                 Logger::log('JSON-LD signature is signed by ' . $ld_signer, Logger::DEBUG);
93                                 $trust_source = true;
94                         } elseif ($actor == $http_signer) {
95                                 Logger::log('Bad JSON-LD signature, but HTTP signer fits the actor.', Logger::DEBUG);
96                                 $trust_source = true;
97                         } else {
98                                 Logger::log('Invalid JSON-LD signature and the HTTP signer is different.', Logger::DEBUG);
99                                 $trust_source = false;
100                         }
101                 } elseif ($actor == $http_signer) {
102                         Logger::log('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', Logger::DEBUG);
103                         $trust_source = true;
104                 } else {
105                         Logger::log('No JSON-LD signature, different actor.', Logger::DEBUG);
106                         $trust_source = false;
107                 }
108
109                 self::processActivity($ldactivity, $body, $uid, $trust_source);
110         }
111
112         /**
113          * Fetches the object type for a given object id
114          *
115          * @param array   $activity
116          * @param string  $object_id Object ID of the the provided object
117          * @param integer $uid User ID
118          *
119          * @return string with object type
120          */
121         private static function fetchObjectType($activity, $object_id, $uid = 0)
122         {
123                 if (!empty($activity['as:object'])) {
124                         $object_type = JsonLD::fetchElement($activity['as:object'], '@type');
125                         if (!empty($object_type)) {
126                                 return $object_type;
127                         }
128                 }
129
130                 if (Item::exists(['uri' => $object_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]])) {
131                         // We just assume "note" since it doesn't make a difference for the further processing
132                         return 'as:Note';
133                 }
134
135                 $profile = APContact::getByURL($object_id);
136                 if (!empty($profile['type'])) {
137                         return 'as:' . $profile['type'];
138                 }
139
140                 $data = ActivityPub::fetchContent($object_id, $uid);
141                 if (!empty($data)) {
142                         $object = JsonLD::compact($data);
143                         $type = JsonLD::fetchElement($object, '@type');
144                         if (!empty($type)) {
145                                 return $type;
146                         }
147                 }
148
149                 return null;
150         }
151
152         /**
153          * Prepare the object array
154          *
155          * @param array $activity
156          * @param integer $uid User ID
157          * @param $trust_source
158          *
159          * @return array with object data
160          */
161         private static function prepareObjectData($activity, $uid, &$trust_source)
162         {
163                 $actor = JsonLD::fetchElement($activity, 'as:actor');
164                 if (empty($actor)) {
165                         Logger::log('Empty actor', Logger::DEBUG);
166                         return [];
167                 }
168
169                 $type = JsonLD::fetchElement($activity, '@type');
170
171                 // Fetch all receivers from to, cc, bto and bcc
172                 $receivers = self::getReceivers($activity, $actor);
173
174                 // When it is a delivery to a personal inbox we add that user to the receivers
175                 if (!empty($uid)) {
176                         $additional = ['uid:' . $uid => $uid];
177                         $receivers = array_merge($receivers, $additional);
178                 } else {
179                         // We possibly need some user to fetch private content,
180                         // so we fetch the first out ot the list.
181                         $uid = self::getFirstUserFromReceivers($receivers);
182                 }
183
184                 Logger::log('Receivers: ' . $uid . ' - ' . json_encode($receivers), Logger::DEBUG);
185
186                 $object_id = JsonLD::fetchElement($activity, 'as:object');
187                 if (empty($object_id)) {
188                         Logger::log('No object found', Logger::DEBUG);
189                         return [];
190                 }
191
192                 $object_type = self::fetchObjectType($activity, $object_id, $uid);
193
194                 // Fetch the content only on activities where this matters
195                 if (in_array($type, ['as:Create', 'as:Update', 'as:Announce'])) {
196                         if ($type == 'as:Announce') {
197                                 $trust_source = false;
198                         }
199                         $object_data = self::fetchObject($object_id, $activity['as:object'], $trust_source, $uid);
200                         if (empty($object_data)) {
201                                 Logger::log("Object data couldn't be processed", Logger::DEBUG);
202                                 return [];
203                         }
204                         // We had been able to retrieve the object data - so we can trust the source
205                         $trust_source = true;
206                 } elseif (in_array($type, ['as:Like', 'as:Dislike'])) {
207                         // Create a mostly empty array out of the activity data (instead of the object).
208                         // This way we later don't have to check for the existence of ech individual array element.
209                         $object_data = self::processObject($activity);
210                         $object_data['name'] = $type;
211                         $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor');
212                         $object_data['object_id'] = $object_id;
213                         $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
214                 } else {
215                         $object_data = [];
216                         $object_data['id'] = JsonLD::fetchElement($activity, '@id');
217                         $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object');
218                         $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor');
219                         $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
220                         $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
221
222                         // An Undo is done on the object of an object, so we need that type as well
223                         if ($type == 'as:Undo') {
224                                 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $uid);
225                         }
226                 }
227
228                 $object_data = self::addActivityFields($object_data, $activity);
229
230                 if (empty($object_data['object_type'])) {
231                         $object_data['object_type'] = $object_type;
232                 }
233
234                 $object_data['type'] = $type;
235                 $object_data['actor'] = $actor;
236                 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
237
238                 Logger::log('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], Logger::DEBUG);
239
240                 return $object_data;
241         }
242
243         /**
244          * Fetches the first user id from the receiver array
245          *
246          * @param array $receivers Array with receivers
247          * @return integer user id;
248          */
249         public static function getFirstUserFromReceivers($receivers)
250         {
251                 foreach ($receivers as $receiver) {
252                         if (!empty($receiver)) {
253                                 return $receiver;
254                         }
255                 }
256                 return 0;
257         }
258
259         /**
260          * Store the unprocessed data into the conversation table
261          * This has to be done outside the regular function,
262          * since we store everything - not only item posts.
263          *
264          * @param array  $activity Array with activity data
265          * @param string $body     The raw message
266          */
267         private static function storeConversation($activity, $body)
268         {
269                 if (empty($body) || empty($activity['id'])) {
270                         return;
271                 }
272
273                 $conversation = [
274                         'protocol' => Conversation::PARCEL_ACTIVITYPUB,
275                         'item-uri' => $activity['id'],
276                         'reply-to-uri' => defaults($activity, 'reply-to-id', ''),
277                         'conversation-href' => defaults($activity, 'context', ''),
278                         'conversation-uri' => defaults($activity, 'conversation', ''),
279                         'source' => $body,
280                         'received' => DateTimeFormat::utcNow()];
281
282                 DBA::insert('conversation', $conversation, true);
283         }
284
285         /**
286          * Processes the activity object
287          *
288          * @param array   $activity     Array with activity data
289          * @param string  $body
290          * @param integer $uid          User ID
291          * @param boolean $trust_source Do we trust the source?
292          */
293         public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
294         {
295                 $type = JsonLD::fetchElement($activity, '@type');
296                 if (!$type) {
297                         Logger::log('Empty type', Logger::DEBUG);
298                         return;
299                 }
300
301                 if (!JsonLD::fetchElement($activity, 'as:object')) {
302                         Logger::log('Empty object', Logger::DEBUG);
303                         return;
304                 }
305
306                 if (!JsonLD::fetchElement($activity, 'as:actor')) {
307                         Logger::log('Empty actor', Logger::DEBUG);
308                         return;
309
310                 }
311
312                 // $trust_source is called by reference and is set to true if the content was retrieved successfully
313                 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
314                 if (empty($object_data)) {
315                         Logger::log('No object data found', Logger::DEBUG);
316                         return;
317                 }
318
319                 if (!$trust_source) {
320                         Logger::log('No trust for activity type "' . $type . '", so we quit now.', Logger::DEBUG);
321                         return;
322                 }
323
324                 self::storeConversation($object_data, $body);
325
326                 // Internal flag for thread completion. See Processor.php
327                 if (!empty($activity['thread-completion'])) {
328                         $object_data['thread-completion'] = $activity['thread-completion'];
329                 }
330
331                 switch ($type) {
332                         case 'as:Create':
333                         case 'as:Announce':
334                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
335                                         ActivityPub\Processor::createItem($object_data);
336                                 }
337                                 break;
338
339                         case 'as:Like':
340                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
341                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE);
342                                 }
343                                 break;
344
345                         case 'as:Dislike':
346                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
347                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE);
348                                 }
349                                 break;
350
351                         case 'as:TentativeAccept':
352                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
353                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE);
354                                 }
355                                 break;
356
357                         case 'as:Update':
358                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
359                                         ActivityPub\Processor::updateItem($object_data);
360                                 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
361                                         ActivityPub\Processor::updatePerson($object_data, $body);
362                                 }
363                                 break;
364
365                         case 'as:Delete':
366                                 if ($object_data['object_type'] == 'as:Tombstone') {
367                                         ActivityPub\Processor::deleteItem($object_data, $body);
368                                 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
369                                         ActivityPub\Processor::deletePerson($object_data, $body);
370                                 }
371                                 break;
372
373                         case 'as:Follow':
374                                 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
375                                         ActivityPub\Processor::followUser($object_data);
376                                 }
377                                 break;
378
379                         case 'as:Accept':
380                                 if ($object_data['object_type'] == 'as:Follow') {
381                                         ActivityPub\Processor::acceptFollowUser($object_data);
382                                 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
383                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND);
384                                 }
385                                 break;
386
387                         case 'as:Reject':
388                                 if ($object_data['object_type'] == 'as:Follow') {
389                                         ActivityPub\Processor::rejectFollowUser($object_data);
390                                 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
391                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO);
392                                 }
393                                 break;
394
395                         case 'as:Undo':
396                                 if (($object_data['object_type'] == 'as:Follow') &&
397                                         in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
398                                         ActivityPub\Processor::undoFollowUser($object_data);
399                                 } elseif (($object_data['object_type'] == 'as:Accept') &&
400                                         in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
401                                         ActivityPub\Processor::rejectFollowUser($object_data);
402                                 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
403                                         in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
404                                         ActivityPub\Processor::undoActivity($object_data);
405                                 }
406                                 break;
407
408                         default:
409                                 Logger::log('Unknown activity: ' . $type . ' ' . $object_data['object_type'], Logger::DEBUG);
410                                 break;
411                 }
412         }
413
414         /**
415          * Fetch the receiver list from an activity array
416          *
417          * @param array $activity
418          * @param string $actor
419          * @param array $tags
420          *
421          * @return array with receivers (user id)
422          */
423         private static function getReceivers($activity, $actor, $tags = [])
424         {
425                 $receivers = [];
426
427                 // When it is an answer, we inherite the receivers from the parent
428                 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo');
429                 if (!empty($replyto)) {
430                         $parents = Item::select(['uid'], ['uri' => $replyto]);
431                         while ($parent = Item::fetch($parents)) {
432                                 $receivers['uid:' . $parent['uid']] = $parent['uid'];
433                         }
434                 }
435
436                 if (!empty($actor)) {
437                         $profile = APContact::getByURL($actor);
438                         $followers = defaults($profile, 'followers', '');
439
440                         Logger::log('Actor: ' . $actor . ' - Followers: ' . $followers, Logger::DEBUG);
441                 } else {
442                         Logger::log('Empty actor', Logger::DEBUG);
443                         $followers = '';
444                 }
445
446                 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
447                         $receiver_list = JsonLD::fetchElementArray($activity, $element);
448                         if (empty($receiver_list)) {
449                                 continue;
450                         }
451
452                         foreach ($receiver_list as $receiver) {
453                                 if ($receiver == self::PUBLIC_COLLECTION) {
454                                         $receivers['uid:0'] = 0;
455                                 }
456
457                                 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
458                                         // This will most likely catch all OStatus connections to Mastodon
459                                         $condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
460                                                 , 'archive' => false, 'pending' => false];
461                                         $contacts = DBA::select('contact', ['uid'], $condition);
462                                         while ($contact = DBA::fetch($contacts)) {
463                                                 if ($contact['uid'] != 0) {
464                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
465                                                 }
466                                         }
467                                         DBA::close($contacts);
468                                 }
469
470                                 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
471                                         $receivers = array_merge($receivers, self::getReceiverForActor($actor, $tags));
472                                         continue;
473                                 }
474
475                                 // Fetching all directly addressed receivers
476                                 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
477                                 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
478                                 if (!DBA::isResult($contact)) {
479                                         continue;
480                                 }
481
482                                 // Check if the potential receiver is following the actor
483                                 // Exception: The receiver is targetted via "to" or this is a comment
484                                 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::ACCOUNT_TYPE_COMMUNITY)) {
485                                         $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
486                                         $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
487                                                 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
488
489                                         // Forum posts are only accepted from forum contacts
490                                         if ($contact['contact-type'] == Contact::ACCOUNT_TYPE_COMMUNITY) {
491                                                 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
492                                         }
493
494                                         if (!DBA::exists('contact', $condition)) {
495                                                 continue;
496                                         }
497                                 }
498
499                                 $receivers['uid:' . $contact['uid']] = $contact['uid'];
500                         }
501                 }
502
503                 self::switchContacts($receivers, $actor);
504
505                 return $receivers;
506         }
507
508         /**
509          * Fetch the receiver list of a given actor
510          *
511          * @param string $actor
512          * @param array $tags
513          *
514          * @return array with receivers (user id)
515          */
516         public static function getReceiverForActor($actor, $tags)
517         {
518                 $receivers = [];
519                 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
520                 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
521                         'network' => $networks, 'archive' => false, 'pending' => false];
522                 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
523                 while ($contact = DBA::fetch($contacts)) {
524                         if (self::isValidReceiverForActor($contact, $actor, $tags)) {
525                                 $receivers['uid:' . $contact['uid']] = $contact['uid'];
526                         }
527                 }
528                 DBA::close($contacts);
529                 return $receivers;
530         }
531
532         /**
533          * Tests if the contact is a valid receiver for this actor
534          *
535          * @param array $contact
536          * @param string $actor
537          * @param array $tags
538          *
539          * @return array with receivers (user id)
540          */
541         private static function isValidReceiverForActor($contact, $actor, $tags)
542         {
543                 // Public contacts are no valid receiver
544                 if ($contact['uid'] == 0) {
545                         return false;
546                 }
547
548                 // Are we following the contact? Then this is a valid receiver
549                 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
550                         return true;
551                 }
552
553                 // When the possible receiver isn't a community, then it is no valid receiver
554                 $owner = User::getOwnerDataById($contact['uid']);
555                 if (empty($owner) || ($owner['contact-type'] != Contact::ACCOUNT_TYPE_COMMUNITY)) {
556                         return false;
557                 }
558
559                 // Is the community account tagged?
560                 foreach ($tags as $tag) {
561                         if ($tag['type'] != 'Mention') {
562                                 continue;
563                         }
564
565                         if ($tag['href'] == $owner['url']) {
566                                 return true;
567                         }
568                 }
569
570                 return false;
571         }
572
573         /**
574          * Switches existing contacts to ActivityPub
575          *
576          * @param integer $cid Contact ID
577          * @param integer $uid User ID
578          * @param string $url Profile URL
579          */
580         public static function switchContact($cid, $uid, $url)
581         {
582                 $profile = ActivityPub::probeProfile($url);
583                 if (empty($profile)) {
584                         return;
585                 }
586
587                 Logger::log('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' to ActivityPub');
588
589                 $photo = defaults($profile, 'photo', null);
590                 unset($profile['photo']);
591                 unset($profile['baseurl']);
592
593                 $profile['nurl'] = Strings::normaliseLink($profile['url']);
594                 DBA::update('contact', $profile, ['id' => $cid]);
595
596                 Contact::updateAvatar($photo, $uid, $cid);
597
598                 // Send a new follow request to be sure that the connection still exists
599                 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND]])) {
600                         ActivityPub\Transmitter::sendActivity('Follow', $profile['url'], $uid);
601                         Logger::log('Send a new follow request to ' . $profile['url'] . ' for user ' . $uid, Logger::DEBUG);
602                 }
603         }
604
605         /**
606          *
607          *
608          * @param $receivers
609          * @param $actor
610          */
611         private static function switchContacts($receivers, $actor)
612         {
613                 if (empty($actor)) {
614                         return;
615                 }
616
617                 foreach ($receivers as $receiver) {
618                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
619                         if (DBA::isResult($contact)) {
620                                 self::switchContact($contact['id'], $receiver, $actor);
621                         }
622
623                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
624                         if (DBA::isResult($contact)) {
625                                 self::switchContact($contact['id'], $receiver, $actor);
626                         }
627                 }
628         }
629
630         /**
631          *
632          *
633          * @param $object_data
634          * @param array $activity
635          *
636          * @return
637          */
638         private static function addActivityFields($object_data, $activity)
639         {
640                 if (!empty($activity['published']) && empty($object_data['published'])) {
641                         $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
642                 }
643
644                 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
645                         $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid');
646                 }
647
648                 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
649
650                 return $object_data;
651         }
652
653         /**
654          * Fetches the object data from external ressources if needed
655          *
656          * @param string  $object_id    Object ID of the the provided object
657          * @param array   $object       The provided object array
658          * @param boolean $trust_source Do we trust the provided object?
659          * @param integer $uid          User ID for the signature that we use to fetch data
660          *
661          * @return array with trusted and valid object data
662          */
663         private static function fetchObject($object_id, $object = [], $trust_source = false, $uid = 0)
664         {
665                 // By fetching the type we check if the object is complete.
666                 $type = JsonLD::fetchElement($object, '@type');
667
668                 if (!$trust_source || empty($type)) {
669                         $data = ActivityPub::fetchContent($object_id, $uid);
670                         if (!empty($data)) {
671                                 $object = JsonLD::compact($data);
672                                 Logger::log('Fetched content for ' . $object_id, Logger::DEBUG);
673                         } else {
674                                 Logger::log('Empty content for ' . $object_id . ', check if content is available locally.', Logger::DEBUG);
675
676                                 $item = Item::selectFirst([], ['uri' => $object_id]);
677                                 if (!DBA::isResult($item)) {
678                                         Logger::log('Object with url ' . $object_id . ' was not found locally.', Logger::DEBUG);
679                                         return false;
680                                 }
681                                 Logger::log('Using already stored item for url ' . $object_id, Logger::DEBUG);
682                                 $data = ActivityPub\Transmitter::createNote($item);
683                                 $object = JsonLD::compact($data);
684                         }
685                 } else {
686                         Logger::log('Using original object for url ' . $object_id, Logger::DEBUG);
687                 }
688
689                 $type = JsonLD::fetchElement($object, '@type');
690
691                 if (empty($type)) {
692                         Logger::log('Empty type', Logger::DEBUG);
693                         return false;
694                 }
695
696                 if (in_array($type, self::CONTENT_TYPES)) {
697                         return self::processObject($object);
698                 }
699
700                 if ($type == 'as:Announce') {
701                         $object_id = JsonLD::fetchElement($object, 'object');
702                         if (empty($object_id)) {
703                                 return false;
704                         }
705                         return self::fetchObject($object_id, [], false, $uid);
706                 }
707
708                 Logger::log('Unhandled object type: ' . $type, Logger::DEBUG);
709         }
710
711         /**
712          * Convert tags from JSON-LD format into a simplified format
713          *
714          * @param array $tags Tags in JSON-LD format
715          *
716          * @return array with tags in a simplified format
717          */
718         private static function processTags($tags)
719         {
720                 $taglist = [];
721
722                 if (empty($tags)) {
723                         return [];
724                 }
725
726                 foreach ($tags as $tag) {
727                         if (empty($tag)) {
728                                 continue;
729                         }
730
731                         $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
732                                 'href' => JsonLD::fetchElement($tag, 'as:href'),
733                                 'name' => JsonLD::fetchElement($tag, 'as:name')];
734
735                         if (empty($element['type'])) {
736                                 continue;
737                         }
738
739                         $taglist[] = $element;
740                 }
741                 return $taglist;
742         }
743
744         /**
745          * Convert emojis from JSON-LD format into a simplified format
746          *
747          * @param array $tags Tags in JSON-LD format
748          *
749          * @return array with emojis in a simplified format
750          */
751         private static function processEmojis($emojis)
752         {
753                 $emojilist = [];
754
755                 if (empty($emojis)) {
756                         return [];
757                 }
758
759                 foreach ($emojis as $emoji) {
760                         if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
761                                 continue;
762                         }
763
764                         $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url');
765                         $element = ['name' => JsonLD::fetchElement($emoji, 'as:name'),
766                                 'href' => $url];
767
768                         $emojilist[] = $element;
769                 }
770                 return $emojilist;
771         }
772
773         /**
774          * Convert attachments from JSON-LD format into a simplified format
775          *
776          * @param array $attachments Attachments in JSON-LD format
777          *
778          * @return array with attachmants in a simplified format
779          */
780         private static function processAttachments($attachments)
781         {
782                 $attachlist = [];
783
784                 if (empty($attachments)) {
785                         return [];
786                 }
787
788                 foreach ($attachments as $attachment) {
789                         if (empty($attachment)) {
790                                 continue;
791                         }
792
793                         $attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
794                                 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType'),
795                                 'name' => JsonLD::fetchElement($attachment, 'as:name'),
796                                 'url' => JsonLD::fetchElement($attachment, 'as:url')];
797                 }
798                 return $attachlist;
799         }
800
801         /**
802          * Fetches data from the object part of an activity
803          *
804          * @param array $object
805          *
806          * @return array
807          */
808         private static function processObject($object)
809         {
810                 if (!JsonLD::fetchElement($object, '@id')) {
811                         return false;
812                 }
813
814                 $object_data = [];
815                 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
816                 $object_data['id'] = JsonLD::fetchElement($object, '@id');
817
818                 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo');
819
820                 if (empty($object_data['reply-to-id'])) {
821                         $object_data['reply-to-id'] = $object_data['id'];
822                 }
823
824                 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
825                 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
826
827                 if (empty($object_data['updated'])) {
828                         $object_data['updated'] = $object_data['published'];
829                 }
830
831                 if (empty($object_data['published']) && !empty($object_data['updated'])) {
832                         $object_data['published'] = $object_data['updated'];
833                 }
834
835                 $actor = JsonLD::fetchElement($object, 'as:attributedTo');
836                 if (empty($actor)) {
837                         $actor = JsonLD::fetchElement($object, 'as:actor');
838                 }
839
840                 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid');
841                 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment');
842                 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like');
843                 $object_data['actor'] = $object_data['author'] = $actor;
844                 $object_data['context'] = JsonLD::fetchElement($object, 'as:context');
845                 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation');
846                 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
847                 $object_data['name'] = JsonLD::fetchElement($object, 'as:name');
848                 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary');
849                 $object_data['content'] = JsonLD::fetchElement($object, 'as:content');
850                 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
851                 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
852                 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
853                 $object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
854                 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
855                 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
856                 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
857                 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
858                 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
859                 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
860                 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
861                 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
862                 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url');
863
864                 // Special treatment for Hubzilla links
865                 if (is_array($object_data['alternate-url'])) {
866                         $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href');
867
868                         if (!is_string($object_data['alternate-url'])) {
869                                 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href');
870                         }
871                 }
872
873                 $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags']);
874
875                 // Common object data:
876
877                 // Unhandled
878                 // @context, type, actor, signature, mediaType, duration, replies, icon
879
880                 // Also missing: (Defined in the standard, but currently unused)
881                 // audience, preview, endTime, startTime, image
882
883                 // Data in Notes:
884
885                 // Unhandled
886                 // contentMap, announcement_count, announcements, context_id, likes, like_count
887                 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
888
889                 // Data in video:
890
891                 // To-Do?
892                 // category, licence, language, commentsEnabled
893
894                 // Unhandled
895                 // views, waitTranscoding, state, support, subtitleLanguage
896                 // likes, dislikes, shares, comments
897
898                 return $object_data;
899         }
900 }