]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Receiver.php
Preparations for a relocation message / fix for notice
[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          *
117          * @return string with object type
118          */
119         private static function fetchObjectType($activity, $object_id)
120         {
121                 if (!empty($activity['as:object'])) {
122                         $object_type = JsonLD::fetchElement($activity['as:object'], '@type');
123                         if (!empty($object_type)) {
124                                 return $object_type;
125                         }
126                 }
127
128                 if (Item::exists(['uri' => $object_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]])) {
129                         // We just assume "note" since it doesn't make a difference for the further processing
130                         return 'as:Note';
131                 }
132
133                 $profile = APContact::getByURL($object_id);
134                 if (!empty($profile['type'])) {
135                         return 'as:' . $profile['type'];
136                 }
137
138                 $data = ActivityPub::fetchContent($object_id);
139                 if (!empty($data)) {
140                         $object = JsonLD::compact($data);
141                         $type = JsonLD::fetchElement($object, '@type');
142                         if (!empty($type)) {
143                                 return $type;
144                         }
145                 }
146
147                 return null;
148         }
149
150         /**
151          * Prepare the object array
152          *
153          * @param array $activity
154          * @param integer $uid User ID
155          * @param $trust_source
156          *
157          * @return array with object data
158          */
159         private static function prepareObjectData($activity, $uid, &$trust_source)
160         {
161                 $actor = JsonLD::fetchElement($activity, 'as:actor');
162                 if (empty($actor)) {
163                         Logger::log('Empty actor', Logger::DEBUG);
164                         return [];
165                 }
166
167                 $type = JsonLD::fetchElement($activity, '@type');
168
169                 // Fetch all receivers from to, cc, bto and bcc
170                 $receivers = self::getReceivers($activity, $actor);
171
172                 // When it is a delivery to a personal inbox we add that user to the receivers
173                 if (!empty($uid)) {
174                         $owner = User::getOwnerDataById($uid);
175                         $additional = ['uid:' . $uid => $uid];
176                         $receivers = array_merge($receivers, $additional);
177                 }
178
179                 Logger::log('Receivers: ' . json_encode($receivers), Logger::DEBUG);
180
181                 $object_id = JsonLD::fetchElement($activity, 'as:object');
182                 if (empty($object_id)) {
183                         Logger::log('No object found', Logger::DEBUG);
184                         return [];
185                 }
186
187                 $object_type = self::fetchObjectType($activity, $object_id);
188
189                 // Fetch the content only on activities where this matters
190                 if (in_array($type, ['as:Create', 'as:Update', 'as:Announce'])) {
191                         if ($type == 'as:Announce') {
192                                 $trust_source = false;
193                         }
194                         $object_data = self::fetchObject($object_id, $activity['as:object'], $trust_source);
195                         if (empty($object_data)) {
196                                 Logger::log("Object data couldn't be processed", Logger::DEBUG);
197                                 return [];
198                         }
199                         // We had been able to retrieve the object data - so we can trust the source
200                         $trust_source = true;
201                 } elseif (in_array($type, ['as:Like', 'as:Dislike'])) {
202                         // Create a mostly empty array out of the activity data (instead of the object).
203                         // This way we later don't have to check for the existence of ech individual array element.
204                         $object_data = self::processObject($activity);
205                         $object_data['name'] = $type;
206                         $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor');
207                         $object_data['object_id'] = $object_id;
208                         $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
209                 } else {
210                         $object_data = [];
211                         $object_data['id'] = JsonLD::fetchElement($activity, '@id');
212                         $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object');
213                         $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor');
214                         $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
215                         $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
216
217                         // An Undo is done on the object of an object, so we need that type as well
218                         if ($type == 'as:Undo') {
219                                 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object']);
220                         }
221                 }
222
223                 $object_data = self::addActivityFields($object_data, $activity);
224
225                 if (empty($object_data['object_type'])) {
226                         $object_data['object_type'] = $object_type;
227                 }
228
229                 $object_data['type'] = $type;
230                 $object_data['actor'] = $actor;
231                 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
232
233                 Logger::log('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], Logger::DEBUG);
234
235                 return $object_data;
236         }
237
238         /**
239          * Store the unprocessed data into the conversation table
240          * This has to be done outside the regular function,
241          * since we store everything - not only item posts.
242          *
243          * @param array  $activity Array with activity data
244          * @param string $body     The raw message
245          */
246         private static function storeConversation($activity, $body)
247         {
248                 if (empty($body) || empty($activity['id'])) {
249                         return;
250                 }
251
252                 $conversation = [
253                         'protocol' => Conversation::PARCEL_ACTIVITYPUB,
254                         'item-uri' => $activity['id'],
255                         'reply-to-uri' => defaults($activity, 'reply-to-id', ''),
256                         'conversation-href' => defaults($activity, 'context', ''),
257                         'conversation-uri' => defaults($activity, 'conversation', ''),
258                         'source' => $body,
259                         'received' => DateTimeFormat::utcNow()];
260
261                 DBA::insert('conversation', $conversation, true);
262         }
263
264         /**
265          * Processes the activity object
266          *
267          * @param array   $activity     Array with activity data
268          * @param string  $body
269          * @param integer $uid          User ID
270          * @param boolean $trust_source Do we trust the source?
271          */
272         public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
273         {
274                 $type = JsonLD::fetchElement($activity, '@type');
275                 if (!$type) {
276                         Logger::log('Empty type', Logger::DEBUG);
277                         return;
278                 }
279
280                 if (!JsonLD::fetchElement($activity, 'as:object')) {
281                         Logger::log('Empty object', Logger::DEBUG);
282                         return;
283                 }
284
285                 if (!JsonLD::fetchElement($activity, 'as:actor')) {
286                         Logger::log('Empty actor', Logger::DEBUG);
287                         return;
288
289                 }
290
291                 // $trust_source is called by reference and is set to true if the content was retrieved successfully
292                 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
293                 if (empty($object_data)) {
294                         Logger::log('No object data found', Logger::DEBUG);
295                         return;
296                 }
297
298                 if (!$trust_source) {
299                         Logger::log('No trust for activity type "' . $type . '", so we quit now.', Logger::DEBUG);
300                         return;
301                 }
302
303                 self::storeConversation($object_data, $body);
304
305                 // Internal flag for thread completion. See Processor.php
306                 if (!empty($activity['thread-completion'])) {
307                         $object_data['thread-completion'] = $activity['thread-completion'];
308                 }
309
310                 switch ($type) {
311                         case 'as:Create':
312                         case 'as:Announce':
313                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
314                                         ActivityPub\Processor::createItem($object_data);
315                                 }
316                                 break;
317
318                         case 'as:Like':
319                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
320                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE);
321                                 }
322                                 break;
323
324                         case 'as:Dislike':
325                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
326                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE);
327                                 }
328                                 break;
329
330                         case 'as:TentativeAccept':
331                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
332                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE);
333                                 }
334                                 break;
335
336                         case 'as:Update':
337                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
338                                         ActivityPub\Processor::updateItem($object_data);
339                                 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
340                                         ActivityPub\Processor::updatePerson($object_data, $body);
341                                 }
342                                 break;
343
344                         case 'as:Delete':
345                                 if ($object_data['object_type'] == 'as:Tombstone') {
346                                         ActivityPub\Processor::deleteItem($object_data, $body);
347                                 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
348                                         ActivityPub\Processor::deletePerson($object_data, $body);
349                                 }
350                                 break;
351
352                         case 'as:Follow':
353                                 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
354                                         ActivityPub\Processor::followUser($object_data);
355                                 }
356                                 break;
357
358                         case 'as:Accept':
359                                 if ($object_data['object_type'] == 'as:Follow') {
360                                         ActivityPub\Processor::acceptFollowUser($object_data);
361                                 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
362                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND);
363                                 }
364                                 break;
365
366                         case 'as:Reject':
367                                 if ($object_data['object_type'] == 'as:Follow') {
368                                         ActivityPub\Processor::rejectFollowUser($object_data);
369                                 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
370                                         ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO);
371                                 }
372                                 break;
373
374                         case 'as:Undo':
375                                 if (($object_data['object_type'] == 'as:Follow') &&
376                                         in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
377                                         ActivityPub\Processor::undoFollowUser($object_data);
378                                 } elseif (($object_data['object_type'] == 'as:Accept') &&
379                                         in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
380                                         ActivityPub\Processor::rejectFollowUser($object_data);
381                                 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
382                                         in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
383                                         ActivityPub\Processor::undoActivity($object_data);
384                                 }
385                                 break;
386
387                         default:
388                                 Logger::log('Unknown activity: ' . $type . ' ' . $object_data['object_type'], Logger::DEBUG);
389                                 break;
390                 }
391         }
392
393         /**
394          * Fetch the receiver list from an activity array
395          *
396          * @param array $activity
397          * @param string $actor
398          *
399          * @return array with receivers (user id)
400          */
401         private static function getReceivers($activity, $actor)
402         {
403                 $receivers = [];
404
405                 // When it is an answer, we inherite the receivers from the parent
406                 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo');
407                 if (!empty($replyto)) {
408                         $parents = Item::select(['uid'], ['uri' => $replyto]);
409                         while ($parent = Item::fetch($parents)) {
410                                 $receivers['uid:' . $parent['uid']] = $parent['uid'];
411                         }
412                 }
413
414                 if (!empty($actor)) {
415                         $profile = APContact::getByURL($actor);
416                         $followers = defaults($profile, 'followers', '');
417
418                         Logger::log('Actor: ' . $actor . ' - Followers: ' . $followers, Logger::DEBUG);
419                 } else {
420                         Logger::log('Empty actor', Logger::DEBUG);
421                         $followers = '';
422                 }
423
424                 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
425                         $receiver_list = JsonLD::fetchElementArray($activity, $element);
426                         if (empty($receiver_list)) {
427                                 continue;
428                         }
429
430                         foreach ($receiver_list as $receiver) {
431                                 if ($receiver == self::PUBLIC_COLLECTION) {
432                                         $receivers['uid:0'] = 0;
433                                 }
434
435                                 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
436                                         // This will most likely catch all OStatus connections to Mastodon
437                                         $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
438                                                 , 'archive' => false, 'pending' => false];
439                                         $contacts = DBA::select('contact', ['uid'], $condition);
440                                         while ($contact = DBA::fetch($contacts)) {
441                                                 if ($contact['uid'] != 0) {
442                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
443                                                 }
444                                         }
445                                         DBA::close($contacts);
446                                 }
447
448                                 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
449                                         $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
450                                         $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
451                                                 'network' => $networks, 'archive' => false, 'pending' => false];
452                                         $contacts = DBA::select('contact', ['uid'], $condition);
453                                         while ($contact = DBA::fetch($contacts)) {
454                                                 if ($contact['uid'] != 0) {
455                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
456                                                 }
457                                         }
458                                         DBA::close($contacts);
459                                         continue;
460                                 }
461
462                                 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
463                                 $contact = DBA::selectFirst('contact', ['uid'], $condition);
464                                 if (!DBA::isResult($contact)) {
465                                         continue;
466                                 }
467                                 $receivers['uid:' . $contact['uid']] = $contact['uid'];
468                         }
469                 }
470
471                 self::switchContacts($receivers, $actor);
472
473                 return $receivers;
474         }
475
476         /**
477          * Switches existing contacts to ActivityPub
478          *
479          * @param integer $cid Contact ID
480          * @param integer $uid User ID
481          * @param string $url Profile URL
482          */
483         public static function switchContact($cid, $uid, $url)
484         {
485                 $profile = ActivityPub::probeProfile($url);
486                 if (empty($profile)) {
487                         return;
488                 }
489
490                 Logger::log('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' to ActivityPub');
491
492                 $photo = defaults($profile, 'photo', null);
493                 unset($profile['photo']);
494                 unset($profile['baseurl']);
495
496                 $profile['nurl'] = normalise_link($profile['url']);
497                 DBA::update('contact', $profile, ['id' => $cid]);
498
499                 Contact::updateAvatar($photo, $uid, $cid);
500
501                 // Send a new follow request to be sure that the connection still exists
502                 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND]])) {
503                         ActivityPub\Transmitter::sendActivity('Follow', $profile['url'], $uid);
504                         Logger::log('Send a new follow request to ' . $profile['url'] . ' for user ' . $uid, Logger::DEBUG);
505                 }
506         }
507
508         /**
509          *
510          *
511          * @param $receivers
512          * @param $actor
513          */
514         private static function switchContacts($receivers, $actor)
515         {
516                 if (empty($actor)) {
517                         return;
518                 }
519
520                 foreach ($receivers as $receiver) {
521                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
522                         if (DBA::isResult($contact)) {
523                                 self::switchContact($contact['id'], $receiver, $actor);
524                         }
525
526                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
527                         if (DBA::isResult($contact)) {
528                                 self::switchContact($contact['id'], $receiver, $actor);
529                         }
530                 }
531         }
532
533         /**
534          *
535          *
536          * @param $object_data
537          * @param array $activity
538          *
539          * @return
540          */
541         private static function addActivityFields($object_data, $activity)
542         {
543                 if (!empty($activity['published']) && empty($object_data['published'])) {
544                         $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
545                 }
546
547                 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
548                         $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid');
549                 }
550
551                 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
552
553                 return $object_data;
554         }
555
556         /**
557          * Fetches the object data from external ressources if needed
558          *
559          * @param string  $object_id    Object ID of the the provided object
560          * @param array   $object       The provided object array
561          * @param boolean $trust_source Do we trust the provided object?
562          *
563          * @return array with trusted and valid object data
564          */
565         private static function fetchObject($object_id, $object = [], $trust_source = false)
566         {
567                 // By fetching the type we check if the object is complete.
568                 $type = JsonLD::fetchElement($object, '@type');
569
570                 if (!$trust_source || empty($type)) {
571                         $data = ActivityPub::fetchContent($object_id);
572                         if (!empty($data)) {
573                                 $object = JsonLD::compact($data);
574                                 Logger::log('Fetched content for ' . $object_id, Logger::DEBUG);
575                         } else {
576                                 Logger::log('Empty content for ' . $object_id . ', check if content is available locally.', Logger::DEBUG);
577
578                                 $item = Item::selectFirst([], ['uri' => $object_id]);
579                                 if (!DBA::isResult($item)) {
580                                         Logger::log('Object with url ' . $object_id . ' was not found locally.', Logger::DEBUG);
581                                         return false;
582                                 }
583                                 Logger::log('Using already stored item for url ' . $object_id, Logger::DEBUG);
584                                 $data = ActivityPub\Transmitter::createNote($item);
585                                 $object = JsonLD::compact($data);
586                         }
587                 } else {
588                         Logger::log('Using original object for url ' . $object_id, Logger::DEBUG);
589                 }
590
591                 $type = JsonLD::fetchElement($object, '@type');
592
593                 if (empty($type)) {
594                         Logger::log('Empty type', Logger::DEBUG);
595                         return false;
596                 }
597
598                 if (in_array($type, self::CONTENT_TYPES)) {
599                         return self::processObject($object);
600                 }
601
602                 if ($type == 'as:Announce') {
603                         $object_id = JsonLD::fetchElement($object, 'object');
604                         if (empty($object_id)) {
605                                 return false;
606                         }
607                         return self::fetchObject($object_id);
608                 }
609
610                 Logger::log('Unhandled object type: ' . $type, Logger::DEBUG);
611         }
612
613         /**
614          * Convert tags from JSON-LD format into a simplified format
615          *
616          * @param array $tags Tags in JSON-LD format
617          *
618          * @return array with tags in a simplified format
619          */
620         private static function processTags($tags)
621         {
622                 $taglist = [];
623
624                 if (empty($tags)) {
625                         return [];
626                 }
627
628                 foreach ($tags as $tag) {
629                         if (empty($tag)) {
630                                 continue;
631                         }
632
633                         $taglist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
634                                 'href' => JsonLD::fetchElement($tag, 'as:href'),
635                                 'name' => JsonLD::fetchElement($tag, 'as:name')];
636                 }
637                 return $taglist;
638         }
639
640         /**
641          * Convert attachments from JSON-LD format into a simplified format
642          *
643          * @param array $attachments Attachments in JSON-LD format
644          *
645          * @return array with attachmants in a simplified format
646          */
647         private static function processAttachments($attachments)
648         {
649                 $attachlist = [];
650
651                 if (empty($attachments)) {
652                         return [];
653                 }
654
655                 foreach ($attachments as $attachment) {
656                         if (empty($attachment)) {
657                                 continue;
658                         }
659
660                         $attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
661                                 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType'),
662                                 'name' => JsonLD::fetchElement($attachment, 'as:name'),
663                                 'url' => JsonLD::fetchElement($attachment, 'as:url')];
664                 }
665                 return $attachlist;
666         }
667
668         /**
669          * Fetches data from the object part of an activity
670          *
671          * @param array $object
672          *
673          * @return array
674          */
675         private static function processObject($object)
676         {
677                 if (!JsonLD::fetchElement($object, '@id')) {
678                         return false;
679                 }
680
681                 $object_data = [];
682                 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
683                 $object_data['id'] = JsonLD::fetchElement($object, '@id');
684
685                 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo');
686
687                 if (empty($object_data['reply-to-id'])) {
688                         $object_data['reply-to-id'] = $object_data['id'];
689                 }
690
691                 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
692                 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
693
694                 if (empty($object_data['updated'])) {
695                         $object_data['updated'] = $object_data['published'];
696                 }
697
698                 if (empty($object_data['published']) && !empty($object_data['updated'])) {
699                         $object_data['published'] = $object_data['updated'];
700                 }
701
702                 $actor = JsonLD::fetchElement($object, 'as:attributedTo');
703                 if (empty($actor)) {
704                         $actor = JsonLD::fetchElement($object, 'as:actor');
705                 }
706
707                 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid');
708                 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment');
709                 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like');
710                 $object_data['actor'] = $object_data['author'] = $actor;
711                 $object_data['context'] = JsonLD::fetchElement($object, 'as:context');
712                 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation');
713                 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
714                 $object_data['name'] = JsonLD::fetchElement($object, 'as:name');
715                 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary');
716                 $object_data['content'] = JsonLD::fetchElement($object, 'as:content');
717                 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
718                 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
719                 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
720                 $object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
721                 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
722                 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
723                 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
724                 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
725                 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
726                 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
727                 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
728                 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url');
729
730                 // Special treatment for Hubzilla links
731                 if (is_array($object_data['alternate-url'])) {
732                         $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href');
733
734                         if (!is_string($object_data['alternate-url'])) {
735                                 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href');
736                         }
737                 }
738
739                 $object_data['receiver'] = self::getReceivers($object, $object_data['actor']);
740
741                 // Common object data:
742
743                 // Unhandled
744                 // @context, type, actor, signature, mediaType, duration, replies, icon
745
746                 // Also missing: (Defined in the standard, but currently unused)
747                 // audience, preview, endTime, startTime, image
748
749                 // Data in Notes:
750
751                 // Unhandled
752                 // contentMap, announcement_count, announcements, context_id, likes, like_count
753                 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
754
755                 // Data in video:
756
757                 // To-Do?
758                 // category, licence, language, commentsEnabled
759
760                 // Unhandled
761                 // views, waitTranscoding, state, support, subtitleLanguage
762                 // likes, dislikes, shares, comments
763
764                 return $object_data;
765         }
766 }