]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Transmitter.php
Introduce new DI container
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
1 <?php
2 /**
3  * @file src/Protocol/ActivityPub/Transmitter.php
4  */
5 namespace Friendica\Protocol\ActivityPub;
6
7 use Friendica\BaseObject;
8 use Friendica\Content\Feature;
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Content\Text\Plaintext;
11 use Friendica\Core\Cache;
12 use Friendica\Core\Config;
13 use Friendica\Core\Logger;
14 use Friendica\Core\Protocol;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\DI;
18 use Friendica\Model\APContact;
19 use Friendica\Model\Contact;
20 use Friendica\Model\Conversation;
21 use Friendica\Model\Item;
22 use Friendica\Model\Profile;
23 use Friendica\Model\Photo;
24 use Friendica\Model\Term;
25 use Friendica\Model\User;
26 use Friendica\Protocol\Activity;
27 use Friendica\Protocol\ActivityPub;
28 use Friendica\Util\DateTimeFormat;
29 use Friendica\Util\HTTPSignature;
30 use Friendica\Util\Images;
31 use Friendica\Util\JsonLD;
32 use Friendica\Util\LDSignature;
33 use Friendica\Util\Map;
34 use Friendica\Util\Network;
35 use Friendica\Util\XML;
36
37 require_once 'include/api.php';
38 require_once 'mod/share.php';
39
40 /**
41  * @brief ActivityPub Transmitter Protocol class
42  *
43  * To-Do:
44  * - Undo Announce
45  */
46 class Transmitter
47 {
48         /**
49          * collects the lost of followers of the given owner
50          *
51          * @param array   $owner Owner array
52          * @param integer $page  Page number
53          *
54          * @return array of owners
55          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
56          */
57         public static function getFollowers($owner, $page = null)
58         {
59                 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
60                         'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
61                 $count = DBA::count('contact', $condition);
62
63                 $data = ['@context' => ActivityPub::CONTEXT];
64                 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
65                 $data['type'] = 'OrderedCollection';
66                 $data['totalItems'] = $count;
67
68                 // When we hide our friends we will only show the pure number but don't allow more.
69                 $profile = Profile::getByUID($owner['uid']);
70                 if (!empty($profile['hide-friends'])) {
71                         return $data;
72                 }
73
74                 if (empty($page)) {
75                         $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
76                 } else {
77                         $data['type'] = 'OrderedCollectionPage';
78                         $list = [];
79
80                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
81                         while ($contact = DBA::fetch($contacts)) {
82                                 $list[] = $contact['url'];
83                         }
84
85                         if (!empty($list)) {
86                                 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
87                         }
88
89                         $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
90
91                         $data['orderedItems'] = $list;
92                 }
93
94                 return $data;
95         }
96
97         /**
98          * Create list of following contacts
99          *
100          * @param array   $owner Owner array
101          * @param integer $page  Page numbe
102          *
103          * @return array of following contacts
104          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
105          */
106         public static function getFollowing($owner, $page = null)
107         {
108                 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
109                         'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
110                 $count = DBA::count('contact', $condition);
111
112                 $data = ['@context' => ActivityPub::CONTEXT];
113                 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
114                 $data['type'] = 'OrderedCollection';
115                 $data['totalItems'] = $count;
116
117                 // When we hide our friends we will only show the pure number but don't allow more.
118                 $profile = Profile::getByUID($owner['uid']);
119                 if (!empty($profile['hide-friends'])) {
120                         return $data;
121                 }
122
123                 if (empty($page)) {
124                         $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
125                 } else {
126                         $data['type'] = 'OrderedCollectionPage';
127                         $list = [];
128
129                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
130                         while ($contact = DBA::fetch($contacts)) {
131                                 $list[] = $contact['url'];
132                         }
133
134                         if (!empty($list)) {
135                                 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
136                         }
137
138                         $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
139
140                         $data['orderedItems'] = $list;
141                 }
142
143                 return $data;
144         }
145
146         /**
147          * Public posts for the given owner
148          *
149          * @param array   $owner Owner array
150          * @param integer $page  Page numbe
151          *
152          * @return array of posts
153          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
154          * @throws \ImagickException
155          */
156         public static function getOutbox($owner, $page = null)
157         {
158                 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
159
160                 $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
161                         'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
162                         'deleted' => false, 'visible' => true, 'moderated' => false];
163                 $count = DBA::count('item', $condition);
164
165                 $data = ['@context' => ActivityPub::CONTEXT];
166                 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
167                 $data['type'] = 'OrderedCollection';
168                 $data['totalItems'] = $count;
169
170                 if (empty($page)) {
171                         $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
172                 } else {
173                         $data['type'] = 'OrderedCollectionPage';
174                         $list = [];
175
176                         $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
177
178                         $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
179                         while ($item = Item::fetch($items)) {
180                                 $activity = self::createActivityFromItem($item['id'], true);
181                                 // Only list "Create" activity objects here, no reshares
182                                 if (is_array($activity['object']) && ($activity['type'] == 'Create')) {
183                                         $list[] = $activity['object'];
184                                 }
185                         }
186
187                         if (!empty($list)) {
188                                 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
189                         }
190
191                         $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
192
193                         $data['orderedItems'] = $list;
194                 }
195
196                 return $data;
197         }
198
199         /**
200          * Return the service array containing information the used software and it's url
201          *
202          * @return array with service data
203          */
204         private static function getService()
205         {
206                 return ['type' => 'Service',
207                         'name' =>  FRIENDICA_PLATFORM . " '" . FRIENDICA_CODENAME . "' " . FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
208                         'url' => DI::app()->getBaseURL()];
209         }
210
211         /**
212          * Return the ActivityPub profile of the given user
213          *
214          * @param integer $uid User ID
215          * @return array with profile data
216          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
217          */
218         public static function getProfile($uid)
219         {
220                 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
221                         'account_removed' => false, 'verified' => true];
222                 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
223                 $user = DBA::selectFirst('user', $fields, $condition);
224                 if (!DBA::isResult($user)) {
225                         return [];
226                 }
227
228                 $fields = ['locality', 'region', 'country-name'];
229                 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
230                 if (!DBA::isResult($profile)) {
231                         return [];
232                 }
233
234                 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
235                 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
236                 if (!DBA::isResult($contact)) {
237                         return [];
238                 }
239
240                 $data = ['@context' => ActivityPub::CONTEXT];
241                 $data['id'] = $contact['url'];
242                 $data['diaspora:guid'] = $user['guid'];
243                 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
244                 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
245                 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
246                 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
247                 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
248                 $data['preferredUsername'] = $user['nickname'];
249                 $data['name'] = $contact['name'];
250                 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
251                         'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
252                 $data['summary'] = $contact['about'];
253                 $data['url'] = $contact['url'];
254                 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
255                 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
256                         'owner' => $contact['url'],
257                         'publicKeyPem' => $user['pubkey']];
258                 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
259                 $data['icon'] = ['type' => 'Image',
260                         'url' => $contact['photo']];
261
262                 $data['generator'] = self::getService();
263
264                 // tags: https://kitty.town/@inmysocks/100656097926961126.json
265                 return $data;
266         }
267
268         /**
269          * @param string $username
270          * @return array
271          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
272          */
273         public static function getDeletedUser($username)
274         {
275                 return [
276                         '@context' => ActivityPub::CONTEXT,
277                         'id' => System::baseUrl() . '/profile/' . $username,
278                         'type' => 'Tombstone',
279                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
280                         'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
281                         'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
282                 ];
283         }
284
285         /**
286          * Returns an array with permissions of a given item array
287          *
288          * @param array $item
289          *
290          * @return array with permissions
291          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
292          * @throws \ImagickException
293          */
294         private static function fetchPermissionBlockFromConversation($item)
295         {
296                 if (empty($item['thr-parent'])) {
297                         return [];
298                 }
299
300                 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
301                 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
302                 if (!DBA::isResult($conversation)) {
303                         return [];
304                 }
305
306                 $activity = json_decode($conversation['source'], true);
307
308                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
309                 $profile = APContact::getByURL($actor);
310
311                 $item_profile = APContact::getByURL($item['author-link']);
312                 $exclude[] = $item['author-link'];
313
314                 if ($item['gravity'] == GRAVITY_PARENT) {
315                         $exclude[] = $item['owner-link'];
316                 }
317
318                 $permissions['to'][] = $actor;
319
320                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
321                         if (empty($activity[$element])) {
322                                 continue;
323                         }
324                         if (is_string($activity[$element])) {
325                                 $activity[$element] = [$activity[$element]];
326                         }
327
328                         foreach ($activity[$element] as $receiver) {
329                                 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
330                                         $permissions[$element][] = $item_profile['followers'];
331                                 } elseif (!in_array($receiver, $exclude)) {
332                                         $permissions[$element][] = $receiver;
333                                 }
334                         }
335                 }
336                 return $permissions;
337         }
338
339         /**
340          * Creates an array of permissions from an item thread
341          *
342          * @param array   $item       Item array
343          * @param boolean $blindcopy  addressing via "bcc" or "cc"?
344          * @param integer $last_id    Last item id for adding receivers
345          * @param boolean $forum_mode "true" means that we are sending content to a forum
346          *
347          * @return array with permission data
348          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
349          * @throws \ImagickException
350          */
351         private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false)
352         {
353                 if ($last_id == 0) {
354                         $last_id = $item['id'];
355                 }
356
357                 $always_bcc = false;
358
359                 // Check if we should always deliver our stuff via BCC
360                 if (!empty($item['uid'])) {
361                         $profile = Profile::getByUID($item['uid']);
362                         if (!empty($profile)) {
363                                 $always_bcc = $profile['hide-friends'];
364                         }
365                 }
366
367                 if (Config::get('system', 'ap_always_bcc')) {
368                         $always_bcc = true;
369                 }
370
371                 if (self::isAnnounce($item) || Config::get('debug', 'total_ap_delivery')) {
372                         // Will be activated in a later step
373                         $networks = Protocol::FEDERATED;
374                 } else {
375                         // For now only send to these contacts:
376                         $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
377                 }
378
379                 $data = ['to' => [], 'cc' => [], 'bcc' => []];
380
381                 if ($item['gravity'] == GRAVITY_PARENT) {
382                         $actor_profile = APContact::getByURL($item['owner-link']);
383                 } else {
384                         $actor_profile = APContact::getByURL($item['author-link']);
385                 }
386
387                 $terms = Term::tagArrayFromItemId($item['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
388
389                 if (!$item['private']) {
390                         // Directly mention the original author upon a quoted reshare.
391                         // Else just ensure that the original author receives the reshare.
392                         $announce = self::getAnnounceArray($item);
393                         if (!empty($announce['comment'])) {
394                                 $data['to'][] = $announce['actor']['url'];
395                         } elseif (!empty($announce)) {
396                                 $data['cc'][] = $announce['actor']['url'];
397                         }
398
399                         $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
400
401                         $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
402
403                         foreach ($terms as $term) {
404                                 $profile = APContact::getByURL($term['url'], false);
405                                 if (!empty($profile)) {
406                                         $data['to'][] = $profile['url'];
407                                 }
408                         }
409                 } else {
410                         $receiver_list = Item::enumeratePermissions($item, true);
411
412                         foreach ($terms as $term) {
413                                 $cid = Contact::getIdForURL($term['url'], $item['uid']);
414                                 if (!empty($cid) && in_array($cid, $receiver_list)) {
415                                         $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
416                                         if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
417                                                 continue;
418                                         }
419
420                                         if (!empty($profile = APContact::getByURL($contact['url'], false))) {
421                                                 $data['to'][] = $profile['url'];
422                                         }
423                                 }
424                         }
425
426                         foreach ($receiver_list as $receiver) {
427                                 $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
428                                 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
429                                         continue;
430                                 }
431
432                                 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
433                                         if ($contact['hidden'] || $always_bcc) {
434                                                 $data['bcc'][] = $profile['url'];
435                                         } else {
436                                                 $data['cc'][] = $profile['url'];
437                                         }
438                                 }
439                         }
440                 }
441
442                 if (!empty($item['parent'])) {
443                         $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
444                         while ($parent = Item::fetch($parents)) {
445                                 if ($parent['gravity'] == GRAVITY_PARENT) {
446                                         $profile = APContact::getByURL($parent['owner-link'], false);
447                                         if (!empty($profile)) {
448                                                 if ($item['gravity'] != GRAVITY_PARENT) {
449                                                         // Comments to forums are directed to the forum
450                                                         // But comments to forums aren't directed to the followers collection
451                                                         if ($profile['type'] == 'Group') {
452                                                                 $data['to'][] = $profile['url'];
453                                                         } else {
454                                                                 $data['cc'][] = $profile['url'];
455                                                                 if (!$item['private'] && !empty($actor_profile['followers'])) {
456                                                                         $data['cc'][] = $actor_profile['followers'];
457                                                                 }
458                                                         }
459                                                 } else {
460                                                         // Public thread parent post always are directed to the followers
461                                                         if (!$item['private'] && !$forum_mode) {
462                                                                 $data['cc'][] = $actor_profile['followers'];
463                                                         }
464                                                 }
465                                         }
466                                 }
467
468                                 // Don't include data from future posts
469                                 if ($parent['id'] >= $last_id) {
470                                         continue;
471                                 }
472
473                                 $profile = APContact::getByURL($parent['author-link'], false);
474                                 if (!empty($profile)) {
475                                         if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
476                                                 $data['to'][] = $profile['url'];
477                                         } else {
478                                                 $data['cc'][] = $profile['url'];
479                                         }
480                                 }
481                         }
482                         DBA::close($parents);
483                 }
484
485                 $data['to'] = array_unique($data['to']);
486                 $data['cc'] = array_unique($data['cc']);
487                 $data['bcc'] = array_unique($data['bcc']);
488
489                 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
490                         unset($data['to'][$key]);
491                 }
492
493                 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
494                         unset($data['cc'][$key]);
495                 }
496
497                 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
498                         unset($data['bcc'][$key]);
499                 }
500
501                 foreach ($data['to'] as $to) {
502                         if (($key = array_search($to, $data['cc'])) !== false) {
503                                 unset($data['cc'][$key]);
504                         }
505
506                         if (($key = array_search($to, $data['bcc'])) !== false) {
507                                 unset($data['bcc'][$key]);
508                         }
509                 }
510
511                 foreach ($data['cc'] as $cc) {
512                         if (($key = array_search($cc, $data['bcc'])) !== false) {
513                                 unset($data['bcc'][$key]);
514                         }
515                 }
516
517                 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
518
519                 if (!$blindcopy) {
520                         unset($receivers['bcc']);
521                 }
522
523                 return $receivers;
524         }
525
526         /**
527          * Check if an inbox is archived
528          *
529          * @param string $url Inbox url
530          *
531          * @return boolean "true" if inbox is archived
532          */
533         private static function archivedInbox($url)
534         {
535                 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
536         }
537
538         /**
539          * Fetches a list of inboxes of followers of a given user
540          *
541          * @param integer $uid      User ID
542          * @param boolean $personal fetch personal inboxes
543          *
544          * @return array of follower inboxes
545          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
546          * @throws \ImagickException
547          */
548         public static function fetchTargetInboxesforUser($uid, $personal = false)
549         {
550                 $inboxes = [];
551
552                 if (Config::get('debug', 'total_ap_delivery')) {
553                         // Will be activated in a later step
554                         $networks = Protocol::FEDERATED;
555                 } else {
556                         // For now only send to these contacts:
557                         $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
558                 }
559
560                 $condition = ['uid' => $uid, 'archive' => false, 'pending' => false];
561
562                 if (!empty($uid)) {
563                         $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
564                 }
565
566                 $contacts = DBA::select('contact', ['url', 'network', 'protocol'], $condition);
567                 while ($contact = DBA::fetch($contacts)) {
568                         if (Contact::isLocal($contact['url'])) {
569                                 continue;
570                         }
571
572                         if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
573                                 continue;
574                         }
575
576                         if (Network::isUrlBlocked($contact['url'])) {
577                                 continue;
578                         }
579
580                         $profile = APContact::getByURL($contact['url'], false);
581                         if (!empty($profile)) {
582                                 if (empty($profile['sharedinbox']) || $personal) {
583                                         $target = $profile['inbox'];
584                                 } else {
585                                         $target = $profile['sharedinbox'];
586                                 }
587                                 if (!self::archivedInbox($target)) {
588                                         $inboxes[$target] = $target;
589                                 }
590                         }
591                 }
592                 DBA::close($contacts);
593
594                 return $inboxes;
595         }
596
597         /**
598          * Fetches an array of inboxes for the given item and user
599          *
600          * @param array   $item       Item array
601          * @param integer $uid        User ID
602          * @param boolean $personal   fetch personal inboxes
603          * @param integer $last_id    Last item id for adding receivers
604          * @param boolean $forum_mode "true" means that we are sending content to a forum
605          * @return array with inboxes
606          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
607          * @throws \ImagickException
608          */
609         public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
610         {
611                 $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
612                 if (empty($permissions)) {
613                         return [];
614                 }
615
616                 $inboxes = [];
617
618                 if ($item['gravity'] == GRAVITY_ACTIVITY) {
619                         $item_profile = APContact::getByURL($item['author-link'], false);
620                 } else {
621                         $item_profile = APContact::getByURL($item['owner-link'], false);
622                 }
623
624                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
625                         if (empty($permissions[$element])) {
626                                 continue;
627                         }
628
629                         $blindcopy = in_array($element, ['bto', 'bcc']);
630
631                         foreach ($permissions[$element] as $receiver) {
632                                 if (Network::isUrlBlocked($receiver)) {
633                                         continue;
634                                 }
635
636                                 if ($receiver == $item_profile['followers']) {
637                                         $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
638                                 } else {
639                                         if (Contact::isLocal($receiver)) {
640                                                 continue;
641                                         }
642
643                                         $profile = APContact::getByURL($receiver, false);
644                                         if (!empty($profile)) {
645                                                 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
646                                                         $target = $profile['inbox'];
647                                                 } else {
648                                                         $target = $profile['sharedinbox'];
649                                                 }
650                                                 if (!self::archivedInbox($target)) {
651                                                         $inboxes[$target] = $target;
652                                                 }
653                                         }
654                                 }
655                         }
656                 }
657
658                 return $inboxes;
659         }
660
661         /**
662          * Creates an array in the structure of the item table for a given mail id
663          *
664          * @param integer $mail_id
665          *
666          * @return array
667          * @throws \Exception
668          */
669         public static function ItemArrayFromMail($mail_id)
670         {
671                 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
672                 if (!DBA::isResult($mail)) {
673                         return [];
674                 }
675
676                 $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
677
678                 // Making the post more compatible for Mastodon by:
679                 // - Making it a note and not an article (no title)
680                 // - Moving the title into the "summary" field that is used as a "content warning"
681                 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
682                 $mail['title'] = '';
683
684                 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
685                 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
686                 $mail['allow_gid'] = '';
687                 $mail['deny_cid'] = '';
688                 $mail['deny_gid'] = '';
689                 $mail['private'] = true;
690                 $mail['deleted'] = false;
691                 $mail['edited'] = $mail['created'];
692                 $mail['plink'] = $mail['uri'];
693                 $mail['thr-parent'] = $reply['uri'];
694                 $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
695
696                 $mail['event-type'] = '';
697                 $mail['attach'] = '';
698
699                 $mail['parent'] = 0;
700
701                 return $mail;
702         }
703
704         /**
705          * Creates an activity array for a given mail id
706          *
707          * @param integer $mail_id
708          * @param boolean $object_mode Is the activity item is used inside another object?
709          *
710          * @return array of activity
711          * @throws \Exception
712          */
713         public static function createActivityFromMail($mail_id, $object_mode = false)
714         {
715                 $mail = self::ItemArrayFromMail($mail_id);
716                 $object = self::createNote($mail);
717
718                 if (!$object_mode) {
719                         $data = ['@context' => ActivityPub::CONTEXT];
720                 } else {
721                         $data = [];
722                 }
723
724                 $data['id'] = $mail['uri'] . '#Create';
725                 $data['type'] = 'Create';
726                 $data['actor'] = $mail['author-link'];
727                 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
728                 $data['instrument'] = self::getService();
729                 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
730
731                 if (empty($data['to']) && !empty($data['cc'])) {
732                         $data['to'] = $data['cc'];
733                 }
734
735                 if (empty($data['to']) && !empty($data['bcc'])) {
736                         $data['to'] = $data['bcc'];
737                 }
738
739                 unset($data['cc']);
740                 unset($data['bcc']);
741
742                 $object['to'] = $data['to'];
743                 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
744
745                 unset($object['cc']);
746                 unset($object['bcc']);
747
748                 $data['directMessage'] = true;
749
750                 $data['object'] = $object;
751
752                 $owner = User::getOwnerDataById($mail['uid']);
753
754                 if (!$object_mode && !empty($owner)) {
755                         return LDSignature::sign($data, $owner);
756                 } else {
757                         return $data;
758                 }
759         }
760
761         /**
762          * Returns the activity type of a given item
763          *
764          * @param array $item
765          *
766          * @return string with activity type
767          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
768          * @throws \ImagickException
769          */
770         private static function getTypeOfItem($item)
771         {
772                 $reshared = false;
773
774                 // Only check for a reshare, if it is a real reshare and no quoted reshare
775                 if (strpos($item['body'], "[share") === 0) {
776                         $announce = self::getAnnounceArray($item);
777                         $reshared = !empty($announce);
778                 }
779
780                 if ($reshared) {
781                         $type = 'Announce';
782                 } elseif ($item['verb'] == Activity::POST) {
783                         if ($item['created'] == $item['edited']) {
784                                 $type = 'Create';
785                         } else {
786                                 $type = 'Update';
787                         }
788                 } elseif ($item['verb'] == Activity::LIKE) {
789                         $type = 'Like';
790                 } elseif ($item['verb'] == Activity::DISLIKE) {
791                         $type = 'Dislike';
792                 } elseif ($item['verb'] == Activity::ATTEND) {
793                         $type = 'Accept';
794                 } elseif ($item['verb'] == Activity::ATTENDNO) {
795                         $type = 'Reject';
796                 } elseif ($item['verb'] == Activity::ATTENDMAYBE) {
797                         $type = 'TentativeAccept';
798                 } elseif ($item['verb'] == Activity::FOLLOW) {
799                         $type = 'Follow';
800                 } elseif ($item['verb'] == Activity::TAG) {
801                         $type = 'Add';
802                 } else {
803                         $type = '';
804                 }
805
806                 return $type;
807         }
808
809         /**
810          * Creates the activity or fetches it from the cache
811          *
812          * @param integer $item_id
813          * @param boolean $force Force new cache entry
814          *
815          * @return array with the activity
816          * @throws \Exception
817          */
818         public static function createCachedActivityFromItem($item_id, $force = false)
819         {
820                 $cachekey = 'APDelivery:createActivity:' . $item_id;
821
822                 if (!$force) {
823                         $data = Cache::get($cachekey);
824                         if (!is_null($data)) {
825                                 return $data;
826                         }
827                 }
828
829                 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
830
831                 Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
832                 return $data;
833         }
834
835         /**
836          * Creates an activity array for a given item id
837          *
838          * @param integer $item_id
839          * @param boolean $object_mode Is the activity item is used inside another object?
840          *
841          * @return array of activity
842          * @throws \Exception
843          */
844         public static function createActivityFromItem($item_id, $object_mode = false)
845         {
846                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
847
848                 if (!DBA::isResult($item)) {
849                         return false;
850                 }
851
852                 if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
853                         $owner = User::getOwnerDataById($item['uid']);
854                         if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
855                                 $type = 'Announce';
856
857                                 // Disguise forum posts as reshares. Will later be converted to a real announce
858                                 $item['body'] = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
859                                         $item['guid'], $item['created'], $item['plink']) . $item['body'] . '[/share]';
860                         }
861                 }
862
863                 if (empty($type)) {
864                         $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
865                         $conversation = DBA::selectFirst('conversation', ['source'], $condition);
866                         if (DBA::isResult($conversation)) {
867                                 $data = json_decode($conversation['source'], true);
868                                 if (!empty($data)) {
869                                         return $data;
870                                 }
871                         }
872
873                         $type = self::getTypeOfItem($item);
874                 }
875
876                 if (!$object_mode) {
877                         $data = ['@context' => ActivityPub::CONTEXT];
878
879                         if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
880                                 $type = 'Undo';
881                         } elseif ($item['deleted']) {
882                                 $type = 'Delete';
883                         }
884                 } else {
885                         $data = [];
886                 }
887
888                 $data['id'] = $item['uri'] . '#' . $type;
889                 $data['type'] = $type;
890
891                 if (Item::isForumPost($item) && ($type != 'Announce')) {
892                         $data['actor'] = $item['author-link'];
893                 } else {
894                         $data['actor'] = $item['owner-link'];
895                 }
896
897                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
898
899                 $data['instrument'] = self::getService();
900
901                 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
902
903                 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
904                         $data['object'] = self::createNote($item);
905                 } elseif ($data['type'] == 'Add') {
906                         $data = self::createAddTag($item, $data);
907                 } elseif ($data['type'] == 'Announce') {
908                         $data = self::createAnnounce($item, $data);
909                 } elseif ($data['type'] == 'Follow') {
910                         $data['object'] = $item['parent-uri'];
911                 } elseif ($data['type'] == 'Undo') {
912                         $data['object'] = self::createActivityFromItem($item_id, true);
913                 } else {
914                         $data['diaspora:guid'] = $item['guid'];
915                         if (!empty($item['signed_text'])) {
916                                 $data['diaspora:like'] = $item['signed_text'];
917                         }
918                         $data['object'] = $item['thr-parent'];
919                 }
920
921                 if (!empty($item['contact-uid'])) {
922                         $uid = $item['contact-uid'];
923                 } else {
924                         $uid = $item['uid'];
925                 }
926
927                 $owner = User::getOwnerDataById($uid);
928
929                 if (!$object_mode && !empty($owner)) {
930                         return LDSignature::sign($data, $owner);
931                 } else {
932                         return $data;
933                 }
934
935                 /// @todo Create "conversation" entry
936         }
937
938         /**
939          * Creates a location entry for a given item array
940          *
941          * @param array $item
942          *
943          * @return array with location array
944          */
945         private static function createLocation($item)
946         {
947                 $location = ['type' => 'Place'];
948
949                 if (!empty($item['location'])) {
950                         $location['name'] = $item['location'];
951                 }
952
953                 $coord = [];
954
955                 if (empty($item['coord'])) {
956                         $coord = Map::getCoordinates($item['location']);
957                 } else {
958                         $coords = explode(' ', $item['coord']);
959                         if (count($coords) == 2) {
960                                 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
961                         }
962                 }
963
964                 if (!empty($coord['lat']) && !empty($coord['lon'])) {
965                         $location['latitude'] = $coord['lat'];
966                         $location['longitude'] = $coord['lon'];
967                 }
968
969                 return $location;
970         }
971
972         /**
973          * Returns a tag array for a given item array
974          *
975          * @param array $item
976          *
977          * @return array of tags
978          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
979          */
980         private static function createTagList($item)
981         {
982                 $tags = [];
983
984                 $terms = Term::tagArrayFromItemId($item['id'], [Term::HASHTAG, Term::MENTION, Term::IMPLICIT_MENTION]);
985                 foreach ($terms as $term) {
986                         if ($term['type'] == Term::HASHTAG) {
987                                 $url = System::baseUrl() . '/search?tag=' . urlencode($term['term']);
988                                 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
989                         } elseif ($term['type'] == Term::MENTION || $term['type'] == Term::IMPLICIT_MENTION) {
990                                 $contact = Contact::getDetailsByURL($term['url']);
991                                 if (!empty($contact['addr'])) {
992                                         $mention = '@' . $contact['addr'];
993                                 } else {
994                                         $mention = '@' . $term['url'];
995                                 }
996
997                                 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
998                         }
999                 }
1000
1001                 $announce = self::getAnnounceArray($item);
1002                 // Mention the original author upon commented reshares
1003                 if (!empty($announce['comment'])) {
1004                         $tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']];
1005                 }
1006
1007                 return $tags;
1008         }
1009
1010         /**
1011          * Adds attachment data to the JSON document
1012          *
1013          * @param array  $item Data of the item that is to be posted
1014          * @param string $type Object type
1015          *
1016          * @return array with attachment data
1017          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1018          */
1019         private static function createAttachmentList($item, $type)
1020         {
1021                 $attachments = [];
1022
1023                 // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
1024                 // It will be reactivated, once this cleared.
1025                 /*
1026                 $attach_data = BBCode::getAttachmentData($item['body']);
1027                 if (!empty($attach_data['url'])) {
1028                         $attachment = ['type' => 'Page',
1029                                 'mediaType' => 'text/html',
1030                                 'url' => $attach_data['url']];
1031
1032                         if (!empty($attach_data['title'])) {
1033                                 $attachment['name'] = $attach_data['title'];
1034                         }
1035
1036                         if (!empty($attach_data['description'])) {
1037                                 $attachment['summary'] = $attach_data['description'];
1038                         }
1039
1040                         if (!empty($attach_data['image'])) {
1041                                 $imgdata = Images::getInfoFromURLCached($attach_data['image']);
1042                                 if ($imgdata) {
1043                                         $attachment['icon'] = ['type' => 'Image',
1044                                                 'mediaType' => $imgdata['mime'],
1045                                                 'width' => $imgdata[0],
1046                                                 'height' => $imgdata[1],
1047                                                 'url' => $attach_data['image']];
1048                                 }
1049                         }
1050
1051                         $attachments[] = $attachment;
1052                 }
1053                 */
1054                 $arr = explode('[/attach],', $item['attach']);
1055                 if (count($arr)) {
1056                         foreach ($arr as $r) {
1057                                 $matches = false;
1058                                 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1059                                 if ($cnt) {
1060                                         $attributes = ['type' => 'Document',
1061                                                         'mediaType' => $matches[3],
1062                                                         'url' => $matches[1],
1063                                                         'name' => null];
1064
1065                                         if (trim($matches[4]) != '') {
1066                                                 $attributes['name'] = trim($matches[4]);
1067                                         }
1068
1069                                         $attachments[] = $attributes;
1070                                 }
1071                         }
1072                 }
1073
1074                 if ($type != 'Note') {
1075                         return $attachments;
1076                 }
1077
1078                 // Simplify image codes
1079                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
1080
1081                 // Grab all pictures without alternative descriptions and create attachments out of them
1082                 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
1083                         foreach ($pictures[1] as $picture) {
1084                                 $imgdata = Images::getInfoFromURLCached($picture);
1085                                 if ($imgdata) {
1086                                         $attachments[] = ['type' => 'Document',
1087                                                 'mediaType' => $imgdata['mime'],
1088                                                 'url' => $picture,
1089                                                 'name' => null];
1090                                 }
1091                         }
1092                 }
1093
1094                 // Grab all pictures with alternative description and create attachments out of them
1095                 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
1096                         foreach ($pictures as $picture) {
1097                                 $imgdata = Images::getInfoFromURLCached($picture[1]);
1098                                 if ($imgdata) {
1099                                         $attachments[] = ['type' => 'Document',
1100                                                 'mediaType' => $imgdata['mime'],
1101                                                 'url' => $picture[1],
1102                                                 'name' => $picture[2]];
1103                                 }
1104                         }
1105                 }
1106
1107                 return $attachments;
1108         }
1109
1110         /**
1111          * @brief Callback function to replace a Friendica style mention in a mention that is used on AP
1112          *
1113          * @param array $match Matching values for the callback
1114          * @return string Replaced mention
1115          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1116          */
1117         private static function mentionCallback($match)
1118         {
1119                 if (empty($match[1])) {
1120                         return '';
1121                 }
1122
1123                 $data = Contact::getDetailsByURL($match[1]);
1124                 if (empty($data['nick'])) {
1125                         return $match[0];
1126                 }
1127
1128                 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
1129         }
1130
1131         /**
1132          * Remove image elements since they are added as attachment
1133          *
1134          * @param string $body
1135          *
1136          * @return string with removed images
1137          */
1138         private static function removePictures($body)
1139         {
1140                 // Simplify image codes
1141                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
1142                 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $body);
1143
1144                 // Now remove local links
1145                 $body = preg_replace_callback(
1146                         '/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
1147                         function ($match) {
1148                                 // We remove the link when it is a link to a local photo page
1149                                 if (Photo::isLocalPage($match[1])) {
1150                                         return '';
1151                                 }
1152                                 // otherwise we just return the link
1153                                 return '[url]' . $match[1] . '[/url]';
1154                         },
1155                         $body
1156                 );
1157
1158                 // Remove all pictures
1159                 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $body);
1160
1161                 return $body;
1162         }
1163
1164         /**
1165          * Fetches the "context" value for a givem item array from the "conversation" table
1166          *
1167          * @param array $item
1168          *
1169          * @return string with context url
1170          * @throws \Exception
1171          */
1172         private static function fetchContextURLForItem($item)
1173         {
1174                 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
1175                 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
1176                         $context_uri = $conversation['conversation-href'];
1177                 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
1178                         $context_uri = $conversation['conversation-uri'];
1179                 } else {
1180                         $context_uri = $item['parent-uri'] . '#context';
1181                 }
1182                 return $context_uri;
1183         }
1184
1185         /**
1186          * Returns if the post contains sensitive content ("nsfw")
1187          *
1188          * @param integer $item_id
1189          *
1190          * @return boolean
1191          * @throws \Exception
1192          */
1193         private static function isSensitive($item_id)
1194         {
1195                 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
1196                 return DBA::exists('term', $condition);
1197         }
1198
1199         /**
1200          * Creates event data
1201          *
1202          * @param array $item
1203          *
1204          * @return array with the event data
1205          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1206          */
1207         public static function createEvent($item)
1208         {
1209                 $event = [];
1210                 $event['name'] = $item['event-summary'];
1211                 $event['content'] = BBCode::convert($item['event-desc'], false, 9);
1212                 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
1213
1214                 if (!$item['event-nofinish']) {
1215                         $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
1216                 }
1217
1218                 if (!empty($item['event-location'])) {
1219                         $item['location'] = $item['event-location'];
1220                         $event['location'] = self::createLocation($item);
1221                 }
1222
1223                 return $event;
1224         }
1225
1226         /**
1227          * Creates a note/article object array
1228          *
1229          * @param array $item
1230          *
1231          * @return array with the object data
1232          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1233          * @throws \ImagickException
1234          */
1235         public static function createNote($item)
1236         {
1237                 if (empty($item)) {
1238                         return [];
1239                 }
1240
1241                 if ($item['event-type'] == 'event') {
1242                         $type = 'Event';
1243                 } elseif (!empty($item['title'])) {
1244                         $type = 'Article';
1245                 } else {
1246                         $type = 'Note';
1247                 }
1248
1249                 if ($item['deleted']) {
1250                         $type = 'Tombstone';
1251                 }
1252
1253                 $data = [];
1254                 $data['id'] = $item['uri'];
1255                 $data['type'] = $type;
1256
1257                 if ($item['deleted']) {
1258                         return $data;
1259                 }
1260
1261                 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1262
1263                 if ($item['uri'] != $item['thr-parent']) {
1264                         $data['inReplyTo'] = $item['thr-parent'];
1265                 } else {
1266                         $data['inReplyTo'] = null;
1267                 }
1268
1269                 $data['diaspora:guid'] = $item['guid'];
1270                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1271
1272                 if ($item['created'] != $item['edited']) {
1273                         $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1274                 }
1275
1276                 $data['url'] = $item['plink'];
1277                 $data['attributedTo'] = $item['author-link'];
1278                 $data['sensitive'] = self::isSensitive($item['id']);
1279                 $data['context'] = self::fetchContextURLForItem($item);
1280
1281                 if (!empty($item['title'])) {
1282                         $data['name'] = BBCode::toPlaintext($item['title'], false);
1283                 }
1284
1285                 $permission_block = self::createPermissionBlockForItem($item, false);
1286
1287                 $body = $item['body'];
1288
1289                 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1290                         $body = self::prependMentions($body, $permission_block);
1291                 }
1292
1293                 if ($type == 'Note') {
1294                         $body = self::removePictures($body);
1295                 } elseif (($type == 'Article') && empty($data['summary'])) {
1296                         $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
1297                 }
1298
1299                 if ($type == 'Event') {
1300                         $data = array_merge($data, self::createEvent($item));
1301                 } else {
1302                         $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1303                         $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1304
1305                         $data['content'] = BBCode::convert($body, false, 9);
1306                 }
1307
1308                 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1309                 $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
1310                 $richbody = BBCode::removeAttachment($richbody);
1311
1312                 $data['contentMap']['text/html'] = BBCode::convert($richbody, false);
1313                 $data['contentMap']['text/markdown'] = BBCode::toMarkdown($item["body"]);
1314
1315                 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1316
1317                 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1318                         $data['diaspora:comment'] = $item['signed_text'];
1319                 }
1320
1321                 $data['attachment'] = self::createAttachmentList($item, $type);
1322                 $data['tag'] = self::createTagList($item);
1323
1324                 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1325                         $data['location'] = self::createLocation($item);
1326                 }
1327
1328                 if (!empty($item['app'])) {
1329                         $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1330                 }
1331
1332                 $data = array_merge($data, $permission_block);
1333
1334                 return $data;
1335         }
1336
1337         /**
1338          * Creates an an "add tag" entry
1339          *
1340          * @param array $item
1341          * @param array $data activity data
1342          *
1343          * @return array with activity data for adding tags
1344          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1345          * @throws \ImagickException
1346          */
1347         private static function createAddTag($item, $data)
1348         {
1349                 $object = XML::parseString($item['object'], false);
1350                 $target = XML::parseString($item["target"], false);
1351
1352                 $data['diaspora:guid'] = $item['guid'];
1353                 $data['actor'] = $item['author-link'];
1354                 $data['target'] = (string)$target->id;
1355                 $data['summary'] = BBCode::toPlaintext($item['body']);
1356                 $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1357
1358                 return $data;
1359         }
1360
1361         /**
1362          * Creates an announce object entry
1363          *
1364          * @param array $item
1365          * @param array $data activity data
1366          *
1367          * @return array with activity data
1368          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1369          * @throws \ImagickException
1370          */
1371         private static function createAnnounce($item, $data)
1372         {
1373                 $orig_body = $item['body'];
1374                 $announce = self::getAnnounceArray($item);
1375                 if (empty($announce)) {
1376                         $data['type'] = 'Create';
1377                         $data['object'] = self::createNote($item);
1378                         return $data;
1379                 }
1380
1381                 if (empty($announce['comment'])) {
1382                         // Pure announce, without a quote
1383                         $data['type'] = 'Announce';
1384                         $data['object'] = $announce['object']['uri'];
1385                         return $data;
1386                 }
1387
1388                 // Quote
1389                 $data['type'] = 'Create';
1390                 $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
1391                 $data['object'] = self::createNote($item);
1392
1393                 /// @todo Finally descide how to implement this in AP. This is a possible way:
1394                 $data['object']['attachment'][] = self::createNote($announce['object']);
1395
1396                 $data['object']['source']['content'] = $orig_body;
1397                 return $data;
1398         }
1399
1400         /**
1401          * Return announce related data if the item is an annunce
1402          *
1403          * @param array $item
1404          *
1405          * @return array
1406          */
1407         public static function getAnnounceArray($item)
1408         {
1409                 $reshared = Item::getShareArray($item);
1410                 if (empty($reshared['guid'])) {
1411                         return [];
1412                 }
1413
1414                 $reshared_item = Item::selectFirst([], ['guid' => $reshared['guid']]);
1415                 if (!DBA::isResult($reshared_item)) {
1416                         return [];
1417                 }
1418
1419                 if (!in_array($reshared_item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
1420                         return [];
1421                 }
1422
1423                 $profile = APContact::getByURL($reshared_item['author-link'], false);
1424                 if (empty($profile)) {
1425                         return [];
1426                 }
1427
1428                 return ['object' => $reshared_item, 'actor' => $profile, 'comment' => $reshared['comment']];
1429         }
1430
1431         /**
1432          * Checks if the provided item array is an announce
1433          *
1434          * @param array $item
1435          *
1436          * @return boolean
1437          */
1438         public static function isAnnounce($item)
1439         {
1440                 $announce = self::getAnnounceArray($item);
1441                 if (empty($announce)) {
1442                         return false;
1443                 }
1444
1445                 return empty($announce['comment']);
1446         }
1447
1448         /**
1449          * Creates an activity id for a given contact id
1450          *
1451          * @param integer $cid Contact ID of target
1452          *
1453          * @return bool|string activity id
1454          */
1455         public static function activityIDFromContact($cid)
1456         {
1457                 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1458                 if (!DBA::isResult($contact)) {
1459                         return false;
1460                 }
1461
1462                 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1463                 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1464                 return System::baseUrl() . '/activity/' . $uuid;
1465         }
1466
1467         /**
1468          * Transmits a contact suggestion to a given inbox
1469          *
1470          * @param integer $uid           User ID
1471          * @param string  $inbox         Target inbox
1472          * @param integer $suggestion_id Suggestion ID
1473          *
1474          * @return boolean was the transmission successful?
1475          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1476          */
1477         public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1478         {
1479                 $owner = User::getOwnerDataById($uid);
1480
1481                 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
1482
1483                 $data = ['@context' => ActivityPub::CONTEXT,
1484                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1485                         'type' => 'Announce',
1486                         'actor' => $owner['url'],
1487                         'object' => $suggestion['url'],
1488                         'content' => $suggestion['note'],
1489                         'instrument' => self::getService(),
1490                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1491                         'cc' => []];
1492
1493                 $signed = LDSignature::sign($data, $owner);
1494
1495                 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1496                 return HTTPSignature::transmit($signed, $inbox, $uid);
1497         }
1498
1499         /**
1500          * Transmits a profile relocation to a given inbox
1501          *
1502          * @param integer $uid   User ID
1503          * @param string  $inbox Target inbox
1504          *
1505          * @return boolean was the transmission successful?
1506          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1507          */
1508         public static function sendProfileRelocation($uid, $inbox)
1509         {
1510                 $owner = User::getOwnerDataById($uid);
1511
1512                 $data = ['@context' => ActivityPub::CONTEXT,
1513                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1514                         'type' => 'dfrn:relocate',
1515                         'actor' => $owner['url'],
1516                         'object' => $owner['url'],
1517                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1518                         'instrument' => self::getService(),
1519                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1520                         'cc' => []];
1521
1522                 $signed = LDSignature::sign($data, $owner);
1523
1524                 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1525                 return HTTPSignature::transmit($signed, $inbox, $uid);
1526         }
1527
1528         /**
1529          * Transmits a profile deletion to a given inbox
1530          *
1531          * @param integer $uid   User ID
1532          * @param string  $inbox Target inbox
1533          *
1534          * @return boolean was the transmission successful?
1535          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1536          */
1537         public static function sendProfileDeletion($uid, $inbox)
1538         {
1539                 $owner = User::getOwnerDataById($uid);
1540
1541                 if (empty($owner)) {
1542                         Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
1543                         return false;
1544                 }
1545
1546                 if (empty($owner['uprvkey'])) {
1547                         Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
1548                         return false;
1549                 }
1550
1551                 $data = ['@context' => ActivityPub::CONTEXT,
1552                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1553                         'type' => 'Delete',
1554                         'actor' => $owner['url'],
1555                         'object' => $owner['url'],
1556                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1557                         'instrument' => self::getService(),
1558                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1559                         'cc' => []];
1560
1561                 $signed = LDSignature::sign($data, $owner);
1562
1563                 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1564                 return HTTPSignature::transmit($signed, $inbox, $uid);
1565         }
1566
1567         /**
1568          * Transmits a profile change to a given inbox
1569          *
1570          * @param integer $uid   User ID
1571          * @param string  $inbox Target inbox
1572          *
1573          * @return boolean was the transmission successful?
1574          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1575          * @throws \ImagickException
1576          */
1577         public static function sendProfileUpdate($uid, $inbox)
1578         {
1579                 $owner = User::getOwnerDataById($uid);
1580                 $profile = APContact::getByURL($owner['url']);
1581
1582                 $data = ['@context' => ActivityPub::CONTEXT,
1583                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1584                         'type' => 'Update',
1585                         'actor' => $owner['url'],
1586                         'object' => self::getProfile($uid),
1587                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1588                         'instrument' => self::getService(),
1589                         'to' => [$profile['followers']],
1590                         'cc' => []];
1591
1592                 $signed = LDSignature::sign($data, $owner);
1593
1594                 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1595                 return HTTPSignature::transmit($signed, $inbox, $uid);
1596         }
1597
1598         /**
1599          * Transmits a given activity to a target
1600          *
1601          * @param string  $activity Type name
1602          * @param string  $target   Target profile
1603          * @param integer $uid      User ID
1604          * @return bool
1605          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1606          * @throws \ImagickException
1607          * @throws \Exception
1608          */
1609         public static function sendActivity($activity, $target, $uid, $id = '')
1610         {
1611                 $profile = APContact::getByURL($target);
1612                 if (empty($profile['inbox'])) {
1613                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1614                         return;
1615                 }
1616
1617                 $owner = User::getOwnerDataById($uid);
1618
1619                 if (empty($id)) {
1620                         $id = System::baseUrl() . '/activity/' . System::createGUID();
1621                 }
1622
1623                 $data = ['@context' => ActivityPub::CONTEXT,
1624                         'id' => $id,
1625                         'type' => $activity,
1626                         'actor' => $owner['url'],
1627                         'object' => $profile['url'],
1628                         'instrument' => self::getService(),
1629                         'to' => [$profile['url']]];
1630
1631                 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1632
1633                 $signed = LDSignature::sign($data, $owner);
1634                 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1635         }
1636
1637         /**
1638          * Transmits a "follow object" activity to a target
1639          * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
1640          *
1641          * @param string  $object Object URL
1642          * @param string  $target Target profile
1643          * @param integer $uid    User ID
1644          * @return bool
1645          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1646          * @throws \ImagickException
1647          * @throws \Exception
1648          */
1649         public static function sendFollowObject($object, $target, $uid = 0)
1650         {
1651                 $profile = APContact::getByURL($target);
1652                 if (empty($profile['inbox'])) {
1653                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1654                         return;
1655                 }
1656
1657                 if (empty($uid)) {
1658                         // Fetch the list of administrators
1659                         $admin_mail = explode(',', str_replace(' ', '', Config::get('config', 'admin_email')));
1660
1661                         // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
1662                         $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
1663                         $first_user = DBA::selectFirst('user', ['uid'], $condition);
1664                         $uid = $first_user['uid'];
1665                 }
1666
1667                 $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
1668                         'author-id' => Contact::getPublicIdByUserId($uid)];
1669                 if (Item::exists($condition)) {
1670                         Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
1671                         return false;
1672                 }
1673
1674                 $owner = User::getOwnerDataById($uid);
1675
1676                 $data = ['@context' => ActivityPub::CONTEXT,
1677                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1678                         'type' => 'Follow',
1679                         'actor' => $owner['url'],
1680                         'object' => $object,
1681                         'instrument' => self::getService(),
1682                         'to' => [$profile['url']]];
1683
1684                 Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1685
1686                 $signed = LDSignature::sign($data, $owner);
1687                 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1688         }
1689
1690         /**
1691          * Transmit a message that the contact request had been accepted
1692          *
1693          * @param string  $target Target profile
1694          * @param         $id
1695          * @param integer $uid    User ID
1696          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1697          * @throws \ImagickException
1698          */
1699         public static function sendContactAccept($target, $id, $uid)
1700         {
1701                 $profile = APContact::getByURL($target);
1702                 if (empty($profile['inbox'])) {
1703                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1704                         return;
1705                 }
1706
1707                 $owner = User::getOwnerDataById($uid);
1708                 $data = ['@context' => ActivityPub::CONTEXT,
1709                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1710                         'type' => 'Accept',
1711                         'actor' => $owner['url'],
1712                         'object' => [
1713                                 'id' => (string)$id,
1714                                 'type' => 'Follow',
1715                                 'actor' => $profile['url'],
1716                                 'object' => $owner['url']
1717                         ],
1718                         'instrument' => self::getService(),
1719                         'to' => [$profile['url']]];
1720
1721                 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1722
1723                 $signed = LDSignature::sign($data, $owner);
1724                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1725         }
1726
1727         /**
1728          * Reject a contact request or terminates the contact relation
1729          *
1730          * @param string  $target Target profile
1731          * @param         $id
1732          * @param integer $uid    User ID
1733          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1734          * @throws \ImagickException
1735          */
1736         public static function sendContactReject($target, $id, $uid)
1737         {
1738                 $profile = APContact::getByURL($target);
1739                 if (empty($profile['inbox'])) {
1740                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1741                         return;
1742                 }
1743
1744                 $owner = User::getOwnerDataById($uid);
1745                 $data = ['@context' => ActivityPub::CONTEXT,
1746                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1747                         'type' => 'Reject',
1748                         'actor' => $owner['url'],
1749                         'object' => [
1750                                 'id' => (string)$id,
1751                                 'type' => 'Follow',
1752                                 'actor' => $profile['url'],
1753                                 'object' => $owner['url']
1754                         ],
1755                         'instrument' => self::getService(),
1756                         'to' => [$profile['url']]];
1757
1758                 Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1759
1760                 $signed = LDSignature::sign($data, $owner);
1761                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1762         }
1763
1764         /**
1765          * Transmits a message that we don't want to follow this contact anymore
1766          *
1767          * @param string  $target Target profile
1768          * @param integer $uid    User ID
1769          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1770          * @throws \ImagickException
1771          * @throws \Exception
1772          */
1773         public static function sendContactUndo($target, $cid, $uid)
1774         {
1775                 $profile = APContact::getByURL($target);
1776                 if (empty($profile['inbox'])) {
1777                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1778                         return;
1779                 }
1780
1781                 $object_id = self::activityIDFromContact($cid);
1782                 if (empty($object_id)) {
1783                         return;
1784                 }
1785
1786                 $id = System::baseUrl() . '/activity/' . System::createGUID();
1787
1788                 $owner = User::getOwnerDataById($uid);
1789                 $data = ['@context' => ActivityPub::CONTEXT,
1790                         'id' => $id,
1791                         'type' => 'Undo',
1792                         'actor' => $owner['url'],
1793                         'object' => ['id' => $object_id, 'type' => 'Follow',
1794                                 'actor' => $owner['url'],
1795                                 'object' => $profile['url']],
1796                         'instrument' => self::getService(),
1797                         'to' => [$profile['url']]];
1798
1799                 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1800
1801                 $signed = LDSignature::sign($data, $owner);
1802                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1803         }
1804
1805         private static function prependMentions($body, array $permission_block)
1806         {
1807                 if (Config::get('system', 'disable_implicit_mentions')) {
1808                         return $body;
1809                 }
1810
1811                 $mentions = [];
1812
1813                 foreach ($permission_block['to'] as $profile_url) {
1814                         $profile = Contact::getDetailsByURL($profile_url);
1815                         if (!empty($profile['addr'])
1816                                 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
1817                                 && !strstr($body, $profile['addr'])
1818                                 && !strstr($body, $profile_url)
1819                         ) {
1820                                 $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]';
1821                         }
1822                 }
1823
1824                 $mentions[] = $body;
1825
1826                 return implode(' ', $mentions);
1827         }
1828 }