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