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