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