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