3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Protocol\ActivityPub;
25 use Friendica\Content\Feature;
26 use Friendica\Content\Smilies;
27 use Friendica\Content\Text\BBCode;
28 use Friendica\Core\Cache\Enum\Duration;
29 use Friendica\Core\Logger;
30 use Friendica\Core\Protocol;
31 use Friendica\Core\System;
32 use Friendica\Database\DBA;
34 use Friendica\Model\APContact;
35 use Friendica\Model\Contact;
36 use Friendica\Model\GServer;
37 use Friendica\Model\Item;
38 use Friendica\Model\Photo;
39 use Friendica\Model\Post;
40 use Friendica\Model\Tag;
41 use Friendica\Model\User;
42 use Friendica\Network\HTTPException;
43 use Friendica\Protocol\Activity;
44 use Friendica\Protocol\ActivityPub;
45 use Friendica\Protocol\Relay;
46 use Friendica\Util\DateTimeFormat;
47 use Friendica\Util\HTTPSignature;
48 use Friendica\Util\LDSignature;
49 use Friendica\Util\Map;
50 use Friendica\Util\Network;
51 use Friendica\Util\Strings;
52 use Friendica\Util\XML;
55 * ActivityPub Transmitter Protocol class
62 const CACHEKEY_FEATURED = 'transmitter:getFeatured:';
63 const CACHEKEY_CONTACTS = 'transmitter:getContacts:';
66 * Add relay servers to the list of inboxes
68 * @param array $inboxes
69 * @return array inboxes with added relay servers
71 public static function addRelayServerInboxes(array $inboxes = []): array
73 foreach (Relay::getList(['inbox']) as $contact) {
74 $inboxes[$contact['inbox']] = $contact['inbox'];
81 * Add relay servers to the list of inboxes
83 * @param array $inboxes
84 * @return array inboxes with added relay servers
86 public static function addRelayServerInboxesForItem(int $item_id, array $inboxes = []): array
88 $item = Post::selectFirst(['uid'], ['id' => $item_id]);
93 $relays = Relay::getDirectRelayList($item_id);
98 foreach ($relays as $relay) {
99 $contact = Contact::getByURLForUser($relay['url'], $item['uid'], false, ['id']);
100 $inboxes[$relay['batch']][] = $contact['id'] ?? 0;
106 * Subscribe to a relay and updates contact on success
108 * @param string $url Subscribe actor url
109 * @return bool success
111 public static function sendRelayFollow(string $url): bool
113 $contact = Contact::getByURL($url);
114 if (empty($contact)) {
118 $activity_id = self::activityIDFromContact($contact['id']);
119 $success = self::sendActivity('Follow', $url, 0, $activity_id);
121 Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]);
128 * Unsubscribe from a relay and updates contact on success or forced
130 * @param string $url Subscribe actor url
131 * @param bool $force Set the relay status as non follower even if unsubscribe hadn't worked
132 * @return bool success
134 public static function sendRelayUndoFollow(string $url, bool $force = false): bool
136 $contact = Contact::getByURL($url);
137 if (empty($contact)) {
141 $success = self::sendContactUndo($url, $contact['id'], User::getSystemAccount());
143 if ($success || $force) {
144 Contact::update(['rel' => Contact::NOTHING], ['id' => $contact['id']]);
151 * Collects a list of contacts of the given owner
153 * @param array $owner Owner array
154 * @param array $rel The relevant value(s) contact.rel should match
155 * @param string $module The name of the relevant AP endpoint module (followers|following)
156 * @param integer $page Page number
157 * @param string $requester URL of the requester
158 * @param boolean $nocache Wether to bypass caching
159 * @return array of owners
162 public static function getContacts(array $owner, array $rel, string $module, int $page = null, string $requester = null, bool $nocache = false): array
165 $cachekey = self::CACHEKEY_CONTACTS . $module . ':'. $owner['uid'];
166 $result = DI::cache()->get($cachekey);
167 if (!$nocache && !is_null($result)) {
174 'uid' => $owner['uid'],
183 $condition = DBA::mergeConditions($parameters, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
185 $total = DBA::count('contact', $condition);
187 $modulePath = '/' . $module . '/';
189 $data = ['@context' => ActivityPub::CONTEXT];
190 $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
191 $data['type'] = 'OrderedCollection';
192 $data['totalItems'] = $total;
195 $data['id'] .= '?' . http_build_query(['page' => $page]);
198 // When we hide our friends we will only show the pure number but don't allow more.
199 $show_contacts = ActivityPub::isAcceptedRequester($owner['uid']) && empty($owner['hide-friends']);
201 // Allow fetching the contact list when the requester is part of the list.
202 if (($owner['page-flags'] == User::PAGE_FLAGS_PRVGROUP) && !empty($requester)) {
203 $show_contacts = DBA::exists('contact', ['nurl' => Strings::normaliseLink($requester), 'uid' => $owner['uid'], 'blocked' => false]);
206 if (!$show_contacts) {
207 if (!empty($cachekey)) {
208 DI::cache()->set($cachekey, $data, Duration::DAY);
215 $data['first'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=1';
217 $data['type'] = 'OrderedCollectionPage';
220 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
221 while ($contact = DBA::fetch($contacts)) {
222 $list[] = $contact['url'];
224 DBA::close($contacts);
226 if (count($list) == 100) {
227 $data['next'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=' . ($page + 1);
230 $data['partOf'] = DI::baseUrl() . $modulePath . $owner['nickname'];
232 $data['orderedItems'] = $list;
235 if (!empty($cachekey)) {
236 DI::cache()->set($cachekey, $data, Duration::DAY);
243 * Public posts for the given owner
245 * @param array $owner Owner array
246 * @param integer $page Page number
247 * @param boolean $nocache Wether to bypass caching
249 * @return array of posts
250 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
251 * @throws \ImagickException
253 public static function getFeatured(array $owner, int $page = null, bool $nocache = false): array
256 $cachekey = self::CACHEKEY_FEATURED . $owner['uid'];
257 $result = DI::cache()->get($cachekey);
258 if (!$nocache && !is_null($result)) {
263 $owner_cid = Contact::getIdForURL($owner['url'], 0, false);
265 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
266 $owner_cid, Post\Collection::FEATURED];
268 $condition = DBA::mergeConditions($condition, [
269 'uid' => $owner['uid'],
270 'author-id' => $owner_cid,
271 'private' => [Item::PUBLIC, Item::UNLISTED],
272 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT],
273 'network' => Protocol::FEDERATED,
274 'parent-network' => Protocol::FEDERATED,
280 $count = Post::count($condition);
282 $data = ['@context' => ActivityPub::CONTEXT];
283 $data['id'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
284 $data['type'] = 'OrderedCollection';
285 $data['totalItems'] = $count;
288 $data['id'] .= '?' . http_build_query(['page' => $page]);
292 $items = Post::select(['id'], $condition, ['limit' => 20, 'order' => ['created' => true]]);
294 $data['type'] = 'OrderedCollectionPage';
295 $items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
299 while ($item = Post::fetch($items)) {
300 $activity = self::createActivityFromItem($item['id'], true);
301 if (!empty($activity)) {
307 if (count($list) == 20) {
308 $data['next'] = DI::baseUrl() . '/featured/' . $owner['nickname'] . '?page=' . ($page + 1);
312 $data['partOf'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
315 $data['orderedItems'] = $list;
317 if (!empty($cachekey)) {
318 DI::cache()->set($cachekey, $data, Duration::DAY);
325 * Return the service array containing information the used software and it's url
327 * @return array with service data
329 public static function getService(): array
333 'name' => App::PLATFORM . " '" . App::CODENAME . "' " . App::VERSION . '-' . DB_UPDATE_VERSION,
334 'url' => (string)DI::baseUrl()
339 * Return the ActivityPub profile of the given user
341 * @param int $uid User ID
342 * @param bool $full If not full, only the basic information is returned
343 * @return array with profile data
344 * @throws HTTPException\NotFoundException
345 * @throws HTTPException\InternalServerErrorException
347 public static function getProfile(int $uid, bool $full = true): array
349 $owner = User::getOwnerDataById($uid);
350 if (!isset($owner['id'])) {
351 DI::logger()->error('Unable to find owner data for uid', ['uid' => $uid]);
352 throw new HTTPException\NotFoundException('User not found.');
355 $data = ['@context' => ActivityPub::CONTEXT];
356 $data['id'] = $owner['url'];
358 if (!empty($owner['guid'])) {
359 $data['diaspora:guid'] = $owner['guid'];
362 $data['type'] = ActivityPub::ACCOUNT_TYPES[$owner['account-type']];
365 $data['following'] = DI::baseUrl() . '/following/' . $owner['nick'];
366 $data['followers'] = DI::baseUrl() . '/followers/' . $owner['nick'];
367 $data['inbox'] = DI::baseUrl() . '/inbox/' . $owner['nick'];
368 $data['outbox'] = DI::baseUrl() . '/outbox/' . $owner['nick'];
369 $data['featured'] = DI::baseUrl() . '/featured/' . $owner['nick'];
371 $data['inbox'] = DI::baseUrl() . '/friendica/inbox';
374 $data['preferredUsername'] = $owner['nick'];
375 $data['name'] = $owner['name'];
377 if (!$full && !empty($owner['country-name'] . $owner['region'] . $owner['locality'])) {
378 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $owner['country-name'],
379 'vcard:region' => $owner['region'], 'vcard:locality' => $owner['locality']];
382 if ($full && !empty($owner['about'])) {
383 $data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
386 if ($full && (!empty($owner['xmpp']) || !empty($owner['matrix']))) {
387 $data['vcard:hasInstantMessage'] = [];
389 if (!empty($owner['xmpp'])) {
390 $data['vcard:hasInstantMessage'][] = 'xmpp:' . $owner['xmpp'];
392 if (!empty($owner['matrix'])) {
393 $data['vcard:hasInstantMessage'][] = 'matrix:' . $owner['matrix'];
397 $data['url'] = $owner['url'];
398 $data['manuallyApprovesFollowers'] = in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
399 $data['discoverable'] = (bool)$owner['net-publish'];
400 $data['publicKey'] = ['id' => $owner['url'] . '#main-key',
401 'owner' => $owner['url'],
402 'publicKeyPem' => $owner['pubkey']];
403 $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
404 if ($full && $uid != 0) {
405 $data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
407 $resourceid = Photo::ridFromURI($owner['photo']);
408 if (!empty($resourceid)) {
409 $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
410 if (!empty($photo['type'])) {
411 $data['icon']['mediaType'] = $photo['type'];
415 if (!empty($owner['header'])) {
416 $data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($owner['id'], '', $owner['updated'])];
418 $resourceid = Photo::ridFromURI($owner['header']);
419 if (!empty($resourceid)) {
420 $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
421 if (!empty($photo['type'])) {
422 $data['image']['mediaType'] = $photo['type'];
429 foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) {
431 'type' => 'PropertyValue',
432 'name' => $profile_field->label,
433 'value' => BBCode::convertForUriId($owner['uri-id'], $profile_field->value)
437 if (!empty($custom_fields)) {
438 $data['attachment'] = $custom_fields;
442 $data['generator'] = self::getService();
444 // tags: https://kitty.town/@inmysocks/100656097926961126.json
449 * Get a minimal actor array for the C2S API
451 * @param integer $cid
454 private static function getActorArrayByCid(int $cid): array
456 $contact = Contact::getById($cid);
458 'id' => $contact['url'],
459 'type' => $data['type'] = ActivityPub::ACCOUNT_TYPES[$contact['contact-type']],
460 'url' => $contact['alias'],
461 'preferredUsername' => $contact['nick'],
462 'name' => $contact['name'],
463 'icon' => ['type' => 'Image', 'url' => Contact::getAvatarUrlForId($cid, '', $contact['updated'])],
464 'image' => ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($cid, '', $contact['updated'])],
465 'manuallyApprovesFollowers' => (bool)$contact['manually-approve'],
466 'discoverable' => !$contact['unsearchable'],
469 if (empty($data['url'])) {
470 $data['url'] = $data['id'];
477 * @param string $username
479 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
481 public static function getDeletedUser(string $username): array
484 '@context' => ActivityPub::CONTEXT,
485 'id' => DI::baseUrl() . '/profile/' . $username,
486 'type' => 'Tombstone',
487 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
488 'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
489 'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
494 * Returns an array with permissions of the thread parent of the given item array
498 * @return array with permissions
499 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
500 * @throws \ImagickException
502 private static function fetchPermissionBlockFromThreadParent(array $item, bool $is_group_thread): array
504 if (empty($item['thr-parent-id'])) {
508 $parent = Post::selectFirstPost(['author-link'], ['uri-id' => $item['thr-parent-id']]);
509 if (empty($parent)) {
514 'to' => [$parent['author-link']],
521 $parent_profile = APContact::getByURL($parent['author-link']);
523 $item_profile = APContact::getByURL($item['author-link']);
524 $exclude[] = $item['author-link'];
526 if ($item['gravity'] == Item::GRAVITY_PARENT) {
527 $exclude[] = $item['owner-link'];
530 $type = [Tag::TO => 'to', Tag::CC => 'cc', Tag::BTO => 'bto', Tag::BCC => 'bcc', Tag::AUDIENCE => 'audience'];
531 foreach (Tag::getByURIId($item['thr-parent-id'], [Tag::TO, Tag::CC, Tag::BTO, Tag::BCC, Tag::AUDIENCE]) as $receiver) {
532 if (!empty($parent_profile['followers']) && $receiver['url'] == $parent_profile['followers'] && !empty($item_profile['followers'])) {
533 if (!$is_group_thread) {
534 $permissions[$type[$receiver['type']]][] = $item_profile['followers'];
536 } elseif (!in_array($receiver['url'], $exclude)) {
537 $permissions[$type[$receiver['type']]][] = $receiver['url'];
545 * Check if the given item id is from ActivityPub
547 * @param integer $item_id
548 * @return boolean "true" if the post is from ActivityPub
550 private static function isAPPost(int $item_id): bool
552 if (empty($item_id)) {
556 return Post::exists(['id' => $item_id, 'network' => Protocol::ACTIVITYPUB]);
560 * Creates an array of permissions from an item thread
562 * @param array $item Item array
563 * @param boolean $blindcopy addressing via "bcc" or "cc"?
564 * @param integer $last_id Last item id for adding receivers
566 * @return array with permission data
567 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
568 * @throws \ImagickException
570 private static function createPermissionBlockForItem(array $item, bool $blindcopy, int $last_id = 0): array
573 $last_id = $item['id'];
583 // Check if we should always deliver our stuff via BCC
584 if (!empty($item['uid'])) {
585 $owner = User::getOwnerDataById($item['uid']);
586 if (!empty($owner)) {
587 $always_bcc = $owner['hide-friends'];
588 $is_group = ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
590 $profile = APContact::getByURL($owner['url'], false);
591 $follower = $profile['followers'] ?? '';
595 if (DI::config()->get('system', 'ap_always_bcc')) {
599 $parent = Post::selectFirst(['causer-link', 'post-reason'], ['id' => $item['parent']]);
600 if (!empty($parent) && ($parent['post-reason'] == Item::PR_ANNOUNCEMENT) && !empty($parent['causer-link'])) {
601 $profile = APContact::getByURL($parent['causer-link'], false);
602 $is_group_thread = isset($profile['type']) && $profile['type'] == 'Group';
604 $is_group_thread = false;
608 $parent_tags = Tag::getByURIId($item['parent-uri-id'], [Tag::AUDIENCE, Tag::MENTION]);
609 if (!empty($parent_tags)) {
610 $is_group_thread = false;
611 foreach ($parent_tags as $tag) {
612 if ($tag['type'] != Tag::AUDIENCE) {
615 $profile = APContact::getByURL($tag['url'], false);
616 if (!empty($profile) && ($profile['type'] == 'Group')) {
617 $audience[] = $tag['url'];
618 $is_group_thread = true;
621 if ($is_group_thread) {
622 foreach ($parent_tags as $tag) {
623 if (($tag['type'] == Tag::MENTION) && in_array($tag['url'], $audience)) {
627 $exclusive = !$mention;
629 } elseif ($is_group_thread) {
630 foreach (Tag::getByURIId($item['parent-uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $term) {
631 $profile = APContact::getByURL($term['url'], false);
632 if (!empty($profile) && ($profile['type'] == 'Group')) {
633 if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
634 $audience[] = $term['url'];
636 } elseif ($term['type'] == Tag::MENTION) {
643 $audience[] = $owner['url'];
646 if (self::isAnnounce($item) || self::isAPPost($last_id)) {
647 // Will be activated in a later step
648 $networks = Protocol::FEDERATED;
650 // For now only send to these contacts:
651 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
654 $data = ['to' => [], 'cc' => [], 'bcc' => [] , 'audience' => $audience];
656 if ($item['gravity'] == Item::GRAVITY_PARENT) {
657 $actor_profile = APContact::getByURL($item['owner-link']);
659 $actor_profile = APContact::getByURL($item['author-link']);
662 $terms = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION, Tag::AUDIENCE]);
664 if ($item['private'] != Item::PRIVATE) {
665 // Directly mention the original author upon a quoted reshare.
666 // Else just ensure that the original author receives the reshare.
667 $announce = self::getAnnounceArray($item);
668 if (!empty($announce['comment'])) {
669 $data['to'][] = $announce['actor']['url'];
670 } elseif (!empty($announce)) {
671 $data['cc'][] = $announce['actor']['url'];
675 $data = array_merge_recursive($data, self::fetchPermissionBlockFromThreadParent($item, $is_group_thread));
678 // Check if the item is completely public or unlisted
679 if ($item['private'] == Item::PUBLIC) {
680 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
682 $data['cc'][] = ActivityPub::PUBLIC_COLLECTION;
685 foreach ($terms as $term) {
686 $profile = APContact::getByURL($term['url'], false);
687 if (!empty($profile)) {
688 if (($term['type'] == Tag::AUDIENCE) && ($profile['type'] == 'Group')) {
689 $data['audience'][] = $profile['url'];
691 if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
693 if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
694 $data['cc'][] = $profile['followers'];
695 $data['audience'][] = $profile['url'];
697 } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
700 $data['to'][] = $profile['url'];
703 if (!$exclusive && ($item['private'] == Item::UNLISTED)) {
704 $data['to'][] = $actor_profile['followers'];
707 $receiver_list = Item::enumeratePermissions($item, true, false);
709 foreach ($terms as $term) {
710 $cid = Contact::getIdForURL($term['url'], $item['uid']);
711 if (!empty($cid) && in_array($cid, $receiver_list)) {
712 $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol', 'gsid'], ['id' => $cid, 'network' => Protocol::FEDERATED]);
713 if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
717 $profile = APContact::getByURL($term['url'], false);
718 if (!empty($profile)) {
719 if (($term['type'] == Tag::AUDIENCE) && ($profile['type'] == 'Group')) {
720 $data['audience'][] = $profile['url'];
722 if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
724 if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
725 $data['cc'][] = $profile['followers'];
726 $data['audience'][] = $profile['url'];
728 } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
731 $data['to'][] = $profile['url'];
740 if ($is_group && !$exclusive && !empty($follower)) {
741 $data['cc'][] = $follower;
742 } elseif (!$exclusive) {
743 foreach ($receiver_list as $receiver) {
744 if ($receiver == -1) {
745 $data['to'][] = $actor_profile['followers'];
749 $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]);
750 if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
754 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
755 if ($contact['hidden'] || $always_bcc) {
756 $data['bcc'][] = $profile['url'];
758 $data['cc'][] = $profile['url'];
765 if (!empty($item['parent']) && (!$exclusive || ($item['private'] == Item::PRIVATE))) {
766 if ($item['private'] == Item::PRIVATE) {
767 $condition = ['parent' => $item['parent'], 'uri-id' => $item['thr-parent-id']];
769 $condition = ['parent' => $item['parent']];
771 $parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], $condition, ['order' => ['id']]);
772 while ($parent = Post::fetch($parents)) {
773 if ($parent['gravity'] == Item::GRAVITY_PARENT) {
774 $profile = APContact::getByURL($parent['owner-link'], false);
775 if (!empty($profile)) {
776 if ($item['gravity'] != Item::GRAVITY_PARENT) {
777 // Comments to groups are directed to the group
778 // But comments to groups aren't directed to the followers collection
779 // This rule is only valid when the actor isn't the group.
780 // The group needs to transmit their content to their followers.
781 if (($profile['type'] == 'Group') && ($profile['url'] != ($actor_profile['url'] ?? ''))) {
782 $data['to'][] = $profile['url'];
784 $data['cc'][] = $profile['url'];
785 if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers']) && (!$exclusive || !$is_group_thread)) {
786 $data['cc'][] = $actor_profile['followers'];
789 } elseif (!$exclusive && !$is_group_thread) {
790 // Public thread parent post always are directed to the followers.
791 if ($item['private'] != Item::PRIVATE) {
792 $data['cc'][] = $actor_profile['followers'];
798 // Don't include data from future posts
799 if ($parent['id'] >= $last_id) {
803 $profile = APContact::getByURL($parent['author-link'], false);
804 if (!empty($profile)) {
805 if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
806 $data['to'][] = $profile['url'];
808 $data['cc'][] = $profile['url'];
812 DBA::close($parents);
815 $data['to'] = array_unique($data['to']);
816 $data['cc'] = array_unique($data['cc']);
817 $data['bcc'] = array_unique($data['bcc']);
818 $data['audience'] = array_unique($data['audience']);
820 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
821 unset($data['to'][$key]);
824 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
825 unset($data['cc'][$key]);
828 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
829 unset($data['bcc'][$key]);
832 foreach ($data['to'] as $to) {
833 if (($key = array_search($to, $data['cc'])) !== false) {
834 unset($data['cc'][$key]);
837 if (($key = array_search($to, $data['bcc'])) !== false) {
838 unset($data['bcc'][$key]);
842 foreach ($data['cc'] as $cc) {
843 if (($key = array_search($cc, $data['bcc'])) !== false) {
844 unset($data['bcc'][$key]);
848 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc']), 'audience' => array_values($data['audience'])];
851 unset($receivers['bcc']);
854 if (!$blindcopy && count($receivers['audience']) == 1) {
855 $receivers['audience'] = $receivers['audience'][0];
856 } elseif (!$receivers['audience']) {
857 unset($receivers['audience']);
864 * Store the receivers for the given item
869 public static function storeReceiversForItem(array $item)
871 $receivers = self::createPermissionBlockForItem($item, true);
872 if (empty($receivers)) {
876 foreach (['to' => Tag::TO, 'cc' => Tag::CC, 'bcc' => Tag::BCC, 'audience' => Tag::AUDIENCE] as $element => $type) {
877 if (!empty($receivers[$element])) {
878 foreach ($receivers[$element] as $receiver) {
879 if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
880 $name = Receiver::PUBLIC_COLLECTION;
882 $name = trim(parse_url($receiver, PHP_URL_PATH), '/');
884 Tag::store($item['uri-id'], $type, $name, $receiver);
891 * Get a list of receivers for the provided uri-id
894 * @param boolean $blindcopy
897 public static function getReceiversForUriId(int $uri_id, bool $blindcopy)
899 $tags = Tag::getByURIId($uri_id, [Tag::TO, Tag::CC, Tag::BCC, Tag::AUDIENCE]);
901 Logger::debug('No receivers found', ['uri-id' => $uri_id]);
902 $post = Post::selectFirst(Item::DELIVER_FIELDLIST, ['uri-id' => $uri_id, 'origin' => true]);
904 ActivityPub\Transmitter::storeReceiversForItem($post);
905 $tags = Tag::getByURIId($uri_id, [Tag::TO, Tag::CC, Tag::BCC, Tag::AUDIENCE]);
906 Logger::debug('Receivers are created', ['uri-id' => $uri_id, 'receivers' => count($tags)]);
908 Logger::debug('Origin item not found', ['uri-id' => $uri_id]);
919 foreach ($tags as $receiver) {
920 switch ($receiver['type']) {
922 $receivers['to'][] = $receiver['url'];
925 $receivers['cc'][] = $receiver['url'];
928 $receivers['bcc'][] = $receiver['url'];
931 $receivers['audience'][] = $receiver['url'];
937 unset($receivers['bcc']);
940 if (!$blindcopy && count($receivers['audience']) == 1) {
941 $receivers['audience'] = $receivers['audience'][0];
942 } elseif (!$receivers['audience']) {
943 unset($receivers['audience']);
950 * Check if an inbox is archived
952 * @param string $url Inbox url
953 * @return boolean "true" if inbox is archived
955 public static function archivedInbox(string $url): bool
957 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
961 * Check if a given contact should be delivered via AP
963 * @param array $contact Contact array
964 * @param array $networks Array with networks
965 * @return bool Whether the used protocol matches ACTIVITYPUB
968 private static function isAPContact(array $contact, array $networks): bool
970 if (in_array($contact['network'], $networks) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
974 return GServer::getProtocol($contact['gsid'] ?? 0) == Post\DeliveryData::ACTIVITYPUB;
978 * Fetches a list of inboxes of followers of a given user
980 * @param integer $uid User ID
981 * @param boolean $all_ap Retrieve all AP enabled inboxes
982 * @return array of follower inboxes
983 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
984 * @throws \ImagickException
986 public static function fetchTargetInboxesforUser(int $uid, bool $all_ap = false): array
991 if (!empty($item['uid'])) {
992 $profile = User::getOwnerDataById($item['uid']);
993 if (!empty($profile)) {
994 $isGroup = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
999 // Will be activated in a later step
1000 $networks = Protocol::FEDERATED;
1002 // For now only send to these contacts:
1003 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
1012 'network' => Protocol::FEDERATED,
1013 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON, Contact::TYPE_NEWS, Contact::TYPE_ORGANISATION],
1017 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
1020 $contacts = DBA::select('contact', ['id', 'url', 'network', 'protocol', 'gsid'], $condition);
1021 while ($contact = DBA::fetch($contacts)) {
1022 if (!self::isAPContact($contact, $networks)) {
1026 if ($isGroup && ($contact['network'] == Protocol::DFRN)) {
1030 if (Network::isUrlBlocked($contact['url'])) {
1034 $profile = APContact::getByURL($contact['url'], false);
1035 if (!empty($profile)) {
1036 if (empty($profile['sharedinbox']) || Contact::isLocal($contact['url'])) {
1037 $target = $profile['inbox'];
1039 $target = $profile['sharedinbox'];
1041 if (!self::archivedInbox($target)) {
1042 $inboxes[$target][] = $contact['id'];
1046 DBA::close($contacts);
1052 * Fetches an array of inboxes for the given item and user
1054 * @param array $item Item array
1055 * @param integer $uid User ID
1056 * @return array with inboxes
1057 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1058 * @throws \ImagickException
1060 public static function fetchTargetInboxes(array $item, int $uid): array
1062 $permissions = self::getReceiversForUriId($item['uri-id'], true);
1063 if (empty($permissions)) {
1069 if ($item['gravity'] == Item::GRAVITY_ACTIVITY) {
1070 $item_profile = APContact::getByURL($item['author-link'], false);
1072 $item_profile = APContact::getByURL($item['owner-link'], false);
1075 if (empty($item_profile)) {
1079 $profile_uid = User::getIdForURL($item_profile['url']);
1081 foreach (['to', 'cc', 'bto', 'bcc', 'audience'] as $element) {
1082 if (empty($permissions[$element])) {
1086 $blindcopy = in_array($element, ['bto', 'bcc']);
1088 foreach ($permissions[$element] as $receiver) {
1089 if (empty($receiver) || Network::isUrlBlocked($receiver)) {
1093 if ($item_profile && ($receiver == $item_profile['followers']) && ($uid == $profile_uid)) {
1094 $inboxes = array_merge_recursive($inboxes, self::fetchTargetInboxesforUser($uid, true));
1096 $profile = APContact::getByURL($receiver, false);
1097 if (!empty($profile)) {
1098 $contact = Contact::getByURLForUser($receiver, $uid, false, ['id']);
1100 if (empty($profile['sharedinbox']) || $blindcopy || Contact::isLocal($receiver)) {
1101 $target = $profile['inbox'];
1103 $target = $profile['sharedinbox'];
1105 if (!self::archivedInbox($target) && !in_array($contact['id'], $inboxes[$target] ?? [])) {
1106 $inboxes[$target][] = $contact['id'] ?? 0;
1117 * Fetch the target inboxes for a given mail id
1119 * @param integer $mail_id
1122 public static function fetchTargetInboxesFromMail(int $mail_id): array
1124 $mail = DBA::selectFirst('mail', ['uid', 'parent-uri', 'from-url'], ['id' => $mail_id]);
1125 if (!DBA::isResult($mail)) {
1129 $reply = DBA::selectFirst('mail', ['from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
1130 if (!DBA::isResult($reply)) {
1134 $apcontact = APContact::getByURL($reply['from-url'], false);
1136 return [$apcontact['inbox'] => [Contact::getIdForURL($reply['from-url'])]];
1140 * Creates an array in the structure of the item table for a given mail id
1142 * @param integer $mail_id Mail id
1144 * @throws \Exception
1146 public static function getItemArrayFromMail(int $mail_id, bool $use_title = false): array
1148 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
1149 if (!DBA::isResult($mail)) {
1153 $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
1154 if (!DBA::isResult($reply)) {
1158 // Making the post more compatible for Mastodon by:
1159 // - Making it a note and not an article (no title)
1160 // - Moving the title into the "summary" field that is used as a "content warning"
1163 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
1164 $mail['title'] = '';
1167 $mail['content-warning'] = '';
1168 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
1169 $mail['owner-id'] = $mail['author-id'];
1170 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
1171 $mail['allow_gid'] = '';
1172 $mail['deny_cid'] = '';
1173 $mail['deny_gid'] = '';
1174 $mail['private'] = Item::PRIVATE;
1175 $mail['deleted'] = false;
1176 $mail['edited'] = $mail['created'];
1177 $mail['plink'] = DI::baseUrl() . '/message/' . $mail['id'];
1178 $mail['parent-uri'] = $reply['uri'];
1179 $mail['parent-uri-id'] = $reply['uri-id'];
1180 $mail['parent-author-id'] = Contact::getIdForURL($reply['from-url'], 0, false);
1181 $mail['gravity'] = ($mail['reply'] ? Item::GRAVITY_COMMENT: Item::GRAVITY_PARENT);
1182 $mail['event-type'] = '';
1183 $mail['language'] = '';
1184 $mail['parent'] = 0;
1190 * Creates an activity array for a given mail id
1192 * @param integer $mail_id
1193 * @param boolean $object_mode Is the activity item is used inside another object?
1195 * @return array of activity
1196 * @throws \Exception
1198 public static function createActivityFromMail(int $mail_id, bool $object_mode = false): array
1200 $mail = self::getItemArrayFromMail($mail_id);
1204 $object = self::createNote($mail);
1206 if (!$object_mode) {
1207 $data = ['@context' => ActivityPub::CONTEXT];
1212 $data['id'] = $mail['uri'] . '/Create';
1213 $data['type'] = 'Create';
1214 $data['actor'] = $mail['author-link'];
1215 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
1216 $data['instrument'] = self::getService();
1217 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
1219 if (empty($data['to']) && !empty($data['cc'])) {
1220 $data['to'] = $data['cc'];
1223 if (empty($data['to']) && !empty($data['bcc'])) {
1224 $data['to'] = $data['bcc'];
1228 unset($data['bcc']);
1229 unset($data['audience']);
1231 $object['to'] = $data['to'];
1232 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
1234 unset($object['cc']);
1235 unset($object['bcc']);
1236 unset($object['audience']);
1238 $data['directMessage'] = true;
1240 $data['object'] = $object;
1242 $owner = User::getOwnerDataById($mail['uid']);
1244 if (!$object_mode && !empty($owner)) {
1245 return LDSignature::sign($data, $owner);
1252 * Returns the activity type of a given item
1254 * @param array $item Item array
1255 * @return string with activity type
1256 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1257 * @throws \ImagickException
1259 private static function getTypeOfItem(array $item): string
1263 // Only check for a reshare, if it is a real reshare and no quoted reshare
1264 if (strpos($item['body'], '[share') === 0) {
1265 $announce = self::getAnnounceArray($item);
1266 $reshared = !empty($announce);
1271 } elseif ($item['verb'] == Activity::POST) {
1272 if ($item['created'] == $item['edited']) {
1277 } elseif ($item['verb'] == Activity::LIKE) {
1279 } elseif ($item['verb'] == Activity::DISLIKE) {
1281 } elseif ($item['verb'] == Activity::ATTEND) {
1283 } elseif ($item['verb'] == Activity::ATTENDNO) {
1285 } elseif ($item['verb'] == Activity::ATTENDMAYBE) {
1286 $type = 'TentativeAccept';
1287 } elseif ($item['verb'] == Activity::FOLLOW) {
1289 } elseif ($item['verb'] == Activity::TAG) {
1291 } elseif ($item['verb'] == Activity::ANNOUNCE) {
1301 * Creates the activity or fetches it from the cache
1303 * @param integer $item_id Item id
1304 * @param boolean $force Force new cache entry
1305 * @param boolean $object_mode true = Create the object, false = create the activity with the object
1306 * @param boolean $announce_activity true = the announced object is the activity, false = we announce the object link
1307 * @return array|false activity or false on failure
1308 * @throws \Exception
1310 public static function createCachedActivityFromItem(int $item_id, bool $force = false, bool $object_mode = false, $announce_activity = false)
1312 $cachekey = 'APDelivery:createActivity:' . $item_id . ':' . (int)$object_mode . ':' . (int)$announce_activity;
1315 $data = DI::cache()->get($cachekey);
1316 if (!is_null($data)) {
1321 $data = self::createActivityFromItem($item_id, $object_mode, false, $announce_activity);
1323 DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
1328 * Creates an activity array for a given item id
1330 * @param integer $item_id
1331 * @param boolean $object_mode true = Create the object, false = create the activity with the object
1332 * @param boolean $api_mode true = used for the API
1333 * @param boolean $announce_activity true = the announced object is the activity, false = we announce the object link
1334 * @return false|array
1335 * @throws \Exception
1337 public static function createActivityFromItem(int $item_id, bool $object_mode = false, $api_mode = false, $announce_activity = false)
1339 $condition = ['id' => $item_id];
1341 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
1343 Logger::info('Fetching activity', $condition);
1344 $item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
1345 if (!DBA::isResult($item)) {
1348 return self::createActivityFromArray($item, $object_mode, $api_mode, $announce_activity);
1352 * Creates an activity array for a given URI-Id and uid
1354 * @param integer $uri_id
1355 * @param integer $uid
1356 * @param boolean $object_mode true = Create the object, false = create the activity with the object
1357 * @param boolean $api_mode true = used for the API
1358 * @param boolean $announce_activity true = the announced object is the activity, false = we announce the object link
1359 * @return false|array
1360 * @throws \Exception
1362 public static function createActivityFromUriId(int $uri_id, int $uid, bool $object_mode = false, $api_mode = false, $announce_activity = false)
1364 $condition = ['uri-id' => $uri_id, 'uid' => [0, $uid]];
1366 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
1368 Logger::info('Fetching activity', $condition);
1369 $item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition, ['order' => ['uid' => true]]);
1370 if (!DBA::isResult($item)) {
1374 return self::createActivityFromArray($item, $object_mode, $api_mode, $announce_activity);
1378 * Creates an activity array for a given item id
1380 * @param integer $item_id
1381 * @param boolean $object_mode true = Create the object, false = create the activity with the object
1382 * @param boolean $api_mode true = used for the API
1383 * @param boolean $announce_activity true = the announced object is the activity, false = we announce the object link
1384 * @return false|array
1385 * @throws \Exception
1387 private static function createActivityFromArray(array $item, bool $object_mode = false, $api_mode = false, $announce_activity = false)
1389 if (!$api_mode && !$item['deleted'] && $item['network'] == Protocol::ACTIVITYPUB) {
1390 $data = Post\Activity::getByURIId($item['uri-id']);
1391 if (!$item['origin'] && !empty($data)) {
1392 if (!$object_mode) {
1393 Logger::info('Return stored conversation', ['item' => $item['id']]);
1395 } elseif (!empty($data['object'])) {
1396 Logger::info('Return stored conversation object', ['item' => $item['id']]);
1397 return $data['object'];
1402 if (!$api_mode && !$item['origin']) {
1403 Logger::debug('Post is not ours and is not stored', ['id' => $item['id'], 'uri-id' => $item['uri-id']]);
1407 $type = self::getTypeOfItem($item);
1409 if (!$object_mode) {
1410 $data = ['@context' => $context ?? ActivityPub::CONTEXT];
1412 if ($item['deleted'] && ($item['gravity'] == Item::GRAVITY_ACTIVITY)) {
1414 } elseif ($item['deleted']) {
1421 if ($type == 'Delete') {
1422 $data['id'] = Item::newURI($item['guid']) . '/' . $type;;
1423 } elseif (($item['gravity'] == Item::GRAVITY_ACTIVITY) && ($type != 'Undo')) {
1424 $data['id'] = $item['uri'];
1426 $data['id'] = $item['uri'] . '/' . $type;
1429 $data['type'] = $type;
1431 if (($type != 'Announce') || ($item['gravity'] != Item::GRAVITY_PARENT)) {
1432 $link = $item['author-link'];
1433 $id = $item['author-id'];
1435 $link = $item['owner-link'];
1436 $id = $item['owner-id'];
1440 $data['actor'] = self::getActorArrayByCid($id);
1442 $data['actor'] = $link;
1445 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1447 $data['instrument'] = self::getService();
1449 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
1451 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
1452 $data['object'] = self::createNote($item, $api_mode);
1453 } elseif ($data['type'] == 'Add') {
1454 $data = self::createAddTag($item, $data);
1455 } elseif ($data['type'] == 'Announce') {
1456 if ($item['verb'] == ACTIVITY::ANNOUNCE) {
1457 if ($announce_activity) {
1458 $anounced_item = Post::selectFirst(['uid'], ['uri-id' => $item['thr-parent-id'], 'origin' => true]);
1459 $data['object'] = self::createActivityFromUriId($item['thr-parent-id'], $anounced_item['uid'] ?? 0);
1460 unset($data['object']['@context']);
1462 $data['object'] = $item['thr-parent'];
1465 $data = self::createAnnounce($item, $data, $api_mode);
1467 } elseif ($data['type'] == 'Follow') {
1468 $data['object'] = $item['parent-uri'];
1469 } elseif ($data['type'] == 'Undo') {
1470 $data['object'] = self::createActivityFromItem($item['id'], true);
1472 $data['diaspora:guid'] = $item['guid'];
1473 if (!empty($item['signed_text'])) {
1474 $data['diaspora:like'] = $item['signed_text'];
1476 $data['object'] = $item['thr-parent'];
1479 if (!empty($item['contact-uid'])) {
1480 $uid = $item['contact-uid'];
1482 $uid = $item['uid'];
1485 Logger::info('Fetched activity', ['item' => $item['id'], 'uid' => $uid]);
1487 // We only sign our own activities
1488 if (!$api_mode && !$object_mode && $item['origin']) {
1489 $owner = User::getOwnerDataById($uid);
1490 return LDSignature::sign($data, $owner);
1495 /// @todo Create "conversation" entry
1499 * Creates a location entry for a given item array
1501 * @param array $item Item array
1502 * @return array with location array
1504 private static function createLocation(array $item): array
1506 $location = ['type' => 'Place'];
1508 if (!empty($item['location'])) {
1509 $location['name'] = $item['location'];
1514 if (empty($item['coord'])) {
1515 $coord = Map::getCoordinates($item['location']);
1517 $coords = explode(' ', $item['coord']);
1518 if (count($coords) == 2) {
1519 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
1523 if (!empty($coord['lat']) && !empty($coord['lon'])) {
1524 $location['latitude'] = $coord['lat'];
1525 $location['longitude'] = $coord['lon'];
1532 * Appends emoji tags to a tag array according to the tags used.
1534 * @param array $tags Tag array
1535 * @param string $text Text containing tags like :tag:
1536 * @return string normalized text
1538 private static function addEmojiTags(array &$tags, string $text): string
1540 $emojis = Smilies::extractUsedSmilies($text, $normalized);
1541 foreach ($emojis as $name => $url) {
1555 * Returns a tag array for a given item array
1557 * @param array $item Item array
1558 * @param string $quote_url Url of the attached quote link
1559 * @return array of tags
1560 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1562 private static function createTagList(array $item, string $quote_url): array
1566 $terms = Tag::getByURIId($item['uri-id'], [Tag::HASHTAG, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
1567 foreach ($terms as $term) {
1568 if ($term['type'] == Tag::HASHTAG) {
1569 $url = DI::baseUrl() . '/search?tag=' . urlencode($term['name']);
1570 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']];
1572 $contact = Contact::getByURL($term['url'], false, ['addr']);
1573 if (empty($contact)) {
1576 if (!empty($contact['addr'])) {
1577 $mention = '@' . $contact['addr'];
1579 $mention = '@' . $term['url'];
1582 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
1586 $announce = self::getAnnounceArray($item);
1587 // Mention the original author upon commented reshares
1588 if (!empty($announce['comment'])) {
1589 $tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']];
1592 // @see https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-e232.md
1593 if (!empty($quote_url)) {
1594 // Currently deactivated because of compatibility issues with Pleroma
1596 // 'type' => 'Link',
1597 // 'mediaType' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
1598 // 'href' => $quote_url,
1599 // 'name' => '♲ ' . BBCode::convertForUriId($item['uri-id'], $quote_url, BBCode::ACTIVITYPUB)
1607 * Adds attachment data to the JSON document
1609 * @param array $item Data of the item that is to be posted
1611 * @return array with attachment data
1612 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1614 private static function createAttachmentList(array $item): array
1619 foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO, Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
1620 if (in_array($attachment['url'], $urls)) {
1623 $urls[] = $attachment['url'];
1625 $attach = ['type' => 'Document',
1626 'mediaType' => $attachment['mimetype'],
1627 'url' => $attachment['url'],
1628 'name' => $attachment['description']];
1630 if (!empty($attachment['height'])) {
1631 $attach['height'] = $attachment['height'];
1634 if (!empty($attachment['width'])) {
1635 $attach['width'] = $attachment['width'];
1638 if (!empty($attachment['preview'])) {
1639 $attach['image'] = $attachment['preview'];
1642 $attachments[] = $attach;
1645 return $attachments;
1649 * Callback function to replace a Friendica style mention in a mention for a summary
1651 * @param array $match Matching values for the callback
1652 * @return string Replaced mention
1653 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1655 private static function mentionAddrCallback(array $match): string
1657 if (empty($match[1])) {
1661 $data = Contact::getByURL($match[1], false, ['addr']);
1662 if (empty($data['addr'])) {
1666 return '@' . $data['addr'];
1670 * Remove image elements since they are added as attachment
1672 * @param string $body HTML code
1673 * @return string with removed images
1675 private static function removePictures(string $body): string
1677 return BBCode::performWithEscapedTags($body, ['code', 'noparse', 'nobb', 'pre'], function ($text) {
1678 // Simplify image codes
1679 $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $text);
1680 $text = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $text);
1682 // Now remove local links
1683 $text = preg_replace_callback(
1684 '/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
1686 // We remove the link when it is a link to a local photo page
1687 if (Photo::isLocalPage($match[1])) {
1690 // otherwise we just return the link
1691 return '[url]' . $match[1] . '[/url]';
1696 // Remove all pictures
1697 return preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $text);
1702 * Returns if the post contains sensitive content ("nsfw")
1704 * @param integer $uri_id URI id
1705 * @return boolean Whether URI id was found
1706 * @throws \Exception
1708 private static function isSensitive(int $uri_id): bool
1710 return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw', 'type' => Tag::HASHTAG]);
1714 * Creates event data
1716 * @param array $item Item array
1717 * @return array with the event data
1718 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1720 private static function createEvent(array $item): array
1723 $event['name'] = $item['event-summary'];
1724 $event['content'] = BBCode::convertForUriId($item['uri-id'], $item['event-desc'], BBCode::ACTIVITYPUB);
1725 $event['startTime'] = DateTimeFormat::utc($item['event-start'], 'c');
1727 if (!$item['event-nofinish']) {
1728 $event['endTime'] = DateTimeFormat::utc($item['event-finish'], 'c');
1731 if (!empty($item['event-location'])) {
1732 $item['location'] = $item['event-location'];
1733 $event['location'] = self::createLocation($item);
1736 // 2021.12: Backward compatibility value, all the events now "adjust" to the viewer timezone
1737 $event['dfrn:adjust'] = true;
1743 * Creates a note/article object array
1745 * @param array $item
1746 * @param bool $api_mode
1747 * @return array with the object data
1748 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1749 * @throws \ImagickException
1751 public static function createNote(array $item, bool $api_mode = false): array
1757 // We are treating posts differently when they are directed to a community.
1758 // This is done to better support Lemmy. Most of the changes should work with other systems as well.
1759 // But to not risk compatibility issues we currently perform the changes only for communities.
1760 if ($item['gravity'] == Item::GRAVITY_PARENT) {
1761 $isCommunityPost = !empty(Tag::getByURIId($item['uri-id'], [Tag::EXCLUSIVE_MENTION]));
1762 $links = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
1763 if ($isCommunityPost && (count($links) == 1)) {
1764 $link = $links[0]['url'];
1767 $isCommunityPost = false;
1770 if ($item['event-type'] == 'event') {
1772 } elseif (!empty($item['title'])) {
1773 if (!$isCommunityPost || empty($link)) {
1776 // "Page" is used by Lemmy for posts that contain an external link
1783 if ($item['deleted']) {
1784 $type = 'Tombstone';
1788 $data['id'] = $item['uri'];
1789 $data['type'] = $type;
1791 if ($item['deleted']) {
1795 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1797 if ($item['uri'] != $item['thr-parent']) {
1798 $data['inReplyTo'] = $item['thr-parent'];
1800 $data['inReplyTo'] = null;
1803 $data['diaspora:guid'] = $item['guid'];
1804 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1806 if ($item['created'] != $item['edited']) {
1807 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1810 $data['url'] = $link ?? $item['plink'];
1812 $data['attributedTo'] = self::getActorArrayByCid($item['author-id']);
1814 $data['attributedTo'] = $item['author-link'];
1816 $data['sensitive'] = self::isSensitive($item['uri-id']);
1818 if (!empty($item['conversation']) && ($item['conversation'] != './')) {
1819 $data['conversation'] = $data['context'] = $item['conversation'];
1822 if (!empty($item['title'])) {
1823 $data['name'] = BBCode::toPlaintext($item['title'], false);
1826 $permission_block = self::getReceiversForUriId($item['uri-id'], false);
1828 $real_quote = false;
1830 $item = Post\Media::addHTMLAttachmentToItem($item);
1832 $body = $item['body'];
1834 if ($type == 'Note') {
1835 $body = $item['raw-body'] ?? self::removePictures($body);
1837 $body = self::addEmojiTags($emojis, $body);
1840 * @todo Improve the automated summary
1841 * This part is currently deactivated. The automated summary seems to be more
1842 * confusing than helping. But possibly we will find a better way.
1843 * So the code is left here for now as a reminder
1845 * } elseif (($type == 'Article') && empty($data['summary'])) {
1846 * $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1847 * $summary = preg_replace_callback($regexp, [self::class, 'mentionAddrCallback'], $body);
1848 * $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($summary), 1000));
1852 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1853 $body = self::prependMentions($body, $item['uri-id'], $item['author-link']);
1856 if ($type == 'Event') {
1857 $data = array_merge($data, self::createEvent($item));
1859 if ($isCommunityPost) {
1860 // For community posts we remove the visible "!user@domain.tld".
1861 // This improves the look at systems like Lemmy.
1862 // Also in the future we should control the community delivery via other methods.
1863 $body = preg_replace("/!\[url\=[^\[\]]*\][^\[\]]*\[\/url\]/ism", '', $body);
1866 if ($type == 'Page') {
1867 // When we transmit "Page" posts we have to remove the attachment.
1868 // The attachment contains the link that we already transmit in the "url" field.
1869 $body = BBCode::removeAttachment($body);
1872 $body = BBCode::setMentionsToNicknames($body);
1874 if (!empty($item['quote-uri-id']) && ($item['quote-uri-id'] != $item['uri-id'])) {
1875 if (Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
1877 $data['quoteUrl'] = $item['quote-uri'];
1878 $body = DI::contentItem()->addShareLink($body, $item['quote-uri-id']);
1880 $body = DI::contentItem()->addSharedPost($item, $body);
1884 $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
1887 // The regular "content" field does contain a minimized HTML. This is done since systems like
1888 // Mastodon has got problems with - for example - embedded pictures.
1889 // The contentMap does contain the unmodified HTML.
1890 $language = self::getLanguage($item);
1891 if (!empty($language)) {
1892 $richbody = BBCode::setMentionsToNicknames($item['body'] ?? '');
1893 $richbody = Post\Media::removeFromEndOfBody($richbody);
1894 if (!empty($item['quote-uri-id']) && ($item['quote-uri-id'] != $item['uri-id'])) {
1896 $richbody = DI::contentItem()->addShareLink($richbody, $item['quote-uri-id']);
1898 $richbody = DI::contentItem()->addSharedPost($item, $richbody);
1901 $richbody = BBCode::replaceAttachment($richbody);
1903 $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
1906 if (!empty($item['quote-uri-id']) && ($item['quote-uri-id'] != $item['uri-id'])) {
1907 $source = DI::contentItem()->addSharedPost($item, $item['body']);
1909 $source = $item['body'];
1912 $data['source'] = ['content' => $source, 'mediaType' => "text/bbcode"];
1914 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1915 $data['diaspora:comment'] = $item['signed_text'];
1918 $data['attachment'] = self::createAttachmentList($item);
1919 $data['tag'] = array_merge(self::createTagList($item, $data['quoteUrl'] ?? ''), $emojis);
1921 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1922 $data['location'] = self::createLocation($item);
1925 if (!empty($item['app'])) {
1926 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1929 $data = array_merge($data, $permission_block);
1935 * Fetches the language from the post, the user or the system.
1937 * @param array $item
1938 * @return string language string
1940 private static function getLanguage(array $item): string
1942 // Try to fetch the language from the post itself
1943 if (!empty($item['language'])) {
1944 $languages = array_keys(json_decode($item['language'], true));
1945 if (!empty($languages[0])) {
1946 return DI::l10n()->toISO6391($languages[0]);
1950 // Otherwise use the user's language
1951 if (!empty($item['uid'])) {
1952 $user = DBA::selectFirst('user', ['language'], ['uid' => $item['uid']]);
1953 if (!empty($user['language'])) {
1954 return DI::l10n()->toISO6391($user['language']);
1958 // And finally just use the system language
1959 return DI::l10n()->toISO6391(DI::config()->get('system', 'language'));
1963 * Creates an an "add tag" entry
1965 * @param array $item Item array
1966 * @param array $activity activity data
1967 * @return array with activity data for adding tags
1968 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1969 * @throws \ImagickException
1971 private static function createAddTag(array $item, array $activity): array
1973 $object = XML::parseString($item['object']);
1974 $target = XML::parseString($item['target']);
1976 $activity['diaspora:guid'] = $item['guid'];
1977 $activity['actor'] = $item['author-link'];
1978 $activity['target'] = (string)$target->id;
1979 $activity['summary'] = BBCode::toPlaintext($item['body']);
1980 $activity['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1986 * Creates an announce object entry
1988 * @param array $item Item array
1989 * @param array $activity activity data
1990 * @param bool $api_mode
1991 * @return array with activity data
1992 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1993 * @throws \ImagickException
1995 private static function createAnnounce(array $item, array $activity, bool $api_mode = false): array
1997 $orig_body = $item['body'];
1998 $announce = self::getAnnounceArray($item);
1999 if (empty($announce)) {
2000 $activity['type'] = 'Create';
2001 $activity['object'] = self::createNote($item, $api_mode);
2005 if (empty($announce['comment'])) {
2006 // Pure announce, without a quote
2007 $activity['type'] = 'Announce';
2008 $activity['object'] = $announce['object']['uri'];
2013 $activity['type'] = 'Create';
2014 $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
2015 $activity['object'] = self::createNote($item, $api_mode);
2017 /// @todo Finally decide how to implement this in AP. This is a possible way:
2018 $activity['object']['attachment'][] = self::createNote($announce['object']);
2020 $activity['object']['source']['content'] = $orig_body;
2025 * Return announce related data if the item is an announce
2027 * @param array $item
2028 * @return array Announcement array
2030 private static function getAnnounceArray(array $item): array
2032 $reshared = DI::contentItem()->getSharedPost($item, Item::DELIVER_FIELDLIST);
2033 if (empty($reshared)) {
2037 if (!in_array($reshared['post']['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
2041 $profile = APContact::getByURL($reshared['post']['author-link'], false);
2042 if (empty($profile)) {
2046 return ['object' => $reshared['post'], 'actor' => $profile, 'comment' => $reshared['comment']];
2050 * Checks if the provided item array is an announce
2052 * @param array $item Item array
2053 * @return boolean Whether item is an announcement
2055 public static function isAnnounce(array $item): bool
2057 if (!empty($item['verb']) && ($item['verb'] == Activity::ANNOUNCE)) {
2061 $announce = self::getAnnounceArray($item);
2062 if (empty($announce)) {
2066 return empty($announce['comment']);
2070 * Creates an activity id for a given contact id
2072 * @param integer $cid Contact ID of target
2074 * @return bool|string activity id
2076 public static function activityIDFromContact(int $cid)
2078 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
2079 if (!DBA::isResult($contact)) {
2083 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
2084 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
2085 return DI::baseUrl() . '/activity/' . $uuid;
2089 * Transmits a contact suggestion to a given inbox
2091 * @param array $owner Sender owner-view record
2092 * @param string $inbox Target inbox
2093 * @param integer $suggestion_id Suggestion ID
2094 * @return boolean was the transmission successful?
2095 * @throws \Exception
2097 public static function sendContactSuggestion(array $owner, string $inbox, int $suggestion_id): bool
2099 $suggestion = DI::fsuggest()->selectOneById($suggestion_id);
2102 '@context' => ActivityPub::CONTEXT,
2103 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2104 'type' => 'Announce',
2105 'actor' => $owner['url'],
2106 'object' => $suggestion->url,
2107 'content' => $suggestion->note,
2108 'instrument' => self::getService(),
2109 'to' => [ActivityPub::PUBLIC_COLLECTION],
2113 $signed = LDSignature::sign($data, $owner);
2115 Logger::info('Deliver profile deletion for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2116 return HTTPSignature::transmit($signed, $inbox, $owner);
2120 * Transmits a profile relocation to a given inbox
2122 * @param array $owner Sender owner-view record
2123 * @param string $inbox Target inbox
2124 * @return boolean was the transmission successful?
2125 * @throws \Exception
2127 public static function sendProfileRelocation(array $owner, string $inbox): bool
2130 '@context' => ActivityPub::CONTEXT,
2131 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2132 'type' => 'dfrn:relocate',
2133 'actor' => $owner['url'],
2134 'object' => $owner['url'],
2135 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
2136 'instrument' => self::getService(),
2137 'to' => [ActivityPub::PUBLIC_COLLECTION],
2141 $signed = LDSignature::sign($data, $owner);
2143 Logger::info('Deliver profile relocation for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2144 return HTTPSignature::transmit($signed, $inbox, $owner);
2148 * Transmits a profile deletion to a given inbox
2150 * @param array $owner Sender owner-view record
2151 * @param string $inbox Target inbox
2152 * @return boolean was the transmission successful?
2153 * @throws \Exception
2155 public static function sendProfileDeletion(array $owner, string $inbox): bool
2157 if (empty($owner['uprvkey'])) {
2158 Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $owner['uid']]);
2162 $data = ['@context' => ActivityPub::CONTEXT,
2163 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2165 'actor' => $owner['url'],
2166 'object' => $owner['url'],
2167 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
2168 'instrument' => self::getService(),
2169 'to' => [ActivityPub::PUBLIC_COLLECTION],
2172 $signed = LDSignature::sign($data, $owner);
2174 Logger::info('Deliver profile deletion for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2175 return HTTPSignature::transmit($signed, $inbox, $owner);
2179 * Transmits a profile change to a given inbox
2181 * @param array $owner Sender owner-view record
2182 * @param string $inbox Target inbox
2183 * @return boolean was the transmission successful?
2184 * @throws HTTPException\InternalServerErrorException
2185 * @throws HTTPException\NotFoundException
2186 * @throws \ImagickException
2188 public static function sendProfileUpdate(array $owner, string $inbox): bool
2190 $profile = APContact::getByURL($owner['url']);
2192 $data = ['@context' => ActivityPub::CONTEXT,
2193 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2195 'actor' => $owner['url'],
2196 'object' => self::getProfile($owner['uid']),
2197 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
2198 'instrument' => self::getService(),
2199 'to' => [$profile['followers']],
2202 $signed = LDSignature::sign($data, $owner);
2204 Logger::info('Deliver profile update for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2205 return HTTPSignature::transmit($signed, $inbox, $owner);
2209 * Transmits a given activity to a target
2211 * @param string $activity Type name
2212 * @param string $target Target profile
2213 * @param integer $uid User ID
2214 * @param string $id Activity-identifier
2216 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2217 * @throws \ImagickException
2218 * @throws \Exception
2220 public static function sendActivity(string $activity, string $target, int $uid, string $id = ''): bool
2222 $profile = APContact::getByURL($target);
2223 if (empty($profile['inbox'])) {
2224 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2228 $owner = User::getOwnerDataById($uid);
2229 if (empty($owner)) {
2230 Logger::warning('No user found for actor, aborting', ['uid' => $uid]);
2235 $id = DI::baseUrl() . '/activity/' . System::createGUID();
2239 '@context' => ActivityPub::CONTEXT,
2241 'type' => $activity,
2242 'actor' => $owner['url'],
2243 'object' => $profile['url'],
2244 'instrument' => self::getService(),
2245 'to' => [$profile['url']],
2248 Logger::info('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid);
2250 $signed = LDSignature::sign($data, $owner);
2251 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2255 * Transmits a "follow object" activity to a target
2256 * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
2258 * @param string $object Object URL
2259 * @param string $target Target profile
2260 * @param integer $uid User ID
2262 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2263 * @throws \ImagickException
2264 * @throws \Exception
2266 public static function sendFollowObject(string $object, string $target, int $uid = 0): bool
2268 $profile = APContact::getByURL($target);
2269 if (empty($profile['inbox'])) {
2270 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2275 // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
2276 $admin = User::getFirstAdmin(['uid']);
2278 Logger::warning('No available admin user for transmission', ['target' => $target]);
2282 $uid = $admin['uid'];
2285 $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
2286 'author-id' => Contact::getPublicIdByUserId($uid)];
2287 if (Post::exists($condition)) {
2288 Logger::info('Follow for ' . $object . ' for user ' . $uid . ' does already exist.');
2292 $owner = User::getOwnerDataById($uid);
2295 '@context' => ActivityPub::CONTEXT,
2296 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2298 'actor' => $owner['url'],
2299 'object' => $object,
2300 'instrument' => self::getService(),
2301 'to' => [$profile['url']],
2304 Logger::info('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid);
2306 $signed = LDSignature::sign($data, $owner);
2307 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2311 * Transmit a message that the contact request had been accepted
2313 * @param string $target Target profile
2314 * @param string $id Object id
2315 * @param integer $uid User ID
2317 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2318 * @throws \ImagickException
2320 public static function sendContactAccept(string $target, string $id, int $uid)
2322 $profile = APContact::getByURL($target);
2323 if (empty($profile['inbox'])) {
2324 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2328 $owner = User::getOwnerDataById($uid);
2330 Logger::notice('No user found for actor', ['uid' => $uid]);
2335 '@context' => ActivityPub::CONTEXT,
2336 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2338 'actor' => $owner['url'],
2342 'actor' => $profile['url'],
2343 'object' => $owner['url']
2345 'instrument' => self::getService(),
2346 'to' => [$profile['url']],
2349 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
2351 $signed = LDSignature::sign($data, $owner);
2352 HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2356 * Reject a contact request or terminates the contact relation
2358 * @param string $target Target profile
2359 * @param string $objectId Object id
2360 * @param array $owner Sender owner-view record
2361 * @return bool Operation success
2362 * @throws HTTPException\InternalServerErrorException
2363 * @throws \ImagickException
2365 public static function sendContactReject(string $target, string $objectId, array $owner): bool
2367 $profile = APContact::getByURL($target);
2368 if (empty($profile['inbox'])) {
2369 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2374 '@context' => ActivityPub::CONTEXT,
2375 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2377 'actor' => $owner['url'],
2381 'actor' => $profile['url'],
2382 'object' => $owner['url']
2384 'instrument' => self::getService(),
2385 'to' => [$profile['url']],
2388 Logger::debug('Sending reject to ' . $target . ' for user ' . $owner['uid'] . ' with id ' . $objectId);
2390 $signed = LDSignature::sign($data, $owner);
2391 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2395 * Transmits a message that we don't want to follow this contact anymore
2397 * @param string $target Target profile
2398 * @param integer $cid Contact id
2399 * @param array $owner Sender owner-view record
2400 * @return bool success
2401 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2402 * @throws \ImagickException
2403 * @throws \Exception
2405 public static function sendContactUndo(string $target, int $cid, array $owner): bool
2407 $profile = APContact::getByURL($target);
2408 if (empty($profile['inbox'])) {
2409 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2413 $object_id = self::activityIDFromContact($cid);
2414 if (empty($object_id)) {
2418 $objectId = DI::baseUrl() . '/activity/' . System::createGUID();
2421 '@context' => ActivityPub::CONTEXT,
2424 'actor' => $owner['url'],
2428 'actor' => $owner['url'],
2429 'object' => $profile['url']
2431 'instrument' => self::getService(),
2432 'to' => [$profile['url']],
2435 Logger::info('Sending undo to ' . $target . ' for user ' . $owner['uid'] . ' with id ' . $objectId);
2437 $signed = LDSignature::sign($data, $owner);
2438 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2442 * Prepends mentions (@) to $body variable
2444 * @param string $body HTML code
2446 * @param string $authorLink Author link
2447 * @return string HTML code with prepended mentions
2449 private static function prependMentions(string $body, int $uriid, string $authorLink): string
2453 foreach (Tag::getByURIId($uriid, [Tag::IMPLICIT_MENTION]) as $tag) {
2454 $profile = Contact::getByURL($tag['url'], false, ['addr', 'contact-type', 'nick']);
2455 if (!empty($profile['addr'])
2456 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
2457 && !strstr($body, $profile['addr'])
2458 && !strstr($body, $tag['url'])
2459 && $tag['url'] !== $authorLink
2461 $mentions[] = '@[url=' . $tag['url'] . ']' . $profile['nick'] . '[/url]';
2465 $mentions[] = $body;
2467 return implode(' ', $mentions);