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\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;
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;
54 * ActivityPub Transmitter Protocol class
61 const CACHEKEY_FEATURED = 'transmitter:getFeatured:';
62 const CACHEKEY_CONTACTS = 'transmitter:getContacts:';
65 * Add relay servers to the list of inboxes
67 * @param array $inboxes
68 * @return array inboxes with added relay servers
70 public static function addRelayServerInboxes(array $inboxes = []): array
72 foreach (Relay::getList(['inbox']) as $contact) {
73 $inboxes[$contact['inbox']] = $contact['inbox'];
80 * Add relay servers to the list of inboxes
82 * @param array $inboxes
83 * @return array inboxes with added relay servers
85 public static function addRelayServerInboxesForItem(int $item_id, array $inboxes = []): array
87 $item = Post::selectFirst(['uid'], ['id' => $item_id]);
92 $relays = Relay::getDirectRelayList($item_id);
97 foreach ($relays as $relay) {
98 $contact = Contact::getByURLForUser($relay['url'], $item['uid'], false, ['id']);
99 $inboxes[$relay['batch']][] = $contact['id'] ?? 0;
105 * Subscribe to a relay and updates contact on success
107 * @param string $url Subscribe actor url
108 * @return bool success
110 public static function sendRelayFollow(string $url): bool
112 $contact = Contact::getByURL($url);
113 if (empty($contact)) {
117 $activity_id = self::activityIDFromContact($contact['id']);
118 $success = self::sendActivity('Follow', $url, 0, $activity_id);
120 Contact::update(['rel' => Contact::FRIEND], ['id' => $contact['id']]);
127 * Unsubscribe from a relay and updates contact on success or forced
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
133 public static function sendRelayUndoFollow(string $url, bool $force = false): bool
135 $contact = Contact::getByURL($url);
136 if (empty($contact)) {
140 $success = self::sendContactUndo($url, $contact['id'], User::getSystemAccount());
142 if ($success || $force) {
143 Contact::update(['rel' => Contact::NOTHING], ['id' => $contact['id']]);
150 * Collects a list of contacts of the given owner
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
161 public static function getContacts(array $owner, array $rel, string $module, int $page = null, string $requester = null, bool $nocache = false): array
164 $cachekey = self::CACHEKEY_CONTACTS . $module . ':'. $owner['uid'];
165 $result = DI::cache()->get($cachekey);
166 if (!$nocache && !is_null($result)) {
173 'uid' => $owner['uid'],
182 $condition = DBA::mergeConditions($parameters, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
184 $total = DBA::count('contact', $condition);
186 $modulePath = '/' . $module . '/';
188 $data = ['@context' => ActivityPub::CONTEXT];
189 $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
190 $data['type'] = 'OrderedCollection';
191 $data['totalItems'] = $total;
194 $data['id'] .= '?' . http_build_query(['page' => $page]);
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']);
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]);
205 if (!$show_contacts) {
206 if (!empty($cachekey)) {
207 DI::cache()->set($cachekey, $data, Duration::DAY);
214 $data['first'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=1';
216 $data['type'] = 'OrderedCollectionPage';
219 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
220 while ($contact = DBA::fetch($contacts)) {
221 $list[] = $contact['url'];
223 DBA::close($contacts);
225 if (count($list) == 100) {
226 $data['next'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=' . ($page + 1);
229 $data['partOf'] = DI::baseUrl() . $modulePath . $owner['nickname'];
231 $data['orderedItems'] = $list;
234 if (!empty($cachekey)) {
235 DI::cache()->set($cachekey, $data, Duration::DAY);
242 * Public posts for the given owner
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
254 public static function getOutbox(array $owner, int $uid, int $page = null, int $max_id = null, string $requester = ''): array
256 $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => [Item::PUBLIC, Item::UNLISTED]];
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'])])];
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,
280 $apcontact = APContact::getByURL($owner['url']);
282 return self::getCollection($condition, DI::baseUrl() . '/outbox/' . $owner['nickname'], $page, $max_id, $uid, $apcontact['statuses_count']);
285 public static function getInbox(int $uid, int $page = null, int $max_id = null)
287 $owner = User::getOwnerDataById($uid);
289 $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'uid' => $uid];
291 return self::getCollection($condition, DI::baseUrl() . '/inbox/' . $owner['nickname'], $page, $max_id, $uid, null);
294 public static function getPublicInbox(int $uid, int $page = null, int $max_id = null)
296 $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => Item::PUBLIC,
297 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'author-blocked' => false, 'author-hidden' => false];
299 return self::getCollection($condition, DI::baseUrl() . '/inbox', $page, $max_id, $uid, null);
302 private static function getCollection(array $condition, string $path, int $page = null, int $max_id = null, int $uid = null, int $total_items = null)
304 $data = ['@context' => ActivityPub::CONTEXT];
306 $data['type'] = 'OrderedCollection';
308 if (!is_null($total_items)) {
309 $data['totalItems'] = $total_items;
313 $data['id'] .= '?' . http_build_query(['page' => $page]);
316 if (empty($page) && empty($max_id)) {
317 $data['first'] = $path . '?page=1';
319 $data['type'] = 'OrderedCollectionPage';
322 if (!empty($max_id)) {
323 $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]);
327 $params = ['limit' => [($page - 1) * 20, 20], 'order' => ['uri-id' => true]];
329 $params = ['limit' => 20, 'order' => ['uri-id' => true]];
332 if (!is_null($uid)) {
333 $items = Post::selectForUser($uid, ['id', 'uri-id'], $condition, $params);
335 $items = Post::select(['id', 'uri-id'], $condition, $params);
339 while ($item = Post::fetch($items)) {
340 $activity = self::createActivityFromItem($item['id'], false, !is_null($uid));
341 if (!empty($activity)) {
343 $last_id = $item['uri-id'];
349 if (count($list) == 20) {
350 $data['next'] = $path . '?max_id=' . $last_id;
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;
361 $data['partOf'] = $path;
363 $data['orderedItems'] = $list;
370 * Public posts for the given owner
372 * @param array $owner Owner array
373 * @param integer $page Page number
374 * @param boolean $nocache Wether to bypass caching
376 * @return array of posts
377 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
378 * @throws \ImagickException
380 public static function getFeatured(array $owner, int $page = null, bool $nocache = false): array
383 $cachekey = self::CACHEKEY_FEATURED . $owner['uid'];
384 $result = DI::cache()->get($cachekey);
385 if (!$nocache && !is_null($result)) {
390 $owner_cid = Contact::getIdForURL($owner['url'], 0, false);
392 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
393 $owner_cid, Post\Collection::FEATURED];
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,
407 $count = Post::count($condition);
409 $data = ['@context' => ActivityPub::CONTEXT];
410 $data['id'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
411 $data['type'] = 'OrderedCollection';
412 $data['totalItems'] = $count;
415 $data['id'] .= '?' . http_build_query(['page' => $page]);
419 $items = Post::select(['id'], $condition, ['limit' => 20, 'order' => ['created' => true]]);
421 $data['type'] = 'OrderedCollectionPage';
422 $items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
426 while ($item = Post::fetch($items)) {
427 $activity = self::createActivityFromItem($item['id'], true);
428 if (!empty($activity)) {
434 if (count($list) == 20) {
435 $data['next'] = DI::baseUrl() . '/featured/' . $owner['nickname'] . '?page=' . ($page + 1);
439 $data['partOf'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
442 $data['orderedItems'] = $list;
444 if (!empty($cachekey)) {
445 DI::cache()->set($cachekey, $data, Duration::DAY);
452 * Return the service array containing information the used software and it's url
454 * @return array with service data
456 public static function getService(): array
460 'name' => App::PLATFORM . " '" . App::CODENAME . "' " . App::VERSION . '-' . DB_UPDATE_VERSION,
461 'url' => DI::baseUrl()->get()
466 * Return the ActivityPub profile of the given user
468 * @param int $uid User ID
469 * @return array with profile data
470 * @throws HTTPException\NotFoundException
471 * @throws HTTPException\InternalServerErrorException
473 public static function getProfile(int $uid): array
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.');
481 $data = ['@context' => ActivityPub::CONTEXT];
482 $data['id'] = $owner['url'];
484 if (!empty($owner['guid'])) {
485 $data['diaspora:guid'] = $owner['guid'];
488 $data['type'] = ActivityPub::ACCOUNT_TYPES[$owner['account-type']];
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'];
497 $data['inbox'] = DI::baseUrl() . '/friendica/inbox';
500 $data['preferredUsername'] = $owner['nick'];
501 $data['name'] = $owner['name'];
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']];
508 if (!empty($owner['about'])) {
509 $data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
512 if (!empty($owner['xmpp']) || !empty($owner['matrix'])) {
513 $data['vcard:hasInstantMessage'] = [];
515 if (!empty($owner['xmpp'])) {
516 $data['vcard:hasInstantMessage'][] = 'xmpp:' . $owner['xmpp'];
518 if (!empty($owner['matrix'])) {
519 $data['vcard:hasInstantMessage'][] = 'matrix:' . $owner['matrix'];
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'];
531 $data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
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'];
541 if (!empty($owner['header'])) {
542 $data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($owner['id'], '', $owner['updated'])];
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'];
555 foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) {
557 'type' => 'PropertyValue',
558 'name' => $profile_field->label,
559 'value' => BBCode::convertForUriId($owner['uri-id'], $profile_field->value)
563 if (!empty($custom_fields)) {
564 $data['attachment'] = $custom_fields;
568 $data['generator'] = self::getService();
570 // tags: https://kitty.town/@inmysocks/100656097926961126.json
575 * Get a minimal actror array for the C2S API
577 * @param integer $cid
580 private static function getActorArrayByCid(int $cid): array
582 $contact = Contact::getById($cid);
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'],
595 if (empty($data['url'])) {
596 $data['url'] = $data['id'];
603 * @param string $username
605 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
607 public static function getDeletedUser(string $username): array
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),
620 * Returns an array with permissions of the thread parent of the given item array
623 * @param bool $is_forum_thread
625 * @return array with permissions
626 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
627 * @throws \ImagickException
629 private static function fetchPermissionBlockFromThreadParent(array $item, bool $is_forum_thread): array
631 if (empty($item['thr-parent-id'])) {
635 $parent = Post::selectFirstPost(['author-link'], ['uri-id' => $item['thr-parent-id']]);
636 if (empty($parent)) {
641 'to' => [$parent['author-link']],
647 $parent_profile = APContact::getByURL($parent['author-link']);
649 $item_profile = APContact::getByURL($item['author-link']);
650 $exclude[] = $item['author-link'];
652 if ($item['gravity'] == Item::GRAVITY_PARENT) {
653 $exclude[] = $item['owner-link'];
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'];
662 } elseif (!in_array($receiver['url'], $exclude)) {
663 $permissions[$type[$receiver['type']]][] = $receiver['url'];
671 * Check if the given item id is from ActivityPub
673 * @param integer $item_id
674 * @return boolean "true" if the post is from ActivityPub
676 private static function isAPPost(int $item_id): bool
678 if (empty($item_id)) {
682 return Post::exists(['id' => $item_id, 'network' => Protocol::ACTIVITYPUB]);
686 * Creates an array of permissions from an item thread
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
692 * @return array with permission data
693 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
694 * @throws \ImagickException
696 private static function createPermissionBlockForItem(array $item, bool $blindcopy, int $last_id = 0): array
699 $last_id = $item['id'];
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'];
713 $profile = APContact::getByURL($owner['url'], false);
714 $follower = $profile['followers'] ?? '';
718 if (DI::config()->get('system', 'ap_always_bcc')) {
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';
727 $is_forum_thread = false;
730 if (self::isAnnounce($item) || self::isAPPost($last_id)) {
731 // Will be activated in a later step
732 $networks = Protocol::FEDERATED;
734 // For now only send to these contacts:
735 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
738 $data = ['to' => [], 'cc' => [], 'bcc' => []];
740 if ($item['gravity'] == Item::GRAVITY_PARENT) {
741 $actor_profile = APContact::getByURL($item['owner-link']);
743 $actor_profile = APContact::getByURL($item['author-link']);
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) {
755 } elseif ($term['type'] == Tag::MENTION) {
762 $terms = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
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'];
774 $data = array_merge($data, self::fetchPermissionBlockFromThreadParent($item, $is_forum_thread));
776 // Check if the item is completely public or unlisted
777 if ($item['private'] == Item::PUBLIC) {
778 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
780 $data['cc'][] = ActivityPub::PUBLIC_COLLECTION;
783 foreach ($terms as $term) {
784 $profile = APContact::getByURL($term['url'], false);
785 if (!empty($profile)) {
786 if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
788 if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
789 $data['cc'][] = $profile['followers'];
791 } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
794 $data['to'][] = $profile['url'];
798 $receiver_list = Item::enumeratePermissions($item, true);
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)) {
808 $profile = APContact::getByURL($term['url'], false);
809 if (!empty($profile)) {
810 if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
812 if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
813 $data['cc'][] = $profile['followers'];
815 } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
818 $data['to'][] = $profile['url'];
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)) {
836 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
837 if ($contact['hidden'] || $always_bcc) {
838 $data['bcc'][] = $profile['url'];
840 $data['cc'][] = $profile['url'];
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'];
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'];
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'];
875 // Don't include data from future posts
876 if ($parent['id'] >= $last_id) {
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'];
885 $data['cc'][] = $profile['url'];
889 DBA::close($parents);
892 $data['to'] = array_unique($data['to']);
893 $data['cc'] = array_unique($data['cc']);
894 $data['bcc'] = array_unique($data['bcc']);
896 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
897 unset($data['to'][$key]);
900 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
901 unset($data['cc'][$key]);
904 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
905 unset($data['bcc'][$key]);
908 foreach ($data['to'] as $to) {
909 if (($key = array_search($to, $data['cc'])) !== false) {
910 unset($data['cc'][$key]);
913 if (($key = array_search($to, $data['bcc'])) !== false) {
914 unset($data['bcc'][$key]);
918 foreach ($data['cc'] as $cc) {
919 if (($key = array_search($cc, $data['bcc'])) !== false) {
920 unset($data['bcc'][$key]);
924 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
927 unset($receivers['bcc']);
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;
936 $name = trim(parse_url($receiver, PHP_URL_PATH), '/');
938 Tag::store($item['uri-id'], $type, $name, $receiver);
947 * Check if an inbox is archived
949 * @param string $url Inbox url
950 * @return boolean "true" if inbox is archived
952 public static function archivedInbox(string $url): bool
954 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
958 * Check if a given contact should be delivered via AP
960 * @param array $contact Contact array
961 * @param array $networks Array with networks
962 * @return bool Whether the used protocol matches ACTIVITYPUB
965 private static function isAPContact(array $contact, array $networks): bool
967 if (in_array($contact['network'], $networks) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
971 return GServer::getProtocol($contact['gsid'] ?? 0) == Post\DeliveryData::ACTIVITYPUB;
975 * Fetches a list of inboxes of followers of a given user
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
984 public static function fetchTargetInboxesforUser(int $uid, bool $personal = false, bool $all_ap = false): array
990 if (!empty($item['uid'])) {
991 $profile = User::getOwnerDataById($item['uid']);
992 if (!empty($profile)) {
993 $isforum = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
998 // Will be activated in a later step
999 $networks = Protocol::FEDERATED;
1001 // For now only send to these contacts:
1002 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
1010 'network' => Protocol::FEDERATED,
1014 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
1017 $contacts = DBA::select('contact', ['id', 'url', 'network', 'protocol', 'gsid'], $condition);
1018 while ($contact = DBA::fetch($contacts)) {
1019 if (!self::isAPContact($contact, $networks)) {
1023 if ($isforum && ($contact['network'] == Protocol::DFRN)) {
1027 if (Network::isUrlBlocked($contact['url'])) {
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'];
1036 $target = $profile['sharedinbox'];
1038 if (!self::archivedInbox($target)) {
1039 $inboxes[$target][] = $contact['id'];
1043 DBA::close($contacts);
1049 * Fetches an array of inboxes for the given item and user
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
1059 public static function fetchTargetInboxes(array $item, int $uid, bool $personal = false, int $last_id = 0): array
1061 $permissions = self::createPermissionBlockForItem($item, true, $last_id);
1062 if (empty($permissions)) {
1068 if ($item['gravity'] == Item::GRAVITY_ACTIVITY) {
1069 $item_profile = APContact::getByURL($item['author-link'], false);
1071 $item_profile = APContact::getByURL($item['owner-link'], false);
1074 if (empty($item_profile)) {
1078 $profile_uid = User::getIdForURL($item_profile['url']);
1080 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
1081 if (empty($permissions[$element])) {
1085 $blindcopy = in_array($element, ['bto', 'bcc']);
1087 foreach ($permissions[$element] as $receiver) {
1088 if (empty($receiver) || Network::isUrlBlocked($receiver)) {
1092 if ($item_profile && ($receiver == $item_profile['followers']) && ($uid == $profile_uid)) {
1093 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal, self::isAPPost($last_id)));
1095 $profile = APContact::getByURL($receiver, false);
1096 if (!empty($profile)) {
1097 $contact = Contact::getByURLForUser($receiver, $uid, false, ['id']);
1099 if (empty($profile['sharedinbox']) || $personal || $blindcopy || Contact::isLocal($receiver)) {
1100 $target = $profile['inbox'];
1102 $target = $profile['sharedinbox'];
1104 if (!self::archivedInbox($target)) {
1105 $inboxes[$target][] = $contact['id'] ?? 0;
1116 * Creates an array in the structure of the item table for a given mail id
1118 * @param integer $mail_id Mail id
1120 * @throws \Exception
1122 public static function getItemArrayFromMail(int $mail_id, bool $use_title = false): array
1124 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
1125 if (!DBA::isResult($mail)) {
1129 $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
1130 if (!DBA::isResult($reply)) {
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"
1139 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
1140 $mail['title'] = '';
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;
1166 * Creates an activity array for a given mail id
1168 * @param integer $mail_id
1169 * @param boolean $object_mode Is the activity item is used inside another object?
1171 * @return array of activity
1172 * @throws \Exception
1174 public static function createActivityFromMail(int $mail_id, bool $object_mode = false): array
1176 $mail = self::getItemArrayFromMail($mail_id);
1180 $object = self::createNote($mail);
1182 if (!$object_mode) {
1183 $data = ['@context' => ActivityPub::CONTEXT];
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));
1195 if (empty($data['to']) && !empty($data['cc'])) {
1196 $data['to'] = $data['cc'];
1199 if (empty($data['to']) && !empty($data['bcc'])) {
1200 $data['to'] = $data['bcc'];
1204 unset($data['bcc']);
1206 $object['to'] = $data['to'];
1207 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
1209 unset($object['cc']);
1210 unset($object['bcc']);
1212 $data['directMessage'] = true;
1214 $data['object'] = $object;
1216 $owner = User::getOwnerDataById($mail['uid']);
1218 if (!$object_mode && !empty($owner)) {
1219 return LDSignature::sign($data, $owner);
1226 * Returns the activity type of a given item
1228 * @param array $item Item array
1229 * @return string with activity type
1230 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1231 * @throws \ImagickException
1233 private static function getTypeOfItem(array $item): string
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);
1245 } elseif ($item['verb'] == Activity::POST) {
1246 if ($item['created'] == $item['edited']) {
1251 } elseif ($item['verb'] == Activity::LIKE) {
1253 } elseif ($item['verb'] == Activity::DISLIKE) {
1255 } elseif ($item['verb'] == Activity::ATTEND) {
1257 } elseif ($item['verb'] == Activity::ATTENDNO) {
1259 } elseif ($item['verb'] == Activity::ATTENDMAYBE) {
1260 $type = 'TentativeAccept';
1261 } elseif ($item['verb'] == Activity::FOLLOW) {
1263 } elseif ($item['verb'] == Activity::TAG) {
1265 } elseif ($item['verb'] == Activity::ANNOUNCE) {
1275 * Creates the activity or fetches it from the cache
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
1282 public static function createCachedActivityFromItem(int $item_id, bool $force = false, bool $object_mode = false)
1284 $cachekey = 'APDelivery:createActivity:' . $item_id . ':' . (int)$object_mode;
1287 $data = DI::cache()->get($cachekey);
1288 if (!is_null($data)) {
1293 $data = self::createActivityFromItem($item_id, $object_mode);
1295 DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
1300 * Creates an activity array for a given item id
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
1308 public static function createActivityFromItem(int $item_id, bool $object_mode = false, $api_mode = false)
1310 $condition = ['id' => $item_id];
1312 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
1314 Logger::info('Fetching activity', $condition);
1315 $item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
1316 if (!DBA::isResult($item)) {
1319 return self::createActivityFromArray($item, $object_mode, $api_mode);
1323 * Creates an activity array for a given URI-Id and uid
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
1332 public static function createActivityFromUriId(int $uri_id, int $uid, bool $object_mode = false, $api_mode = false)
1334 $condition = ['uri-id' => $uri_id, 'uid' => [0, $uid]];
1336 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
1338 Logger::info('Fetching activity', $condition);
1339 $item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition, ['order' => ['uid' => true]]);
1340 if (!DBA::isResult($item)) {
1344 return self::createActivityFromArray($item, $object_mode, $api_mode);
1348 * Creates an activity array for a given item id
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
1356 private static function createActivityFromArray(array $item, bool $object_mode = false, $api_mode = false)
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']]);
1364 } elseif (!empty($data['object'])) {
1365 Logger::info('Return stored conversation object', ['item' => $item['id']]);
1366 return $data['object'];
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']]);
1376 $type = self::getTypeOfItem($item);
1378 if (!$object_mode) {
1379 $data = ['@context' => $context ?? ActivityPub::CONTEXT];
1381 if ($item['deleted'] && ($item['gravity'] == Item::GRAVITY_ACTIVITY)) {
1383 } elseif ($item['deleted']) {
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'];
1395 $data['id'] = $item['uri'] . '/' . $type;
1398 $data['type'] = $type;
1400 if (($type != 'Announce') || ($item['gravity'] != Item::GRAVITY_PARENT)) {
1401 $link = $item['author-link'];
1402 $id = $item['author-id'];
1404 $link = $item['owner-link'];
1405 $id = $item['owner-id'];
1409 $data['actor'] = self::getActorArrayByCid($id);
1411 $data['actor'] = $link;
1414 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1416 $data['instrument'] = self::getService();
1418 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
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'];
1428 $data = self::createAnnounce($item, $data, $api_mode);
1430 } elseif ($data['type'] == 'Follow') {
1431 $data['object'] = $item['parent-uri'];
1432 } elseif ($data['type'] == 'Undo') {
1433 $data['object'] = self::createActivityFromItem($item['id'], true);
1435 $data['diaspora:guid'] = $item['guid'];
1436 if (!empty($item['signed_text'])) {
1437 $data['diaspora:like'] = $item['signed_text'];
1439 $data['object'] = $item['thr-parent'];
1442 if (!empty($item['contact-uid'])) {
1443 $uid = $item['contact-uid'];
1445 $uid = $item['uid'];
1448 Logger::info('Fetched activity', ['item' => $item['id'], 'uid' => $uid]);
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);
1458 /// @todo Create "conversation" entry
1462 * Creates a location entry for a given item array
1464 * @param array $item Item array
1465 * @return array with location array
1467 private static function createLocation(array $item): array
1469 $location = ['type' => 'Place'];
1471 if (!empty($item['location'])) {
1472 $location['name'] = $item['location'];
1477 if (empty($item['coord'])) {
1478 $coord = Map::getCoordinates($item['location']);
1480 $coords = explode(' ', $item['coord']);
1481 if (count($coords) == 2) {
1482 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
1486 if (!empty($coord['lat']) && !empty($coord['lon'])) {
1487 $location['latitude'] = $coord['lat'];
1488 $location['longitude'] = $coord['lon'];
1495 * Returns a tag array for a given item array
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
1502 private static function createTagList(array $item, string $quote_url): array
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']];
1512 $contact = Contact::getByURL($term['url'], false, ['addr']);
1513 if (empty($contact)) {
1516 if (!empty($contact['addr'])) {
1517 $mention = '@' . $contact['addr'];
1519 $mention = '@' . $term['url'];
1522 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
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']];
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
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)
1547 * Adds attachment data to the JSON document
1549 * @param array $item Data of the item that is to be posted
1551 * @return array with attachment data
1552 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1554 private static function createAttachmentList(array $item): array
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)) {
1563 $urls[] = $attachment['url'];
1565 $attach = ['type' => 'Document',
1566 'mediaType' => $attachment['mimetype'],
1567 'url' => $attachment['url'],
1568 'name' => $attachment['description']];
1570 if (!empty($attachment['height'])) {
1571 $attach['height'] = $attachment['height'];
1574 if (!empty($attachment['width'])) {
1575 $attach['width'] = $attachment['width'];
1578 if (!empty($attachment['preview'])) {
1579 $attach['image'] = $attachment['preview'];
1582 $attachments[] = $attach;
1585 return $attachments;
1589 * Callback function to replace a Friendica style mention in a mention for a summary
1591 * @param array $match Matching values for the callback
1592 * @return string Replaced mention
1593 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1595 private static function mentionAddrCallback(array $match): string
1597 if (empty($match[1])) {
1601 $data = Contact::getByURL($match[1], false, ['addr']);
1602 if (empty($data['addr'])) {
1606 return '@' . $data['addr'];
1610 * Remove image elements since they are added as attachment
1612 * @param string $body HTML code
1613 * @return string with removed images
1615 private static function removePictures(string $body): string
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);
1622 // Now remove local links
1623 $text = preg_replace_callback(
1624 '/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
1626 // We remove the link when it is a link to a local photo page
1627 if (Photo::isLocalPage($match[1])) {
1630 // otherwise we just return the link
1631 return '[url]' . $match[1] . '[/url]';
1636 // Remove all pictures
1637 return preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $text);
1642 * Returns if the post contains sensitive content ("nsfw")
1644 * @param integer $uri_id URI id
1645 * @return boolean Whether URI id was found
1646 * @throws \Exception
1648 private static function isSensitive(int $uri_id): bool
1650 return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw', 'type' => Tag::HASHTAG]);
1654 * Creates event data
1656 * @param array $item Item array
1657 * @return array with the event data
1658 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1660 private static function createEvent(array $item): array
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');
1667 if (!$item['event-nofinish']) {
1668 $event['endTime'] = DateTimeFormat::utc($item['event-finish'], 'c');
1671 if (!empty($item['event-location'])) {
1672 $item['location'] = $item['event-location'];
1673 $event['location'] = self::createLocation($item);
1676 // 2021.12: Backward compatibility value, all the events now "adjust" to the viewer timezone
1677 $event['dfrn:adjust'] = true;
1683 * Creates a note/article object array
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
1691 public static function createNote(array $item, bool $api_mode = false): array
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'];
1707 $isCommunityPost = false;
1710 if ($item['event-type'] == 'event') {
1712 } elseif (!empty($item['title'])) {
1713 if (!$isCommunityPost || empty($link)) {
1716 // "Page" is used by Lemmy for posts that contain an external link
1723 if ($item['deleted']) {
1724 $type = 'Tombstone';
1728 $data['id'] = $item['uri'];
1729 $data['type'] = $type;
1731 if ($item['deleted']) {
1735 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1737 if ($item['uri'] != $item['thr-parent']) {
1738 $data['inReplyTo'] = $item['thr-parent'];
1740 $data['inReplyTo'] = null;
1743 $data['diaspora:guid'] = $item['guid'];
1744 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1746 if ($item['created'] != $item['edited']) {
1747 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1750 $data['url'] = $link ?? $item['plink'];
1752 $data['attributedTo'] = self::getActorArrayByCid($item['author-id']);
1754 $data['attributedTo'] = $item['author-link'];
1756 $data['sensitive'] = self::isSensitive($item['uri-id']);
1758 if (!empty($item['conversation']) && ($item['conversation'] != './')) {
1759 $data['conversation'] = $data['context'] = $item['conversation'];
1762 if (!empty($item['title'])) {
1763 $data['name'] = BBCode::toPlaintext($item['title'], false);
1766 $permission_block = self::createPermissionBlockForItem($item, false);
1768 $real_quote = false;
1770 $body = $item['body'];
1772 if ($type == 'Note') {
1773 $body = $item['raw-body'] ?? self::removePictures($body);
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
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));
1789 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1790 $body = self::prependMentions($body, $item['uri-id'], $item['author-link']);
1793 if ($type == 'Event') {
1794 $data = array_merge($data, self::createEvent($item));
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);
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);
1809 $body = BBCode::setMentionsToNicknames($body);
1811 if (!empty($item['quote-uri-id'])) {
1812 if (Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
1814 $data['quoteUrl'] = $item['quote-uri'];
1815 $body = DI::contentItem()->addShareLink($body, $item['quote-uri-id']);
1817 $body = DI::contentItem()->addSharedPost($item, $body);
1821 $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
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'])) {
1833 $richbody = DI::contentItem()->addShareLink($richbody, $item['quote-uri-id']);
1835 $richbody = DI::contentItem()->addSharedPost($item, $richbody);
1838 $richbody = BBCode::removeAttachment($richbody);
1840 $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
1843 if (!empty($item['quote-uri-id'])) {
1844 $source = DI::contentItem()->addSharedPost($item, $item['body']);
1846 $source = $item['body'];
1849 $data['source'] = ['content' => $source, 'mediaType' => "text/bbcode"];
1851 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1852 $data['diaspora:comment'] = $item['signed_text'];
1855 $data['attachment'] = self::createAttachmentList($item);
1856 $data['tag'] = self::createTagList($item, $data['quoteUrl'] ?? '');
1858 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1859 $data['location'] = self::createLocation($item);
1862 if (!empty($item['app'])) {
1863 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1866 $data = array_merge($data, $permission_block);
1872 * Fetches the language from the post, the user or the system.
1874 * @param array $item
1875 * @return string language string
1877 private static function getLanguage(array $item): string
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];
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'];
1895 // And finally just use the system language
1896 return DI::config()->get('system', 'language');
1900 * Creates an an "add tag" entry
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
1908 private static function createAddTag(array $item, array $activity): array
1910 $object = XML::parseString($item['object']);
1911 $target = XML::parseString($item['target']);
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];
1923 * Creates an announce object entry
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
1932 private static function createAnnounce(array $item, array $activity, bool $api_mode = false): array
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);
1942 if (empty($announce['comment'])) {
1943 // Pure announce, without a quote
1944 $activity['type'] = 'Announce';
1945 $activity['object'] = $announce['object']['uri'];
1950 $activity['type'] = 'Create';
1951 $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
1952 $activity['object'] = self::createNote($item, $api_mode);
1954 /// @todo Finally descide how to implement this in AP. This is a possible way:
1955 $activity['object']['attachment'][] = self::createNote($announce['object']);
1957 $activity['object']['source']['content'] = $orig_body;
1962 * Return announce related data if the item is an annunce
1964 * @param array $item
1965 * @return array Announcement array
1967 private static function getAnnounceArray(array $item): array
1969 $reshared = DI::contentItem()->getSharedPost($item, Item::DELIVER_FIELDLIST);
1970 if (empty($reshared)) {
1974 if (!in_array($reshared['post']['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
1978 $profile = APContact::getByURL($reshared['post']['author-link'], false);
1979 if (empty($profile)) {
1983 return ['object' => $reshared['post'], 'actor' => $profile, 'comment' => $reshared['comment']];
1987 * Checks if the provided item array is an announce
1989 * @param array $item Item array
1990 * @return boolean Whether item is an announcement
1992 public static function isAnnounce(array $item): bool
1994 if (!empty($item['verb']) && ($item['verb'] == Activity::ANNOUNCE)) {
1998 $announce = self::getAnnounceArray($item);
1999 if (empty($announce)) {
2003 return empty($announce['comment']);
2007 * Creates an activity id for a given contact id
2009 * @param integer $cid Contact ID of target
2011 * @return bool|string activity id
2013 public static function activityIDFromContact(int $cid)
2015 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
2016 if (!DBA::isResult($contact)) {
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;
2026 * Transmits a contact suggestion to a given inbox
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
2034 public static function sendContactSuggestion(array $owner, string $inbox, int $suggestion_id): bool
2036 $suggestion = DI::fsuggest()->selectOneById($suggestion_id);
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],
2050 $signed = LDSignature::sign($data, $owner);
2052 Logger::info('Deliver profile deletion for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2053 return HTTPSignature::transmit($signed, $inbox, $owner);
2057 * Transmits a profile relocation to a given inbox
2059 * @param array $owner Sender owner-view record
2060 * @param string $inbox Target inbox
2061 * @return boolean was the transmission successful?
2062 * @throws \Exception
2064 public static function sendProfileRelocation(array $owner, string $inbox): bool
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],
2078 $signed = LDSignature::sign($data, $owner);
2080 Logger::info('Deliver profile relocation for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2081 return HTTPSignature::transmit($signed, $inbox, $owner);
2085 * Transmits a profile deletion to a given inbox
2087 * @param array $owner Sender owner-view record
2088 * @param string $inbox Target inbox
2089 * @return boolean was the transmission successful?
2090 * @throws \Exception
2092 public static function sendProfileDeletion(array $owner, string $inbox): bool
2094 if (empty($owner['uprvkey'])) {
2095 Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $owner['uid']]);
2099 $data = ['@context' => ActivityPub::CONTEXT,
2100 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2102 'actor' => $owner['url'],
2103 'object' => $owner['url'],
2104 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
2105 'instrument' => self::getService(),
2106 'to' => [ActivityPub::PUBLIC_COLLECTION],
2109 $signed = LDSignature::sign($data, $owner);
2111 Logger::info('Deliver profile deletion for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2112 return HTTPSignature::transmit($signed, $inbox, $owner);
2116 * Transmits a profile change to a given inbox
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
2125 public static function sendProfileUpdate(array $owner, string $inbox): bool
2127 $profile = APContact::getByURL($owner['url']);
2129 $data = ['@context' => ActivityPub::CONTEXT,
2130 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2132 'actor' => $owner['url'],
2133 'object' => self::getProfile($owner['uid']),
2134 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
2135 'instrument' => self::getService(),
2136 'to' => [$profile['followers']],
2139 $signed = LDSignature::sign($data, $owner);
2141 Logger::info('Deliver profile update for user ' . $owner['uid'] . ' to ' . $inbox . ' via ActivityPub');
2142 return HTTPSignature::transmit($signed, $inbox, $owner);
2146 * Transmits a given activity to a target
2148 * @param string $activity Type name
2149 * @param string $target Target profile
2150 * @param integer $uid User ID
2151 * @param string $id Activity-identifier
2153 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2154 * @throws \ImagickException
2155 * @throws \Exception
2157 public static function sendActivity(string $activity, string $target, int $uid, string $id = ''): bool
2159 $profile = APContact::getByURL($target);
2160 if (empty($profile['inbox'])) {
2161 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2165 $owner = User::getOwnerDataById($uid);
2166 if (empty($owner)) {
2167 Logger::warning('No user found for actor, aborting', ['uid' => $uid]);
2172 $id = DI::baseUrl() . '/activity/' . System::createGUID();
2176 '@context' => ActivityPub::CONTEXT,
2178 'type' => $activity,
2179 'actor' => $owner['url'],
2180 'object' => $profile['url'],
2181 'instrument' => self::getService(),
2182 'to' => [$profile['url']],
2185 Logger::info('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid);
2187 $signed = LDSignature::sign($data, $owner);
2188 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2192 * Transmits a "follow object" activity to a target
2193 * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
2195 * @param string $object Object URL
2196 * @param string $target Target profile
2197 * @param integer $uid User ID
2199 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2200 * @throws \ImagickException
2201 * @throws \Exception
2203 public static function sendFollowObject(string $object, string $target, int $uid = 0): bool
2205 $profile = APContact::getByURL($target);
2206 if (empty($profile['inbox'])) {
2207 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
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']);
2215 Logger::warning('No available admin user for transmission', ['target' => $target]);
2219 $uid = $admin['uid'];
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.');
2229 $owner = User::getOwnerDataById($uid);
2232 '@context' => ActivityPub::CONTEXT,
2233 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2235 'actor' => $owner['url'],
2236 'object' => $object,
2237 'instrument' => self::getService(),
2238 'to' => [$profile['url']],
2241 Logger::info('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid);
2243 $signed = LDSignature::sign($data, $owner);
2244 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2248 * Transmit a message that the contact request had been accepted
2250 * @param string $target Target profile
2251 * @param string $id Object id
2252 * @param integer $uid User ID
2254 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2255 * @throws \ImagickException
2257 public static function sendContactAccept(string $target, string $id, int $uid)
2259 $profile = APContact::getByURL($target);
2260 if (empty($profile['inbox'])) {
2261 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2265 $owner = User::getOwnerDataById($uid);
2267 Logger::notice('No user found for actor', ['uid' => $uid]);
2272 '@context' => ActivityPub::CONTEXT,
2273 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2275 'actor' => $owner['url'],
2279 'actor' => $profile['url'],
2280 'object' => $owner['url']
2282 'instrument' => self::getService(),
2283 'to' => [$profile['url']],
2286 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
2288 $signed = LDSignature::sign($data, $owner);
2289 HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2293 * Reject a contact request or terminates the contact relation
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
2302 public static function sendContactReject(string $target, string $objectId, array $owner): bool
2304 $profile = APContact::getByURL($target);
2305 if (empty($profile['inbox'])) {
2306 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2311 '@context' => ActivityPub::CONTEXT,
2312 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
2314 'actor' => $owner['url'],
2318 'actor' => $profile['url'],
2319 'object' => $owner['url']
2321 'instrument' => self::getService(),
2322 'to' => [$profile['url']],
2325 Logger::debug('Sending reject to ' . $target . ' for user ' . $owner['uid'] . ' with id ' . $objectId);
2327 $signed = LDSignature::sign($data, $owner);
2328 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2332 * Transmits a message that we don't want to follow this contact anymore
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
2342 public static function sendContactUndo(string $target, int $cid, array $owner): bool
2344 $profile = APContact::getByURL($target);
2345 if (empty($profile['inbox'])) {
2346 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
2350 $object_id = self::activityIDFromContact($cid);
2351 if (empty($object_id)) {
2355 $objectId = DI::baseUrl() . '/activity/' . System::createGUID();
2358 '@context' => ActivityPub::CONTEXT,
2361 'actor' => $owner['url'],
2365 'actor' => $owner['url'],
2366 'object' => $profile['url']
2368 'instrument' => self::getService(),
2369 'to' => [$profile['url']],
2372 Logger::info('Sending undo to ' . $target . ' for user ' . $owner['uid'] . ' with id ' . $objectId);
2374 $signed = LDSignature::sign($data, $owner);
2375 return HTTPSignature::transmit($signed, $profile['inbox'], $owner);
2379 * Prepends mentions (@) to $body variable
2381 * @param string $body HTML code
2383 * @param string $authorLink Author link
2384 * @return string HTML code with prepended mentions
2386 private static function prependMentions(string $body, int $uriid, string $authorLink): string
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
2398 $mentions[] = '@[url=' . $tag['url'] . ']' . $profile['nick'] . '[/url]';
2402 $mentions[] = $body;
2404 return implode(' ', $mentions);