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