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