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