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