]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Transmitter.php
Use the post language for the language detection / config for quality
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Protocol\ActivityPub;
23
24 use Friendica\App;
25 use Friendica\Content\Feature;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Core\Cache\Enum\Duration;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Core\System;
31 use Friendica\Database\DBA;
32 use Friendica\DI;
33 use Friendica\Model\APContact;
34 use Friendica\Model\Contact;
35 use Friendica\Model\GServer;
36 use Friendica\Model\Item;
37 use Friendica\Model\Photo;
38 use Friendica\Model\Post;
39 use Friendica\Model\Tag;
40 use Friendica\Model\User;
41 use Friendica\Network\HTTPException;
42 use Friendica\Protocol\Activity;
43 use Friendica\Protocol\ActivityPub;
44 use Friendica\Protocol\Relay;
45 use Friendica\Util\DateTimeFormat;
46 use Friendica\Util\HTTPSignature;
47 use Friendica\Util\LDSignature;
48 use Friendica\Util\Map;
49 use Friendica\Util\Network;
50 use Friendica\Util\Strings;
51 use Friendica\Util\XML;
52
53 /**
54  * ActivityPub Transmitter Protocol class
55  *
56  * To-Do:
57  * @todo Undo Announce
58  */
59 class Transmitter
60 {
61         const CACHEKEY_FEATURED = 'transmitter:getFeatured:';
62         const CACHEKEY_CONTACTS = 'transmitter:getContacts:';
63
64         /**
65          * Add relay servers to the list of inboxes
66          *
67          * @param array $inboxes
68          * @return array inboxes with added relay servers
69          */
70         public static function addRelayServerInboxes(array $inboxes = []): array
71         {
72                 foreach (Relay::getList(['inbox']) as $contact) {
73                         $inboxes[$contact['inbox']] = $contact['inbox'];
74                 }
75
76                 return $inboxes;
77         }
78
79         /**
80          * Add relay servers to the list of inboxes
81          *
82          * @param array $inboxes
83          * @return array inboxes with added relay servers
84          */
85         public static function addRelayServerInboxesForItem(int $item_id, array $inboxes = []): array
86         {
87                 $item = Post::selectFirst(['uid'], ['id' => $item_id]);
88                 if (empty($item)) {
89                         return $inboxes;
90                 }
91
92                 $relays = Relay::getDirectRelayList($item_id);
93                 if (empty($relays)) {
94                         return $inboxes;
95                 }
96
97                 foreach ($relays as $relay) {
98                         $contact = Contact::getByURLForUser($relay['url'], $item['uid'], false, ['id']);
99                         $inboxes[$relay['batch']][] = $contact['id'] ?? 0;
100                 }
101                 return $inboxes;
102         }
103
104         /**
105          * Subscribe to a relay and updates contact on success
106          *
107          * @param string $url Subscribe actor url
108          * @return bool success
109          */
110         public static function sendRelayFollow(string $url): bool
111         {
112                 $contact = Contact::getByURL($url);
113                 if (empty($contact)) {
114                         return false;
115                 }
116
117                 $activity_id = self::activityIDFromContact($contact['id']);
118                 $success = self::sendActivity('Follow', $url, 0, $activity_id);
119                 if ($success) {
120                         Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]);
121                 }
122
123                 return $success;
124         }
125
126         /**
127          * Unsubscribe from a relay and updates contact on success or forced
128          *
129          * @param string $url   Subscribe actor url
130          * @param bool   $force Set the relay status as non follower even if unsubscribe hadn't worked
131          * @return bool success
132          */
133         public static function sendRelayUndoFollow(string $url, bool $force = false): bool
134         {
135                 $contact = Contact::getByURL($url);
136                 if (empty($contact)) {
137                         return false;
138                 }
139
140                 $success = self::sendContactUndo($url, $contact['id'], User::getSystemAccount());
141
142                 if ($success || $force) {
143                         Contact::update(['rel' => Contact::NOTHING], ['id' => $contact['id']]);
144                 }
145
146                 return $success;
147         }
148
149         /**
150          * Collects a list of contacts of the given owner
151          *
152          * @param array   $owner     Owner array
153          * @param array   $rel       The relevant value(s) contact.rel should match
154          * @param string  $module    The name of the relevant AP endpoint module (followers|following)
155          * @param integer $page      Page number
156          * @param string  $requester URL of the requester
157          * @param boolean $nocache   Wether to bypass caching
158          * @return array of owners
159          * @throws \Exception
160          */
161         public static function getContacts(array $owner, array $rel, string $module, int $page = null, string $requester = null, bool $nocache = false): array
162         {
163                 if (empty($page)) {
164                         $cachekey = self::CACHEKEY_CONTACTS . $module . ':'. $owner['uid'];
165                         $result = DI::cache()->get($cachekey);
166                         if (!$nocache && !is_null($result)) {
167                                 return $result;
168                         }
169                 }
170
171                 $parameters = [
172                         'rel' => $rel,
173                         'uid' => $owner['uid'],
174                         'self' => false,
175                         'deleted' => false,
176                         'hidden' => false,
177                         'archive' => false,
178                         'pending' => false,
179                         'blocked' => false,
180                 ];
181
182                 $condition = DBA::mergeConditions($parameters, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
183
184                 $total = DBA::count('contact', $condition);
185
186                 $modulePath = '/' . $module . '/';
187
188                 $data = ['@context' => ActivityPub::CONTEXT];
189                 $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
190                 $data['type'] = 'OrderedCollection';
191                 $data['totalItems'] = $total;
192
193                 if (!empty($page)) {
194                         $data['id'] .= '?' . http_build_query(['page' => $page]);
195                 }
196
197                 // When we hide our friends we will only show the pure number but don't allow more.
198                 $show_contacts = ActivityPub::isAcceptedRequester($owner['uid']) && empty($owner['hide-friends']);
199
200                 // Allow fetching the contact list when the requester is part of the list.
201                 if (($owner['page-flags'] == User::PAGE_FLAGS_PRVGROUP) && !empty($requester)) {
202                         $show_contacts = DBA::exists('contact', ['nurl' => Strings::normaliseLink($requester), 'uid' => $owner['uid'], 'blocked' => false]);
203                 }
204
205                 if (!$show_contacts) {
206                         if (!empty($cachekey)) {
207                                 DI::cache()->set($cachekey, $data, Duration::DAY);
208                         }
209
210                         return $data;
211                 }
212
213                 if (empty($page)) {
214                         $data['first'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=1';
215                 } else {
216                         $data['type'] = 'OrderedCollectionPage';
217                         $list = [];
218
219                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
220                         while ($contact = DBA::fetch($contacts)) {
221                                 $list[] = $contact['url'];
222                         }
223                         DBA::close($contacts);
224
225                         if (count($list) == 100) {
226                                 $data['next'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=' . ($page + 1);
227                         }
228
229                         $data['partOf'] = DI::baseUrl() . $modulePath . $owner['nickname'];
230
231                         $data['orderedItems'] = $list;
232                 }
233
234                 if (!empty($cachekey)) {
235                         DI::cache()->set($cachekey, $data, Duration::DAY);
236                 }
237
238                 return $data;
239         }
240
241         /**
242          * Public posts for the given owner
243          *
244          * @param array   $owner   Owner array
245          * @param integer $page    Page number
246          * @param boolean $nocache Wether to bypass caching
247          *
248          * @return array of posts
249          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
250          * @throws \ImagickException
251          */
252         public static function getFeatured(array $owner, int $page = null, bool $nocache = false): array
253         {
254                 if (empty($page)) {
255                         $cachekey = self::CACHEKEY_FEATURED . $owner['uid'];
256                         $result = DI::cache()->get($cachekey);
257                         if (!$nocache && !is_null($result)) {
258                                 return $result;
259                         }
260                 }
261
262                 $owner_cid = Contact::getIdForURL($owner['url'], 0, false);
263
264                 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
265                         $owner_cid, Post\Collection::FEATURED];
266
267                 $condition = DBA::mergeConditions($condition, [
268                         'uid'           => $owner['uid'],
269                         'author-id'      => $owner_cid,
270                         'private'        => [Item::PUBLIC, Item::UNLISTED],
271                         'gravity'        => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT],
272                         'network'        => Protocol::FEDERATED,
273                         'parent-network' => Protocol::FEDERATED,
274                         'origin'         => true,
275                         'deleted'        => false,
276                         'visible'        => true
277                 ]);
278
279                 $count = Post::count($condition);
280
281                 $data = ['@context' => ActivityPub::CONTEXT];
282                 $data['id'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
283                 $data['type'] = 'OrderedCollection';
284                 $data['totalItems'] = $count;
285
286                 if (!empty($page)) {
287                         $data['id'] .= '?' . http_build_query(['page' => $page]);
288                 }
289
290                 if (empty($page)) {
291                         $items = Post::select(['id'], $condition, ['limit' => 20, 'order' => ['created' => true]]);
292                 } else {
293                         $data['type'] = 'OrderedCollectionPage';
294                         $items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
295                 }
296                 $list = [];
297
298                 while ($item = Post::fetch($items)) {
299                         $activity = self::createActivityFromItem($item['id'], true);
300                         if (!empty($activity)) {
301                                 $list[] = $activity;
302                         }
303                 }
304                 DBA::close($items);
305
306                 if (count($list) == 20) {
307                         $data['next'] = DI::baseUrl() . '/featured/' . $owner['nickname'] . '?page=' . ($page + 1);
308                 }
309
310                 if (!empty($page)) {
311                         $data['partOf'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
312                 }
313
314                 $data['orderedItems'] = $list;
315
316                 if (!empty($cachekey)) {
317                         DI::cache()->set($cachekey, $data, Duration::DAY);
318                 }
319
320                 return $data;
321         }
322
323         /**
324          * Return the service array containing information the used software and it's url
325          *
326          * @return array with service data
327          */
328         public static function getService(): array
329         {
330                 return [
331                         'type' => 'Service',
332                         'name' =>  App::PLATFORM . " '" . App::CODENAME . "' " . App::VERSION . '-' . DB_UPDATE_VERSION,
333                         'url' => (string)DI::baseUrl()
334                 ];
335         }
336
337         /**
338          * Return the ActivityPub profile of the given user
339          *
340          * @param int  $uid  User ID
341          * @param bool $full If not full, only the basic information is returned
342          * @return array with profile data
343          * @throws HTTPException\NotFoundException
344          * @throws HTTPException\InternalServerErrorException
345          */
346         public static function getProfile(int $uid, bool $full = true): array
347         {
348                 $owner = User::getOwnerDataById($uid);
349                 if (!isset($owner['id'])) {
350                         DI::logger()->error('Unable to find owner data for uid', ['uid' => $uid]);
351                         throw new HTTPException\NotFoundException('User not found.');
352                 }
353
354                 $data = ['@context' => ActivityPub::CONTEXT];
355                 $data['id'] = $owner['url'];
356
357                 if (!empty($owner['guid'])) {
358                         $data['diaspora:guid'] = $owner['guid'];
359                 }
360
361                 $data['type'] = ActivityPub::ACCOUNT_TYPES[$owner['account-type']];
362
363                 if ($uid != 0) {
364                         $data['following'] = DI::baseUrl() . '/following/' . $owner['nick'];
365                         $data['followers'] = DI::baseUrl() . '/followers/' . $owner['nick'];
366                         $data['inbox']     = DI::baseUrl() . '/inbox/' . $owner['nick'];
367                         $data['outbox']    = DI::baseUrl() . '/outbox/' . $owner['nick'];
368                         $data['featured']  = DI::baseUrl() . '/featured/' . $owner['nick'];
369                 } else {
370                         $data['inbox'] = DI::baseUrl() . '/friendica/inbox';
371                 }
372
373                 $data['preferredUsername'] = $owner['nick'];
374                 $data['name'] = $owner['name'];
375
376                 if (!$full && !empty($owner['country-name'] . $owner['region'] . $owner['locality'])) {
377                         $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $owner['country-name'],
378                                 'vcard:region' => $owner['region'], 'vcard:locality' => $owner['locality']];
379                 }
380
381                 if ($full && !empty($owner['about'])) {
382                         $data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
383                 }
384
385                 if ($full && (!empty($owner['xmpp']) || !empty($owner['matrix']))) {
386                         $data['vcard:hasInstantMessage'] = [];
387
388                         if (!empty($owner['xmpp'])) {
389                                 $data['vcard:hasInstantMessage'][] = 'xmpp:' . $owner['xmpp'];
390                         }
391                         if (!empty($owner['matrix'])) {
392                                 $data['vcard:hasInstantMessage'][] = 'matrix:' . $owner['matrix'];
393                         }
394                 }
395
396                 $data['url'] = $owner['url'];
397                 $data['manuallyApprovesFollowers'] = in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
398                 $data['discoverable'] = (bool)$owner['net-publish'];
399                 $data['publicKey'] = ['id' => $owner['url'] . '#main-key',
400                         'owner' => $owner['url'],
401                         'publicKeyPem' => $owner['pubkey']];
402                 $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
403                 if ($full && $uid != 0) {
404                         $data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
405
406                         $resourceid = Photo::ridFromURI($owner['photo']);
407                         if (!empty($resourceid)) {
408                                 $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
409                                 if (!empty($photo['type'])) {
410                                         $data['icon']['mediaType'] = $photo['type'];
411                                 }
412                         }
413
414                         if (!empty($owner['header'])) {
415                                 $data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($owner['id'], '', $owner['updated'])];
416
417                                 $resourceid = Photo::ridFromURI($owner['header']);
418                                 if (!empty($resourceid)) {
419                                         $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
420                                         if (!empty($photo['type'])) {
421                                                 $data['image']['mediaType'] = $photo['type'];
422                                         }
423                                 }
424                         }
425
426                         $custom_fields = [];
427
428                         foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) {
429                                 $custom_fields[] = [
430                                         'type' => 'PropertyValue',
431                                         'name' => $profile_field->label,
432                                         'value' => BBCode::convertForUriId($owner['uri-id'], $profile_field->value)
433                                 ];
434                         };
435
436                         if (!empty($custom_fields)) {
437                                 $data['attachment'] = $custom_fields;
438                         }
439                 }
440
441                 $data['generator'] = self::getService();
442
443                 // tags: https://kitty.town/@inmysocks/100656097926961126.json
444                 return $data;
445         }
446
447         /**
448          * Get a minimal actor array for the C2S API
449          *
450          * @param integer $cid
451          * @return array
452          */
453         private static function getActorArrayByCid(int $cid): array
454         {
455                 $contact = Contact::getById($cid);
456                 $data = [
457                         'id'                        => $contact['url'],
458                         'type'                      => $data['type'] = ActivityPub::ACCOUNT_TYPES[$contact['contact-type']],
459                         'url'                       => $contact['alias'],
460                         'preferredUsername'         => $contact['nick'],
461                         'name'                      => $contact['name'],
462                         'icon'                      => ['type' => 'Image', 'url' => Contact::getAvatarUrlForId($cid, '', $contact['updated'])],
463                         'image'                     => ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($cid, '', $contact['updated'])],
464                         'manuallyApprovesFollowers' => (bool)$contact['manually-approve'],
465                         'discoverable'              => !$contact['unsearchable'],
466                 ];
467
468                 if (empty($data['url'])) {
469                         $data['url'] = $data['id'];
470                 }
471
472                 return $data;
473         }
474
475         /**
476          * @param string $username
477          * @return array
478          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
479          */
480         public static function getDeletedUser(string $username): array
481         {
482                 return [
483                         '@context' => ActivityPub::CONTEXT,
484                         'id' => DI::baseUrl() . '/profile/' . $username,
485                         'type' => 'Tombstone',
486                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
487                         'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
488                         'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
489                 ];
490         }
491
492         /**
493          * Returns an array with permissions of the thread parent of the given item array
494          *
495          * @param array $item
496          *
497          * @return array with permissions
498          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
499          * @throws \ImagickException
500          */
501         private static function fetchPermissionBlockFromThreadParent(array $item, bool $is_group_thread): array
502         {
503                 if (empty($item['thr-parent-id'])) {
504                         return [];
505                 }
506
507                 $parent = Post::selectFirstPost(['author-link'], ['uri-id' => $item['thr-parent-id']]);
508                 if (empty($parent)) {
509                         return [];
510                 }
511
512                 $permissions = [
513                         'to' => [$parent['author-link']],
514                         'cc' => [],
515                         'bto' => [],
516                         'bcc' => [],
517                         'audience' => [],
518                 ];
519
520                 $parent_profile = APContact::getByURL($parent['author-link']);
521
522                 $item_profile = APContact::getByURL($item['author-link']);
523                 $exclude[] = $item['author-link'];
524
525                 if ($item['gravity'] == Item::GRAVITY_PARENT) {
526                         $exclude[] = $item['owner-link'];
527                 }
528
529                 $type = [Tag::TO => 'to', Tag::CC => 'cc', Tag::BTO => 'bto', Tag::BCC => 'bcc', Tag::AUDIENCE => 'audience'];
530                 foreach (Tag::getByURIId($item['thr-parent-id'], [Tag::TO, Tag::CC, Tag::BTO, Tag::BCC, Tag::AUDIENCE]) as $receiver) {
531                         if (!empty($parent_profile['followers']) && $receiver['url'] == $parent_profile['followers'] && !empty($item_profile['followers'])) {
532                                 if (!$is_group_thread) {
533                                         $permissions[$type[$receiver['type']]][] = $item_profile['followers'];
534                                 }
535                         } elseif (!in_array($receiver['url'], $exclude)) {
536                                 $permissions[$type[$receiver['type']]][] = $receiver['url'];
537                         }
538                 }
539
540                 return $permissions;
541         }
542
543         /**
544          * Check if the given item id is from ActivityPub
545          *
546          * @param integer $item_id
547          * @return boolean "true" if the post is from ActivityPub
548          */
549         private static function isAPPost(int $item_id): bool
550         {
551                 if (empty($item_id)) {
552                         return false;
553                 }
554
555                 return Post::exists(['id' => $item_id, 'network' => Protocol::ACTIVITYPUB]);
556         }
557
558         /**
559          * Creates an array of permissions from an item thread
560          *
561          * @param array   $item             Item array
562          * @param boolean $blindcopy        addressing via "bcc" or "cc"?
563          * @param integer $last_id          Last item id for adding receivers
564          *
565          * @return array with permission data
566          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
567          * @throws \ImagickException
568          */
569         private static function createPermissionBlockForItem(array $item, bool $blindcopy, int $last_id = 0): array
570         {
571                 if ($last_id == 0) {
572                         $last_id = $item['id'];
573                 }
574
575                 $always_bcc = false;
576                 $is_group   = false;
577                 $follower   = '';
578                 $exclusive  = false;
579                 $mention    = false;
580                 $audience   = [];
581
582                 // Check if we should always deliver our stuff via BCC
583                 if (!empty($item['uid'])) {
584                         $owner = User::getOwnerDataById($item['uid']);
585                         if (!empty($owner)) {
586                                 $always_bcc = $owner['hide-friends'];
587                                 $is_group   = ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
588
589                                 $profile  = APContact::getByURL($owner['url'], false);
590                                 $follower = $profile['followers'] ?? '';
591                         }
592                 }
593
594                 if (DI::config()->get('system', 'ap_always_bcc')) {
595                         $always_bcc = true;
596                 }
597
598                 $parent = Post::selectFirst(['causer-link', 'post-reason'], ['id' => $item['parent']]);
599                 if (!empty($parent) && ($parent['post-reason'] == Item::PR_ANNOUNCEMENT) && !empty($parent['causer-link'])) {
600                         $profile = APContact::getByURL($parent['causer-link'], false);
601                         $is_group_thread = isset($profile['type']) && $profile['type'] == 'Group';
602                 } else {
603                         $is_group_thread = false;
604                 }
605
606                 if (!$is_group) {
607                         $parent_tags = Tag::getByURIId($item['parent-uri-id'], [Tag::AUDIENCE, Tag::MENTION]);
608                         if (!empty($parent_tags)) {
609                                 $is_group_thread = false;
610                                 foreach ($parent_tags as $tag) {
611                                         if ($tag['type'] != Tag::AUDIENCE) {
612                                                 continue;
613                                         }
614                                         $profile = APContact::getByURL($tag['url'], false);
615                                         if (!empty($profile) && ($profile['type'] == 'Group')) {
616                                                 $audience[] = $tag['url'];
617                                                 $is_group_thread = true;
618                                         }
619                                 }
620                                 if ($is_group_thread) {
621                                         foreach ($parent_tags as $tag) {
622                                                 if (($tag['type'] == Tag::MENTION) && in_array($tag['url'], $audience)) {
623                                                         $mention = true;
624                                                 }
625                                         }
626                                         $exclusive = !$mention;
627                                 }
628                         } elseif ($is_group_thread) {
629                                 foreach (Tag::getByURIId($item['parent-uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $term) {
630                                         $profile = APContact::getByURL($term['url'], false);
631                                         if (!empty($profile) && ($profile['type'] == 'Group')) {
632                                                 if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
633                                                         $audience[] = $term['url'];
634                                                         $exclusive  = true;
635                                                 } elseif ($term['type'] == Tag::MENTION) {
636                                                         $mention = true;
637                                                 }
638                                         }
639                                 }
640                         }
641                 } else {
642                         $audience[] = $owner['url'];
643                 }
644
645                 if (self::isAnnounce($item) || self::isAPPost($last_id)) {
646                         // Will be activated in a later step
647                         $networks = Protocol::FEDERATED;
648                 } else {
649                         // For now only send to these contacts:
650                         $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
651                 }
652
653                 $data = ['to' => [], 'cc' => [], 'bcc' => [] , 'audience' => $audience];
654
655                 if ($item['gravity'] == Item::GRAVITY_PARENT) {
656                         $actor_profile = APContact::getByURL($item['owner-link']);
657                 } else {
658                         $actor_profile = APContact::getByURL($item['author-link']);
659                 }
660
661                 $terms = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION, Tag::AUDIENCE]);
662
663                 if ($item['private'] != Item::PRIVATE) {
664                         // Directly mention the original author upon a quoted reshare.
665                         // Else just ensure that the original author receives the reshare.
666                         $announce = self::getAnnounceArray($item);
667                         if (!empty($announce['comment'])) {
668                                 $data['to'][] = $announce['actor']['url'];
669                         } elseif (!empty($announce)) {
670                                 $data['cc'][] = $announce['actor']['url'];
671                         }
672
673                         if (!$exclusive) {
674                                 $data = array_merge_recursive($data, self::fetchPermissionBlockFromThreadParent($item, $is_group_thread));
675                         }
676
677                         // Check if the item is completely public or unlisted
678                         if ($item['private'] == Item::PUBLIC) {
679                                 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
680                         } else {
681                                 $data['cc'][] = ActivityPub::PUBLIC_COLLECTION;
682                         }
683
684                         foreach ($terms as $term) {
685                                 $profile = APContact::getByURL($term['url'], false);
686                                 if (!empty($profile)) {
687                                         if (($term['type'] == Tag::AUDIENCE) && ($profile['type'] == 'Group')) {
688                                                 $data['audience'][] = $profile['url'];
689                                         }
690                                         if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
691                                                 $exclusive = true;
692                                                 if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
693                                                         $data['cc'][]       = $profile['followers'];
694                                                         $data['audience'][] = $profile['url'];
695                                                 }
696                                         } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
697                                                 $mention = true;
698                                         }
699                                         $data['to'][] = $profile['url'];
700                                 }
701                         }
702                         if (!$exclusive && ($item['private'] == Item::UNLISTED)) {
703                                 $data['to'][] = $actor_profile['followers'];
704                         }
705                 } else {
706                         $receiver_list = Item::enumeratePermissions($item, true, false);
707
708                         foreach ($terms as $term) {
709                                 $cid = Contact::getIdForURL($term['url'], $item['uid']);
710                                 if (!empty($cid) && in_array($cid, $receiver_list)) {
711                                         $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol', 'gsid'], ['id' => $cid, 'network' => Protocol::FEDERATED]);
712                                         if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
713                                                 continue;
714                                         }
715
716                                         $profile = APContact::getByURL($term['url'], false);
717                                         if (!empty($profile)) {
718                                                 if (($term['type'] == Tag::AUDIENCE) && ($profile['type'] == 'Group')) {
719                                                         $data['audience'][] = $profile['url'];
720                                                 }
721                                                 if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
722                                                         $exclusive = true;
723                                                         if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
724                                                                 $data['cc'][]       = $profile['followers'];
725                                                                 $data['audience'][] = $profile['url'];
726                                                         }
727                                                 } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
728                                                         $mention = true;
729                                                 }
730                                                 $data['to'][] = $profile['url'];
731                                         }
732                                 }
733                         }
734
735                         if ($mention) {
736                                 $exclusive = false;
737                         }
738
739                         if ($is_group && !$exclusive && !empty($follower)) {
740                                 $data['cc'][] = $follower;
741                         } elseif (!$exclusive) {
742                                 foreach ($receiver_list as $receiver) {
743                                         if ($receiver == -1) {
744                                                 $data['to'][] = $actor_profile['followers'];
745                                                 continue;
746                                         }
747
748                                         $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]);
749                                         if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
750                                                 continue;
751                                         }
752
753                                         if (!empty($profile = APContact::getByURL($contact['url'], false))) {
754                                                 if ($contact['hidden'] || $always_bcc) {
755                                                         $data['bcc'][] = $profile['url'];
756                                                 } else {
757                                                         $data['cc'][] = $profile['url'];
758                                                 }
759                                         }
760                                 }
761                         }
762                 }
763
764                 if (!empty($item['parent']) && (!$exclusive || ($item['private'] == Item::PRIVATE))) {
765                         if ($item['private'] == Item::PRIVATE) {
766                                 $condition = ['parent' => $item['parent'], 'uri-id' => $item['thr-parent-id']];
767                         } else {
768                                 $condition = ['parent' => $item['parent']];
769                         }
770                         $parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], $condition, ['order' => ['id']]);
771                         while ($parent = Post::fetch($parents)) {
772                                 if ($parent['gravity'] == Item::GRAVITY_PARENT) {
773                                         $profile = APContact::getByURL($parent['owner-link'], false);
774                                         if (!empty($profile)) {
775                                                 if ($item['gravity'] != Item::GRAVITY_PARENT) {
776                                                         // Comments to groups are directed to the group
777                                                         // But comments to groups aren't directed to the followers collection
778                                                         // This rule is only valid when the actor isn't the group.
779                                                         // The group needs to transmit their content to their followers.
780                                                         if (($profile['type'] == 'Group') && ($profile['url'] != ($actor_profile['url'] ?? ''))) {
781                                                                 $data['to'][] = $profile['url'];
782                                                         } else {
783                                                                 $data['cc'][] = $profile['url'];
784                                                                 if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers']) && (!$exclusive || !$is_group_thread)) {
785                                                                         $data['cc'][] = $actor_profile['followers'];
786                                                                 }
787                                                         }
788                                                 } elseif (!$exclusive && !$is_group_thread) {
789                                                         // Public thread parent post always are directed to the followers.
790                                                         if ($item['private'] != Item::PRIVATE) {
791                                                                 $data['cc'][] = $actor_profile['followers'];
792                                                         }
793                                                 }
794                                         }
795                                 }
796
797                                 // Don't include data from future posts
798                                 if ($parent['id'] >= $last_id) {
799                                         continue;
800                                 }
801
802                                 $profile = APContact::getByURL($parent['author-link'], false);
803                                 if (!empty($profile)) {
804                                         if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
805                                                 $data['to'][] = $profile['url'];
806                                         } else {
807                                                 $data['cc'][] = $profile['url'];
808                                         }
809                                 }
810                         }
811                         DBA::close($parents);
812                 }
813
814                 $data['to']       = array_unique($data['to']);
815                 $data['cc']       = array_unique($data['cc']);
816                 $data['bcc']      = array_unique($data['bcc']);
817                 $data['audience'] = array_unique($data['audience']);
818
819                 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
820                         unset($data['to'][$key]);
821                 }
822
823                 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
824                         unset($data['cc'][$key]);
825                 }
826
827                 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
828                         unset($data['bcc'][$key]);
829                 }
830
831                 foreach ($data['to'] as $to) {
832                         if (($key = array_search($to, $data['cc'])) !== false) {
833                                 unset($data['cc'][$key]);
834                         }
835
836                         if (($key = array_search($to, $data['bcc'])) !== false) {
837                                 unset($data['bcc'][$key]);
838                         }
839                 }
840
841                 foreach ($data['cc'] as $cc) {
842                         if (($key = array_search($cc, $data['bcc'])) !== false) {
843                                 unset($data['bcc'][$key]);
844                         }
845                 }
846
847                 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc']), 'audience' => array_values($data['audience'])];
848
849                 if (!$blindcopy) {
850                         unset($receivers['bcc']);
851                 }
852
853                 if (!$blindcopy && count($receivers['audience']) == 1) {
854                         $receivers['audience'] = $receivers['audience'][0];
855                 } elseif (!$receivers['audience']) {
856                         unset($receivers['audience']);
857                 }
858
859                 return $receivers;
860         }
861
862         /**
863          * Store the receivers for the given item
864          *
865          * @param array $item
866          * @return void
867          */
868         public static function storeReceiversForItem(array $item)
869         {
870                 $receivers = self::createPermissionBlockForItem($item, true);
871                 if (empty($receivers)) {
872                         return;
873                 }
874
875                 foreach (['to' => Tag::TO, 'cc' => Tag::CC, 'bcc' => Tag::BCC, 'audience' => Tag::AUDIENCE] as $element => $type) {
876                         if (!empty($receivers[$element])) {
877                                 foreach ($receivers[$element] as $receiver) {
878                                         if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
879                                                 $name = Receiver::PUBLIC_COLLECTION;
880                                         } else {
881                                                 $name = trim(parse_url($receiver, PHP_URL_PATH), '/');
882                                         }
883                                         Tag::store($item['uri-id'], $type, $name, $receiver);
884                                 }
885                         }
886                 }
887         }
888
889         /**
890          * Get a list of receivers for the provided uri-id
891          *
892          * @param array $item
893          * @param boolean $blindcopy
894          * @return void
895          */
896         public static function getReceiversForUriId(int $uri_id, bool $blindcopy)
897         {
898                 $tags = Tag::getByURIId($uri_id, [Tag::TO, Tag::CC, Tag::BCC, Tag::AUDIENCE]);
899                 if (empty($tags)) {
900                         Logger::debug('No receivers found', ['uri-id' => $uri_id]);
901                         $post = Post::selectFirst([Item::DELIVER_FIELDLIST], ['uri-id' => $uri_id, 'origin' => true]);
902                         if (!empty($post)) {
903                                 ActivityPub\Transmitter::storeReceiversForItem($post);
904                                 $tags = Tag::getByURIId($uri_id, [Tag::TO, Tag::CC, Tag::BCC, Tag::AUDIENCE]);
905                                 Logger::debug('Receivers are created', ['uri-id' => $uri_id, 'receivers' => count($tags)]);
906                         } else {
907                                 Logger::debug('Origin item not found', ['uri-id' => $uri_id]);
908                         }
909                 }
910
911                 $receivers = [
912                         'to'       => [],
913                         'cc'       => [],
914                         'bcc'      => [],
915                         'audience' => [],
916                 ];
917
918                 foreach ($tags as $receiver) {
919                         switch ($receiver['type']) {
920                                 case Tag::TO:
921                                         $receivers['to'][] = $receiver['url'];
922                                         break;
923                                 case Tag::CC:
924                                         $receivers['cc'][] = $receiver['url'];
925                                         break;
926                                 case Tag::BCC:
927                                         $receivers['bcc'][] = $receiver['url'];
928                                         break;
929                                 case Tag::AUDIENCE:
930                                         $receivers['audience'][] = $receiver['url'];
931                                         break;
932                         }
933                 }
934
935                 if (!$blindcopy) {
936                         unset($receivers['bcc']);
937                 }
938
939                 if (!$blindcopy && count($receivers['audience']) == 1) {
940                         $receivers['audience'] = $receivers['audience'][0];
941                 } elseif (!$receivers['audience']) {
942                         unset($receivers['audience']);
943                 }
944
945                 return $receivers;
946         }
947
948         /**
949          * Check if an inbox is archived
950          *
951          * @param string $url Inbox url
952          * @return boolean "true" if inbox is archived
953          */
954         public static function archivedInbox(string $url): bool
955         {
956                 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
957         }
958
959         /**
960          * Check if a given contact should be delivered via AP
961          *
962          * @param array $contact Contact array
963          * @param array $networks Array with networks
964          * @return bool Whether the used protocol matches ACTIVITYPUB
965          * @throws Exception
966          */
967         private static function isAPContact(array $contact, array $networks): bool
968         {
969                 if (in_array($contact['network'], $networks) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
970                         return true;
971                 }
972
973                 return GServer::getProtocol($contact['gsid'] ?? 0) == Post\DeliveryData::ACTIVITYPUB;
974         }
975
976         /**
977          * Fetches a list of inboxes of followers of a given user
978          *
979          * @param integer $uid      User ID
980          * @param boolean $personal fetch personal inboxes
981          * @param boolean $all_ap   Retrieve all AP enabled inboxes
982          * @return array of follower inboxes
983          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
984          * @throws \ImagickException
985          */
986         public static function fetchTargetInboxesforUser(int $uid, bool $personal = false, bool $all_ap = false): array
987         {
988                 $inboxes = [];
989
990                 $isGroup = false;
991                 if (!empty($item['uid'])) {
992                         $profile = User::getOwnerDataById($item['uid']);
993                         if (!empty($profile)) {
994                                 $isGroup = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
995                         }
996                 }
997
998                 if ($all_ap) {
999                         // Will be activated in a later step
1000                         $networks = Protocol::FEDERATED;
1001                 } else {
1002                         // For now only send to these contacts:
1003                         $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
1004                 }
1005
1006                 $condition = [
1007                         'uid'          => $uid,
1008                         'self'         => false,
1009                         'archive'      => false,
1010                         'pending'      => false,
1011                         'blocked'      => false,
1012                         'network'      => Protocol::FEDERATED,
1013                         'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON, Contact::TYPE_NEWS, Contact::TYPE_ORGANISATION],
1014                 ];
1015
1016                 if (!empty($uid)) {
1017                         $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
1018                 }
1019
1020                 $contacts = DBA::select('contact', ['id', 'url', 'network', 'protocol', 'gsid'], $condition);
1021                 while ($contact = DBA::fetch($contacts)) {
1022                         if (!self::isAPContact($contact, $networks)) {
1023                                 continue;
1024                         }
1025
1026                         if ($isGroup && ($contact['network'] == Protocol::DFRN)) {
1027                                 continue;
1028                         }
1029
1030                         if (Network::isUrlBlocked($contact['url'])) {
1031                                 continue;
1032                         }
1033
1034                         $profile = APContact::getByURL($contact['url'], false);
1035                         if (!empty($profile)) {
1036                                 if (empty($profile['sharedinbox']) || $personal || Contact::isLocal($contact['url'])) {
1037                                         $target = $profile['inbox'];
1038                                 } else {
1039                                         $target = $profile['sharedinbox'];
1040                                 }
1041                                 if (!self::archivedInbox($target)) {
1042                                         $inboxes[$target][] = $contact['id'];
1043                                 }
1044                         }
1045                 }
1046                 DBA::close($contacts);
1047
1048                 return $inboxes;
1049         }
1050
1051         /**
1052          * Fetches an array of inboxes for the given item and user
1053          *
1054          * @param array   $item     Item array
1055          * @param integer $uid      User ID
1056          * @param boolean $personal fetch personal inboxes
1057          * @return array with inboxes
1058          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1059          * @throws \ImagickException
1060          */
1061         public static function fetchTargetInboxes(array $item, int $uid, bool $personal = false): array
1062         {
1063                 $permissions = self::getReceiversForUriId($item['uri-id'], true);
1064                 if (empty($permissions)) {
1065                         return [];
1066                 }
1067
1068                 $inboxes = [];
1069
1070                 if ($item['gravity'] == Item::GRAVITY_ACTIVITY) {
1071                         $item_profile = APContact::getByURL($item['author-link'], false);
1072                 } else {
1073                         $item_profile = APContact::getByURL($item['owner-link'], false);
1074                 }
1075
1076                 if (empty($item_profile)) {
1077                         return [];
1078                 }
1079
1080                 $profile_uid = User::getIdForURL($item_profile['url']);
1081
1082                 foreach (['to', 'cc', 'bto', 'bcc', 'audience'] as $element) {
1083                         if (empty($permissions[$element])) {
1084                                 continue;
1085                         }
1086
1087                         $blindcopy = in_array($element, ['bto', 'bcc']);
1088
1089                         foreach ($permissions[$element] as $receiver) {
1090                                 if (empty($receiver) || Network::isUrlBlocked($receiver)) {
1091                                         continue;
1092                                 }
1093
1094                                 if ($item_profile && ($receiver == $item_profile['followers']) && ($uid == $profile_uid)) {
1095                                         $inboxes = array_merge_recursive($inboxes, self::fetchTargetInboxesforUser($uid, $personal, true));
1096                                 } else {
1097                                         $profile = APContact::getByURL($receiver, false);
1098                                         if (!empty($profile)) {
1099                                                 $contact = Contact::getByURLForUser($receiver, $uid, false, ['id']);
1100
1101                                                 if (empty($profile['sharedinbox']) || $personal || $blindcopy || Contact::isLocal($receiver)) {
1102                                                         $target = $profile['inbox'];
1103                                                 } else {
1104                                                         $target = $profile['sharedinbox'];
1105                                                 }
1106                                                 if (!self::archivedInbox($target) && !in_array($contact['id'], $inboxes[$target] ?? [])) {
1107                                                         $inboxes[$target][] = $contact['id'] ?? 0;
1108                                                 }
1109                                         }
1110                                 }
1111                         }
1112                 }
1113
1114                 return $inboxes;
1115         }
1116
1117         /**
1118          * Creates an array in the structure of the item table for a given mail id
1119          *
1120          * @param integer $mail_id Mail id
1121          * @return array
1122          * @throws \Exception
1123          */
1124         public static function getItemArrayFromMail(int $mail_id, bool $use_title = false): array
1125         {
1126                 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
1127                 if (!DBA::isResult($mail)) {
1128                         return [];
1129                 }
1130
1131                 $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
1132                 if (!DBA::isResult($reply)) {
1133                         $reply = $mail;
1134                 }
1135
1136                 // Making the post more compatible for Mastodon by:
1137                 // - Making it a note and not an article (no title)
1138                 // - Moving the title into the "summary" field that is used as a "content warning"
1139
1140                 if (!$use_title) {
1141                         $mail['body']         = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
1142                         $mail['title']        = '';
1143                 }
1144
1145                 $mail['content-warning']  = '';
1146                 $mail['author-link']      = $mail['owner-link'] = $mail['from-url'];
1147                 $mail['owner-id']         = $mail['author-id'];
1148                 $mail['allow_cid']        = '<'.$mail['contact-id'].'>';
1149                 $mail['allow_gid']        = '';
1150                 $mail['deny_cid']         = '';
1151                 $mail['deny_gid']         = '';
1152                 $mail['private']          = Item::PRIVATE;
1153                 $mail['deleted']          = false;
1154                 $mail['edited']           = $mail['created'];
1155                 $mail['plink']            = DI::baseUrl() . '/message/' . $mail['id'];
1156                 $mail['parent-uri']       = $reply['uri'];
1157                 $mail['parent-uri-id']    = $reply['uri-id'];
1158                 $mail['parent-author-id'] = Contact::getIdForURL($reply['from-url'], 0, false);
1159                 $mail['gravity']          = ($mail['reply'] ? Item::GRAVITY_COMMENT: Item::GRAVITY_PARENT);
1160                 $mail['event-type']       = '';
1161                 $mail['language']         = '';
1162                 $mail['parent']           = 0;
1163
1164                 return $mail;
1165         }
1166
1167         /**
1168          * Creates an activity array for a given mail id
1169          *
1170          * @param integer $mail_id
1171          * @param boolean $object_mode Is the activity item is used inside another object?
1172          *
1173          * @return array of activity
1174          * @throws \Exception
1175          */
1176         public static function createActivityFromMail(int $mail_id, bool $object_mode = false): array
1177         {
1178                 $mail = self::getItemArrayFromMail($mail_id);
1179                 if (empty($mail)) {
1180                         return [];
1181                 }
1182                 $object = self::createNote($mail);
1183
1184                 if (!$object_mode) {
1185                         $data = ['@context' => ActivityPub::CONTEXT];
1186                 } else {
1187                         $data = [];
1188                 }
1189
1190                 $data['id'] = $mail['uri'] . '/Create';
1191                 $data['type'] = 'Create';
1192                 $data['actor'] = $mail['author-link'];
1193                 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
1194                 $data['instrument'] = self::getService();
1195                 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
1196
1197                 if (empty($data['to']) && !empty($data['cc'])) {
1198                         $data['to'] = $data['cc'];
1199                 }
1200
1201                 if (empty($data['to']) && !empty($data['bcc'])) {
1202                         $data['to'] = $data['bcc'];
1203                 }
1204
1205                 unset($data['cc']);
1206                 unset($data['bcc']);
1207                 unset($data['audience']);
1208
1209                 $object['to'] = $data['to'];
1210                 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
1211
1212                 unset($object['cc']);
1213                 unset($object['bcc']);
1214                 unset($object['audience']);
1215
1216                 $data['directMessage'] = true;
1217
1218                 $data['object'] = $object;
1219
1220                 $owner = User::getOwnerDataById($mail['uid']);
1221
1222                 if (!$object_mode && !empty($owner)) {
1223                         return LDSignature::sign($data, $owner);
1224                 } else {
1225                         return $data;
1226                 }
1227         }
1228
1229         /**
1230          * Returns the activity type of a given item
1231          *
1232          * @param array $item Item array
1233          * @return string with activity type
1234          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1235          * @throws \ImagickException
1236          */
1237         private static function getTypeOfItem(array $item): string
1238         {
1239                 $reshared = false;
1240
1241                 // Only check for a reshare, if it is a real reshare and no quoted reshare
1242                 if (strpos($item['body'], '[share') === 0) {
1243                         $announce = self::getAnnounceArray($item);
1244                         $reshared = !empty($announce);
1245                 }
1246
1247                 if ($reshared) {
1248                         $type = 'Announce';
1249                 } elseif ($item['verb'] == Activity::POST) {
1250                         if ($item['created'] == $item['edited']) {
1251                                 $type = 'Create';
1252                         } else {
1253                                 $type = 'Update';
1254                         }
1255                 } elseif ($item['verb'] == Activity::LIKE) {
1256                         $type = 'Like';
1257                 } elseif ($item['verb'] == Activity::DISLIKE) {
1258                         $type = 'Dislike';
1259                 } elseif ($item['verb'] == Activity::ATTEND) {
1260                         $type = 'Accept';
1261                 } elseif ($item['verb'] == Activity::ATTENDNO) {
1262                         $type = 'Reject';
1263                 } elseif ($item['verb'] == Activity::ATTENDMAYBE) {
1264                         $type = 'TentativeAccept';
1265                 } elseif ($item['verb'] == Activity::FOLLOW) {
1266                         $type = 'Follow';
1267                 } elseif ($item['verb'] == Activity::TAG) {
1268                         $type = 'Add';
1269                 } elseif ($item['verb'] == Activity::ANNOUNCE) {
1270                         $type = 'Announce';
1271                 } else {
1272                         $type = '';
1273                 }
1274
1275                 return $type;
1276         }
1277
1278         /**
1279          * Creates the activity or fetches it from the cache
1280          *
1281          * @param integer $item_id           Item id
1282          * @param boolean $force             Force new cache entry
1283          * @param boolean $object_mode       true = Create the object, false = create the activity with the object
1284          * @param boolean $announce_activity true = the announced object is the activity, false = we announce the object link
1285          * @return array|false activity or false on failure
1286          * @throws \Exception
1287          */
1288         public static function createCachedActivityFromItem(int $item_id, bool $force = false, bool $object_mode = false, $announce_activity = false)
1289         {
1290                 $cachekey = 'APDelivery:createActivity:' . $item_id . ':' . (int)$object_mode . ':' . (int)$announce_activity;
1291
1292                 if (!$force) {
1293                         $data = DI::cache()->get($cachekey);
1294                         if (!is_null($data)) {
1295                                 return $data;
1296                         }
1297                 }
1298
1299                 $data = self::createActivityFromItem($item_id, $object_mode, false, $announce_activity);
1300
1301                 DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
1302                 return $data;
1303         }
1304
1305         /**
1306          * Creates an activity array for a given item id
1307          *
1308          * @param integer $item_id
1309          * @param boolean $object_mode       true = Create the object, false = create the activity with the object
1310          * @param boolean $api_mode          true = used for the API
1311          * @param boolean $announce_activity true = the announced object is the activity, false = we announce the object link
1312          * @return false|array
1313          * @throws \Exception
1314          */
1315         public static function createActivityFromItem(int $item_id, bool $object_mode = false, $api_mode = false, $announce_activity = false)
1316         {
1317                 $condition = ['id' => $item_id];
1318                 if (!$api_mode) {
1319                         $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
1320                 }
1321                 Logger::info('Fetching activity', $condition);
1322                 $item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
1323                 if (!DBA::isResult($item)) {
1324                         return false;
1325                 }
1326                 return self::createActivityFromArray($item, $object_mode, $api_mode, $announce_activity);
1327         }
1328
1329         /**
1330          * Creates an activity array for a given URI-Id and uid
1331          *
1332          * @param integer $uri_id
1333          * @param integer $uid
1334          * @param boolean $object_mode       true = Create the object, false = create the activity with the object
1335          * @param boolean $api_mode          true = used for the API
1336          * @param boolean $announce_activity true = the announced object is the activity, false = we announce the object link
1337          * @return false|array
1338          * @throws \Exception
1339          */
1340         public static function createActivityFromUriId(int $uri_id, int $uid, bool $object_mode = false, $api_mode = false, $announce_activity = false)
1341         {
1342                 $condition = ['uri-id' => $uri_id, 'uid' => [0, $uid]];
1343                 if (!$api_mode) {
1344                         $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
1345                 }
1346                 Logger::info('Fetching activity', $condition);
1347                 $item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition, ['order' => ['uid' => true]]);
1348                 if (!DBA::isResult($item)) {
1349                         return false;
1350                 }
1351
1352                 return self::createActivityFromArray($item, $object_mode, $api_mode, $announce_activity);
1353         }
1354
1355         /**
1356          * Creates an activity array for a given item id
1357          *
1358          * @param integer $item_id
1359          * @param boolean $object_mode       true = Create the object, false = create the activity with the object
1360          * @param boolean $api_mode          true = used for the API
1361          * @param boolean $announce_activity true = the announced object is the activity, false = we announce the object link
1362          * @return false|array
1363          * @throws \Exception
1364          */
1365         private static function createActivityFromArray(array $item, bool $object_mode = false, $api_mode = false, $announce_activity = false)
1366         {
1367                 if (!$api_mode && !$item['deleted'] && $item['network'] == Protocol::ACTIVITYPUB) {
1368                         $data = Post\Activity::getByURIId($item['uri-id']);
1369                         if (!$item['origin'] && !empty($data)) {
1370                                 if (!$object_mode) {
1371                                         Logger::info('Return stored conversation', ['item' => $item['id']]);
1372                                         return $data;
1373                                 } elseif (!empty($data['object'])) {
1374                                         Logger::info('Return stored conversation object', ['item' => $item['id']]);
1375                                         return $data['object'];
1376                                 }
1377                         }
1378                 }
1379
1380                 if (!$api_mode && !$item['origin']) {
1381                         Logger::debug('Post is not ours and is not stored', ['id' => $item['id'], 'uri-id' => $item['uri-id']]);
1382                         return false;
1383                 }
1384
1385                 $type = self::getTypeOfItem($item);
1386
1387                 if (!$object_mode) {
1388                         $data = ['@context' => $context ?? ActivityPub::CONTEXT];
1389
1390                         if ($item['deleted'] && ($item['gravity'] == Item::GRAVITY_ACTIVITY)) {
1391                                 $type = 'Undo';
1392                         } elseif ($item['deleted']) {
1393                                 $type = 'Delete';
1394                         }
1395                 } else {
1396                         $data = [];
1397                 }
1398
1399                 if ($type == 'Delete') {
1400                         $data['id'] = Item::newURI($item['guid']) . '/' . $type;;
1401                 } elseif (($item['gravity'] == Item::GRAVITY_ACTIVITY) && ($type != 'Undo')) {
1402                         $data['id'] = $item['uri'];
1403                 } else {
1404                         $data['id'] = $item['uri'] . '/' . $type;
1405                 }
1406
1407                 $data['type'] = $type;
1408
1409                 if (($type != 'Announce') || ($item['gravity'] != Item::GRAVITY_PARENT)) {
1410                         $link = $item['author-link'];
1411                         $id   = $item['author-id'];
1412                 } else {
1413                         $link = $item['owner-link'];
1414                         $id   = $item['owner-id'];
1415                 }
1416
1417                 if ($api_mode) {
1418                         $data['actor'] = self::getActorArrayByCid($id);
1419                 } else {
1420                         $data['actor'] = $link;
1421                 }
1422
1423                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1424
1425                 $data['instrument'] = self::getService();
1426
1427                 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
1428
1429                 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
1430                         $data['object'] = self::createNote($item, $api_mode);
1431                 } elseif ($data['type'] == 'Add') {
1432                         $data = self::createAddTag($item, $data);
1433                 } elseif ($data['type'] == 'Announce') {
1434                         if ($item['verb'] == ACTIVITY::ANNOUNCE) {
1435                                 if ($announce_activity) {
1436                                         $anounced_item = Post::selectFirst(['uid'], ['uri-id' => $item['thr-parent-id'], 'origin' => true]);
1437                                         $data['object'] = self::createActivityFromUriId($item['thr-parent-id'], $anounced_item['uid'] ?? 0);
1438                                         unset($data['object']['@context']);
1439                                 } else {
1440                                         $data['object'] = $item['thr-parent'];
1441                                 }
1442                         } else {
1443                                 $data = self::createAnnounce($item, $data, $api_mode);
1444                         }
1445                 } elseif ($data['type'] == 'Follow') {
1446                         $data['object'] = $item['parent-uri'];
1447                 } elseif ($data['type'] == 'Undo') {
1448                         $data['object'] = self::createActivityFromItem($item['id'], true);
1449                 } else {
1450                         $data['diaspora:guid'] = $item['guid'];
1451                         if (!empty($item['signed_text'])) {
1452                                 $data['diaspora:like'] = $item['signed_text'];
1453                         }
1454                         $data['object'] = $item['thr-parent'];
1455                 }
1456
1457                 if (!empty($item['contact-uid'])) {
1458                         $uid = $item['contact-uid'];
1459                 } else {
1460                         $uid = $item['uid'];
1461                 }
1462
1463                 Logger::info('Fetched activity', ['item' => $item['id'], 'uid' => $uid]);
1464
1465                 // We only sign our own activities
1466                 if (!$api_mode && !$object_mode && $item['origin']) {
1467                         $owner = User::getOwnerDataById($uid);
1468                         return LDSignature::sign($data, $owner);
1469                 } else {
1470                         return $data;
1471                 }
1472
1473                 /// @todo Create "conversation" entry
1474         }
1475
1476         /**
1477          * Creates a location entry for a given item array
1478          *
1479          * @param array $item Item array
1480          * @return array with location array
1481          */
1482         private static function createLocation(array $item): array
1483         {
1484                 $location = ['type' => 'Place'];
1485
1486                 if (!empty($item['location'])) {
1487                         $location['name'] = $item['location'];
1488                 }
1489
1490                 $coord = [];
1491
1492                 if (empty($item['coord'])) {
1493                         $coord = Map::getCoordinates($item['location']);
1494                 } else {
1495                         $coords = explode(' ', $item['coord']);
1496                         if (count($coords) == 2) {
1497                                 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
1498                         }
1499                 }
1500
1501                 if (!empty($coord['lat']) && !empty($coord['lon'])) {
1502                         $location['latitude'] = $coord['lat'];
1503                         $location['longitude'] = $coord['lon'];
1504                 }
1505
1506                 return $location;
1507         }
1508
1509         /**
1510          * Returns a tag array for a given item array
1511          *
1512          * @param array  $item      Item array
1513          * @param string $quote_url Url of the attached quote link
1514          * @return array of tags
1515          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1516          */
1517         private static function createTagList(array $item, string $quote_url): array
1518         {
1519                 $tags = [];
1520
1521                 $terms = Tag::getByURIId($item['uri-id'], [Tag::HASHTAG, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
1522                 foreach ($terms as $term) {
1523                         if ($term['type'] == Tag::HASHTAG) {
1524                                 $url = DI::baseUrl() . '/search?tag=' . urlencode($term['name']);
1525                                 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']];
1526                         } else {
1527                                 $contact = Contact::getByURL($term['url'], false, ['addr']);
1528                                 if (empty($contact)) {
1529                                         continue;
1530                                 }
1531                                 if (!empty($contact['addr'])) {
1532                                         $mention = '@' . $contact['addr'];
1533                                 } else {
1534                                         $mention = '@' . $term['url'];
1535                                 }
1536
1537                                 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
1538                         }
1539                 }
1540
1541                 $announce = self::getAnnounceArray($item);
1542                 // Mention the original author upon commented reshares
1543                 if (!empty($announce['comment'])) {
1544                         $tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']];
1545                 }
1546
1547                 // @see https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-e232.md
1548                 if (!empty($quote_url)) {
1549                         // Currently deactivated because of compatibility issues with Pleroma
1550                         //$tags[] = [
1551                         //      'type'      => 'Link',
1552                         //      'mediaType' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
1553                         //      'href'      => $quote_url,
1554                         //      'name'      => '♲ ' . BBCode::convertForUriId($item['uri-id'], $quote_url, BBCode::ACTIVITYPUB)
1555                         //];
1556                 }
1557
1558                 return $tags;
1559         }
1560
1561         /**
1562          * Adds attachment data to the JSON document
1563          *
1564          * @param array  $item Data of the item that is to be posted
1565          *
1566          * @return array with attachment data
1567          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1568          */
1569         private static function createAttachmentList(array $item): array
1570         {
1571                 $attachments = [];
1572
1573                 $urls = [];
1574                 foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO, Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
1575                         if (in_array($attachment['url'], $urls)) {
1576                                 continue;
1577                         }
1578                         $urls[] = $attachment['url'];
1579
1580                         $attach = ['type' => 'Document',
1581                                 'mediaType' => $attachment['mimetype'],
1582                                 'url' => $attachment['url'],
1583                                 'name' => $attachment['description']];
1584
1585                         if (!empty($attachment['height'])) {
1586                                 $attach['height'] = $attachment['height'];
1587                         }
1588
1589                         if (!empty($attachment['width'])) {
1590                                 $attach['width'] = $attachment['width'];
1591                         }
1592
1593                         if (!empty($attachment['preview'])) {
1594                                 $attach['image'] = $attachment['preview'];
1595                         }
1596
1597                         $attachments[] = $attach;
1598                 }
1599
1600                 return $attachments;
1601         }
1602
1603         /**
1604          * Callback function to replace a Friendica style mention in a mention for a summary
1605          *
1606          * @param array $match Matching values for the callback
1607          * @return string Replaced mention
1608          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1609          */
1610         private static function mentionAddrCallback(array $match): string
1611         {
1612                 if (empty($match[1])) {
1613                         return '';
1614                 }
1615
1616                 $data = Contact::getByURL($match[1], false, ['addr']);
1617                 if (empty($data['addr'])) {
1618                         return $match[0];
1619                 }
1620
1621                 return '@' . $data['addr'];
1622         }
1623
1624         /**
1625          * Remove image elements since they are added as attachment
1626          *
1627          * @param string $body HTML code
1628          * @return string with removed images
1629          */
1630         private static function removePictures(string $body): string
1631         {
1632                 return BBCode::performWithEscapedTags($body, ['code', 'noparse', 'nobb', 'pre'], function ($text) {
1633                         // Simplify image codes
1634                         $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $text);
1635                         $text = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $text);
1636
1637                         // Now remove local links
1638                         $text = preg_replace_callback(
1639                                 '/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
1640                                 function ($match) {
1641                                         // We remove the link when it is a link to a local photo page
1642                                         if (Photo::isLocalPage($match[1])) {
1643                                                 return '';
1644                                         }
1645                                         // otherwise we just return the link
1646                                         return '[url]' . $match[1] . '[/url]';
1647                                 },
1648                                 $text
1649                         );
1650
1651                         // Remove all pictures
1652                         return preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $text);
1653                 });
1654         }
1655
1656         /**
1657          * Returns if the post contains sensitive content ("nsfw")
1658          *
1659          * @param integer $uri_id URI id
1660          * @return boolean Whether URI id was found
1661          * @throws \Exception
1662          */
1663         private static function isSensitive(int $uri_id): bool
1664         {
1665                 return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw', 'type' => Tag::HASHTAG]);
1666         }
1667
1668         /**
1669          * Creates event data
1670          *
1671          * @param array $item Item array
1672          * @return array with the event data
1673          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1674          */
1675         private static function createEvent(array $item): array
1676         {
1677                 $event = [];
1678                 $event['name'] = $item['event-summary'];
1679                 $event['content'] = BBCode::convertForUriId($item['uri-id'], $item['event-desc'], BBCode::ACTIVITYPUB);
1680                 $event['startTime'] = DateTimeFormat::utc($item['event-start'], 'c');
1681
1682                 if (!$item['event-nofinish']) {
1683                         $event['endTime'] = DateTimeFormat::utc($item['event-finish'], 'c');
1684                 }
1685
1686                 if (!empty($item['event-location'])) {
1687                         $item['location'] = $item['event-location'];
1688                         $event['location'] = self::createLocation($item);
1689                 }
1690
1691                 // 2021.12: Backward compatibility value, all the events now "adjust" to the viewer timezone
1692                 $event['dfrn:adjust'] = true;
1693
1694                 return $event;
1695         }
1696
1697         /**
1698          * Creates a note/article object array
1699          *
1700          * @param array $item
1701          * @param bool  $api_mode
1702          * @return array with the object data
1703          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1704          * @throws \ImagickException
1705          */
1706         public static function createNote(array $item, bool $api_mode = false): array
1707         {
1708                 if (empty($item)) {
1709                         return [];
1710                 }
1711
1712                 // We are treating posts differently when they are directed to a community.
1713                 // This is done to better support Lemmy. Most of the changes should work with other systems as well.
1714                 // But to not risk compatibility issues we currently perform the changes only for communities.
1715                 if ($item['gravity'] == Item::GRAVITY_PARENT) {
1716                         $isCommunityPost = !empty(Tag::getByURIId($item['uri-id'], [Tag::EXCLUSIVE_MENTION]));
1717                         $links = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
1718                         if ($isCommunityPost && (count($links) == 1)) {
1719                                 $link = $links[0]['url'];
1720                         }
1721                 } else {
1722                         $isCommunityPost = false;
1723                 }
1724
1725                 if ($item['event-type'] == 'event') {
1726                         $type = 'Event';
1727                 } elseif (!empty($item['title'])) {
1728                         if (!$isCommunityPost || empty($link)) {
1729                                 $type = 'Article';
1730                         } else {
1731                                 // "Page" is used by Lemmy for posts that contain an external link
1732                                 $type = 'Page';
1733                         }
1734                 } else {
1735                         $type = 'Note';
1736                 }
1737
1738                 if ($item['deleted']) {
1739                         $type = 'Tombstone';
1740                 }
1741
1742                 $data = [];
1743                 $data['id'] = $item['uri'];
1744                 $data['type'] = $type;
1745
1746                 if ($item['deleted']) {
1747                         return $data;
1748                 }
1749
1750                 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1751
1752                 if ($item['uri'] != $item['thr-parent']) {
1753                         $data['inReplyTo'] = $item['thr-parent'];
1754                 } else {
1755                         $data['inReplyTo'] = null;
1756                 }
1757
1758                 $data['diaspora:guid'] = $item['guid'];
1759                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1760
1761                 if ($item['created'] != $item['edited']) {
1762                         $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1763                 }
1764
1765                 $data['url'] = $link ?? $item['plink'];
1766                 if ($api_mode) {
1767                         $data['attributedTo'] = self::getActorArrayByCid($item['author-id']);
1768                 } else {
1769                         $data['attributedTo'] = $item['author-link'];
1770                 }
1771                 $data['sensitive'] = self::isSensitive($item['uri-id']);
1772
1773                 if (!empty($item['conversation']) && ($item['conversation'] != './')) {
1774                         $data['conversation'] = $data['context'] = $item['conversation'];
1775                 }
1776
1777                 if (!empty($item['title'])) {
1778                         $data['name'] = BBCode::toPlaintext($item['title'], false);
1779                 }
1780
1781                 $permission_block = self::getReceiversForUriId($item['uri-id'], false);
1782
1783                 $real_quote = false;
1784
1785                 $item = Post\Media::addHTMLAttachmentToItem($item);
1786
1787                 $body = $item['body'];
1788
1789                 if ($type == 'Note') {
1790                         $body = $item['raw-body'] ?? self::removePictures($body);
1791                 }
1792
1793                 /**
1794                  * @todo Improve the automated summary
1795                  * This part is currently deactivated. The automated summary seems to be more
1796                  * confusing than helping. But possibly we will find a better way.
1797                  * So the code is left here for now as a reminder
1798                  *
1799                  * } elseif (($type == 'Article') && empty($data['summary'])) {
1800                  *              $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1801                  *              $summary = preg_replace_callback($regexp, [self::class, 'mentionAddrCallback'], $body);
1802                  *              $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($summary), 1000));
1803                  * }
1804                  */
1805
1806                 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1807                         $body = self::prependMentions($body, $item['uri-id'], $item['author-link']);
1808                 }
1809
1810                 if ($type == 'Event') {
1811                         $data = array_merge($data, self::createEvent($item));
1812                 } else {
1813                         if ($isCommunityPost) {
1814                                 // For community posts we remove the visible "!user@domain.tld".
1815                                 // This improves the look at systems like Lemmy.
1816                                 // Also in the future we should control the community delivery via other methods.
1817                                 $body = preg_replace("/!\[url\=[^\[\]]*\][^\[\]]*\[\/url\]/ism", '', $body);
1818                         }
1819
1820                         if ($type == 'Page') {
1821                                 // When we transmit "Page" posts we have to remove the attachment.
1822                                 // The attachment contains the link that we already transmit in the "url" field.
1823                                 $body = BBCode::removeAttachment($body);
1824                         }
1825
1826                         $body = BBCode::setMentionsToNicknames($body);
1827
1828                         if (!empty($item['quote-uri-id']) && ($item['quote-uri-id'] != $item['uri-id'])) {
1829                                 if (Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
1830                                         $real_quote = true;
1831                                         $data['quoteUrl'] = $item['quote-uri'];
1832                                         $body = DI::contentItem()->addShareLink($body, $item['quote-uri-id']);
1833                                 } else {
1834                                         $body = DI::contentItem()->addSharedPost($item, $body);
1835                                 }
1836                         }
1837
1838                         $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
1839                 }
1840
1841                 // The regular "content" field does contain a minimized HTML. This is done since systems like
1842                 // Mastodon has got problems with - for example - embedded pictures.
1843                 // The contentMap does contain the unmodified HTML.
1844                 $language = self::getLanguage($item);
1845                 if (!empty($language)) {
1846                         $richbody = BBCode::setMentionsToNicknames($item['body'] ?? '');
1847                         $richbody = Post\Media::removeFromEndOfBody($richbody);
1848                         if (!empty($item['quote-uri-id']) && ($item['quote-uri-id'] != $item['uri-id'])) {
1849                                 if ($real_quote) {
1850                                         $richbody = DI::contentItem()->addShareLink($richbody, $item['quote-uri-id']);
1851                                 } else {
1852                                         $richbody = DI::contentItem()->addSharedPost($item, $richbody);
1853                                 }
1854                         }
1855                         $richbody = BBCode::replaceAttachment($richbody);
1856
1857                         $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
1858                 }
1859
1860                 if (!empty($item['quote-uri-id']) && ($item['quote-uri-id'] != $item['uri-id'])) {
1861                         $source = DI::contentItem()->addSharedPost($item, $item['body']);
1862                 } else {
1863                         $source = $item['body'];
1864                 }
1865
1866                 $data['source'] = ['content' => $source, 'mediaType' => "text/bbcode"];
1867
1868                 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1869                         $data['diaspora:comment'] = $item['signed_text'];
1870                 }
1871
1872                 $data['attachment'] = self::createAttachmentList($item);
1873                 $data['tag'] = self::createTagList($item, $data['quoteUrl'] ?? '');
1874
1875                 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1876                         $data['location'] = self::createLocation($item);
1877                 }
1878
1879                 if (!empty($item['app'])) {
1880                         $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1881                 }
1882
1883                 $data = array_merge($data, $permission_block);
1884
1885                 return $data;
1886         }
1887
1888         /**
1889          * Fetches the language from the post, the user or the system.
1890          *
1891          * @param array $item
1892          * @return string language string
1893          */
1894         private static function getLanguage(array $item): string
1895         {
1896                 // Try to fetch the language from the post itself
1897                 if (!empty($item['language'])) {
1898                         $languages = array_keys(json_decode($item['language'], true));
1899                         if (!empty($languages[0])) {
1900                                 return DI::l10n()->toISO6391($languages[0]);
1901                         }
1902                 }
1903
1904                 // Otherwise use the user's language
1905                 if (!empty($item['uid'])) {
1906                         $user = DBA::selectFirst('user', ['language'], ['uid' => $item['uid']]);
1907                         if (!empty($user['language'])) {
1908                                 return DI::l10n()->toISO6391($user['language']);
1909                         }
1910                 }
1911
1912                 // And finally just use the system language
1913                 return DI::l10n()->toISO6391(DI::config()->get('system', 'language'));
1914         }
1915
1916         /**
1917          * Creates an an "add tag" entry
1918          *
1919          * @param array $item Item array
1920          * @param array $activity activity data
1921          * @return array with activity data for adding tags
1922          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1923          * @throws \ImagickException
1924          */
1925         private static function createAddTag(array $item, array $activity): array
1926         {
1927                 $object = XML::parseString($item['object']);
1928                 $target = XML::parseString($item['target']);
1929
1930                 $activity['diaspora:guid'] = $item['guid'];
1931                 $activity['actor'] = $item['author-link'];
1932                 $activity['target'] = (string)$target->id;
1933                 $activity['summary'] = BBCode::toPlaintext($item['body']);
1934                 $activity['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1935
1936                 return $activity;
1937         }
1938
1939         /**
1940          * Creates an announce object entry
1941          *
1942          * @param array $item Item array
1943          * @param array $activity activity data
1944          * @param bool  $api_mode
1945          * @return array with activity data
1946          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1947          * @throws \ImagickException
1948          */
1949         private static function createAnnounce(array $item, array $activity, bool $api_mode = false): array
1950         {
1951                 $orig_body = $item['body'];
1952                 $announce = self::getAnnounceArray($item);
1953                 if (empty($announce)) {
1954                         $activity['type'] = 'Create';
1955                         $activity['object'] = self::createNote($item, $api_mode);
1956                         return $activity;
1957                 }
1958
1959                 if (empty($announce['comment'])) {
1960                         // Pure announce, without a quote
1961                         $activity['type'] = 'Announce';
1962                         $activity['object'] = $announce['object']['uri'];
1963                         return $activity;
1964                 }
1965
1966                 // Quote
1967                 $activity['type'] = 'Create';
1968                 $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
1969                 $activity['object'] = self::createNote($item, $api_mode);
1970
1971                 /// @todo Finally decide how to implement this in AP. This is a possible way:
1972                 $activity['object']['attachment'][] = self::createNote($announce['object']);
1973
1974                 $activity['object']['source']['content'] = $orig_body;
1975                 return $activity;
1976         }
1977
1978         /**
1979          * Return announce related data if the item is an announce
1980          *
1981          * @param array $item
1982          * @return array Announcement array
1983          */
1984         private static function getAnnounceArray(array $item): array
1985         {
1986                 $reshared = DI::contentItem()->getSharedPost($item, Item::DELIVER_FIELDLIST);
1987                 if (empty($reshared)) {
1988                         return [];
1989                 }
1990
1991                 if (!in_array($reshared['post']['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
1992                         return [];
1993                 }
1994
1995                 $profile = APContact::getByURL($reshared['post']['author-link'], false);
1996                 if (empty($profile)) {
1997                         return [];
1998                 }
1999
2000                 return ['object' => $reshared['post'], 'actor' => $profile, 'comment' => $reshared['comment']];
2001         }
2002
2003         /**
2004          * Checks if the provided item array is an announce
2005          *
2006          * @param array $item Item array
2007          * @return boolean Whether item is an announcement
2008          */
2009         public static function isAnnounce(array $item): bool
2010         {
2011                 if (!empty($item['verb']) && ($item['verb'] == Activity::ANNOUNCE)) {
2012                         return true;
2013                 }
2014
2015                 $announce = self::getAnnounceArray($item);
2016                 if (empty($announce)) {
2017                         return false;
2018                 }
2019
2020                 return empty($announce['comment']);
2021         }
2022
2023         /**
2024          * Creates an activity id for a given contact id
2025          *
2026          * @param integer $cid Contact ID of target
2027          *
2028          * @return bool|string activity id
2029          */
2030         public static function activityIDFromContact(int $cid)
2031         {
2032                 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
2033                 if (!DBA::isResult($contact)) {
2034                         return false;
2035                 }
2036
2037                 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
2038                 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
2039                 return DI::baseUrl() . '/activity/' . $uuid;
2040         }
2041
2042         /**
2043          * Transmits a contact suggestion to a given inbox
2044          *
2045          * @param array   $owner         Sender owner-view record
2046          * @param string  $inbox         Target inbox
2047          * @param integer $suggestion_id Suggestion ID
2048          * @return boolean was the transmission successful?
2049          * @throws \Exception
2050          */
2051         public static function sendContactSuggestion(array $owner, string $inbox, int $suggestion_id): bool
2052         {
2053                 $suggestion = DI::fsuggest()->selectOneById($suggestion_id);
2054
2055                 $data = [
2056                         '@context' => ActivityPub::CONTEXT,
2057                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2058                         'type' => 'Announce',
2059                         'actor' => $owner['url'],
2060                         'object' => $suggestion->url,
2061                         'content' => $suggestion->note,
2062                         'instrument' => self::getService(),
2063                         'to' => [ActivityPub::PUBLIC_COLLECTION],
2064                         'cc' => []
2065                 ];
2066
2067                 $signed = LDSignature::sign($data, $owner);
2068
2069                 Logger::info('Deliver profile deletion for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2070                 return HTTPSignature::transmit($signed, $inbox, $owner);
2071         }
2072
2073         /**
2074          * Transmits a profile relocation to a given inbox
2075          *
2076          * @param array  $owner Sender owner-view record
2077          * @param string $inbox Target inbox
2078          * @return boolean was the transmission successful?
2079          * @throws \Exception
2080          */
2081         public static function sendProfileRelocation(array $owner, string $inbox): bool
2082         {
2083                 $data = [
2084                         '@context' => ActivityPub::CONTEXT,
2085                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2086                         'type' => 'dfrn:relocate',
2087                         'actor' => $owner['url'],
2088                         'object' => $owner['url'],
2089                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
2090                         'instrument' => self::getService(),
2091                         'to' => [ActivityPub::PUBLIC_COLLECTION],
2092                         'cc' => []
2093                 ];
2094
2095                 $signed = LDSignature::sign($data, $owner);
2096
2097                 Logger::info('Deliver profile relocation for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2098                 return HTTPSignature::transmit($signed, $inbox, $owner);
2099         }
2100
2101         /**
2102          * Transmits a profile deletion to a given inbox
2103          *
2104          * @param array  $owner Sender owner-view record
2105          * @param string $inbox Target inbox
2106          * @return boolean was the transmission successful?
2107          * @throws \Exception
2108          */
2109         public static function sendProfileDeletion(array $owner, string $inbox): bool
2110         {
2111                 if (empty($owner['uprvkey'])) {
2112                         Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $owner['uid']]);
2113                         return false;
2114                 }
2115
2116                 $data = ['@context' => ActivityPub::CONTEXT,
2117                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2118                         'type' => 'Delete',
2119                         'actor' => $owner['url'],
2120                         'object' => $owner['url'],
2121                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
2122                         'instrument' => self::getService(),
2123                         'to' => [ActivityPub::PUBLIC_COLLECTION],
2124                         'cc' => []];
2125
2126                 $signed = LDSignature::sign($data, $owner);
2127
2128                 Logger::info('Deliver profile deletion for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2129                 return HTTPSignature::transmit($signed, $inbox, $owner);
2130         }
2131
2132         /**
2133          * Transmits a profile change to a given inbox
2134          *
2135          * @param array  $owner Sender owner-view record
2136          * @param string $inbox Target inbox
2137          * @return boolean was the transmission successful?
2138          * @throws HTTPException\InternalServerErrorException
2139          * @throws HTTPException\NotFoundException
2140          * @throws \ImagickException
2141          */
2142         public static function sendProfileUpdate(array $owner, string $inbox): bool
2143         {
2144                 $profile = APContact::getByURL($owner['url']);
2145
2146                 $data = ['@context' => ActivityPub::CONTEXT,
2147                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2148                         'type' => 'Update',
2149                         'actor' => $owner['url'],
2150                         'object' => self::getProfile($owner['uid']),
2151                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
2152                         'instrument' => self::getService(),
2153                         'to' => [$profile['followers']],
2154                         'cc' => []];
2155
2156                 $signed = LDSignature::sign($data, $owner);
2157
2158                 Logger::info('Deliver profile update for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2159                 return HTTPSignature::transmit($signed, $inbox, $owner);
2160         }
2161
2162         /**
2163          * Transmits a given activity to a target
2164          *
2165          * @param string  $activity Type name
2166          * @param string  $target   Target profile
2167          * @param integer $uid      User ID
2168          * @param string  $id Activity-identifier
2169          * @return bool
2170          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2171          * @throws \ImagickException
2172          * @throws \Exception
2173          */
2174         public static function sendActivity(string $activity, string $target, int $uid, string $id = ''): bool
2175         {
2176                 $profile = APContact::getByURL($target);
2177                 if (empty($profile['inbox'])) {
2178                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2179                         return false;
2180                 }
2181
2182                 $owner = User::getOwnerDataById($uid);
2183                 if (empty($owner)) {
2184                         Logger::warning('No user found for actor, aborting', ['uid' => $uid]);
2185                         return false;
2186                 }
2187
2188                 if (empty($id)) {
2189                         $id = DI::baseUrl() . '/activity/' . System::createGUID();
2190                 }
2191
2192                 $data = [
2193                         '@context' => ActivityPub::CONTEXT,
2194                         'id' => $id,
2195                         'type' => $activity,
2196                         'actor' => $owner['url'],
2197                         'object' => $profile['url'],
2198                         'instrument' => self::getService(),
2199                         'to' => [$profile['url']],
2200                 ];
2201
2202                 Logger::info('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid);
2203
2204                 $signed = LDSignature::sign($data, $owner);
2205                 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2206         }
2207
2208         /**
2209          * Transmits a "follow object" activity to a target
2210          * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
2211          *
2212          * @param string  $object Object URL
2213          * @param string  $target Target profile
2214          * @param integer $uid    User ID
2215          * @return bool
2216          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2217          * @throws \ImagickException
2218          * @throws \Exception
2219          */
2220         public static function sendFollowObject(string $object, string $target, int $uid = 0): bool
2221         {
2222                 $profile = APContact::getByURL($target);
2223                 if (empty($profile['inbox'])) {
2224                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2225                         return false;
2226                 }
2227
2228                 if (empty($uid)) {
2229                         // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
2230                         $admin = User::getFirstAdmin(['uid']);
2231                         if (!$admin) {
2232                                 Logger::warning('No available admin user for transmission', ['target' => $target]);
2233                                 return false;
2234                         }
2235
2236                         $uid = $admin['uid'];
2237                 }
2238
2239                 $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
2240                         'author-id' => Contact::getPublicIdByUserId($uid)];
2241                 if (Post::exists($condition)) {
2242                         Logger::info('Follow for ' . $object . ' for user ' . $uid . ' does already exist.');
2243                         return false;
2244                 }
2245
2246                 $owner = User::getOwnerDataById($uid);
2247
2248                 $data = [
2249                         '@context' => ActivityPub::CONTEXT,
2250                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2251                         'type' => 'Follow',
2252                         'actor' => $owner['url'],
2253                         'object' => $object,
2254                         'instrument' => self::getService(),
2255                         'to' => [$profile['url']],
2256                 ];
2257
2258                 Logger::info('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid);
2259
2260                 $signed = LDSignature::sign($data, $owner);
2261                 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2262         }
2263
2264         /**
2265          * Transmit a message that the contact request had been accepted
2266          *
2267          * @param string  $target Target profile
2268          * @param string  $id Object id
2269          * @param integer $uid    User ID
2270          * @return void
2271          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2272          * @throws \ImagickException
2273          */
2274         public static function sendContactAccept(string $target, string $id, int $uid)
2275         {
2276                 $profile = APContact::getByURL($target);
2277                 if (empty($profile['inbox'])) {
2278                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2279                         return;
2280                 }
2281
2282                 $owner = User::getOwnerDataById($uid);
2283                 if (!$owner) {
2284                         Logger::notice('No user found for actor', ['uid' => $uid]);
2285                         return;
2286                 }
2287
2288                 $data = [
2289                         '@context' => ActivityPub::CONTEXT,
2290                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2291                         'type' => 'Accept',
2292                         'actor' => $owner['url'],
2293                         'object' => [
2294                                 'id' => $id,
2295                                 'type' => 'Follow',
2296                                 'actor' => $profile['url'],
2297                                 'object' => $owner['url']
2298                         ],
2299                         'instrument' => self::getService(),
2300                         'to' => [$profile['url']],
2301                 ];
2302
2303                 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
2304
2305                 $signed = LDSignature::sign($data, $owner);
2306                 HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2307         }
2308
2309         /**
2310          * Reject a contact request or terminates the contact relation
2311          *
2312          * @param string $target   Target profile
2313          * @param string $objectId Object id
2314          * @param array  $owner    Sender owner-view record
2315          * @return bool Operation success
2316          * @throws HTTPException\InternalServerErrorException
2317          * @throws \ImagickException
2318          */
2319         public static function sendContactReject(string $target, string $objectId, array $owner): bool
2320         {
2321                 $profile = APContact::getByURL($target);
2322                 if (empty($profile['inbox'])) {
2323                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2324                         return false;
2325                 }
2326
2327                 $data = [
2328                         '@context' => ActivityPub::CONTEXT,
2329                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2330                         'type' => 'Reject',
2331                         'actor'  => $owner['url'],
2332                         'object' => [
2333                                 'id' => $objectId,
2334                                 'type' => 'Follow',
2335                                 'actor' => $profile['url'],
2336                                 'object' => $owner['url']
2337                         ],
2338                         'instrument' => self::getService(),
2339                         'to' => [$profile['url']],
2340                 ];
2341
2342                 Logger::debug('Sending reject to ' . $target . ' for user ' . $owner['uid'] . ' with id ' . $objectId);
2343
2344                 $signed = LDSignature::sign($data, $owner);
2345                 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2346         }
2347
2348         /**
2349          * Transmits a message that we don't want to follow this contact anymore
2350          *
2351          * @param string  $target Target profile
2352          * @param integer $cid    Contact id
2353          * @param array   $owner  Sender owner-view record
2354          * @return bool success
2355          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2356          * @throws \ImagickException
2357          * @throws \Exception
2358          */
2359         public static function sendContactUndo(string $target, int $cid, array $owner): bool
2360         {
2361                 $profile = APContact::getByURL($target);
2362                 if (empty($profile['inbox'])) {
2363                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2364                         return false;
2365                 }
2366
2367                 $object_id = self::activityIDFromContact($cid);
2368                 if (empty($object_id)) {
2369                         return false;
2370                 }
2371
2372                 $objectId = DI::baseUrl() . '/activity/' . System::createGUID();
2373
2374                 $data = [
2375                         '@context' => ActivityPub::CONTEXT,
2376                         'id' => $objectId,
2377                         'type' => 'Undo',
2378                         'actor' => $owner['url'],
2379                         'object' => [
2380                                 'id' => $object_id,
2381                                 'type' => 'Follow',
2382                                 'actor' => $owner['url'],
2383                                 'object' => $profile['url']
2384                         ],
2385                         'instrument' => self::getService(),
2386                         'to' => [$profile['url']],
2387                 ];
2388
2389                 Logger::info('Sending undo to ' . $target . ' for user ' . $owner['uid'] . ' with id ' . $objectId);
2390
2391                 $signed = LDSignature::sign($data, $owner);
2392                 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2393         }
2394
2395         /**
2396          * Prepends mentions (@) to $body variable
2397          *
2398          * @param string $body HTML code
2399          * @param int    $uriId
2400          * @param string $authorLink Author link
2401          * @return string HTML code with prepended mentions
2402          */
2403         private static function prependMentions(string $body, int $uriid, string $authorLink): string
2404         {
2405                 $mentions = [];
2406
2407                 foreach (Tag::getByURIId($uriid, [Tag::IMPLICIT_MENTION]) as $tag) {
2408                         $profile = Contact::getByURL($tag['url'], false, ['addr', 'contact-type', 'nick']);
2409                         if (!empty($profile['addr'])
2410                                 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
2411                                 && !strstr($body, $profile['addr'])
2412                                 && !strstr($body, $tag['url'])
2413                                 && $tag['url'] !== $authorLink
2414                         ) {
2415                                 $mentions[] = '@[url=' . $tag['url'] . ']' . $profile['nick'] . '[/url]';
2416                         }
2417                 }
2418
2419                 $mentions[] = $body;
2420
2421                 return implode(' ', $mentions);
2422         }
2423 }