]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Receiver.php
Merge branch 'bug/phpinfo-accessible-hotfix' into 2020.09-rc
[friendica.git] / src / Protocol / ActivityPub / Receiver.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Protocol\ActivityPub;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Database\DBA;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Content\Text\Markdown;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Model\Contact;
31 use Friendica\Model\APContact;
32 use Friendica\Model\Item;
33 use Friendica\Model\User;
34 use Friendica\Protocol\Activity;
35 use Friendica\Protocol\ActivityPub;
36 use Friendica\Util\DateTimeFormat;
37 use Friendica\Util\HTTPSignature;
38 use Friendica\Util\JsonLD;
39 use Friendica\Util\LDSignature;
40 use Friendica\Util\Strings;
41
42 /**
43  * ActivityPub Receiver Protocol class
44  *
45  * To-Do:
46  * @todo Undo Announce
47  *
48  * Check what this is meant to do:
49  * - Add
50  * - Block
51  * - Flag
52  * - Remove
53  * - Undo Block
54  */
55 class Receiver
56 {
57         const PUBLIC_COLLECTION = 'as:Public';
58         const ACCOUNT_TYPES = ['as:Person', 'as:Organization', 'as:Service', 'as:Group', 'as:Application'];
59         const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event', 'as:Audio'];
60         const ACTIVITY_TYPES = ['as:Like', 'as:Dislike', 'as:Accept', 'as:Reject', 'as:TentativeAccept'];
61
62         /**
63          * Checks if the web request is done for the AP protocol
64          *
65          * @return bool is it AP?
66          */
67         public static function isRequest()
68         {
69                 return stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
70                         stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
71         }
72
73         /**
74          * Checks incoming message from the inbox
75          *
76          * @param         $body
77          * @param         $header
78          * @param integer $uid User ID
79          * @throws \Exception
80          */
81         public static function processInbox($body, $header, $uid)
82         {
83                 $http_signer = HTTPSignature::getSigner($body, $header);
84                 if (empty($http_signer)) {
85                         Logger::warning('Invalid HTTP signature, message will be discarded.');
86                         return;
87                 } else {
88                         Logger::info('Valid HTTP signature', ['signer' => $http_signer]);
89                 }
90
91                 $activity = json_decode($body, true);
92
93                 if (empty($activity)) {
94                         Logger::warning('Invalid body.');
95                         return;
96                 }
97
98                 $ldactivity = JsonLD::compact($activity);
99
100                 $actor = JsonLD::fetchElement($ldactivity, 'as:actor', '@id');
101
102                 Logger::info('Message for user ' . $uid . ' is from actor ' . $actor);
103
104                 if (LDSignature::isSigned($activity)) {
105                         $ld_signer = LDSignature::getSigner($activity);
106                         if (empty($ld_signer)) {
107                                 Logger::log('Invalid JSON-LD signature from ' . $actor, Logger::DEBUG);
108                         }
109                         if (!empty($ld_signer && ($actor == $http_signer))) {
110                                 Logger::log('The HTTP and the JSON-LD signature belong to ' . $ld_signer, Logger::DEBUG);
111                                 $trust_source = true;
112                         } elseif (!empty($ld_signer)) {
113                                 Logger::log('JSON-LD signature is signed by ' . $ld_signer, Logger::DEBUG);
114                                 $trust_source = true;
115                         } elseif ($actor == $http_signer) {
116                                 Logger::log('Bad JSON-LD signature, but HTTP signer fits the actor.', Logger::DEBUG);
117                                 $trust_source = true;
118                         } else {
119                                 Logger::log('Invalid JSON-LD signature and the HTTP signer is different.', Logger::DEBUG);
120                                 $trust_source = false;
121                         }
122                 } elseif ($actor == $http_signer) {
123                         Logger::log('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', Logger::DEBUG);
124                         $trust_source = true;
125                 } else {
126                         Logger::log('No JSON-LD signature, different actor.', Logger::DEBUG);
127                         $trust_source = false;
128                 }
129
130                 self::processActivity($ldactivity, $body, $uid, $trust_source, true);
131         }
132
133         /**
134          * Fetches the object type for a given object id
135          *
136          * @param array   $activity
137          * @param string  $object_id Object ID of the the provided object
138          * @param integer $uid       User ID
139          *
140          * @return string with object type
141          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
142          * @throws \ImagickException
143          */
144         private static function fetchObjectType($activity, $object_id, $uid = 0)
145         {
146                 if (!empty($activity['as:object'])) {
147                         $object_type = JsonLD::fetchElement($activity['as:object'], '@type');
148                         if (!empty($object_type)) {
149                                 return $object_type;
150                         }
151                 }
152
153                 if (Item::exists(['uri' => $object_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]])) {
154                         // We just assume "note" since it doesn't make a difference for the further processing
155                         return 'as:Note';
156                 }
157
158                 $profile = APContact::getByURL($object_id);
159                 if (!empty($profile['type'])) {
160                         return 'as:' . $profile['type'];
161                 }
162
163                 $data = ActivityPub::fetchContent($object_id, $uid);
164                 if (!empty($data)) {
165                         $object = JsonLD::compact($data);
166                         $type = JsonLD::fetchElement($object, '@type');
167                         if (!empty($type)) {
168                                 return $type;
169                         }
170                 }
171
172                 return null;
173         }
174
175         /**
176          * Prepare the object array
177          *
178          * @param array   $activity     Array with activity data
179          * @param integer $uid          User ID
180          * @param boolean $push         Message had been pushed to our system
181          * @param boolean $trust_source Do we trust the source?
182          *
183          * @return array with object data
184          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
185          * @throws \ImagickException
186          */
187         public static function prepareObjectData($activity, $uid, $push, &$trust_source)
188         {
189                 $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
190                 if (empty($actor)) {
191                         Logger::log('Empty actor', Logger::DEBUG);
192                         return [];
193                 }
194
195                 $type = JsonLD::fetchElement($activity, '@type');
196
197                 // Fetch all receivers from to, cc, bto and bcc
198                 $receivers = self::getReceivers($activity, $actor);
199
200                 // When it is a delivery to a personal inbox we add that user to the receivers
201                 if (!empty($uid)) {
202                         $additional = ['uid:' . $uid => $uid];
203                         $receivers = array_merge($receivers, $additional);
204                 } else {
205                         // We possibly need some user to fetch private content,
206                         // so we fetch the first out ot the list.
207                         $uid = self::getFirstUserFromReceivers($receivers);
208                 }
209
210                 Logger::log('Receivers: ' . $uid . ' - ' . json_encode($receivers), Logger::DEBUG);
211
212                 $object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
213                 if (empty($object_id)) {
214                         Logger::log('No object found', Logger::DEBUG);
215                         return [];
216                 }
217
218                 if (!is_string($object_id)) {
219                         Logger::info('Invalid object id', ['object' => $object_id]);
220                         return [];
221                 }
222
223                 $object_type = self::fetchObjectType($activity, $object_id, $uid);
224
225                 // Fetch the content only on activities where this matters
226                 if (in_array($type, ['as:Create', 'as:Update', 'as:Announce'])) {
227                         if ($type == 'as:Announce') {
228                                 $trust_source = false;
229                         }
230
231                         $object_data = self::fetchObject($object_id, $activity['as:object'], $trust_source, $uid);
232                         if (empty($object_data)) {
233                                 Logger::log("Object data couldn't be processed", Logger::DEBUG);
234                                 return [];
235                         }
236
237                         $object_data['object_id'] = $object_id;
238
239                         if ($type == 'as:Announce') {
240                                 $object_data['push'] = false;
241                         } else {
242                                 $object_data['push'] = $push;
243                         }
244
245                         // Test if it is an answer to a mail
246                         if (DBA::exists('mail', ['uri' => $object_data['reply-to-id']])) {
247                                 $object_data['directmessage'] = true;
248                         } else {
249                                 $object_data['directmessage'] = JsonLD::fetchElement($activity, 'litepub:directMessage');
250                         }
251
252                         // We had been able to retrieve the object data - so we can trust the source
253                         $trust_source = true;
254                 } elseif (in_array($type, array_merge(self::ACTIVITY_TYPES, ['as:Follow'])) && in_array($object_type, self::CONTENT_TYPES)) {
255                         // Create a mostly empty array out of the activity data (instead of the object).
256                         // This way we later don't have to check for the existence of ech individual array element.
257                         $object_data = self::processObject($activity);
258                         $object_data['name'] = $type;
259                         $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
260                         $object_data['object_id'] = $object_id;
261                         $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
262                 } elseif (in_array($type, ['as:Add'])) {
263                         $object_data = [];
264                         $object_data['id'] = JsonLD::fetchElement($activity, '@id');
265                         $object_data['target_id'] = JsonLD::fetchElement($activity, 'as:target', '@id');
266                         $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
267                         $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
268                         $object_data['object_content'] = JsonLD::fetchElement($activity['as:object'], 'as:content', '@type');
269                 } else {
270                         $object_data = [];
271                         $object_data['id'] = JsonLD::fetchElement($activity, '@id');
272                         $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object', '@id');
273                         $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor', '@id');
274                         $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
275                         $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
276
277                         // An Undo is done on the object of an object, so we need that type as well
278                         if (($type == 'as:Undo') && !empty($object_data['object_object'])) {
279                                 $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $uid);
280                         }
281                 }
282
283                 $object_data = self::addActivityFields($object_data, $activity);
284
285                 if (empty($object_data['object_type'])) {
286                         $object_data['object_type'] = $object_type;
287                 }
288
289                 $object_data['type'] = $type;
290                 $object_data['actor'] = $actor;
291                 $object_data['item_receiver'] = $receivers;
292                 $object_data['receiver'] = array_merge($object_data['receiver'] ?? [], $receivers);
293
294                 Logger::log('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], Logger::DEBUG);
295
296                 return $object_data;
297         }
298
299         /**
300          * Fetches the first user id from the receiver array
301          *
302          * @param array $receivers Array with receivers
303          * @return integer user id;
304          */
305         public static function getFirstUserFromReceivers($receivers)
306         {
307                 foreach ($receivers as $receiver) {
308                         if (!empty($receiver)) {
309                                 return $receiver;
310                         }
311                 }
312                 return 0;
313         }
314
315         /**
316          * Processes the activity object
317          *
318          * @param array   $activity     Array with activity data
319          * @param string  $body
320          * @param integer $uid          User ID
321          * @param boolean $trust_source Do we trust the source?
322          * @param boolean $push         Message had been pushed to our system
323          * @throws \Exception
324          */
325         public static function processActivity($activity, $body = '', $uid = null, $trust_source = false, $push = false)
326         {
327                 $type = JsonLD::fetchElement($activity, '@type');
328                 if (!$type) {
329                         Logger::log('Empty type', Logger::DEBUG);
330                         return;
331                 }
332
333                 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
334                         Logger::log('Empty object', Logger::DEBUG);
335                         return;
336                 }
337
338                 if (!JsonLD::fetchElement($activity, 'as:actor', '@id')) {
339                         Logger::log('Empty actor', Logger::DEBUG);
340                         return;
341                 }
342
343                 // Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
344                 if ($trust_source && ($type == 'as:Create') && is_array($activity['as:object'])) {
345                         $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
346                         $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
347                         $trust_source = ($actor == $attributed_to);
348                         if (!$trust_source) {
349                                 Logger::log('Not trusting actor: ' . $actor . '. It differs from attributedTo: ' . $attributed_to, Logger::DEBUG);
350                         }
351                 }
352
353                 // $trust_source is called by reference and is set to true if the content was retrieved successfully
354                 $object_data = self::prepareObjectData($activity, $uid, $push, $trust_source);
355                 if (empty($object_data)) {
356                         Logger::log('No object data found', Logger::DEBUG);
357                         return;
358                 }
359
360                 if (!$trust_source) {
361                         Logger::log('No trust for activity type "' . $type . '", so we quit now.', Logger::DEBUG);
362                         return;
363                 }
364
365                 if (!empty($body) && empty($object_data['raw'])) {
366                         $object_data['raw'] = $body;
367                 }
368
369                 // Internal flag for thread completion. See Processor.php
370                 if (!empty($activity['thread-completion'])) {
371                         $object_data['thread-completion'] = $activity['thread-completion'];
372                 }
373
374                 switch ($type) {
375                         case 'as:Create':
376                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
377                                         $item = ActivityPub\Processor::createItem($object_data);
378                                         ActivityPub\Processor::postItem($object_data, $item);
379                                 }
380                                 break;
381
382                         case 'as:Add':
383                                 if ($object_data['object_type'] == 'as:tag') {
384                                         ActivityPub\Processor::addTag($object_data);
385                                 }
386                                 break;
387
388                         case 'as:Announce':
389                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
390                                         $object_data['thread-completion'] = true;
391
392                                         $item = ActivityPub\Processor::createItem($object_data);
393                                         ActivityPub\Processor::postItem($object_data, $item);
394
395                                         $announce_object_data = self::processObject($activity);
396                                         $announce_object_data['name'] = $type;
397                                         $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
398                                         $announce_object_data['object_id'] = $object_data['object_id'];
399                                         $announce_object_data['object_type'] = $object_data['object_type'];
400                                         $announce_object_data['push'] = $push;
401
402                                         if (!empty($body)) {
403                                                 $announce_object_data['raw'] = $body;
404                                         }
405
406                                         ActivityPub\Processor::createActivity($announce_object_data, Activity::ANNOUNCE);
407                                 }
408                                 break;
409
410                         case 'as:Like':
411                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
412                                         ActivityPub\Processor::createActivity($object_data, Activity::LIKE);
413                                 }
414                                 break;
415
416                         case 'as:Dislike':
417                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
418                                         ActivityPub\Processor::createActivity($object_data, Activity::DISLIKE);
419                                 }
420                                 break;
421
422                         case 'as:TentativeAccept':
423                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
424                                         ActivityPub\Processor::createActivity($object_data, Activity::ATTENDMAYBE);
425                                 }
426                                 break;
427
428                         case 'as:Update':
429                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
430                                         ActivityPub\Processor::updateItem($object_data);
431                                 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
432                                         ActivityPub\Processor::updatePerson($object_data);
433                                 }
434                                 break;
435
436                         case 'as:Delete':
437                                 if ($object_data['object_type'] == 'as:Tombstone') {
438                                         ActivityPub\Processor::deleteItem($object_data);
439                                 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
440                                         ActivityPub\Processor::deletePerson($object_data);
441                                 }
442                                 break;
443
444                         case 'as:Follow':
445                                 if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
446                                         ActivityPub\Processor::followUser($object_data);
447                                 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
448                                         $object_data['reply-to-id'] = $object_data['object_id'];
449                                         ActivityPub\Processor::createActivity($object_data, Activity::FOLLOW);
450                                 }
451                                 break;
452
453                         case 'as:Accept':
454                                 if ($object_data['object_type'] == 'as:Follow') {
455                                         ActivityPub\Processor::acceptFollowUser($object_data);
456                                 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
457                                         ActivityPub\Processor::createActivity($object_data, Activity::ATTEND);
458                                 }
459                                 break;
460
461                         case 'as:Reject':
462                                 if ($object_data['object_type'] == 'as:Follow') {
463                                         ActivityPub\Processor::rejectFollowUser($object_data);
464                                 } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
465                                         ActivityPub\Processor::createActivity($object_data, Activity::ATTENDNO);
466                                 }
467                                 break;
468
469                         case 'as:Undo':
470                                 if (($object_data['object_type'] == 'as:Follow') &&
471                                         in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
472                                         ActivityPub\Processor::undoFollowUser($object_data);
473                                 } elseif (($object_data['object_type'] == 'as:Accept') &&
474                                         in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
475                                         ActivityPub\Processor::rejectFollowUser($object_data);
476                                 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
477                                         in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
478                                         ActivityPub\Processor::undoActivity($object_data);
479                                 }
480                                 break;
481
482                         default:
483                                 Logger::log('Unknown activity: ' . $type . ' ' . $object_data['object_type'], Logger::DEBUG);
484                                 break;
485                 }
486         }
487
488         /**
489          * Fetch the receiver list from an activity array
490          *
491          * @param array   $activity
492          * @param string  $actor
493          * @param array   $tags
494          * @param boolean $fetch_unlisted 
495          *
496          * @return array with receivers (user id)
497          * @throws \Exception
498          */
499         private static function getReceivers($activity, $actor, $tags = [], $fetch_unlisted = false)
500         {
501                 $receivers = [];
502
503                 // When it is an answer, we inherite the receivers from the parent
504                 $replyto = JsonLD::fetchElement($activity, 'as:inReplyTo', '@id');
505                 if (!empty($replyto)) {
506                         // Fix possibly wrong item URI (could be an answer to a plink uri)
507                         $fixedReplyTo = Item::getURIByLink($replyto);
508                         $replyto = $fixedReplyTo ?: $replyto;
509
510                         $parents = Item::select(['uid'], ['uri' => $replyto]);
511                         while ($parent = Item::fetch($parents)) {
512                                 $receivers['uid:' . $parent['uid']] = $parent['uid'];
513                         }
514                 }
515
516                 if (!empty($actor)) {
517                         $profile = APContact::getByURL($actor);
518                         $followers = $profile['followers'] ?? '';
519
520                         Logger::log('Actor: ' . $actor . ' - Followers: ' . $followers, Logger::DEBUG);
521                 } else {
522                         Logger::log('Empty actor', Logger::DEBUG);
523                         $followers = '';
524                 }
525
526                 foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
527                         $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
528                         if (empty($receiver_list)) {
529                                 continue;
530                         }
531
532                         foreach ($receiver_list as $receiver) {
533                                 if ($receiver == self::PUBLIC_COLLECTION) {
534                                         $receivers['uid:0'] = 0;
535                                 }
536
537                                 // Add receiver "-1" for unlisted posts 
538                                 if ($fetch_unlisted && ($receiver == self::PUBLIC_COLLECTION) && ($element == 'as:cc')) {
539                                         $receivers['uid:-1'] = -1;
540                                 }
541
542                                 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
543                                         // This will most likely catch all OStatus connections to Mastodon
544                                         $condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
545                                                 , 'archive' => false, 'pending' => false];
546                                         $contacts = DBA::select('contact', ['uid'], $condition);
547                                         while ($contact = DBA::fetch($contacts)) {
548                                                 if ($contact['uid'] != 0) {
549                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
550                                                 }
551                                         }
552                                         DBA::close($contacts);
553                                 }
554
555                                 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
556                                         $receivers = array_merge($receivers, self::getReceiverForActor($actor, $tags));
557                                         continue;
558                                 }
559
560                                 // Fetching all directly addressed receivers
561                                 $condition = ['self' => true, 'nurl' => Strings::normaliseLink($receiver)];
562                                 $contact = DBA::selectFirst('contact', ['uid', 'contact-type'], $condition);
563                                 if (!DBA::isResult($contact)) {
564                                         continue;
565                                 }
566
567                                 // Check if the potential receiver is following the actor
568                                 // Exception: The receiver is targetted via "to" or this is a comment
569                                 if ((($element != 'as:to') && empty($replyto)) || ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
570                                         $networks = Protocol::FEDERATED;
571                                         $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
572                                                 'network' => $networks, 'archive' => false, 'pending' => false, 'uid' => $contact['uid']];
573
574                                         // Forum posts are only accepted from forum contacts
575                                         if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
576                                                 $condition['rel'] = [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER];
577                                         }
578
579                                         if (!DBA::exists('contact', $condition)) {
580                                                 continue;
581                                         }
582                                 }
583
584                                 $receivers['uid:' . $contact['uid']] = $contact['uid'];
585                         }
586                 }
587
588                 self::switchContacts($receivers, $actor);
589
590                 return $receivers;
591         }
592
593         /**
594          * Fetch the receiver list of a given actor
595          *
596          * @param string $actor
597          * @param array  $tags
598          *
599          * @return array with receivers (user id)
600          * @throws \Exception
601          */
602         public static function getReceiverForActor($actor, $tags)
603         {
604                 $receivers = [];
605                 $networks = Protocol::FEDERATED;
606                 $condition = ['nurl' => Strings::normaliseLink($actor), 'rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
607                         'network' => $networks, 'archive' => false, 'pending' => false];
608                 $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
609                 while ($contact = DBA::fetch($contacts)) {
610                         if (self::isValidReceiverForActor($contact, $actor, $tags)) {
611                                 $receivers['uid:' . $contact['uid']] = $contact['uid'];
612                         }
613                 }
614                 DBA::close($contacts);
615                 return $receivers;
616         }
617
618         /**
619          * Tests if the contact is a valid receiver for this actor
620          *
621          * @param array  $contact
622          * @param string $actor
623          * @param array  $tags
624          *
625          * @return bool with receivers (user id)
626          * @throws \Exception
627          */
628         private static function isValidReceiverForActor($contact, $actor, $tags)
629         {
630                 // Public contacts are no valid receiver
631                 if ($contact['uid'] == 0) {
632                         return false;
633                 }
634
635                 // Are we following the contact? Then this is a valid receiver
636                 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
637                         return true;
638                 }
639
640                 // When the possible receiver isn't a community, then it is no valid receiver
641                 $owner = User::getOwnerDataById($contact['uid']);
642                 if (empty($owner) || ($owner['contact-type'] != Contact::TYPE_COMMUNITY)) {
643                         return false;
644                 }
645
646                 // Is the community account tagged?
647                 foreach ($tags as $tag) {
648                         if ($tag['type'] != 'Mention') {
649                                 continue;
650                         }
651
652                         if ($tag['href'] == $owner['url']) {
653                                 return true;
654                         }
655                 }
656
657                 return false;
658         }
659
660         /**
661          * Switches existing contacts to ActivityPub
662          *
663          * @param integer $cid Contact ID
664          * @param integer $uid User ID
665          * @param string  $url Profile URL
666          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
667          * @throws \ImagickException
668          */
669         public static function switchContact($cid, $uid, $url)
670         {
671                 if (DBA::exists('contact', ['id' => $cid, 'network' => Protocol::ACTIVITYPUB])) {
672                         Logger::info('Contact is already ActivityPub', ['id' => $cid, 'uid' => $uid, 'url' => $url]);
673                         return;
674                 }
675
676                 if (Contact::updateFromProbe($cid)) {
677                         Logger::info('Update was successful', ['id' => $cid, 'uid' => $uid, 'url' => $url]);
678                 }
679
680                 // Send a new follow request to be sure that the connection still exists
681                 if (($uid != 0) && DBA::exists('contact', ['id' => $cid, 'rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB])) {
682                         Logger::info('Contact had been switched to ActivityPub. Sending a new follow request.', ['uid' => $uid, 'url' => $url]);
683                         ActivityPub\Transmitter::sendActivity('Follow', $url, $uid);
684                 }
685         }
686
687         /**
688          *
689          *
690          * @param $receivers
691          * @param $actor
692          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
693          * @throws \ImagickException
694          */
695         private static function switchContacts($receivers, $actor)
696         {
697                 if (empty($actor)) {
698                         return;
699                 }
700
701                 foreach ($receivers as $receiver) {
702                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => Strings::normaliseLink($actor)]);
703                         if (DBA::isResult($contact)) {
704                                 self::switchContact($contact['id'], $receiver, $actor);
705                         }
706
707                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [Strings::normaliseLink($actor), $actor]]);
708                         if (DBA::isResult($contact)) {
709                                 self::switchContact($contact['id'], $receiver, $actor);
710                         }
711                 }
712         }
713
714         /**
715          *
716          *
717          * @param       $object_data
718          * @param array $activity
719          *
720          * @return mixed
721          */
722         private static function addActivityFields($object_data, $activity)
723         {
724                 if (!empty($activity['published']) && empty($object_data['published'])) {
725                         $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value');
726                 }
727
728                 if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
729                         $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid', '@value');
730                 }
731
732                 $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service');
733                 $object_data['service'] = JsonLD::fetchElement($object_data, 'service', '@value');
734
735                 if (!empty($object_data['object_id'])) {
736                         // Some systems (e.g. GNU Social) don't reply to the "id" field but the "uri" field.
737                         $objectId = Item::getURIByLink($object_data['object_id']);
738                         if (!empty($objectId) && ($object_data['object_id'] != $objectId)) {
739                                 Logger::notice('Fix wrong object-id', ['received' => $object_data['object_id'], 'correct' => $objectId]);
740                                 $object_data['object_id'] = $objectId;
741                         }
742                 }
743
744                 return $object_data;
745         }
746
747         /**
748          * Fetches the object data from external ressources if needed
749          *
750          * @param string  $object_id    Object ID of the the provided object
751          * @param array   $object       The provided object array
752          * @param boolean $trust_source Do we trust the provided object?
753          * @param integer $uid          User ID for the signature that we use to fetch data
754          *
755          * @return array|false with trusted and valid object data
756          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
757          * @throws \ImagickException
758          */
759         private static function fetchObject(string $object_id, array $object = [], bool $trust_source = false, int $uid = 0)
760         {
761                 // By fetching the type we check if the object is complete.
762                 $type = JsonLD::fetchElement($object, '@type');
763
764                 if (!$trust_source || empty($type)) {
765                         $data = ActivityPub::fetchContent($object_id, $uid);
766                         if (!empty($data)) {
767                                 $object = JsonLD::compact($data);
768                                 Logger::log('Fetched content for ' . $object_id, Logger::DEBUG);
769                         } else {
770                                 Logger::log('Empty content for ' . $object_id . ', check if content is available locally.', Logger::DEBUG);
771
772                                 $item = Item::selectFirst([], ['uri' => $object_id]);
773                                 if (!DBA::isResult($item)) {
774                                         Logger::log('Object with url ' . $object_id . ' was not found locally.', Logger::DEBUG);
775                                         return false;
776                                 }
777                                 Logger::log('Using already stored item for url ' . $object_id, Logger::DEBUG);
778                                 $data = ActivityPub\Transmitter::createNote($item);
779                                 $object = JsonLD::compact($data);
780                         }
781                 } else {
782                         Logger::log('Using original object for url ' . $object_id, Logger::DEBUG);
783                 }
784
785                 $type = JsonLD::fetchElement($object, '@type');
786
787                 if (empty($type)) {
788                         Logger::log('Empty type', Logger::DEBUG);
789                         return false;
790                 }
791
792                 if (in_array($type, self::CONTENT_TYPES)) {
793                         $object_data = self::processObject($object);
794
795                         if (!empty($data)) {
796                                 $object_data['raw'] = json_encode($data);
797                         }
798                         return $object_data;
799                 }
800
801                 if ($type == 'as:Announce') {
802                         $object_id = JsonLD::fetchElement($object, 'object', '@id');
803                         if (empty($object_id) || !is_string($object_id)) {
804                                 return false;
805                         }
806                         return self::fetchObject($object_id, [], false, $uid);
807                 }
808
809                 Logger::log('Unhandled object type: ' . $type, Logger::DEBUG);
810                 return false;
811         }
812
813         /**
814          * Convert tags from JSON-LD format into a simplified format
815          *
816          * @param array $tags Tags in JSON-LD format
817          *
818          * @return array with tags in a simplified format
819          */
820         private static function processTags(array $tags)
821         {
822                 $taglist = [];
823
824                 foreach ($tags as $tag) {
825                         if (empty($tag)) {
826                                 continue;
827                         }
828
829                         $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
830                                 'href' => JsonLD::fetchElement($tag, 'as:href', '@id'),
831                                 'name' => JsonLD::fetchElement($tag, 'as:name', '@value')];
832
833                         if (empty($element['type'])) {
834                                 continue;
835                         }
836
837                         if (empty($element['href'])) {
838                                 $element['href'] = $element['name'];
839                         }
840
841                         $taglist[] = $element;
842                 }
843                 return $taglist;
844         }
845
846         /**
847          * Convert emojis from JSON-LD format into a simplified format
848          *
849          * @param array $emojis
850          * @return array with emojis in a simplified format
851          */
852         private static function processEmojis(array $emojis)
853         {
854                 $emojilist = [];
855
856                 foreach ($emojis as $emoji) {
857                         if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
858                                 continue;
859                         }
860
861                         $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url', '@id');
862                         $element = ['name' => JsonLD::fetchElement($emoji, 'as:name', '@value'),
863                                 'href' => $url];
864
865                         $emojilist[] = $element;
866                 }
867
868                 return $emojilist;
869         }
870
871         /**
872          * Convert attachments from JSON-LD format into a simplified format
873          *
874          * @param array $attachments Attachments in JSON-LD format
875          *
876          * @return array Attachments in a simplified format
877          */
878         private static function processAttachments(array $attachments)
879         {
880                 $attachlist = [];
881
882                 // Removes empty values
883                 $attachments = array_filter($attachments);
884
885                 foreach ($attachments as $attachment) {
886                         switch (JsonLD::fetchElement($attachment, '@type')) {
887                                 case 'as:Page':
888                                         $pageUrl = null;
889                                         $pageImage = null;
890
891                                         $urls = JsonLD::fetchElementArray($attachment, 'as:url');
892                                         foreach ($urls as $url) {
893                                                 // Single scalar URL case
894                                                 if (is_string($url)) {
895                                                         $pageUrl = $url;
896                                                         continue;
897                                                 }
898
899                                                 $href = JsonLD::fetchElement($url, 'as:href', '@id');
900                                                 $mediaType = JsonLD::fetchElement($url, 'as:mediaType', '@value');
901                                                 if (Strings::startsWith($mediaType, 'image')) {
902                                                         $pageImage = $href;
903                                                 } else {
904                                                         $pageUrl = $href;
905                                                 }
906                                         }
907
908                                         $attachlist[] = [
909                                                 'type'  => 'link',
910                                                 'title' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
911                                                 'desc'  => JsonLD::fetchElement($attachment, 'as:summary', '@value'),
912                                                 'url'   => $pageUrl,
913                                                 'image' => $pageImage,
914                                         ];
915                                         break;
916                                 case 'as:Link':
917                                         $attachlist[] = [
918                                                 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
919                                                 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
920                                                 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
921                                                 'url' => JsonLD::fetchElement($attachment, 'as:href', '@id')
922                                         ];
923                                         break;
924                                 case 'as:Image':
925                                         $mediaType = JsonLD::fetchElement($attachment, 'as:mediaType', '@value');
926                                         $imageFullUrl = JsonLD::fetchElement($attachment, 'as:url', '@id');
927                                         $imagePreviewUrl = null;
928                                         // Multiple URLs?
929                                         if (!$imageFullUrl && ($urls = JsonLD::fetchElementArray($attachment, 'as:url'))) {
930                                                 $imageVariants = [];
931                                                 $previewVariants = [];
932                                                 foreach ($urls as $url) {
933                                                         // Scalar URL, no discrimination possible
934                                                         if (is_string($url)) {
935                                                                 $imageFullUrl = $url;
936                                                                 continue;
937                                                         }
938
939                                                         // Not sure what to do with a different Link media type than the base Image, we skip
940                                                         if ($mediaType != JsonLD::fetchElement($url, 'as:mediaType', '@value')) {
941                                                                 continue;
942                                                         }
943
944                                                         $href = JsonLD::fetchElement($url, 'as:href', '@id');
945
946                                                         // Default URL choice if no discriminating width is provided
947                                                         $imageFullUrl = $href ?? $imageFullUrl;
948
949                                                         $width = intval(JsonLD::fetchElement($url, 'as:width', '@value') ?? 1);
950
951                                                         if ($href && $width) {
952                                                                 $imageVariants[$width] = $href;
953                                                                 // 632 is the ideal width for full screen frio posts, we compute the absolute distance to it
954                                                                 $previewVariants[abs(632 - $width)] = $href;
955                                                         }
956                                                 }
957
958                                                 if ($imageVariants) {
959                                                         // Taking the maximum size image
960                                                         ksort($imageVariants);
961                                                         $imageFullUrl = array_pop($imageVariants);
962
963                                                         // Taking the minimum number distance to the target distance
964                                                         ksort($previewVariants);
965                                                         $imagePreviewUrl = array_shift($previewVariants);
966                                                 }
967
968                                                 unset($imageVariants);
969                                                 unset($previewVariants);
970                                         }
971
972                                         $attachlist[] = [
973                                                 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
974                                                 'mediaType' => $mediaType,
975                                                 'name'  => JsonLD::fetchElement($attachment, 'as:name', '@value'),
976                                                 'url'   => $imageFullUrl,
977                                                 'image' => $imagePreviewUrl !== $imageFullUrl ? $imagePreviewUrl : null,
978                                         ];
979                                         break;
980                                 default:
981                                         $attachlist[] = [
982                                                 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
983                                                 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
984                                                 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
985                                                 'url' => JsonLD::fetchElement($attachment, 'as:url', '@id')
986                                         ];
987                         }
988                 }
989
990                 return $attachlist;
991         }
992
993         /**
994          * Fetch the original source or content with the "language" Markdown or HTML
995          *
996          * @param array $object
997          * @param array $object_data
998          *
999          * @return array
1000          * @throws \Exception
1001          */
1002         private static function getSource($object, $object_data)
1003         {
1004                 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode');
1005                 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
1006                 if (!empty($object_data['source'])) {
1007                         return $object_data;
1008                 }
1009
1010                 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/markdown');
1011                 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
1012                 if (!empty($object_data['source'])) {
1013                         $object_data['source'] = Markdown::toBBCode($object_data['source']);
1014                         return $object_data;
1015                 }
1016
1017                 $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/html');
1018                 $object_data['source'] = JsonLD::fetchElement($object_data, 'source', '@value');
1019                 if (!empty($object_data['source'])) {
1020                         $object_data['source'] = HTML::toBBCode($object_data['source']);
1021                         return $object_data;
1022                 }
1023
1024                 return $object_data;
1025         }
1026
1027         /**
1028          * Check if the "as:url" element is an array with multiple links
1029          * This is the case with audio and video posts.
1030          * Then the links are added as attachments
1031          *
1032          * @param array $object      The raw object
1033          * @param array $object_data The parsed object data for later processing
1034          * @return array the object data
1035          */
1036         private static function processAttachmentUrls(array $object, array $object_data) {
1037                 // Check if this is some url with multiple links
1038                 if (empty($object['as:url'])) {
1039                         return $object_data;
1040                 }
1041                 
1042                 $urls = $object['as:url'];
1043                 $keys = array_keys($urls);
1044                 if (!is_numeric(array_pop($keys))) {
1045                         return $object_data;
1046                 }
1047
1048                 $attachments = [];
1049
1050                 foreach ($urls as $url) {
1051                         if (empty($url['@type']) || ($url['@type'] != 'as:Link')) {
1052                                 continue;
1053                         }
1054
1055                         $href = JsonLD::fetchElement($url, 'as:href', '@id');
1056                         if (empty($href)) {
1057                                 continue;
1058                         }
1059
1060                         $mediatype = JsonLD::fetchElement($url, 'as:mediaType');
1061                         if (empty($mediatype)) {
1062                                 continue;
1063                         }
1064
1065                         if ($mediatype == 'text/html') {
1066                                 $object_data['alternate-url'] = $href;
1067                         }
1068
1069                         $filetype = strtolower(substr($mediatype, 0, strpos($mediatype, '/')));
1070
1071                         if ($filetype == 'audio') {
1072                                 $attachments[$filetype] = ['type' => $mediatype, 'url' => $href];
1073                         } elseif ($filetype == 'video') {
1074                                 $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
1075
1076                                 // We save bandwidth by using a moderate height
1077                                 // Peertube normally uses these heights: 240, 360, 480, 720, 1080
1078                                 if (!empty($attachments[$filetype]['height']) &&
1079                                         (($height > 480) || $height < $attachments[$filetype]['height'])) {
1080                                         continue;
1081                                 }
1082
1083                                 $attachments[$filetype] = ['type' => $mediatype, 'url' => $href, 'height' => $height];
1084                         }
1085                 }
1086
1087                 foreach ($attachments as $type => $attachment) {
1088                         $object_data['attachments'][] = ['type' => $type,
1089                                 'mediaType' => $attachment['type'],
1090                                 'name' => '',
1091                                 'url' => $attachment['url']];
1092                 }
1093                 return $object_data;
1094         }
1095
1096         /**
1097          * Fetches data from the object part of an activity
1098          *
1099          * @param array $object
1100          *
1101          * @return array
1102          * @throws \Exception
1103          */
1104         private static function processObject($object)
1105         {
1106                 if (!JsonLD::fetchElement($object, '@id')) {
1107                         return false;
1108                 }
1109
1110                 $object_data = [];
1111                 $object_data['object_type'] = JsonLD::fetchElement($object, '@type');
1112                 $object_data['id'] = JsonLD::fetchElement($object, '@id');
1113                 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'as:inReplyTo', '@id');
1114
1115                 // An empty "id" field is translated to "./" by the compactor, so we have to check for this content
1116                 if (empty($object_data['reply-to-id']) || ($object_data['reply-to-id'] == './')) {
1117                         $object_data['reply-to-id'] = $object_data['id'];
1118                 } else {
1119                         // Some systems (e.g. GNU Social) don't reply to the "id" field but the "uri" field.
1120                         $replyToId = Item::getURIByLink($object_data['reply-to-id']);
1121                         if (!empty($replyToId) && ($object_data['reply-to-id'] != $replyToId)) {
1122                                 Logger::notice('Fix wrong reply-to', ['received' => $object_data['reply-to-id'], 'correct' => $replyToId]);
1123                                 $object_data['reply-to-id'] = $replyToId;
1124                         }
1125                 }
1126
1127                 $object_data['published'] = JsonLD::fetchElement($object, 'as:published', '@value');
1128                 $object_data['updated'] = JsonLD::fetchElement($object, 'as:updated', '@value');
1129
1130                 if (empty($object_data['updated'])) {
1131                         $object_data['updated'] = $object_data['published'];
1132                 }
1133
1134                 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1135                         $object_data['published'] = $object_data['updated'];
1136                 }
1137
1138                 $actor = JsonLD::fetchElement($object, 'as:attributedTo', '@id');
1139                 if (empty($actor)) {
1140                         $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
1141                 }
1142
1143                 $location = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place');
1144                 $location = JsonLD::fetchElement($location, 'location', '@value');
1145                 if ($location) {
1146                         // Some AP software allow formatted text in post location, so we run all the text converters we have to boil
1147                         // down to HTML and then finally format to plaintext.
1148                         $location = Markdown::convert($location);
1149                         $location = BBCode::convert($location);
1150                         $location = HTML::toPlaintext($location);
1151                 }
1152
1153                 $object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value');
1154                 $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
1155                 $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
1156                 $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
1157                 $object_data['actor'] = $object_data['author'] = $actor;
1158                 $object_data['context'] = JsonLD::fetchElement($object, 'as:context', '@id');
1159                 $object_data['conversation'] = JsonLD::fetchElement($object, 'ostatus:conversation', '@id');
1160                 $object_data['sensitive'] = JsonLD::fetchElement($object, 'as:sensitive');
1161                 $object_data['name'] = JsonLD::fetchElement($object, 'as:name', '@value');
1162                 $object_data['summary'] = JsonLD::fetchElement($object, 'as:summary', '@value');
1163                 $object_data['content'] = JsonLD::fetchElement($object, 'as:content', '@value');
1164                 $object_data = self::getSource($object, $object_data);
1165                 $object_data['start-time'] = JsonLD::fetchElement($object, 'as:startTime', '@value');
1166                 $object_data['end-time'] = JsonLD::fetchElement($object, 'as:endTime', '@value');
1167                 $object_data['location'] = $location;
1168                 $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place');
1169                 $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value');
1170                 $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place');
1171                 $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
1172                 $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []);
1173                 $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []);
1174                 $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji') ?? []);
1175                 $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
1176                 $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
1177                 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
1178
1179                 // Special treatment for Hubzilla links
1180                 if (is_array($object_data['alternate-url'])) {
1181                         $object_data['alternate-url'] = JsonLD::fetchElement($object_data['alternate-url'], 'as:href', '@id');
1182
1183                         if (!is_string($object_data['alternate-url'])) {
1184                                 $object_data['alternate-url'] = JsonLD::fetchElement($object['as:url'], 'as:href', '@id');
1185                         }
1186                 }
1187
1188                 if (in_array($object_data['object_type'], ['as:Audio', 'as:Video'])) {
1189                         $object_data = self::processAttachmentUrls($object, $object_data);
1190                 }
1191
1192                 $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags'], true);
1193                 $object_data['unlisted'] = in_array(-1, $object_data['receiver']);
1194                 unset($object_data['receiver']['uid:-1']);
1195
1196                 // Common object data:
1197
1198                 // Unhandled
1199                 // @context, type, actor, signature, mediaType, duration, replies, icon
1200
1201                 // Also missing: (Defined in the standard, but currently unused)
1202                 // audience, preview, endTime, startTime, image
1203
1204                 // Data in Notes:
1205
1206                 // Unhandled
1207                 // contentMap, announcement_count, announcements, context_id, likes, like_count
1208                 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1209
1210                 // Data in video:
1211
1212                 // To-Do?
1213                 // category, licence, language, commentsEnabled
1214
1215                 // Unhandled
1216                 // views, waitTranscoding, state, support, subtitleLanguage
1217                 // likes, dislikes, shares, comments
1218
1219                 return $object_data;
1220         }
1221 }