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