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