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