3 * @copyright Copyright (C) 2020, Friendica
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;
24 use Friendica\Content\Feature;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Content\Text\Plaintext;
27 use Friendica\Core\Cache\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\Conversation;
36 use Friendica\Model\Item;
37 use Friendica\Model\ItemURI;
38 use Friendica\Model\Profile;
39 use Friendica\Model\Photo;
40 use Friendica\Model\Tag;
41 use Friendica\Model\User;
42 use Friendica\Protocol\Activity;
43 use Friendica\Protocol\ActivityPub;
44 use Friendica\Util\DateTimeFormat;
45 use Friendica\Util\HTTPSignature;
46 use Friendica\Util\Images;
47 use Friendica\Util\JsonLD;
48 use Friendica\Util\LDSignature;
49 use Friendica\Util\Map;
50 use Friendica\Util\Network;
51 use Friendica\Util\XML;
53 require_once 'include/api.php';
54 require_once 'mod/share.php';
57 * ActivityPub Transmitter Protocol class
65 * Collects a list of contacts of the given owner
67 * @param array $owner Owner array
68 * @param int|array $rel The relevant value(s) contact.rel should match
69 * @param string $module The name of the relevant AP endpoint module (followers|following)
70 * @param integer $page Page number
72 * @return array of owners
75 public static function getContacts($owner, $rel, $module, $page = null)
77 $condition = ['rel' => $rel, 'network' => Protocol::FEDERATED, 'uid' => $owner['uid'],
78 'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
79 $count = DBA::count('contact', $condition);
81 $modulePath = '/' . $module . '/';
83 $data = ['@context' => ActivityPub::CONTEXT];
84 $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
85 $data['type'] = 'OrderedCollection';
86 $data['totalItems'] = $count;
88 // When we hide our friends we will only show the pure number but don't allow more.
89 $profile = Profile::getByUID($owner['uid']);
90 if (!empty($profile['hide-friends'])) {
95 $data['first'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=1';
97 $data['type'] = 'OrderedCollectionPage';
100 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
101 while ($contact = DBA::fetch($contacts)) {
102 $list[] = $contact['url'];
104 DBA::close($contacts);
107 $data['next'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=' . ($page + 1);
110 $data['partOf'] = DI::baseUrl() . $modulePath . $owner['nickname'];
112 $data['orderedItems'] = $list;
119 * Public posts for the given owner
121 * @param array $owner Owner array
122 * @param integer $page Page numbe
124 * @return array of posts
125 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
126 * @throws \ImagickException
128 public static function getOutbox($owner, $page = null)
130 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
132 $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
133 'private' => [Item::PUBLIC, Item::UNLISTED], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
134 'deleted' => false, 'visible' => true, 'moderated' => false];
135 $count = DBA::count('item', $condition);
137 $data = ['@context' => ActivityPub::CONTEXT];
138 $data['id'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
139 $data['type'] = 'OrderedCollection';
140 $data['totalItems'] = $count;
143 $data['first'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
145 $data['type'] = 'OrderedCollectionPage';
148 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
150 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
151 while ($item = Item::fetch($items)) {
152 $activity = self::createActivityFromItem($item['id'], true);
153 $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
155 // Only list "Create" activity objects here, no reshares
156 if (!empty($activity['object']) && ($activity['type'] == 'Create')) {
157 $list[] = $activity['object'];
162 $data['next'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
165 $data['partOf'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
167 $data['orderedItems'] = $list;
174 * Return the service array containing information the used software and it's url
176 * @return array with service data
178 private static function getService()
180 return ['type' => 'Service',
181 'name' => FRIENDICA_PLATFORM . " '" . FRIENDICA_CODENAME . "' " . FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
182 'url' => DI::baseUrl()->get()];
186 * Return the ActivityPub profile of the given user
188 * @param integer $uid User ID
189 * @return array with profile data
190 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
192 public static function getProfile($uid)
194 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
195 'account_removed' => false, 'verified' => true];
196 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
197 $user = DBA::selectFirst('user', $fields, $condition);
198 if (!DBA::isResult($user)) {
202 $fields = ['locality', 'region', 'country-name'];
203 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
204 if (!DBA::isResult($profile)) {
208 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
209 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
210 if (!DBA::isResult($contact)) {
214 $data = ['@context' => ActivityPub::CONTEXT];
215 $data['id'] = $contact['url'];
216 $data['diaspora:guid'] = $user['guid'];
217 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
218 $data['following'] = DI::baseUrl() . '/following/' . $user['nickname'];
219 $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname'];
220 $data['inbox'] = DI::baseUrl() . '/inbox/' . $user['nickname'];
221 $data['outbox'] = DI::baseUrl() . '/outbox/' . $user['nickname'];
222 $data['preferredUsername'] = $user['nickname'];
223 $data['name'] = $contact['name'];
224 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
225 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
226 $data['summary'] = BBCode::convert($contact['about'], false);
227 $data['url'] = $contact['url'];
228 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
229 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
230 'owner' => $contact['url'],
231 'publicKeyPem' => $user['pubkey']];
232 $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
233 $data['icon'] = ['type' => 'Image',
234 'url' => $contact['photo']];
236 $data['generator'] = self::getService();
238 // tags: https://kitty.town/@inmysocks/100656097926961126.json
243 * @param string $username
245 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
247 public static function getDeletedUser($username)
250 '@context' => ActivityPub::CONTEXT,
251 'id' => DI::baseUrl() . '/profile/' . $username,
252 'type' => 'Tombstone',
253 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
254 'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
255 'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
260 * Returns an array with permissions of a given item array
264 * @return array with permissions
265 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
266 * @throws \ImagickException
268 private static function fetchPermissionBlockFromConversation($item)
270 if (empty($item['thr-parent'])) {
274 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
275 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
276 if (!DBA::isResult($conversation)) {
280 $activity = json_decode($conversation['source'], true);
282 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
283 $profile = APContact::getByURL($actor);
285 $item_profile = APContact::getByURL($item['author-link']);
286 $exclude[] = $item['author-link'];
288 if ($item['gravity'] == GRAVITY_PARENT) {
289 $exclude[] = $item['owner-link'];
292 $permissions['to'][] = $actor;
294 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
295 if (empty($activity[$element])) {
298 if (is_string($activity[$element])) {
299 $activity[$element] = [$activity[$element]];
302 foreach ($activity[$element] as $receiver) {
303 if (empty($receiver)) {
307 if (!empty($profile['followers']) && $receiver == $profile['followers'] && !empty($item_profile['followers'])) {
308 $permissions[$element][] = $item_profile['followers'];
309 } elseif (!in_array($receiver, $exclude)) {
310 $permissions[$element][] = $receiver;
318 * Creates an array of permissions from an item thread
320 * @param array $item Item array
321 * @param boolean $blindcopy addressing via "bcc" or "cc"?
322 * @param integer $last_id Last item id for adding receivers
323 * @param boolean $forum_mode "true" means that we are sending content to a forum
325 * @return array with permission data
326 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
327 * @throws \ImagickException
329 private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false)
332 $last_id = $item['id'];
337 // Check if we should always deliver our stuff via BCC
338 if (!empty($item['uid'])) {
339 $profile = Profile::getByUID($item['uid']);
340 if (!empty($profile)) {
341 $always_bcc = $profile['hide-friends'];
345 if (DI::config()->get('system', 'ap_always_bcc')) {
349 if (self::isAnnounce($item) || DI::config()->get('debug', 'total_ap_delivery')) {
350 // Will be activated in a later step
351 $networks = Protocol::FEDERATED;
353 // For now only send to these contacts:
354 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
357 $data = ['to' => [], 'cc' => [], 'bcc' => []];
359 if ($item['gravity'] == GRAVITY_PARENT) {
360 $actor_profile = APContact::getByURL($item['owner-link']);
362 $actor_profile = APContact::getByURL($item['author-link']);
365 $terms = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
367 if ($item['private'] != Item::PRIVATE) {
368 // Directly mention the original author upon a quoted reshare.
369 // Else just ensure that the original author receives the reshare.
370 $announce = self::getAnnounceArray($item);
371 if (!empty($announce['comment'])) {
372 $data['to'][] = $announce['actor']['url'];
373 } elseif (!empty($announce)) {
374 $data['cc'][] = $announce['actor']['url'];
377 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
379 // Check if the item is completely public or unlisted
380 if ($item['private'] == Item::PUBLIC) {
381 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
383 $data['cc'][] = ActivityPub::PUBLIC_COLLECTION;
386 foreach ($terms as $term) {
387 $profile = APContact::getByURL($term['url'], false);
388 if (!empty($profile)) {
389 $data['to'][] = $profile['url'];
393 $receiver_list = Item::enumeratePermissions($item, true);
395 foreach ($terms as $term) {
396 $cid = Contact::getIdForURL($term['url'], $item['uid']);
397 if (!empty($cid) && in_array($cid, $receiver_list)) {
398 $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
399 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
403 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
404 $data['to'][] = $profile['url'];
409 foreach ($receiver_list as $receiver) {
410 $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
411 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
415 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
416 if ($contact['hidden'] || $always_bcc) {
417 $data['bcc'][] = $profile['url'];
419 $data['cc'][] = $profile['url'];
425 if (!empty($item['parent'])) {
426 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
427 while ($parent = Item::fetch($parents)) {
428 if ($parent['gravity'] == GRAVITY_PARENT) {
429 $profile = APContact::getByURL($parent['owner-link'], false);
430 if (!empty($profile)) {
431 if ($item['gravity'] != GRAVITY_PARENT) {
432 // Comments to forums are directed to the forum
433 // But comments to forums aren't directed to the followers collection
434 if ($profile['type'] == 'Group') {
435 $data['to'][] = $profile['url'];
437 $data['cc'][] = $profile['url'];
438 if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers'])) {
439 $data['cc'][] = $actor_profile['followers'];
443 // Public thread parent post always are directed to the followers
444 if (($item['private'] != Item::PRIVATE) && !$forum_mode) {
445 $data['cc'][] = $actor_profile['followers'];
451 // Don't include data from future posts
452 if ($parent['id'] >= $last_id) {
456 $profile = APContact::getByURL($parent['author-link'], false);
457 if (!empty($profile)) {
458 if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
459 $data['to'][] = $profile['url'];
461 $data['cc'][] = $profile['url'];
465 DBA::close($parents);
468 $data['to'] = array_unique($data['to']);
469 $data['cc'] = array_unique($data['cc']);
470 $data['bcc'] = array_unique($data['bcc']);
472 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
473 unset($data['to'][$key]);
476 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
477 unset($data['cc'][$key]);
480 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
481 unset($data['bcc'][$key]);
484 foreach ($data['to'] as $to) {
485 if (($key = array_search($to, $data['cc'])) !== false) {
486 unset($data['cc'][$key]);
489 if (($key = array_search($to, $data['bcc'])) !== false) {
490 unset($data['bcc'][$key]);
494 foreach ($data['cc'] as $cc) {
495 if (($key = array_search($cc, $data['bcc'])) !== false) {
496 unset($data['bcc'][$key]);
500 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
503 unset($receivers['bcc']);
510 * Check if an inbox is archived
512 * @param string $url Inbox url
514 * @return boolean "true" if inbox is archived
516 private static function archivedInbox($url)
518 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
522 * Fetches a list of inboxes of followers of a given user
524 * @param integer $uid User ID
525 * @param boolean $personal fetch personal inboxes
527 * @return array of follower inboxes
528 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
529 * @throws \ImagickException
531 public static function fetchTargetInboxesforUser($uid, $personal = false)
535 if (DI::config()->get('debug', 'total_ap_delivery')) {
536 // Will be activated in a later step
537 $networks = Protocol::FEDERATED;
539 // For now only send to these contacts:
540 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
543 $condition = ['uid' => $uid, 'archive' => false, 'pending' => false];
546 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
549 $contacts = DBA::select('contact', ['url', 'network', 'protocol'], $condition);
550 while ($contact = DBA::fetch($contacts)) {
551 if (Contact::isLocal($contact['url'])) {
555 if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
559 if (Network::isUrlBlocked($contact['url'])) {
563 $profile = APContact::getByURL($contact['url'], false);
564 if (!empty($profile)) {
565 if (empty($profile['sharedinbox']) || $personal) {
566 $target = $profile['inbox'];
568 $target = $profile['sharedinbox'];
570 if (!self::archivedInbox($target)) {
571 $inboxes[$target] = $target;
575 DBA::close($contacts);
581 * Fetches an array of inboxes for the given item and user
583 * @param array $item Item array
584 * @param integer $uid User ID
585 * @param boolean $personal fetch personal inboxes
586 * @param integer $last_id Last item id for adding receivers
587 * @param boolean $forum_mode "true" means that we are sending content to a forum
588 * @return array with inboxes
589 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
590 * @throws \ImagickException
592 public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
594 $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
595 if (empty($permissions)) {
601 if ($item['gravity'] == GRAVITY_ACTIVITY) {
602 $item_profile = APContact::getByURL($item['author-link'], false);
604 $item_profile = APContact::getByURL($item['owner-link'], false);
607 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
608 if (empty($permissions[$element])) {
612 $blindcopy = in_array($element, ['bto', 'bcc']);
614 foreach ($permissions[$element] as $receiver) {
615 if (empty($receiver) || Network::isUrlBlocked($receiver)) {
619 if ($receiver == $item_profile['followers']) {
620 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
622 if (Contact::isLocal($receiver)) {
626 $profile = APContact::getByURL($receiver, false);
627 if (!empty($profile)) {
628 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
629 $target = $profile['inbox'];
631 $target = $profile['sharedinbox'];
633 if (!self::archivedInbox($target)) {
634 $inboxes[$target] = $target;
645 * Creates an array in the structure of the item table for a given mail id
647 * @param integer $mail_id
652 public static function ItemArrayFromMail($mail_id)
654 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
655 if (!DBA::isResult($mail)) {
659 $mail['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
661 $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
663 // Making the post more compatible for Mastodon by:
664 // - Making it a note and not an article (no title)
665 // - Moving the title into the "summary" field that is used as a "content warning"
666 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
669 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
670 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
671 $mail['allow_gid'] = '';
672 $mail['deny_cid'] = '';
673 $mail['deny_gid'] = '';
674 $mail['private'] = true;
675 $mail['deleted'] = false;
676 $mail['edited'] = $mail['created'];
677 $mail['plink'] = $mail['uri'];
678 $mail['thr-parent'] = $reply['uri'];
679 $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
681 $mail['event-type'] = '';
682 $mail['attach'] = '';
690 * Creates an activity array for a given mail id
692 * @param integer $mail_id
693 * @param boolean $object_mode Is the activity item is used inside another object?
695 * @return array of activity
698 public static function createActivityFromMail($mail_id, $object_mode = false)
700 $mail = self::ItemArrayFromMail($mail_id);
701 $object = self::createNote($mail);
704 $data = ['@context' => ActivityPub::CONTEXT];
709 $data['id'] = $mail['uri'] . '#Create';
710 $data['type'] = 'Create';
711 $data['actor'] = $mail['author-link'];
712 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
713 $data['instrument'] = self::getService();
714 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
716 if (empty($data['to']) && !empty($data['cc'])) {
717 $data['to'] = $data['cc'];
720 if (empty($data['to']) && !empty($data['bcc'])) {
721 $data['to'] = $data['bcc'];
727 $object['to'] = $data['to'];
728 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
730 unset($object['cc']);
731 unset($object['bcc']);
733 $data['directMessage'] = true;
735 $data['object'] = $object;
737 $owner = User::getOwnerDataById($mail['uid']);
739 if (!$object_mode && !empty($owner)) {
740 return LDSignature::sign($data, $owner);
747 * Returns the activity type of a given item
751 * @return string with activity type
752 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
753 * @throws \ImagickException
755 private static function getTypeOfItem($item)
759 // Only check for a reshare, if it is a real reshare and no quoted reshare
760 if (strpos($item['body'], "[share") === 0) {
761 $announce = self::getAnnounceArray($item);
762 $reshared = !empty($announce);
767 } elseif ($item['verb'] == Activity::POST) {
768 if ($item['created'] == $item['edited']) {
773 } elseif ($item['verb'] == Activity::LIKE) {
775 } elseif ($item['verb'] == Activity::DISLIKE) {
777 } elseif ($item['verb'] == Activity::ATTEND) {
779 } elseif ($item['verb'] == Activity::ATTENDNO) {
781 } elseif ($item['verb'] == Activity::ATTENDMAYBE) {
782 $type = 'TentativeAccept';
783 } elseif ($item['verb'] == Activity::FOLLOW) {
785 } elseif ($item['verb'] == Activity::TAG) {
795 * Creates the activity or fetches it from the cache
797 * @param integer $item_id
798 * @param boolean $force Force new cache entry
800 * @return array with the activity
803 public static function createCachedActivityFromItem($item_id, $force = false)
805 $cachekey = 'APDelivery:createActivity:' . $item_id;
808 $data = DI::cache()->get($cachekey);
809 if (!is_null($data)) {
814 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
816 DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
821 * Creates an activity array for a given item id
823 * @param integer $item_id
824 * @param boolean $object_mode Is the activity item is used inside another object?
826 * @return array of activity
829 public static function createActivityFromItem($item_id, $object_mode = false)
831 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
833 if (!DBA::isResult($item)) {
837 if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
838 $owner = User::getOwnerDataById($item['uid']);
839 if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
842 // Disguise forum posts as reshares. Will later be converted to a real announce
843 $item['body'] = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
844 $item['guid'], $item['created'], $item['plink']) . $item['body'] . '[/share]';
849 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
850 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
851 if (DBA::isResult($conversation)) {
852 $data = json_decode($conversation['source'], true);
858 $type = self::getTypeOfItem($item);
862 $data = ['@context' => ActivityPub::CONTEXT];
864 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
866 } elseif ($item['deleted']) {
873 $data['id'] = $item['uri'] . '#' . $type;
874 $data['type'] = $type;
876 if (Item::isForumPost($item) && ($type != 'Announce')) {
877 $data['actor'] = $item['author-link'];
879 $data['actor'] = $item['owner-link'];
882 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
884 $data['instrument'] = self::getService();
886 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
888 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
889 $data['object'] = self::createNote($item);
890 } elseif ($data['type'] == 'Add') {
891 $data = self::createAddTag($item, $data);
892 } elseif ($data['type'] == 'Announce') {
893 $data = self::createAnnounce($item, $data);
894 } elseif ($data['type'] == 'Follow') {
895 $data['object'] = $item['parent-uri'];
896 } elseif ($data['type'] == 'Undo') {
897 $data['object'] = self::createActivityFromItem($item_id, true);
899 $data['diaspora:guid'] = $item['guid'];
900 if (!empty($item['signed_text'])) {
901 $data['diaspora:like'] = $item['signed_text'];
903 $data['object'] = $item['thr-parent'];
906 if (!empty($item['contact-uid'])) {
907 $uid = $item['contact-uid'];
912 $owner = User::getOwnerDataById($uid);
914 if (!$object_mode && !empty($owner)) {
915 return LDSignature::sign($data, $owner);
920 /// @todo Create "conversation" entry
924 * Creates a location entry for a given item array
928 * @return array with location array
930 private static function createLocation($item)
932 $location = ['type' => 'Place'];
934 if (!empty($item['location'])) {
935 $location['name'] = $item['location'];
940 if (empty($item['coord'])) {
941 $coord = Map::getCoordinates($item['location']);
943 $coords = explode(' ', $item['coord']);
944 if (count($coords) == 2) {
945 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
949 if (!empty($coord['lat']) && !empty($coord['lon'])) {
950 $location['latitude'] = $coord['lat'];
951 $location['longitude'] = $coord['lon'];
958 * Returns a tag array for a given item array
962 * @return array of tags
963 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
965 private static function createTagList($item)
969 $terms = Tag::getByURIId($item['uri-id'], [Tag::HASHTAG, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
970 foreach ($terms as $term) {
971 if ($term['type'] == Tag::HASHTAG) {
972 $url = DI::baseUrl() . '/search?tag=' . urlencode($term['name']);
973 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']];
975 $contact = Contact::getDetailsByURL($term['url']);
976 if (!empty($contact['addr'])) {
977 $mention = '@' . $contact['addr'];
979 $mention = '@' . $term['url'];
982 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
986 $announce = self::getAnnounceArray($item);
987 // Mention the original author upon commented reshares
988 if (!empty($announce['comment'])) {
989 $tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']];
996 * Adds attachment data to the JSON document
998 * @param array $item Data of the item that is to be posted
999 * @param string $type Object type
1001 * @return array with attachment data
1002 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1004 private static function createAttachmentList($item, $type)
1008 // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
1009 // It will be reactivated, once this cleared.
1011 $attach_data = BBCode::getAttachmentData($item['body']);
1012 if (!empty($attach_data['url'])) {
1013 $attachment = ['type' => 'Page',
1014 'mediaType' => 'text/html',
1015 'url' => $attach_data['url']];
1017 if (!empty($attach_data['title'])) {
1018 $attachment['name'] = $attach_data['title'];
1021 if (!empty($attach_data['description'])) {
1022 $attachment['summary'] = $attach_data['description'];
1025 if (!empty($attach_data['image'])) {
1026 $imgdata = Images::getInfoFromURLCached($attach_data['image']);
1028 $attachment['icon'] = ['type' => 'Image',
1029 'mediaType' => $imgdata['mime'],
1030 'width' => $imgdata[0],
1031 'height' => $imgdata[1],
1032 'url' => $attach_data['image']];
1036 $attachments[] = $attachment;
1039 $arr = explode('[/attach],', $item['attach']);
1041 foreach ($arr as $r) {
1043 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1045 $attributes = ['type' => 'Document',
1046 'mediaType' => $matches[3],
1047 'url' => $matches[1],
1050 if (trim($matches[4]) != '') {
1051 $attributes['name'] = trim($matches[4]);
1054 $attachments[] = $attributes;
1059 if ($type != 'Note') {
1060 return $attachments;
1063 // Simplify image codes
1064 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
1066 // Grab all pictures without alternative descriptions and create attachments out of them
1067 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
1068 foreach ($pictures[1] as $picture) {
1069 $imgdata = Images::getInfoFromURLCached($picture);
1071 $attachments[] = ['type' => 'Document',
1072 'mediaType' => $imgdata['mime'],
1079 // Grab all pictures with alternative description and create attachments out of them
1080 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
1081 foreach ($pictures as $picture) {
1082 $imgdata = Images::getInfoFromURLCached($picture[1]);
1084 $attachments[] = ['type' => 'Document',
1085 'mediaType' => $imgdata['mime'],
1086 'url' => $picture[1],
1087 'name' => $picture[2]];
1092 return $attachments;
1096 * Callback function to replace a Friendica style mention in a mention that is used on AP
1098 * @param array $match Matching values for the callback
1099 * @return string Replaced mention
1100 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1102 private static function mentionCallback($match)
1104 if (empty($match[1])) {
1108 $data = Contact::getDetailsByURL($match[1]);
1109 if (empty($data['nick'])) {
1113 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
1117 * Remove image elements since they are added as attachment
1119 * @param string $body
1121 * @return string with removed images
1123 private static function removePictures($body)
1125 // Simplify image codes
1126 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
1127 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $body);
1129 // Now remove local links
1130 $body = preg_replace_callback(
1131 '/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
1133 // We remove the link when it is a link to a local photo page
1134 if (Photo::isLocalPage($match[1])) {
1137 // otherwise we just return the link
1138 return '[url]' . $match[1] . '[/url]';
1143 // Remove all pictures
1144 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $body);
1150 * Fetches the "context" value for a givem item array from the "conversation" table
1152 * @param array $item
1154 * @return string with context url
1155 * @throws \Exception
1157 private static function fetchContextURLForItem($item)
1159 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
1160 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
1161 $context_uri = $conversation['conversation-href'];
1162 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
1163 $context_uri = $conversation['conversation-uri'];
1165 $context_uri = $item['parent-uri'] . '#context';
1167 return $context_uri;
1171 * Returns if the post contains sensitive content ("nsfw")
1173 * @param integer $uri_id
1176 * @throws \Exception
1178 private static function isSensitive($uri_id)
1180 return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw']);
1184 * Creates event data
1186 * @param array $item
1188 * @return array with the event data
1189 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1191 public static function createEvent($item)
1194 $event['name'] = $item['event-summary'];
1195 $event['content'] = BBCode::convert($item['event-desc'], false, 9);
1196 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
1198 if (!$item['event-nofinish']) {
1199 $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
1202 if (!empty($item['event-location'])) {
1203 $item['location'] = $item['event-location'];
1204 $event['location'] = self::createLocation($item);
1211 * Creates a note/article object array
1213 * @param array $item
1215 * @return array with the object data
1216 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1217 * @throws \ImagickException
1219 public static function createNote($item)
1225 if ($item['event-type'] == 'event') {
1227 } elseif (!empty($item['title'])) {
1233 if ($item['deleted']) {
1234 $type = 'Tombstone';
1238 $data['id'] = $item['uri'];
1239 $data['type'] = $type;
1241 if ($item['deleted']) {
1245 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1247 if ($item['uri'] != $item['thr-parent']) {
1248 $data['inReplyTo'] = $item['thr-parent'];
1250 $data['inReplyTo'] = null;
1253 $data['diaspora:guid'] = $item['guid'];
1254 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1256 if ($item['created'] != $item['edited']) {
1257 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1260 $data['url'] = $item['plink'];
1261 $data['attributedTo'] = $item['author-link'];
1262 $data['sensitive'] = self::isSensitive($item['uri-id']);
1263 $data['context'] = self::fetchContextURLForItem($item);
1265 if (!empty($item['title'])) {
1266 $data['name'] = BBCode::toPlaintext($item['title'], false);
1269 $permission_block = self::createPermissionBlockForItem($item, false);
1271 $body = $item['body'];
1273 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1274 $body = self::prependMentions($body, $permission_block);
1277 if ($type == 'Note') {
1278 $body = self::removePictures($body);
1279 } elseif (($type == 'Article') && empty($data['summary'])) {
1280 $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
1283 if ($type == 'Event') {
1284 $data = array_merge($data, self::createEvent($item));
1286 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1287 $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1289 $data['content'] = BBCode::convert($body, false, 9);
1292 // The regular "content" field does contain a minimized HTML. This is done since systems like
1293 // Mastodon has got problems with - for example - embedded pictures.
1294 // The contentMap does contain the unmodified HTML.
1295 $language = self::getLanguage($item);
1296 if (!empty($language)) {
1297 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1298 $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
1299 $richbody = BBCode::removeAttachment($richbody);
1301 $data['contentMap'][$language] = BBCode::convert($richbody, false);
1304 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1306 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1307 $data['diaspora:comment'] = $item['signed_text'];
1310 $data['attachment'] = self::createAttachmentList($item, $type);
1311 $data['tag'] = self::createTagList($item);
1313 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1314 $data['location'] = self::createLocation($item);
1317 if (!empty($item['app'])) {
1318 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1321 $data = array_merge($data, $permission_block);
1327 * Fetches the language from the post, the user or the system.
1329 * @param array $item
1331 * @return string language string
1333 private static function getLanguage(array $item)
1335 // Try to fetch the language from the post itself
1336 if (!empty($item['language'])) {
1337 $languages = array_keys(json_decode($item['language'], true));
1338 if (!empty($languages[0])) {
1339 return $languages[0];
1343 // Otherwise use the user's language
1344 if (!empty($item['uid'])) {
1345 $user = DBA::selectFirst('user', ['language'], ['uid' => $item['uid']]);
1346 if (!empty($user['language'])) {
1347 return $user['language'];
1351 // And finally just use the system language
1352 return DI::config()->get('system', 'language');
1356 * Creates an an "add tag" entry
1358 * @param array $item
1359 * @param array $data activity data
1361 * @return array with activity data for adding tags
1362 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1363 * @throws \ImagickException
1365 private static function createAddTag($item, $data)
1367 $object = XML::parseString($item['object']);
1368 $target = XML::parseString($item["target"]);
1370 $data['diaspora:guid'] = $item['guid'];
1371 $data['actor'] = $item['author-link'];
1372 $data['target'] = (string)$target->id;
1373 $data['summary'] = BBCode::toPlaintext($item['body']);
1374 $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1380 * Creates an announce object entry
1382 * @param array $item
1383 * @param array $data activity data
1385 * @return array with activity data
1386 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1387 * @throws \ImagickException
1389 private static function createAnnounce($item, $data)
1391 $orig_body = $item['body'];
1392 $announce = self::getAnnounceArray($item);
1393 if (empty($announce)) {
1394 $data['type'] = 'Create';
1395 $data['object'] = self::createNote($item);
1399 if (empty($announce['comment'])) {
1400 // Pure announce, without a quote
1401 $data['type'] = 'Announce';
1402 $data['object'] = $announce['object']['uri'];
1407 $data['type'] = 'Create';
1408 $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
1409 $data['object'] = self::createNote($item);
1411 /// @todo Finally descide how to implement this in AP. This is a possible way:
1412 $data['object']['attachment'][] = self::createNote($announce['object']);
1414 $data['object']['source']['content'] = $orig_body;
1419 * Return announce related data if the item is an annunce
1421 * @param array $item
1425 public static function getAnnounceArray($item)
1427 $reshared = Item::getShareArray($item);
1428 if (empty($reshared['guid'])) {
1432 $reshared_item = Item::selectFirst([], ['guid' => $reshared['guid']]);
1433 if (!DBA::isResult($reshared_item)) {
1437 if (!in_array($reshared_item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
1441 $profile = APContact::getByURL($reshared_item['author-link'], false);
1442 if (empty($profile)) {
1446 return ['object' => $reshared_item, 'actor' => $profile, 'comment' => $reshared['comment']];
1450 * Checks if the provided item array is an announce
1452 * @param array $item
1456 public static function isAnnounce($item)
1458 $announce = self::getAnnounceArray($item);
1459 if (empty($announce)) {
1463 return empty($announce['comment']);
1467 * Creates an activity id for a given contact id
1469 * @param integer $cid Contact ID of target
1471 * @return bool|string activity id
1473 public static function activityIDFromContact($cid)
1475 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1476 if (!DBA::isResult($contact)) {
1480 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1481 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1482 return DI::baseUrl() . '/activity/' . $uuid;
1486 * Transmits a contact suggestion to a given inbox
1488 * @param integer $uid User ID
1489 * @param string $inbox Target inbox
1490 * @param integer $suggestion_id Suggestion ID
1492 * @return boolean was the transmission successful?
1493 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1495 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1497 $owner = User::getOwnerDataById($uid);
1499 $suggestion = DI::fsuggest()->getById($suggestion_id);
1501 $data = ['@context' => ActivityPub::CONTEXT,
1502 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1503 'type' => 'Announce',
1504 'actor' => $owner['url'],
1505 'object' => $suggestion->url,
1506 'content' => $suggestion->note,
1507 'instrument' => self::getService(),
1508 'to' => [ActivityPub::PUBLIC_COLLECTION],
1511 $signed = LDSignature::sign($data, $owner);
1513 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1514 return HTTPSignature::transmit($signed, $inbox, $uid);
1518 * Transmits a profile relocation to a given inbox
1520 * @param integer $uid User ID
1521 * @param string $inbox Target inbox
1523 * @return boolean was the transmission successful?
1524 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1526 public static function sendProfileRelocation($uid, $inbox)
1528 $owner = User::getOwnerDataById($uid);
1530 $data = ['@context' => ActivityPub::CONTEXT,
1531 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1532 'type' => 'dfrn:relocate',
1533 'actor' => $owner['url'],
1534 'object' => $owner['url'],
1535 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1536 'instrument' => self::getService(),
1537 'to' => [ActivityPub::PUBLIC_COLLECTION],
1540 $signed = LDSignature::sign($data, $owner);
1542 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1543 return HTTPSignature::transmit($signed, $inbox, $uid);
1547 * Transmits a profile deletion to a given inbox
1549 * @param integer $uid User ID
1550 * @param string $inbox Target inbox
1552 * @return boolean was the transmission successful?
1553 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1555 public static function sendProfileDeletion($uid, $inbox)
1557 $owner = User::getOwnerDataById($uid);
1559 if (empty($owner)) {
1560 Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
1564 if (empty($owner['uprvkey'])) {
1565 Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
1569 $data = ['@context' => ActivityPub::CONTEXT,
1570 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1572 'actor' => $owner['url'],
1573 'object' => $owner['url'],
1574 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1575 'instrument' => self::getService(),
1576 'to' => [ActivityPub::PUBLIC_COLLECTION],
1579 $signed = LDSignature::sign($data, $owner);
1581 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1582 return HTTPSignature::transmit($signed, $inbox, $uid);
1586 * Transmits a profile change to a given inbox
1588 * @param integer $uid User ID
1589 * @param string $inbox Target inbox
1591 * @return boolean was the transmission successful?
1592 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1593 * @throws \ImagickException
1595 public static function sendProfileUpdate($uid, $inbox)
1597 $owner = User::getOwnerDataById($uid);
1598 $profile = APContact::getByURL($owner['url']);
1600 $data = ['@context' => ActivityPub::CONTEXT,
1601 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1603 'actor' => $owner['url'],
1604 'object' => self::getProfile($uid),
1605 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1606 'instrument' => self::getService(),
1607 'to' => [$profile['followers']],
1610 $signed = LDSignature::sign($data, $owner);
1612 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1613 return HTTPSignature::transmit($signed, $inbox, $uid);
1617 * Transmits a given activity to a target
1619 * @param string $activity Type name
1620 * @param string $target Target profile
1621 * @param integer $uid User ID
1623 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1624 * @throws \ImagickException
1625 * @throws \Exception
1627 public static function sendActivity($activity, $target, $uid, $id = '')
1629 $profile = APContact::getByURL($target);
1630 if (empty($profile['inbox'])) {
1631 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1635 $owner = User::getOwnerDataById($uid);
1638 $id = DI::baseUrl() . '/activity/' . System::createGUID();
1641 $data = ['@context' => ActivityPub::CONTEXT,
1643 'type' => $activity,
1644 'actor' => $owner['url'],
1645 'object' => $profile['url'],
1646 'instrument' => self::getService(),
1647 'to' => [$profile['url']]];
1649 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1651 $signed = LDSignature::sign($data, $owner);
1652 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1656 * Transmits a "follow object" activity to a target
1657 * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
1659 * @param string $object Object URL
1660 * @param string $target Target profile
1661 * @param integer $uid User ID
1663 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1664 * @throws \ImagickException
1665 * @throws \Exception
1667 public static function sendFollowObject($object, $target, $uid = 0)
1669 $profile = APContact::getByURL($target);
1670 if (empty($profile['inbox'])) {
1671 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1676 // Fetch the list of administrators
1677 $admin_mail = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
1679 // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
1680 $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
1681 $first_user = DBA::selectFirst('user', ['uid'], $condition);
1682 $uid = $first_user['uid'];
1685 $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
1686 'author-id' => Contact::getPublicIdByUserId($uid)];
1687 if (Item::exists($condition)) {
1688 Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
1692 $owner = User::getOwnerDataById($uid);
1694 $data = ['@context' => ActivityPub::CONTEXT,
1695 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1697 'actor' => $owner['url'],
1698 'object' => $object,
1699 'instrument' => self::getService(),
1700 'to' => [$profile['url']]];
1702 Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1704 $signed = LDSignature::sign($data, $owner);
1705 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1709 * Transmit a message that the contact request had been accepted
1711 * @param string $target Target profile
1713 * @param integer $uid User ID
1714 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1715 * @throws \ImagickException
1717 public static function sendContactAccept($target, $id, $uid)
1719 $profile = APContact::getByURL($target);
1720 if (empty($profile['inbox'])) {
1721 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1725 $owner = User::getOwnerDataById($uid);
1726 $data = ['@context' => ActivityPub::CONTEXT,
1727 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1729 'actor' => $owner['url'],
1731 'id' => (string)$id,
1733 'actor' => $profile['url'],
1734 'object' => $owner['url']
1736 'instrument' => self::getService(),
1737 'to' => [$profile['url']]];
1739 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1741 $signed = LDSignature::sign($data, $owner);
1742 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1746 * Reject a contact request or terminates the contact relation
1748 * @param string $target Target profile
1750 * @param integer $uid User ID
1751 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1752 * @throws \ImagickException
1754 public static function sendContactReject($target, $id, $uid)
1756 $profile = APContact::getByURL($target);
1757 if (empty($profile['inbox'])) {
1758 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1762 $owner = User::getOwnerDataById($uid);
1763 $data = ['@context' => ActivityPub::CONTEXT,
1764 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1766 'actor' => $owner['url'],
1768 'id' => (string)$id,
1770 'actor' => $profile['url'],
1771 'object' => $owner['url']
1773 'instrument' => self::getService(),
1774 'to' => [$profile['url']]];
1776 Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1778 $signed = LDSignature::sign($data, $owner);
1779 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1783 * Transmits a message that we don't want to follow this contact anymore
1785 * @param string $target Target profile
1786 * @param integer $uid User ID
1787 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1788 * @throws \ImagickException
1789 * @throws \Exception
1791 public static function sendContactUndo($target, $cid, $uid)
1793 $profile = APContact::getByURL($target);
1794 if (empty($profile['inbox'])) {
1795 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1799 $object_id = self::activityIDFromContact($cid);
1800 if (empty($object_id)) {
1804 $id = DI::baseUrl() . '/activity/' . System::createGUID();
1806 $owner = User::getOwnerDataById($uid);
1807 $data = ['@context' => ActivityPub::CONTEXT,
1810 'actor' => $owner['url'],
1811 'object' => ['id' => $object_id, 'type' => 'Follow',
1812 'actor' => $owner['url'],
1813 'object' => $profile['url']],
1814 'instrument' => self::getService(),
1815 'to' => [$profile['url']]];
1817 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1819 $signed = LDSignature::sign($data, $owner);
1820 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1823 private static function prependMentions($body, array $permission_block)
1825 if (DI::config()->get('system', 'disable_implicit_mentions')) {
1831 foreach ($permission_block['to'] as $profile_url) {
1832 $profile = Contact::getDetailsByURL($profile_url);
1833 if (!empty($profile['addr'])
1834 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
1835 && !strstr($body, $profile['addr'])
1836 && !strstr($body, $profile_url)
1838 $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]';
1842 $mentions[] = $body;
1844 return implode(' ', $mentions);