]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
Merge pull request #5792 from MrPetovan/task/add-cache-console
[friendica.git] / src / Protocol / ActivityPub.php
1 <?php
2 /**
3  * @file src/Protocol/ActivityPub.php
4  */
5 namespace Friendica\Protocol;
6
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Crypto;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\HTML;
24 use Friendica\Util\JsonLD;
25 use Friendica\Util\LDSignature;
26 use Friendica\Core\Config;
27
28 /**
29  * @brief ActivityPub Protocol class
30  * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
31  * https://www.w3.org/TR/activitypub/
32  * https://www.w3.org/TR/activitystreams-core/
33  * https://www.w3.org/TR/activitystreams-vocabulary/
34  *
35  * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
36  * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
37  *
38  * Digest: https://tools.ietf.org/html/rfc5843
39  * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
40  *
41  * Mastodon implementation of supported activities:
42  * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
43  *
44  * To-do:
45  *
46  * Receiver:
47  * - Update (Image, Video, Article, Note)
48  * - Event
49  * - Undo Announce
50  *
51  * Check what this is meant to do:
52  * - Add
53  * - Block
54  * - Flag
55  * - Remove
56  * - Undo Block
57  * - Undo Accept (Problem: This could invert a contact accept or an event accept)
58  *
59  * Transmitter:
60  * - Event
61  *
62  * Complicated:
63  * - Announce
64  * - Undo Announce
65  *
66  * General:
67  * - Attachments
68  * - nsfw (sensitive)
69  * - Queueing unsucessful deliveries
70  * - Polling the outboxes for missing content?
71  * - Possibly using the LD-JSON parser
72  */
73 class ActivityPub
74 {
75         const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
76         const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
77                 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
78                 'diaspora' => 'https://diasporafoundation.org/ns/',
79                 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
80                 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
81         const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
82         const CONTENT_TYPES = ['Note', 'Article', 'Video', 'Image'];
83         const ACTIVITY_TYPES = ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'];
84         /**
85          * @brief Checks if the web request is done for the AP protocol
86          *
87          * @return is it AP?
88          */
89         public static function isRequest()
90         {
91                 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
92                         stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
93         }
94
95         /**
96          * @brief collects the lost of followers of the given owner
97          *
98          * @param array $owner Owner array
99          * @param integer $page Page number
100          *
101          * @return array of owners
102          */
103         public static function getFollowers($owner, $page = null)
104         {
105                 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
106                         'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
107                 $count = DBA::count('contact', $condition);
108
109                 $data = ['@context' => self::CONTEXT];
110                 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
111                 $data['type'] = 'OrderedCollection';
112                 $data['totalItems'] = $count;
113
114                 // When we hide our friends we will only show the pure number but don't allow more.
115                 $profile = Profile::getByUID($owner['uid']);
116                 if (!empty($profile['hide-friends'])) {
117                         return $data;
118                 }
119
120                 if (empty($page)) {
121                         $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
122                 } else {
123                         $list = [];
124
125                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
126                         while ($contact = DBA::fetch($contacts)) {
127                                 $list[] = $contact['url'];
128                         }
129
130                         if (!empty($list)) {
131                                 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
132                         }
133
134                         $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
135
136                         $data['orderedItems'] = $list;
137                 }
138
139                 return $data;
140         }
141
142         /**
143          * @brief Create list of following contacts
144          *
145          * @param array $owner Owner array
146          * @param integer $page Page numbe
147          *
148          * @return array of following contacts
149          */
150         public static function getFollowing($owner, $page = null)
151         {
152                 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
153                         'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
154                 $count = DBA::count('contact', $condition);
155
156                 $data = ['@context' => self::CONTEXT];
157                 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
158                 $data['type'] = 'OrderedCollection';
159                 $data['totalItems'] = $count;
160
161                 // When we hide our friends we will only show the pure number but don't allow more.
162                 $profile = Profile::getByUID($owner['uid']);
163                 if (!empty($profile['hide-friends'])) {
164                         return $data;
165                 }
166
167                 if (empty($page)) {
168                         $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
169                 } else {
170                         $list = [];
171
172                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
173                         while ($contact = DBA::fetch($contacts)) {
174                                 $list[] = $contact['url'];
175                         }
176
177                         if (!empty($list)) {
178                                 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
179                         }
180
181                         $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
182
183                         $data['orderedItems'] = $list;
184                 }
185
186                 return $data;
187         }
188
189         /**
190          * @brief Public posts for the given owner
191          *
192          * @param array $owner Owner array
193          * @param integer $page Page numbe
194          *
195          * @return array of posts
196          */
197         public static function getOutbox($owner, $page = null)
198         {
199                 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
200
201                 $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact,
202                         'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
203                         'deleted' => false, 'visible' => true];
204                 $count = DBA::count('item', $condition);
205
206                 $data = ['@context' => self::CONTEXT];
207                 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
208                 $data['type'] = 'OrderedCollection';
209                 $data['totalItems'] = $count;
210
211                 if (empty($page)) {
212                         $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
213                 } else {
214                         $list = [];
215
216                         $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
217
218                         $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
219                         while ($item = Item::fetch($items)) {
220                                 $object = self::createObjectFromItemID($item['id']);
221                                 unset($object['@context']);
222                                 $list[] = $object;
223                         }
224
225                         if (!empty($list)) {
226                                 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
227                         }
228
229                         $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
230
231                         $data['orderedItems'] = $list;
232                 }
233
234                 return $data;
235         }
236
237         /**
238          * Return the ActivityPub profile of the given user
239          *
240          * @param integer $uid User ID
241          * @return profile array
242          */
243         public static function profile($uid)
244         {
245                 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
246                         'account_removed' => false, 'verified' => true];
247                 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
248                 $user = DBA::selectFirst('user', $fields, $condition);
249                 if (!DBA::isResult($user)) {
250                         return [];
251                 }
252
253                 $fields = ['locality', 'region', 'country-name'];
254                 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
255                 if (!DBA::isResult($profile)) {
256                         return [];
257                 }
258
259                 $fields = ['name', 'url', 'location', 'about', 'avatar'];
260                 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
261                 if (!DBA::isResult($contact)) {
262                         return [];
263                 }
264
265                 $data = ['@context' => self::CONTEXT];
266                 $data['id'] = $contact['url'];
267                 $data['diaspora:guid'] = $user['guid'];
268                 $data['type'] = self::ACCOUNT_TYPES[$user['account-type']];
269                 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
270                 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
271                 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
272                 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
273                 $data['preferredUsername'] = $user['nickname'];
274                 $data['name'] = $contact['name'];
275                 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
276                         'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
277                 $data['summary'] = $contact['about'];
278                 $data['url'] = $contact['url'];
279                 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
280                 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
281                         'owner' => $contact['url'],
282                         'publicKeyPem' => $user['pubkey']];
283                 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
284                 $data['icon'] = ['type' => 'Image',
285                         'url' => $contact['avatar']];
286
287                 // tags: https://kitty.town/@inmysocks/100656097926961126.json
288                 return $data;
289         }
290
291         /**
292          * @brief Returns an array with permissions of a given item array
293          *
294          * @param array $item
295          *
296          * @return array with permissions
297          */
298         private static function fetchPermissionBlockFromConversation($item)
299         {
300                 if (empty($item['thr-parent'])) {
301                         return [];
302                 }
303
304                 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
305                 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
306                 if (!DBA::isResult($conversation)) {
307                         return [];
308                 }
309
310                 $activity = json_decode($conversation['source'], true);
311
312                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
313                 $profile = APContact::getByURL($actor);
314
315                 $item_profile = APContact::getByURL($item['author-link']);
316                 $exclude[] = $item['author-link'];
317
318                 if ($item['gravity'] == GRAVITY_PARENT) {
319                         $exclude[] = $item['owner-link'];
320                 }
321
322                 $permissions['to'][] = $actor;
323
324                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
325                         if (empty($activity[$element])) {
326                                 continue;
327                         }
328                         if (is_string($activity[$element])) {
329                                 $activity[$element] = [$activity[$element]];
330                         }
331
332                         foreach ($activity[$element] as $receiver) {
333                                 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
334                                         $receiver = $item_profile['followers'];
335                                 }
336                                 if (!in_array($receiver, $exclude)) {
337                                         $permissions[$element][] = $receiver;
338                                 }
339                         }
340                 }
341                 return $permissions;
342         }
343
344         /**
345          * @brief Creates an array of permissions from an item thread
346          *
347          * @param array $item
348          *
349          * @return permission array
350          */
351         public static function createPermissionBlockForItem($item)
352         {
353                 $data = ['to' => [], 'cc' => []];
354
355                 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
356
357                 $actor_profile = APContact::getByURL($item['author-link']);
358
359                 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
360
361                 $contacts[$item['author-link']] = $item['author-link'];
362
363                 if (!$item['private']) {
364                         $data['to'][] = self::PUBLIC_COLLECTION;
365                         if (!empty($actor_profile['followers'])) {
366                                 $data['cc'][] = $actor_profile['followers'];
367                         }
368
369                         foreach ($terms as $term) {
370                                 $profile = APContact::getByURL($term['url'], false);
371                                 if (!empty($profile) && empty($contacts[$profile['url']])) {
372                                         $data['cc'][] = $profile['url'];
373                                         $contacts[$profile['url']] = $profile['url'];
374                                 }
375                         }
376                 } else {
377                         $receiver_list = Item::enumeratePermissions($item);
378
379                         $mentioned = [];
380
381                         foreach ($terms as $term) {
382                                 $cid = Contact::getIdForURL($term['url'], $item['uid']);
383                                 if (!empty($cid) && in_array($cid, $receiver_list)) {
384                                         $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
385                                         $data['to'][] = $contact['url'];
386                                         $contacts[$contact['url']] = $contact['url'];
387                                 }
388                         }
389
390                         foreach ($receiver_list as $receiver) {
391                                 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
392                                 if (empty($contacts[$contact['url']])) {
393                                         $data['cc'][] = $contact['url'];
394                                         $contacts[$contact['url']] = $contact['url'];
395                                 }
396                         }
397                 }
398
399                 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
400                 while ($parent = Item::fetch($parents)) {
401                         // Don't include data from future posts
402                         if ($parent['id'] >= $item['id']) {
403                                 continue;
404                         }
405
406                         $profile = APContact::getByURL($parent['author-link'], false);
407                         if (!empty($profile) && empty($contacts[$profile['url']])) {
408                                 $data['cc'][] = $profile['url'];
409                                 $contacts[$profile['url']] = $profile['url'];
410                         }
411
412                         if ($item['gravity'] != GRAVITY_PARENT) {
413                                 continue;
414                         }
415
416                         $profile = APContact::getByURL($parent['owner-link'], false);
417                         if (!empty($profile) && empty($contacts[$profile['url']])) {
418                                 $data['cc'][] = $profile['url'];
419                                 $contacts[$profile['url']] = $profile['url'];
420                         }
421                 }
422                 DBA::close($parents);
423
424                 if (empty($data['to'])) {
425                         $data['to'] = $data['cc'];
426                         $data['cc'] = [];
427                 }
428
429                 return $data;
430         }
431
432         /**
433          * @brief Fetches a list of inboxes of followers of a given user
434          *
435          * @param integer $uid User ID
436          *
437          * @return array of follower inboxes
438          */
439         public static function fetchTargetInboxesforUser($uid)
440         {
441                 $inboxes = [];
442
443                 $condition = ['uid' => $uid, 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
444
445                 if (!empty($uid)) {
446                         $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
447                 }
448
449                 $contacts = DBA::select('contact', ['notify', 'batch'], $condition);
450                 while ($contact = DBA::fetch($contacts)) {
451                         $contact = defaults($contact, 'batch', $contact['notify']);
452                         $inboxes[$contact] = $contact;
453                 }
454                 DBA::close($contacts);
455
456                 return $inboxes;
457         }
458
459         /**
460          * @brief Fetches an array of inboxes for the given item and user
461          *
462          * @param array $item
463          * @param integer $uid User ID
464          *
465          * @return array with inboxes
466          */
467         public static function fetchTargetInboxes($item, $uid)
468         {
469                 $permissions = self::createPermissionBlockForItem($item);
470                 if (empty($permissions)) {
471                         return [];
472                 }
473
474                 $inboxes = [];
475
476                 if ($item['gravity'] == GRAVITY_ACTIVITY) {
477                         $item_profile = APContact::getByURL($item['author-link']);
478                 } else {
479                         $item_profile = APContact::getByURL($item['owner-link']);
480                 }
481
482                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
483                         if (empty($permissions[$element])) {
484                                 continue;
485                         }
486
487                         foreach ($permissions[$element] as $receiver) {
488                                 if ($receiver == $item_profile['followers']) {
489                                         $inboxes = self::fetchTargetInboxesforUser($uid);
490                                 } else {
491                                         $profile = APContact::getByURL($receiver);
492                                         if (!empty($profile)) {
493                                                 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
494                                                 $inboxes[$target] = $target;
495                                         }
496                                 }
497                         }
498                 }
499
500                 return $inboxes;
501         }
502
503         /**
504          * @brief Returns the activity type of a given item
505          *
506          * @param array $item
507          *
508          * @return activity type
509          */
510         public static function getTypeOfItem($item)
511         {
512                 if ($item['verb'] == ACTIVITY_POST) {
513                         if ($item['created'] == $item['edited']) {
514                                 $type = 'Create';
515                         } else {
516                                 $type = 'Update';
517                         }
518                 } elseif ($item['verb'] == ACTIVITY_LIKE) {
519                         $type = 'Like';
520                 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
521                         $type = 'Dislike';
522                 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
523                         $type = 'Accept';
524                 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
525                         $type = 'Reject';
526                 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
527                         $type = 'TentativeAccept';
528                 } else {
529                         $type = '';
530                 }
531
532                 return $type;
533         }
534
535         /**
536          * @brief Creates an activity array for a given item id
537          *
538          * @param integer $item_id
539          * @param boolean $object_mode Is the activity item is used inside another object?
540          *
541          * @return array of activity
542          */
543         public static function createActivityFromItem($item_id, $object_mode = false)
544         {
545                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
546
547                 if (!DBA::isResult($item)) {
548                         return false;
549                 }
550
551                 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
552                 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
553                 if (DBA::isResult($conversation)) {
554                         $data = json_decode($conversation['source']);
555                         if (!empty($data)) {
556                                 return $data;
557                         }
558                 }
559
560                 $type = self::getTypeOfItem($item);
561
562                 if (!$object_mode) {
563                         $data = ['@context' => self::CONTEXT];
564
565                         if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
566                                 $type = 'Undo';
567                         } elseif ($item['deleted']) {
568                                 $type = 'Delete';
569                         }
570                 } else {
571                         $data = [];
572                 }
573
574                 $data['id'] = $item['uri'] . '#' . $type;
575                 $data['type'] = $type;
576                 $data['actor'] = $item['author-link'];
577
578                 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
579
580                 if ($item["created"] != $item["edited"]) {
581                         $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
582                 }
583
584                 $data['context'] = self::fetchContextURLForItem($item);
585
586                 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
587
588                 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
589                         $data['object'] = self::createNote($item);
590                 } elseif ($data['type'] == 'Undo') {
591                         $data['object'] = self::createActivityFromItem($item_id, true);
592                 } else {
593                         $data['object'] = $item['thr-parent'];
594                 }
595
596                 $owner = User::getOwnerDataById($item['uid']);
597
598                 if (!$object_mode) {
599                         return LDSignature::sign($data, $owner);
600                 } else {
601                         return $data;
602                 }
603         }
604
605         /**
606          * @brief Creates an object array for a given item id
607          *
608          * @param integer $item_id
609          *
610          * @return object array
611          */
612         public static function createObjectFromItemID($item_id)
613         {
614                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
615
616                 if (!DBA::isResult($item)) {
617                         return false;
618                 }
619
620                 $data = ['@context' => self::CONTEXT];
621                 $data = array_merge($data, self::createNote($item));
622
623                 return $data;
624         }
625
626         /**
627          * @brief Returns a tag array for a given item array
628          *
629          * @param array $item
630          *
631          * @return array of tags
632          */
633         private static function createTagList($item)
634         {
635                 $tags = [];
636
637                 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
638                 foreach ($terms as $term) {
639                         $contact = Contact::getDetailsByURL($term['url']);
640                         if (!empty($contact['addr'])) {
641                                 $mention = '@' . $contact['addr'];
642                         } else {
643                                 $mention = '@' . $term['url'];
644                         }
645
646                         $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
647                 }
648                 return $tags;
649         }
650
651         /**
652          * @brief Fetches the "context" value for a givem item array from the "conversation" table
653          *
654          * @param array $item
655          *
656          * @return string with context url
657          */
658         private static function fetchContextURLForItem($item)
659         {
660                 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
661                 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
662                         $context_uri = $conversation['conversation-href'];
663                 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
664                         $context_uri = $conversation['conversation-uri'];
665                 } else {
666                         $context_uri = str_replace('/objects/', '/context/', $item['parent-uri']);
667                 }
668                 return $context_uri;
669         }
670
671         /**
672          * @brief Creates a note/article object array
673          *
674          * @param array $item
675          *
676          * @return object array
677          */
678         private static function createNote($item)
679         {
680                 if (!empty($item['title'])) {
681                         $type = 'Article';
682                 } else {
683                         $type = 'Note';
684                 }
685
686                 if ($item['deleted']) {
687                         $type = 'Tombstone';
688                 }
689
690                 $data = [];
691                 $data['id'] = $item['uri'];
692                 $data['type'] = $type;
693
694                 if ($item['deleted']) {
695                         return $data;
696                 }
697
698                 $data['summary'] = null; // Ignore by now
699
700                 if ($item['uri'] != $item['thr-parent']) {
701                         $data['inReplyTo'] = $item['thr-parent'];
702                 } else {
703                         $data['inReplyTo'] = null;
704                 }
705
706                 $data['diaspora:guid'] = $item['guid'];
707                 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
708
709                 if ($item["created"] != $item["edited"]) {
710                         $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
711                 }
712
713                 $data['url'] = $item['plink'];
714                 $data['attributedTo'] = $item['author-link'];
715                 $data['actor'] = $item['author-link'];
716                 $data['sensitive'] = false; // - Query NSFW
717                 $data['context'] = self::fetchContextURLForItem($item);
718
719                 if (!empty($item['title'])) {
720                         $data['name'] = BBCode::convert($item['title'], false, 7);
721                 }
722
723                 $data['content'] = BBCode::convert($item['body'], false, 7);
724                 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
725
726                 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
727                         $data['diaspora:comment'] = $item['signed_text'];
728                 }
729
730                 $data['attachment'] = []; // @ToDo
731                 $data['tag'] = self::createTagList($item);
732                 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
733
734                 return $data;
735         }
736
737         /**
738          * @brief Transmits a profile deletion to a given inbox
739          *
740          * @param integer $uid User ID
741          * @param string $inbox Target inbox
742          */
743         public static function transmitProfileDeletion($uid, $inbox)
744         {
745                 $owner = User::getOwnerDataById($uid);
746                 $profile = APContact::getByURL($owner['url']);
747
748                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
749                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
750                         'type' => 'Delete',
751                         'actor' => $owner['url'],
752                         'object' => self::profile($uid),
753                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
754                         'to' => [self::PUBLIC_COLLECTION],
755                         'cc' => []];
756
757                 $signed = LDSignature::sign($data, $owner);
758
759                 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
760                 HTTPSignature::transmit($signed, $inbox, $uid);
761         }
762
763         /**
764          * @brief Transmits a profile change to a given inbox
765          *
766          * @param integer $uid User ID
767          * @param string $inbox Target inbox
768          */
769         public static function transmitProfileUpdate($uid, $inbox)
770         {
771                 $owner = User::getOwnerDataById($uid);
772                 $profile = APContact::getByURL($owner['url']);
773
774                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
775                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
776                         'type' => 'Update',
777                         'actor' => $owner['url'],
778                         'object' => self::profile($uid),
779                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
780                         'to' => [$profile['followers']],
781                         'cc' => []];
782
783                 $signed = LDSignature::sign($data, $owner);
784
785                 logger('Deliver profile update for user ' . $uid . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
786                 HTTPSignature::transmit($signed, $inbox, $uid);
787         }
788
789         /**
790          * @brief Transmits a given activity to a target
791          *
792          * @param array $activity
793          * @param string $target Target profile
794          * @param integer $uid User ID
795          */
796         public static function transmitActivity($activity, $target, $uid)
797         {
798                 $profile = APContact::getByURL($target);
799
800                 $owner = User::getOwnerDataById($uid);
801
802                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
803                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
804                         'type' => $activity,
805                         'actor' => $owner['url'],
806                         'object' => $profile['url'],
807                         'to' => $profile['url']];
808
809                 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
810
811                 $signed = LDSignature::sign($data, $owner);
812                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
813         }
814
815         /**
816          * @brief Transmit a message that the contact request had been accepted
817          *
818          * @param string $target Target profile
819          * @param $id
820          * @param integer $uid User ID
821          */
822         public static function transmitContactAccept($target, $id, $uid)
823         {
824                 $profile = APContact::getByURL($target);
825
826                 $owner = User::getOwnerDataById($uid);
827                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
828                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
829                         'type' => 'Accept',
830                         'actor' => $owner['url'],
831                         'object' => ['id' => $id, 'type' => 'Follow',
832                                 'actor' => $profile['url'],
833                                 'object' => $owner['url']],
834                         'to' => $profile['url']];
835
836                 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
837
838                 $signed = LDSignature::sign($data, $owner);
839                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
840         }
841
842         /**
843          * @brief 
844          *
845          * @param string $target Target profile
846          * @param $id
847          * @param integer $uid User ID
848          */
849         public static function transmitContactReject($target, $id, $uid)
850         {
851                 $profile = APContact::getByURL($target);
852
853                 $owner = User::getOwnerDataById($uid);
854                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
855                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
856                         'type' => 'Reject',
857                         'actor' => $owner['url'],
858                         'object' => ['id' => $id, 'type' => 'Follow',
859                                 'actor' => $profile['url'],
860                                 'object' => $owner['url']],
861                         'to' => $profile['url']];
862
863                 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
864
865                 $signed = LDSignature::sign($data, $owner);
866                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
867         }
868
869         /**
870          * @brief 
871          *
872          * @param string $target Target profile
873          * @param integer $uid User ID
874          */
875         public static function transmitContactUndo($target, $uid)
876         {
877                 $profile = APContact::getByURL($target);
878
879                 $id = System::baseUrl() . '/activity/' . System::createGUID();
880
881                 $owner = User::getOwnerDataById($uid);
882                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
883                         'id' => $id,
884                         'type' => 'Undo',
885                         'actor' => $owner['url'],
886                         'object' => ['id' => $id, 'type' => 'Follow',
887                                 'actor' => $owner['url'],
888                                 'object' => $profile['url']],
889                         'to' => $profile['url']];
890
891                 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
892
893                 $signed = LDSignature::sign($data, $owner);
894                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
895         }
896
897         /**
898          * Fetches ActivityPub content from the given url
899          *
900          * @param string $url content url
901          * @return array
902          */
903         public static function fetchContent($url)
904         {
905                 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
906                 if (!$ret['success'] || empty($ret['body'])) {
907                         return false;
908                 }
909
910                 return json_decode($ret['body'], true);
911         }
912
913         /**
914          * Fetches a profile from the given url into an array that is compatible to Probe::uri
915          *
916          * @param string $url profile url
917          * @return array
918          */
919         public static function probeProfile($url)
920         {
921                 $apcontact = APContact::getByURL($url, true);
922                 if (empty($apcontact)) {
923                         return false;
924                 }
925
926                 $profile = ['network' => Protocol::ACTIVITYPUB];
927                 $profile['nick'] = $apcontact['nick'];
928                 $profile['name'] = $apcontact['name'];
929                 $profile['guid'] = $apcontact['uuid'];
930                 $profile['url'] = $apcontact['url'];
931                 $profile['addr'] = $apcontact['addr'];
932                 $profile['alias'] = $apcontact['alias'];
933                 $profile['photo'] = $apcontact['photo'];
934                 // $profile['community']
935                 // $profile['keywords']
936                 // $profile['location']
937                 $profile['about'] = $apcontact['about'];
938                 $profile['batch'] = $apcontact['sharedinbox'];
939                 $profile['notify'] = $apcontact['inbox'];
940                 $profile['poll'] = $apcontact['outbox'];
941                 $profile['pubkey'] = $apcontact['pubkey'];
942                 $profile['baseurl'] = $apcontact['baseurl'];
943
944                 // Remove all "null" fields
945                 foreach ($profile as $field => $content) {
946                         if (is_null($content)) {
947                                 unset($profile[$field]);
948                         }
949                 }
950
951                 return $profile;
952         }
953
954         /**
955          * @brief 
956          *
957          * @param $body
958          * @param $header
959          * @param integer $uid User ID
960          */
961         public static function processInbox($body, $header, $uid)
962         {
963                 $http_signer = HTTPSignature::getSigner($body, $header);
964                 if (empty($http_signer)) {
965                         logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
966                         return;
967                 } else {
968                         logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
969                 }
970
971                 $activity = json_decode($body, true);
972
973                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
974                 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
975
976                 if (empty($activity)) {
977                         logger('Invalid body.', LOGGER_DEBUG);
978                         return;
979                 }
980
981                 if (LDSignature::isSigned($activity)) {
982                         $ld_signer = LDSignature::getSigner($activity);
983                         if (empty($ld_signer)) {
984                                 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
985                         }
986                         if (!empty($ld_signer && ($actor == $http_signer))) {
987                                 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
988                                 $trust_source = true;
989                         } elseif (!empty($ld_signer)) {
990                                 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
991                                 $trust_source = true;
992                         } elseif ($actor == $http_signer) {
993                                 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
994                                 $trust_source = true;
995                         } else {
996                                 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
997                                 $trust_source = false;
998                         }
999                 } elseif ($actor == $http_signer) {
1000                         logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
1001                         $trust_source = true;
1002                 } else {
1003                         logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
1004                         $trust_source = false;
1005                 }
1006
1007                 self::processActivity($activity, $body, $uid, $trust_source);
1008         }
1009
1010         /**
1011          * @brief 
1012          *
1013          * @param $url
1014          * @param integer $uid User ID
1015          */
1016         public static function fetchOutbox($url, $uid)
1017         {
1018                 $data = self::fetchContent($url);
1019                 if (empty($data)) {
1020                         return;
1021                 }
1022
1023                 if (!empty($data['orderedItems'])) {
1024                         $items = $data['orderedItems'];
1025                 } elseif (!empty($data['first']['orderedItems'])) {
1026                         $items = $data['first']['orderedItems'];
1027                 } elseif (!empty($data['first'])) {
1028                         self::fetchOutbox($data['first'], $uid);
1029                         return;
1030                 } else {
1031                         $items = [];
1032                 }
1033
1034                 foreach ($items as $activity) {
1035                         self::processActivity($activity, '', $uid, true);
1036                 }
1037         }
1038
1039         /**
1040          * @brief 
1041          *
1042          * @param array $activity
1043          * @param integer $uid User ID
1044          * @param $trust_source
1045          *
1046          * @return 
1047          */
1048         private static function prepareObjectData($activity, $uid, &$trust_source)
1049         {
1050                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
1051                 if (empty($actor)) {
1052                         logger('Empty actor', LOGGER_DEBUG);
1053                         return [];
1054                 }
1055
1056                 // Fetch all receivers from to, cc, bto and bcc
1057                 $receivers = self::getReceivers($activity, $actor);
1058
1059                 // When it is a delivery to a personal inbox we add that user to the receivers
1060                 if (!empty($uid)) {
1061                         $owner = User::getOwnerDataById($uid);
1062                         $additional = ['uid:' . $uid => $uid];
1063                         $receivers = array_merge($receivers, $additional);
1064                 }
1065
1066                 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
1067
1068                 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
1069                 if (empty($object_id)) {
1070                         logger('No object found', LOGGER_DEBUG);
1071                         return [];
1072                 }
1073
1074                 // Fetch the content only on activities where this matters
1075                 if (in_array($activity['type'], ['Create', 'Announce'])) {
1076                         $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
1077                         if (empty($object_data)) {
1078                                 logger("Object data couldn't be processed", LOGGER_DEBUG);
1079                                 return [];
1080                         }
1081                         // We had been able to retrieve the object data - so we can trust the source
1082                         $trust_source = true;
1083                 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
1084                         // Create a mostly empty array out of the activity data (instead of the object).
1085                         // This way we later don't have to check for the existence of ech individual array element.
1086                         $object_data = self::processObject($activity);
1087                         $object_data['name'] = $activity['type'];
1088                         $object_data['author'] = $activity['actor'];
1089                         $object_data['object'] = $object_id;
1090                         $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
1091                 } else {
1092                         $object_data = [];
1093                         $object_data['id'] = $activity['id'];
1094                         $object_data['object'] = $activity['object'];
1095                         $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
1096                 }
1097
1098                 $object_data = self::addActivityFields($object_data, $activity);
1099
1100                 $object_data['type'] = $activity['type'];
1101                 $object_data['owner'] = $actor;
1102                 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
1103
1104                 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
1105
1106                 return $object_data;
1107         }
1108
1109         /**
1110          * @brief 
1111          *
1112          * @param array $activity
1113          * @param $body
1114          * @param integer $uid User ID
1115          * @param $trust_source
1116          */
1117         private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
1118         {
1119                 if (empty($activity['type'])) {
1120                         logger('Empty type', LOGGER_DEBUG);
1121                         return;
1122                 }
1123
1124                 if (empty($activity['object'])) {
1125                         logger('Empty object', LOGGER_DEBUG);
1126                         return;
1127                 }
1128
1129                 if (empty($activity['actor'])) {
1130                         logger('Empty actor', LOGGER_DEBUG);
1131                         return;
1132
1133                 }
1134
1135                 // $trust_source is called by reference and is set to true if the content was retrieved successfully
1136                 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
1137                 if (empty($object_data)) {
1138                         logger('No object data found', LOGGER_DEBUG);
1139                         return;
1140                 }
1141
1142                 if (!$trust_source) {
1143                         logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
1144                 }
1145
1146                 switch ($activity['type']) {
1147                         case 'Create':
1148                         case 'Announce':
1149                                 self::createItem($object_data, $body);
1150                                 break;
1151
1152                         case 'Like':
1153                                 self::likeItem($object_data, $body);
1154                                 break;
1155
1156                         case 'Dislike':
1157                                 self::dislikeItem($object_data, $body);
1158                                 break;
1159
1160                         case 'Update':
1161                                 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
1162                                         /// @todo
1163                                 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
1164                                         self::updatePerson($object_data, $body);
1165                                 }
1166                                 break;
1167
1168                         case 'Delete':
1169                                 if ($object_data['object_type'] == 'Tombstone') {
1170                                         self::deleteItem($object_data, $body);
1171                                 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
1172                                         self::deletePerson($object_data, $body);
1173                                 }
1174                                 break;
1175
1176                         case 'Follow':
1177                                 self::followUser($object_data);
1178                                 break;
1179
1180                         case 'Accept':
1181                                 if ($object_data['object_type'] == 'Follow') {
1182                                         self::acceptFollowUser($object_data);
1183                                 }
1184                                 break;
1185
1186                         case 'Reject':
1187                                 if ($object_data['object_type'] == 'Follow') {
1188                                         self::rejectFollowUser($object_data);
1189                                 }
1190                                 break;
1191
1192                         case 'Undo':
1193                                 if ($object_data['object_type'] == 'Follow') {
1194                                         self::undoFollowUser($object_data);
1195                                 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES)) {
1196                                         self::undoActivity($object_data);
1197                                 }
1198                                 break;
1199
1200                         default:
1201                                 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
1202                                 break;
1203                 }
1204         }
1205
1206         /**
1207          * @brief 
1208          *
1209          * @param array $activity
1210          * @param $actor
1211          *
1212          * @return 
1213          */
1214         private static function getReceivers($activity, $actor)
1215         {
1216                 $receivers = [];
1217
1218                 // When it is an answer, we inherite the receivers from the parent
1219                 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1220                 if (!empty($replyto)) {
1221                         $parents = Item::select(['uid'], ['uri' => $replyto]);
1222                         while ($parent = Item::fetch($parents)) {
1223                                 $receivers['uid:' . $parent['uid']] = $parent['uid'];
1224                         }
1225                 }
1226
1227                 if (!empty($actor)) {
1228                         $profile = APContact::getByURL($actor);
1229                         $followers = defaults($profile, 'followers', '');
1230
1231                         logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
1232                 } else {
1233                         logger('Empty actor', LOGGER_DEBUG);
1234                         $followers = '';
1235                 }
1236
1237                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
1238                         if (empty($activity[$element])) {
1239                                 continue;
1240                         }
1241
1242                         // The receiver can be an array or a string
1243                         if (is_string($activity[$element])) {
1244                                 $activity[$element] = [$activity[$element]];
1245                         }
1246
1247                         foreach ($activity[$element] as $receiver) {
1248                                 if ($receiver == self::PUBLIC_COLLECTION) {
1249                                         $receivers['uid:0'] = 0;
1250                                 }
1251
1252                                 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
1253                                         // This will most likely catch all OStatus connections to Mastodon
1254                                         $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
1255                                                 , 'archive' => false, 'pending' => false];
1256                                         $contacts = DBA::select('contact', ['uid'], $condition);
1257                                         while ($contact = DBA::fetch($contacts)) {
1258                                                 if ($contact['uid'] != 0) {
1259                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
1260                                                 }
1261                                         }
1262                                         DBA::close($contacts);
1263                                 }
1264
1265                                 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
1266                                         $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1267                                                 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
1268                                         $contacts = DBA::select('contact', ['uid'], $condition);
1269                                         while ($contact = DBA::fetch($contacts)) {
1270                                                 if ($contact['uid'] != 0) {
1271                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
1272                                                 }
1273                                         }
1274                                         DBA::close($contacts);
1275                                         continue;
1276                                 }
1277
1278                                 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1279                                 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1280                                 if (!DBA::isResult($contact)) {
1281                                         continue;
1282                                 }
1283                                 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1284                         }
1285                 }
1286
1287                 self::switchContacts($receivers, $actor);
1288
1289                 return $receivers;
1290         }
1291
1292         /**
1293          * @brief 
1294          *
1295          * @param $cid
1296          * @param integer $uid User ID
1297          * @param $url
1298          */
1299         private static function switchContact($cid, $uid, $url)
1300         {
1301                 $profile = ActivityPub::probeProfile($url);
1302                 if (empty($profile)) {
1303                         return;
1304                 }
1305
1306                 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
1307
1308                 $photo = $profile['photo'];
1309                 unset($profile['photo']);
1310                 unset($profile['baseurl']);
1311
1312                 $profile['nurl'] = normalise_link($profile['url']);
1313                 DBA::update('contact', $profile, ['id' => $cid]);
1314
1315                 Contact::updateAvatar($photo, $uid, $cid);
1316         }
1317
1318         /**
1319          * @brief 
1320          *
1321          * @param $receivers
1322          * @param $actor
1323          */
1324         private static function switchContacts($receivers, $actor)
1325         {
1326                 if (empty($actor)) {
1327                         return;
1328                 }
1329
1330                 foreach ($receivers as $receiver) {
1331                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
1332                         if (DBA::isResult($contact)) {
1333                                 self::switchContact($contact['id'], $receiver, $actor);
1334                         }
1335
1336                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
1337                         if (DBA::isResult($contact)) {
1338                                 self::switchContact($contact['id'], $receiver, $actor);
1339                         }
1340                 }
1341         }
1342
1343         /**
1344          * @brief 
1345          *
1346          * @param $object_data
1347          * @param array $activity
1348          *
1349          * @return 
1350          */
1351         private static function addActivityFields($object_data, $activity)
1352         {
1353                 if (!empty($activity['published']) && empty($object_data['published'])) {
1354                         $object_data['published'] = $activity['published'];
1355                 }
1356
1357                 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1358                         $object_data['updated'] = $activity['updated'];
1359                 }
1360
1361                 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1362                         $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1363                 }
1364
1365                 if (!empty($activity['instrument'])) {
1366                         $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1367                 }
1368                 return $object_data;
1369         }
1370
1371         /**
1372          * @brief 
1373          *
1374          * @param $object_id
1375          * @param $object
1376          * @param $trust_source
1377          *
1378          * @return 
1379          */
1380         private static function fetchObject($object_id, $object = [], $trust_source = false)
1381         {
1382                 if (!$trust_source || is_string($object)) {
1383                         $data = self::fetchContent($object_id);
1384                         if (empty($data)) {
1385                                 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
1386                                 $data = $object_id;
1387                         } else {
1388                                 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
1389                         }
1390                 } else {
1391                         logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
1392                         $data = $object;
1393                 }
1394
1395                 if (is_string($data)) {
1396                         $item = Item::selectFirst([], ['uri' => $data]);
1397                         if (!DBA::isResult($item)) {
1398                                 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1399                                 return false;
1400                         }
1401                         logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
1402                         $data = self::createNote($item);
1403                 }
1404
1405                 if (empty($data['type'])) {
1406                         logger('Empty type', LOGGER_DEBUG);
1407                         return false;
1408                 }
1409
1410                 if (in_array($data['type'], self::CONTENT_TYPES)) {
1411                         return self::processObject($data);
1412                 }
1413
1414                 if ($data['type'] == 'Announce') {
1415                         if (empty($data['object'])) {
1416                                 return false;
1417                         }
1418                         return self::fetchObject($data['object']);
1419                 }
1420
1421                 logger('Unhandled object type: ' . $data['type'], LOGGER_DEBUG);
1422         }
1423
1424         /**
1425          * @brief 
1426          *
1427          * @param $object
1428          *
1429          * @return 
1430          */
1431         private static function processObject($object)
1432         {
1433                 if (empty($object['id'])) {
1434                         return false;
1435                 }
1436
1437                 $object_data = [];
1438                 $object_data['object_type'] = $object['type'];
1439                 $object_data['id'] = $object['id'];
1440
1441                 if (!empty($object['inReplyTo'])) {
1442                         $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1443                 } else {
1444                         $object_data['reply-to-id'] = $object_data['id'];
1445                 }
1446
1447                 $object_data['published'] = defaults($object, 'published', null);
1448                 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1449
1450                 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1451                         $object_data['published'] = $object_data['updated'];
1452                 }
1453
1454                 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
1455                 if (empty($actor)) {
1456                         $actor = defaults($object, 'actor', null);
1457                 }
1458
1459                 $object_data['diaspora:guid'] = defaults($object, 'diaspora:guid', null);
1460                 $object_data['owner'] = $object_data['author'] = $actor;
1461                 $object_data['context'] = defaults($object, 'context', null);
1462                 $object_data['conversation'] = defaults($object, 'conversation', null);
1463                 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1464                 $object_data['name'] = defaults($object, 'title', null);
1465                 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1466                 $object_data['summary'] = defaults($object, 'summary', null);
1467                 $object_data['content'] = defaults($object, 'content', null);
1468                 $object_data['source'] = defaults($object, 'source', null);
1469                 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1470                 $object_data['attachments'] = defaults($object, 'attachment', null);
1471                 $object_data['tags'] = defaults($object, 'tag', null);
1472                 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1473                 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1474                 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1475
1476                 // Common object data:
1477
1478                 // Unhandled
1479                 // @context, type, actor, signature, mediaType, duration, replies, icon
1480
1481                 // Also missing: (Defined in the standard, but currently unused)
1482                 // audience, preview, endTime, startTime, generator, image
1483
1484                 // Data in Notes:
1485
1486                 // Unhandled
1487                 // contentMap, announcement_count, announcements, context_id, likes, like_count
1488                 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1489
1490                 // Data in video:
1491
1492                 // To-Do?
1493                 // category, licence, language, commentsEnabled
1494
1495                 // Unhandled
1496                 // views, waitTranscoding, state, support, subtitleLanguage
1497                 // likes, dislikes, shares, comments
1498
1499                 return $object_data;
1500         }
1501
1502         /**
1503          * @brief Converts mentions from Pleroma into the Friendica format
1504          *
1505          * @param string $body
1506          *
1507          * @return converted body
1508          */
1509         private static function convertMentions($body)
1510         {
1511                 $URLSearchString = "^\[\]";
1512                 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1513
1514                 return $body;
1515         }
1516
1517         /**
1518          * @brief Constructs a string with tags for a given tag array
1519          *
1520          * @param array $tags
1521          * @param boolean $sensitive
1522          *
1523          * @return string with tags
1524          */
1525         private static function constructTagList($tags, $sensitive)
1526         {
1527                 if (empty($tags)) {
1528                         return '';
1529                 }
1530
1531                 $tag_text = '';
1532                 foreach ($tags as $tag) {
1533                         if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1534                                 if (!empty($tag_text)) {
1535                                         $tag_text .= ',';
1536                                 }
1537
1538                                 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1539                         }
1540                 }
1541
1542                 /// @todo add nsfw for $sensitive
1543
1544                 return $tag_text;
1545         }
1546
1547         /**
1548          * @brief 
1549          *
1550          * @param $attachments
1551          * @param array $item
1552          *
1553          * @return item array
1554          */
1555         private static function constructAttachList($attachments, $item)
1556         {
1557                 if (empty($attachments)) {
1558                         return $item;
1559                 }
1560
1561                 foreach ($attachments as $attach) {
1562                         $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1563                         if ($filetype == 'image') {
1564                                 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1565                         } else {
1566                                 if (!empty($item["attach"])) {
1567                                         $item["attach"] .= ',';
1568                                 } else {
1569                                         $item["attach"] = '';
1570                                 }
1571                                 if (!isset($attach['length'])) {
1572                                         $attach['length'] = "0";
1573                                 }
1574                                 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1575                         }
1576                 }
1577
1578                 return $item;
1579         }
1580
1581         /**
1582          * @brief 
1583          *
1584          * @param array $activity
1585          * @param $body
1586          */
1587         private static function createItem($activity, $body)
1588         {
1589                 $item = [];
1590                 $item['verb'] = ACTIVITY_POST;
1591                 $item['parent-uri'] = $activity['reply-to-id'];
1592
1593                 if ($activity['reply-to-id'] == $activity['id']) {
1594                         $item['gravity'] = GRAVITY_PARENT;
1595                         $item['object-type'] = ACTIVITY_OBJ_NOTE;
1596                 } else {
1597                         $item['gravity'] = GRAVITY_COMMENT;
1598                         $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1599                 }
1600
1601                 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
1602                         logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
1603                         self::fetchMissingActivity($activity['reply-to-id'], $activity);
1604                 }
1605
1606                 self::postItem($activity, $item, $body);
1607         }
1608
1609         /**
1610          * @brief 
1611          *
1612          * @param array $activity
1613          * @param $body
1614          */
1615         private static function likeItem($activity, $body)
1616         {
1617                 $item = [];
1618                 $item['verb'] = ACTIVITY_LIKE;
1619                 $item['parent-uri'] = $activity['object'];
1620                 $item['gravity'] = GRAVITY_ACTIVITY;
1621                 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1622
1623                 self::postItem($activity, $item, $body);
1624         }
1625
1626         /**
1627          * @brief Delete items
1628          *
1629          * @param array $activity
1630          * @param $body
1631          */
1632         private static function deleteItem($activity)
1633         {
1634                 $owner = Contact::getIdForURL($activity['owner']);
1635                 $object = JsonLD::fetchElement($activity, 'object', 'id');
1636                 logger('Deleting item ' . $object . ' from ' . $owner, LOGGER_DEBUG);
1637                 Item::delete(['uri' => $object, 'owner-id' => $owner]);
1638         }
1639
1640         /**
1641          * @brief 
1642          *
1643          * @param array $activity
1644          * @param $body
1645          */
1646         private static function dislikeItem($activity, $body)
1647         {
1648                 $item = [];
1649                 $item['verb'] = ACTIVITY_DISLIKE;
1650                 $item['parent-uri'] = $activity['object'];
1651                 $item['gravity'] = GRAVITY_ACTIVITY;
1652                 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1653
1654                 self::postItem($activity, $item, $body);
1655         }
1656
1657         /**
1658          * @brief 
1659          *
1660          * @param array $activity
1661          * @param array $item
1662          * @param $body
1663          */
1664         private static function postItem($activity, $item, $body)
1665         {
1666                 /// @todo What to do with $activity['context']?
1667
1668                 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
1669                         logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
1670                         return;
1671                 }
1672
1673                 $item['network'] = Protocol::ACTIVITYPUB;
1674                 $item['private'] = !in_array(0, $activity['receiver']);
1675                 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1676                 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1677                 $item['uri'] = $activity['id'];
1678                 $item['created'] = $activity['published'];
1679                 $item['edited'] = $activity['updated'];
1680                 $item['guid'] = $activity['diaspora:guid'];
1681                 $item['title'] = HTML::toBBCode($activity['name']);
1682                 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1683                 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1684                 $item['location'] = $activity['location'];
1685                 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1686                 $item['app'] = $activity['service'];
1687                 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1688
1689                 $item = self::constructAttachList($activity['attachments'], $item);
1690
1691                 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1692                 if (!empty($source)) {
1693                         $item['body'] = $source;
1694                 }
1695
1696                 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1697                 $item['source'] = $body;
1698                 $item['conversation-href'] = $activity['context'];
1699                 $item['conversation-uri'] = $activity['conversation'];
1700
1701                 foreach ($activity['receiver'] as $receiver) {
1702                         $item['uid'] = $receiver;
1703                         $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1704
1705                         if (($receiver != 0) && empty($item['contact-id'])) {
1706                                 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1707                         }
1708
1709                         $item_id = Item::insert($item);
1710                         logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1711                 }
1712         }
1713
1714         /**
1715          * @brief 
1716          *
1717          * @param $url
1718          * @param $child
1719          */
1720         private static function fetchMissingActivity($url, $child)
1721         {
1722                 if (Config::get('system', 'ostatus_full_threads')) {
1723                         return;
1724                 }
1725
1726                 $object = ActivityPub::fetchContent($url);
1727                 if (empty($object)) {
1728                         logger('Activity ' . $url . ' was not fetchable, aborting.');
1729                         return;
1730                 }
1731
1732                 $activity = [];
1733                 $activity['@context'] = $object['@context'];
1734                 unset($object['@context']);
1735                 $activity['id'] = $object['id'];
1736                 $activity['to'] = defaults($object, 'to', []);
1737                 $activity['cc'] = defaults($object, 'cc', []);
1738                 $activity['actor'] = $child['author'];
1739                 $activity['object'] = $object;
1740                 $activity['published'] = $object['published'];
1741                 $activity['type'] = 'Create';
1742
1743                 self::processActivity($activity);
1744                 logger('Activity ' . $url . ' had been fetched and processed.');
1745         }
1746
1747         /**
1748          * @brief perform a "follow" request
1749          *
1750          * @param array $activity
1751          */
1752         private static function followUser($activity)
1753         {
1754                 $actor = JsonLD::fetchElement($activity, 'object', 'id');
1755                 $uid = User::getIdForURL($actor);
1756                 if (empty($uid)) {
1757                         return;
1758                 }
1759
1760                 $owner = User::getOwnerDataById($uid);
1761
1762                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1763                 if (!empty($cid)) {
1764                         $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1765                 } else {
1766                         $contact = false;
1767                 }
1768
1769                 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1770                         'author-link' => $activity['owner']];
1771
1772                 Contact::addRelationship($owner, $contact, $item);
1773                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1774                 if (empty($cid)) {
1775                         return;
1776                 }
1777
1778                 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1779                 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1780                         Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1781                 }
1782
1783                 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1784                 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1785         }
1786
1787         /**
1788          * @brief Update the given profile
1789          *
1790          * @param array $activity
1791          */
1792         private static function updatePerson($activity)
1793         {
1794                 if (empty($activity['object']['id'])) {
1795                         return;
1796                 }
1797
1798                 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
1799                 APContact::getByURL($activity['object']['id'], true);
1800         }
1801
1802         /**
1803          * @brief Delete the given profile
1804          *
1805          * @param array $activity
1806          */
1807         private static function deletePerson($activity)
1808         {
1809                 if (empty($activity['object']['id']) || empty($activity['object']['actor'])) {
1810                         logger('Empty object id or actor.', LOGGER_DEBUG);
1811                         return;
1812                 }
1813
1814                 if ($activity['object']['id'] != $activity['object']['actor']) {
1815                         logger('Object id does not match actor.', LOGGER_DEBUG);
1816                         return;
1817                 }
1818
1819                 $contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($activity['object']['id'])]);
1820                 while ($contact = DBA::fetch($contacts)) {
1821                         Contact::remove($contact["id"]);
1822                 }
1823                 DBA::close($contacts);
1824
1825                 logger('Deleted contact ' . $activity['object']['id'], LOGGER_DEBUG);
1826         }
1827
1828         /**
1829          * @brief Accept a follow request
1830          *
1831          * @param array $activity
1832          */
1833         private static function acceptFollowUser($activity)
1834         {
1835                 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1836                 $uid = User::getIdForURL($actor);
1837                 if (empty($uid)) {
1838                         return;
1839                 }
1840
1841                 $owner = User::getOwnerDataById($uid);
1842
1843                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1844                 if (empty($cid)) {
1845                         logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1846                         return;
1847                 }
1848
1849                 $fields = ['pending' => false];
1850
1851                 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1852                 if ($contact['rel'] == Contact::FOLLOWER) {
1853                         $fields['rel'] = Contact::FRIEND;
1854                 }
1855
1856                 $condition = ['id' => $cid];
1857                 DBA::update('contact', $fields, $condition);
1858                 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1859         }
1860
1861         /**
1862          * @brief Reject a follow request
1863          *
1864          * @param array $activity
1865          */
1866         private static function rejectFollowUser($activity)
1867         {
1868                 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1869                 $uid = User::getIdForURL($actor);
1870                 if (empty($uid)) {
1871                         return;
1872                 }
1873
1874                 $owner = User::getOwnerDataById($uid);
1875
1876                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1877                 if (empty($cid)) {
1878                         logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1879                         return;
1880                 }
1881
1882                 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING, 'pending' => true])) {
1883                         Contact::remove($cid);
1884                         logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', LOGGER_DEBUG);
1885                 } else {
1886                         logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', LOGGER_DEBUG);
1887                 }
1888         }
1889
1890         /**
1891          * @brief Undo activity like "like" or "dislike"
1892          *
1893          * @param array $activity
1894          */
1895         private static function undoActivity($activity)
1896         {
1897                 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
1898                 if (empty($activity_url)) {
1899                         return;
1900                 }
1901
1902                 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1903                 if (empty($actor)) {
1904                         return;
1905                 }
1906
1907                 $author_id = Contact::getIdForURL($actor);
1908                 if (empty($author_id)) {
1909                         return;
1910                 }
1911
1912                 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1913         }
1914
1915         /**
1916          * @brief Activity to remove a follower
1917          *
1918          * @param array $activity
1919          */
1920         private static function undoFollowUser($activity)
1921         {
1922                 $object = JsonLD::fetchElement($activity, 'object', 'object');
1923                 $uid = User::getIdForURL($object);
1924                 if (empty($uid)) {
1925                         return;
1926                 }
1927
1928                 $owner = User::getOwnerDataById($uid);
1929
1930                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1931                 if (empty($cid)) {
1932                         logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1933                         return;
1934                 }
1935
1936                 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1937                 if (!DBA::isResult($contact)) {
1938                         return;
1939                 }
1940
1941                 Contact::removeFollower($owner, $contact);
1942                 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1943         }
1944 }