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)
79 'uid' => $owner['uid'],
86 $condition = DBA::buildCondition($parameters);
88 $sql = "SELECT COUNT(*) as `count`
90 JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
93 $contacts = DBA::fetchFirst($sql, ...$parameters);
95 $modulePath = '/' . $module . '/';
97 $data = ['@context' => ActivityPub::CONTEXT];
98 $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
99 $data['type'] = 'OrderedCollection';
100 $data['totalItems'] = $contacts['count'];
102 // When we hide our friends we will only show the pure number but don't allow more.
103 $profile = Profile::getByUID($owner['uid']);
104 if (!empty($profile['hide-friends'])) {
109 $data['first'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=1';
111 $data['type'] = 'OrderedCollectionPage';
114 $sql = "SELECT `contact`.`url`
116 JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
120 $parameters[] = ($page - 1) * 100;
123 $contacts = DBA::p($sql, ...$parameters);
124 while ($contact = DBA::fetch($contacts)) {
125 $list[] = $contact['url'];
127 DBA::close($contacts);
130 $data['next'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=' . ($page + 1);
133 $data['partOf'] = DI::baseUrl() . $modulePath . $owner['nickname'];
135 $data['orderedItems'] = $list;
142 * Public posts for the given owner
144 * @param array $owner Owner array
145 * @param integer $page Page numbe
147 * @return array of posts
148 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
149 * @throws \ImagickException
151 public static function getOutbox($owner, $page = null)
153 $public_contact = Contact::getIdForURL($owner['url']);
155 $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
156 'private' => [Item::PUBLIC, Item::UNLISTED], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
157 'deleted' => false, 'visible' => true, 'moderated' => false];
158 $count = DBA::count('item', $condition);
160 $data = ['@context' => ActivityPub::CONTEXT];
161 $data['id'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
162 $data['type'] = 'OrderedCollection';
163 $data['totalItems'] = $count;
166 $data['first'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
168 $data['type'] = 'OrderedCollectionPage';
171 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
173 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
174 while ($item = Item::fetch($items)) {
175 $activity = self::createActivityFromItem($item['id'], true);
176 $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
178 // Only list "Create" activity objects here, no reshares
179 if (!empty($activity['object']) && ($activity['type'] == 'Create')) {
180 $list[] = $activity['object'];
185 $data['next'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
188 $data['partOf'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
190 $data['orderedItems'] = $list;
197 * Return the service array containing information the used software and it's url
199 * @return array with service data
201 private static function getService()
203 return ['type' => 'Service',
204 'name' => FRIENDICA_PLATFORM . " '" . FRIENDICA_CODENAME . "' " . FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
205 'url' => DI::baseUrl()->get()];
209 * Return the ActivityPub profile of the given user
211 * @param integer $uid User ID
212 * @return array with profile data
213 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
215 public static function getProfile($uid)
218 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
219 'account_removed' => false, 'verified' => true];
220 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
221 $user = DBA::selectFirst('user', $fields, $condition);
222 if (!DBA::isResult($user)) {
226 $fields = ['locality', 'region', 'country-name'];
227 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
228 if (!DBA::isResult($profile)) {
232 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
233 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
234 if (!DBA::isResult($contact)) {
238 $contact = User::getSystemAccount();
239 $user = ['guid' => '', 'nickname' => $contact['nick'], 'pubkey' => $contact['pubkey'],
240 'account-type' => $contact['contact-type'], 'page-flags' => User::PAGE_FLAGS_NORMAL];
241 $profile = ['locality' => '', 'region' => '', 'country-name' => ''];
244 $data = ['@context' => ActivityPub::CONTEXT];
245 $data['id'] = $contact['url'];
247 if (!empty($user['guid'])) {
248 $data['diaspora:guid'] = $user['guid'];
251 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
254 $data['following'] = DI::baseUrl() . '/following/' . $user['nickname'];
255 $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname'];
256 $data['inbox'] = DI::baseUrl() . '/inbox/' . $user['nickname'];
257 $data['outbox'] = DI::baseUrl() . '/outbox/' . $user['nickname'];
259 $data['inbox'] = DI::baseUrl() . '/friendica/inbox';
262 $data['preferredUsername'] = $user['nickname'];
263 $data['name'] = $contact['name'];
265 if (!empty($profile['country-name'] . $profile['region'] . $profile['locality'])) {
266 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
267 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
270 if (!empty($contact['about'])) {
271 $data['summary'] = BBCode::convert($contact['about'], false);
274 $data['url'] = $contact['url'];
275 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
276 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
277 'owner' => $contact['url'],
278 'publicKeyPem' => $user['pubkey']];
279 $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
280 $data['icon'] = ['type' => 'Image',
281 'url' => $contact['photo']];
283 $data['generator'] = self::getService();
285 // tags: https://kitty.town/@inmysocks/100656097926961126.json
290 * @param string $username
292 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
294 public static function getDeletedUser($username)
297 '@context' => ActivityPub::CONTEXT,
298 'id' => DI::baseUrl() . '/profile/' . $username,
299 'type' => 'Tombstone',
300 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
301 'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
302 'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
307 * Returns an array with permissions of a given item array
311 * @return array with permissions
312 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
313 * @throws \ImagickException
315 private static function fetchPermissionBlockFromConversation($item)
317 if (empty($item['thr-parent'])) {
321 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
322 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
323 if (!DBA::isResult($conversation)) {
327 $activity = json_decode($conversation['source'], true);
329 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
330 $profile = APContact::getByURL($actor);
332 $item_profile = APContact::getByURL($item['author-link']);
333 $exclude[] = $item['author-link'];
335 if ($item['gravity'] == GRAVITY_PARENT) {
336 $exclude[] = $item['owner-link'];
339 $permissions['to'][] = $actor;
341 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
342 if (empty($activity[$element])) {
345 if (is_string($activity[$element])) {
346 $activity[$element] = [$activity[$element]];
349 foreach ($activity[$element] as $receiver) {
350 if (empty($receiver)) {
354 if (!empty($profile['followers']) && $receiver == $profile['followers'] && !empty($item_profile['followers'])) {
355 $permissions[$element][] = $item_profile['followers'];
356 } elseif (!in_array($receiver, $exclude)) {
357 $permissions[$element][] = $receiver;
365 * Creates an array of permissions from an item thread
367 * @param array $item Item array
368 * @param boolean $blindcopy addressing via "bcc" or "cc"?
369 * @param integer $last_id Last item id for adding receivers
370 * @param boolean $forum_mode "true" means that we are sending content to a forum
372 * @return array with permission data
373 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
374 * @throws \ImagickException
376 private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false)
379 $last_id = $item['id'];
385 // Check if we should always deliver our stuff via BCC
386 if (!empty($item['uid'])) {
387 $profile = User::getOwnerDataById($item['uid']);
388 if (!empty($profile)) {
389 $always_bcc = $profile['hide-friends'];
390 $isforum = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
394 if (DI::config()->get('system', 'ap_always_bcc')) {
398 if ((self::isAnnounce($item) && !$isforum) || DI::config()->get('debug', 'total_ap_delivery')) {
399 // Will be activated in a later step
400 $networks = Protocol::FEDERATED;
402 // For now only send to these contacts:
403 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
406 $data = ['to' => [], 'cc' => [], 'bcc' => []];
408 if ($item['gravity'] == GRAVITY_PARENT) {
409 $actor_profile = APContact::getByURL($item['owner-link']);
411 $actor_profile = APContact::getByURL($item['author-link']);
414 $terms = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
416 if ($item['private'] != Item::PRIVATE) {
417 // Directly mention the original author upon a quoted reshare.
418 // Else just ensure that the original author receives the reshare.
419 $announce = self::getAnnounceArray($item);
420 if (!empty($announce['comment'])) {
421 $data['to'][] = $announce['actor']['url'];
422 } elseif (!empty($announce)) {
423 $data['cc'][] = $announce['actor']['url'];
426 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
428 // Check if the item is completely public or unlisted
429 if ($item['private'] == Item::PUBLIC) {
430 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
432 $data['cc'][] = ActivityPub::PUBLIC_COLLECTION;
435 foreach ($terms as $term) {
436 $profile = APContact::getByURL($term['url'], false);
437 if (!empty($profile)) {
438 $data['to'][] = $profile['url'];
442 $receiver_list = Item::enumeratePermissions($item, true);
444 foreach ($terms as $term) {
445 $cid = Contact::getIdForURL($term['url'], $item['uid']);
446 if (!empty($cid) && in_array($cid, $receiver_list)) {
447 $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
448 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
452 if ($isforum && DBA::isResult($contact) && ($contact['dfrn'] == Protocol::DFRN)) {
456 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
457 $data['to'][] = $profile['url'];
462 foreach ($receiver_list as $receiver) {
463 $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
464 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
468 if ($isforum && DBA::isResult($contact) && ($contact['dfrn'] == Protocol::DFRN)) {
472 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
473 if ($contact['hidden'] || $always_bcc) {
474 $data['bcc'][] = $profile['url'];
476 $data['cc'][] = $profile['url'];
482 if (!empty($item['parent'])) {
483 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
484 while ($parent = Item::fetch($parents)) {
485 if ($parent['gravity'] == GRAVITY_PARENT) {
486 $profile = APContact::getByURL($parent['owner-link'], false);
487 if (!empty($profile)) {
488 if ($item['gravity'] != GRAVITY_PARENT) {
489 // Comments to forums are directed to the forum
490 // But comments to forums aren't directed to the followers collection
491 // This rule is only valid when the actor isn't the forum.
492 // The forum needs to transmit their content to their followers.
493 if (($profile['type'] == 'Group') && ($profile['url'] != $actor_profile['url'])) {
494 $data['to'][] = $profile['url'];
496 $data['cc'][] = $profile['url'];
497 if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers'])) {
498 $data['cc'][] = $actor_profile['followers'];
502 // Public thread parent post always are directed to the followers
503 if (($item['private'] != Item::PRIVATE) && !$forum_mode) {
504 $data['cc'][] = $actor_profile['followers'];
510 // Don't include data from future posts
511 if ($parent['id'] >= $last_id) {
515 $profile = APContact::getByURL($parent['author-link'], false);
516 if (!empty($profile)) {
517 if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
518 $data['to'][] = $profile['url'];
520 $data['cc'][] = $profile['url'];
524 DBA::close($parents);
527 $data['to'] = array_unique($data['to']);
528 $data['cc'] = array_unique($data['cc']);
529 $data['bcc'] = array_unique($data['bcc']);
531 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
532 unset($data['to'][$key]);
535 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
536 unset($data['cc'][$key]);
539 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
540 unset($data['bcc'][$key]);
543 foreach ($data['to'] as $to) {
544 if (($key = array_search($to, $data['cc'])) !== false) {
545 unset($data['cc'][$key]);
548 if (($key = array_search($to, $data['bcc'])) !== false) {
549 unset($data['bcc'][$key]);
553 foreach ($data['cc'] as $cc) {
554 if (($key = array_search($cc, $data['bcc'])) !== false) {
555 unset($data['bcc'][$key]);
559 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
562 unset($receivers['bcc']);
569 * Check if an inbox is archived
571 * @param string $url Inbox url
573 * @return boolean "true" if inbox is archived
575 private static function archivedInbox($url)
577 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
581 * Fetches a list of inboxes of followers of a given user
583 * @param integer $uid User ID
584 * @param boolean $personal fetch personal inboxes
586 * @return array of follower inboxes
587 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
588 * @throws \ImagickException
590 public static function fetchTargetInboxesforUser($uid, $personal = false)
596 if (!empty($item['uid'])) {
597 $profile = User::getOwnerDataById($item['uid']);
598 if (!empty($profile)) {
599 $isforum = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
603 if (DI::config()->get('debug', 'total_ap_delivery')) {
604 // Will be activated in a later step
605 $networks = Protocol::FEDERATED;
607 // For now only send to these contacts:
608 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
611 $condition = ['uid' => $uid, 'archive' => false, 'pending' => false];
614 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
617 $contacts = DBA::select('contact', ['url', 'network', 'protocol'], $condition);
618 while ($contact = DBA::fetch($contacts)) {
619 if (Contact::isLocal($contact['url'])) {
623 if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
627 if ($isforum && ($contact['dfrn'] == Protocol::DFRN)) {
631 if (Network::isUrlBlocked($contact['url'])) {
635 $profile = APContact::getByURL($contact['url'], false);
636 if (!empty($profile)) {
637 if (empty($profile['sharedinbox']) || $personal) {
638 $target = $profile['inbox'];
640 $target = $profile['sharedinbox'];
642 if (!self::archivedInbox($target)) {
643 $inboxes[$target] = $target;
647 DBA::close($contacts);
653 * Fetches an array of inboxes for the given item and user
655 * @param array $item Item array
656 * @param integer $uid User ID
657 * @param boolean $personal fetch personal inboxes
658 * @param integer $last_id Last item id for adding receivers
659 * @param boolean $forum_mode "true" means that we are sending content to a forum
660 * @return array with inboxes
661 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
662 * @throws \ImagickException
664 public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
666 $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
667 if (empty($permissions)) {
673 if ($item['gravity'] == GRAVITY_ACTIVITY) {
674 $item_profile = APContact::getByURL($item['author-link'], false);
676 $item_profile = APContact::getByURL($item['owner-link'], false);
679 if (empty($item_profile)) {
683 $profile_uid = User::getIdForURL($item_profile['url']);
685 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
686 if (empty($permissions[$element])) {
690 $blindcopy = in_array($element, ['bto', 'bcc']);
692 foreach ($permissions[$element] as $receiver) {
693 if (empty($receiver) || Network::isUrlBlocked($receiver)) {
697 if ($item_profile && ($receiver == $item_profile['followers']) && ($uid == $profile_uid)) {
698 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
700 if (Contact::isLocal($receiver)) {
704 $profile = APContact::getByURL($receiver, false);
705 if (!empty($profile)) {
706 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
707 $target = $profile['inbox'];
709 $target = $profile['sharedinbox'];
711 if (!self::archivedInbox($target)) {
712 $inboxes[$target] = $target;
723 * Creates an array in the structure of the item table for a given mail id
725 * @param integer $mail_id
730 public static function ItemArrayFromMail($mail_id)
732 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
733 if (!DBA::isResult($mail)) {
737 $mail['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
739 $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
741 // Making the post more compatible for Mastodon by:
742 // - Making it a note and not an article (no title)
743 // - Moving the title into the "summary" field that is used as a "content warning"
744 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
747 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
748 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
749 $mail['allow_gid'] = '';
750 $mail['deny_cid'] = '';
751 $mail['deny_gid'] = '';
752 $mail['private'] = true;
753 $mail['deleted'] = false;
754 $mail['edited'] = $mail['created'];
755 $mail['plink'] = $mail['uri'];
756 $mail['thr-parent'] = $reply['uri'];
757 $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
759 $mail['event-type'] = '';
760 $mail['attach'] = '';
768 * Creates an activity array for a given mail id
770 * @param integer $mail_id
771 * @param boolean $object_mode Is the activity item is used inside another object?
773 * @return array of activity
776 public static function createActivityFromMail($mail_id, $object_mode = false)
778 $mail = self::ItemArrayFromMail($mail_id);
779 $object = self::createNote($mail);
782 $data = ['@context' => ActivityPub::CONTEXT];
787 $data['id'] = $mail['uri'] . '/Create';
788 $data['type'] = 'Create';
789 $data['actor'] = $mail['author-link'];
790 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
791 $data['instrument'] = self::getService();
792 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
794 if (empty($data['to']) && !empty($data['cc'])) {
795 $data['to'] = $data['cc'];
798 if (empty($data['to']) && !empty($data['bcc'])) {
799 $data['to'] = $data['bcc'];
805 $object['to'] = $data['to'];
806 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
808 unset($object['cc']);
809 unset($object['bcc']);
811 $data['directMessage'] = true;
813 $data['object'] = $object;
815 $owner = User::getOwnerDataById($mail['uid']);
817 if (!$object_mode && !empty($owner)) {
818 return LDSignature::sign($data, $owner);
825 * Returns the activity type of a given item
829 * @return string with activity type
830 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
831 * @throws \ImagickException
833 private static function getTypeOfItem($item)
837 // Only check for a reshare, if it is a real reshare and no quoted reshare
838 if (strpos($item['body'], "[share") === 0) {
839 $announce = self::getAnnounceArray($item);
840 $reshared = !empty($announce);
845 } elseif ($item['verb'] == Activity::POST) {
846 if ($item['created'] == $item['edited']) {
851 } elseif ($item['verb'] == Activity::LIKE) {
853 } elseif ($item['verb'] == Activity::DISLIKE) {
855 } elseif ($item['verb'] == Activity::ATTEND) {
857 } elseif ($item['verb'] == Activity::ATTENDNO) {
859 } elseif ($item['verb'] == Activity::ATTENDMAYBE) {
860 $type = 'TentativeAccept';
861 } elseif ($item['verb'] == Activity::FOLLOW) {
863 } elseif ($item['verb'] == Activity::TAG) {
865 } elseif ($item['verb'] == Activity::ANNOUNCE) {
875 * Creates the activity or fetches it from the cache
877 * @param integer $item_id
878 * @param boolean $force Force new cache entry
880 * @return array with the activity
883 public static function createCachedActivityFromItem($item_id, $force = false)
885 $cachekey = 'APDelivery:createActivity:' . $item_id;
888 $data = DI::cache()->get($cachekey);
889 if (!is_null($data)) {
894 $data = self::createActivityFromItem($item_id);
896 DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
901 * Creates an activity array for a given item id
903 * @param integer $item_id
904 * @param boolean $object_mode Is the activity item is used inside another object?
906 * @return array of activity
909 public static function createActivityFromItem($item_id, $object_mode = false)
911 Logger::info('Fetching activity', ['item' => $item_id]);
912 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
913 if (!DBA::isResult($item)) {
917 // In case of a forum post ensure to return the original post if author and forum are on the same machine
918 if (!empty($item['forum_mode'])) {
919 $author = Contact::getById($item['author-id'], ['nurl']);
920 if (!empty($author['nurl'])) {
921 $self = Contact::selectFirst(['uid'], ['nurl' => $author['nurl'], 'self' => true]);
922 if (!empty($self['uid'])) {
923 $item = Item::selectFirst([], ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]);
929 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
930 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
931 if (DBA::isResult($conversation)) {
932 $data = json_decode($conversation['source'], true);
933 if (!empty($data['type'])) {
934 if (in_array($data['type'], ['Create', 'Update'])) {
936 unset($data['@context']);
937 unset($data['signature']);
939 Logger::info('Return stored conversation', ['item' => $item_id]);
941 } elseif (in_array('as:' . $data['type'], Receiver::CONTENT_TYPES)) {
942 if (!empty($data['@context'])) {
943 $context = $data['@context'];
944 unset($data['@context']);
946 unset($data['actor']);
952 $type = self::getTypeOfItem($item);
956 $data = ['@context' => $context ?? ActivityPub::CONTEXT];
958 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
960 } elseif ($item['deleted']) {
967 $data['id'] = $item['uri'] . '/' . $type;
968 $data['type'] = $type;
970 if (($type != 'Announce') || ($item['gravity'] != GRAVITY_PARENT)) {
971 $data['actor'] = $item['author-link'];
973 $data['actor'] = $item['owner-link'];
976 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
978 $data['instrument'] = self::getService();
980 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
982 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
983 $data['object'] = $object ?? self::createNote($item);
984 } elseif ($data['type'] == 'Add') {
985 $data = self::createAddTag($item, $data);
986 } elseif ($data['type'] == 'Announce') {
987 if ($item['verb'] == ACTIVITY::ANNOUNCE) {
988 $data['object'] = $item['thr-parent'];
990 $data = self::createAnnounce($item, $data);
992 } elseif ($data['type'] == 'Follow') {
993 $data['object'] = $item['parent-uri'];
994 } elseif ($data['type'] == 'Undo') {
995 $data['object'] = self::createActivityFromItem($item_id, true);
997 $data['diaspora:guid'] = $item['guid'];
998 if (!empty($item['signed_text'])) {
999 $data['diaspora:like'] = $item['signed_text'];
1001 $data['object'] = $item['thr-parent'];
1004 if (!empty($item['contact-uid'])) {
1005 $uid = $item['contact-uid'];
1007 $uid = $item['uid'];
1010 $owner = User::getOwnerDataById($uid);
1012 Logger::info('Fetched activity', ['item' => $item_id, 'uid' => $uid]);
1014 // We don't sign if we aren't the actor. This is important for relaying content especially for forums
1015 if (!$object_mode && !empty($owner) && ($data['actor'] == $owner['url'])) {
1016 return LDSignature::sign($data, $owner);
1021 /// @todo Create "conversation" entry
1025 * Creates a location entry for a given item array
1027 * @param array $item
1029 * @return array with location array
1031 private static function createLocation($item)
1033 $location = ['type' => 'Place'];
1035 if (!empty($item['location'])) {
1036 $location['name'] = $item['location'];
1041 if (empty($item['coord'])) {
1042 $coord = Map::getCoordinates($item['location']);
1044 $coords = explode(' ', $item['coord']);
1045 if (count($coords) == 2) {
1046 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
1050 if (!empty($coord['lat']) && !empty($coord['lon'])) {
1051 $location['latitude'] = $coord['lat'];
1052 $location['longitude'] = $coord['lon'];
1059 * Returns a tag array for a given item array
1061 * @param array $item
1063 * @return array of tags
1064 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1066 private static function createTagList($item)
1070 $terms = Tag::getByURIId($item['uri-id'], [Tag::HASHTAG, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
1071 foreach ($terms as $term) {
1072 if ($term['type'] == Tag::HASHTAG) {
1073 $url = DI::baseUrl() . '/search?tag=' . urlencode($term['name']);
1074 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']];
1076 $contact = Contact::getByURL($term['url'], false, ['addr']);
1077 if (empty($contact)) {
1080 if (!empty($contact['addr'])) {
1081 $mention = '@' . $contact['addr'];
1083 $mention = '@' . $term['url'];
1086 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
1090 $announce = self::getAnnounceArray($item);
1091 // Mention the original author upon commented reshares
1092 if (!empty($announce['comment'])) {
1093 $tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']];
1100 * Adds attachment data to the JSON document
1102 * @param array $item Data of the item that is to be posted
1103 * @param string $type Object type
1105 * @return array with attachment data
1106 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1108 private static function createAttachmentList($item, $type)
1112 // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
1113 // It will be reactivated, once this cleared.
1115 $attach_data = BBCode::getAttachmentData($item['body']);
1116 if (!empty($attach_data['url'])) {
1117 $attachment = ['type' => 'Page',
1118 'mediaType' => 'text/html',
1119 'url' => $attach_data['url']];
1121 if (!empty($attach_data['title'])) {
1122 $attachment['name'] = $attach_data['title'];
1125 if (!empty($attach_data['description'])) {
1126 $attachment['summary'] = $attach_data['description'];
1129 if (!empty($attach_data['image'])) {
1130 $imgdata = Images::getInfoFromURLCached($attach_data['image']);
1132 $attachment['icon'] = ['type' => 'Image',
1133 'mediaType' => $imgdata['mime'],
1134 'width' => $imgdata[0],
1135 'height' => $imgdata[1],
1136 'url' => $attach_data['image']];
1140 $attachments[] = $attachment;
1143 $arr = explode('[/attach],', $item['attach']);
1145 foreach ($arr as $r) {
1147 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1149 $attributes = ['type' => 'Document',
1150 'mediaType' => $matches[3],
1151 'url' => $matches[1],
1154 if (trim($matches[4]) != '') {
1155 $attributes['name'] = trim($matches[4]);
1158 $attachments[] = $attributes;
1163 if ($type != 'Note') {
1164 return $attachments;
1167 // Simplify image codes
1168 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
1170 // Grab all pictures without alternative descriptions and create attachments out of them
1171 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
1172 foreach ($pictures[1] as $picture) {
1173 $imgdata = Images::getInfoFromURLCached($picture);
1175 $attachments[] = ['type' => 'Document',
1176 'mediaType' => $imgdata['mime'],
1183 // Grab all pictures with alternative description and create attachments out of them
1184 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
1185 foreach ($pictures as $picture) {
1186 $imgdata = Images::getInfoFromURLCached($picture[1]);
1188 $attachments[] = ['type' => 'Document',
1189 'mediaType' => $imgdata['mime'],
1190 'url' => $picture[1],
1191 'name' => $picture[2]];
1196 return $attachments;
1200 * Callback function to replace a Friendica style mention in a mention that is used on AP
1202 * @param array $match Matching values for the callback
1203 * @return string Replaced mention
1204 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1206 private static function mentionCallback($match)
1208 if (empty($match[1])) {
1212 $data = Contact::getByURL($match[1], false, ['url', 'nick']);
1213 if (empty($data['nick'])) {
1217 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
1221 * Remove image elements since they are added as attachment
1223 * @param string $body
1225 * @return string with removed images
1227 private static function removePictures($body)
1229 // Simplify image codes
1230 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
1231 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $body);
1233 // Now remove local links
1234 $body = preg_replace_callback(
1235 '/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
1237 // We remove the link when it is a link to a local photo page
1238 if (Photo::isLocalPage($match[1])) {
1241 // otherwise we just return the link
1242 return '[url]' . $match[1] . '[/url]';
1247 // Remove all pictures
1248 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $body);
1254 * Fetches the "context" value for a givem item array from the "conversation" table
1256 * @param array $item
1258 * @return string with context url
1259 * @throws \Exception
1261 private static function fetchContextURLForItem($item)
1263 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
1264 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
1265 $context_uri = $conversation['conversation-href'];
1266 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
1267 $context_uri = $conversation['conversation-uri'];
1269 $context_uri = $item['parent-uri'] . '#context';
1271 return $context_uri;
1275 * Returns if the post contains sensitive content ("nsfw")
1277 * @param integer $uri_id
1280 * @throws \Exception
1282 private static function isSensitive($uri_id)
1284 return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw']);
1288 * Creates event data
1290 * @param array $item
1292 * @return array with the event data
1293 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1295 public static function createEvent($item)
1298 $event['name'] = $item['event-summary'];
1299 $event['content'] = BBCode::convert($item['event-desc'], false, BBCode::ACTIVITYPUB);
1300 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
1302 if (!$item['event-nofinish']) {
1303 $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
1306 if (!empty($item['event-location'])) {
1307 $item['location'] = $item['event-location'];
1308 $event['location'] = self::createLocation($item);
1315 * Creates a note/article object array
1317 * @param array $item
1319 * @return array with the object data
1320 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1321 * @throws \ImagickException
1323 public static function createNote($item)
1329 if ($item['event-type'] == 'event') {
1331 } elseif (!empty($item['title'])) {
1337 if ($item['deleted']) {
1338 $type = 'Tombstone';
1342 $data['id'] = $item['uri'];
1343 $data['type'] = $type;
1345 if ($item['deleted']) {
1349 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1351 if ($item['uri'] != $item['thr-parent']) {
1352 $data['inReplyTo'] = $item['thr-parent'];
1354 $data['inReplyTo'] = null;
1357 $data['diaspora:guid'] = $item['guid'];
1358 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1360 if ($item['created'] != $item['edited']) {
1361 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1364 $data['url'] = $item['plink'];
1365 $data['attributedTo'] = $item['author-link'];
1366 $data['sensitive'] = self::isSensitive($item['uri-id']);
1367 $data['context'] = self::fetchContextURLForItem($item);
1369 if (!empty($item['title'])) {
1370 $data['name'] = BBCode::toPlaintext($item['title'], false);
1373 $permission_block = self::createPermissionBlockForItem($item, false);
1375 $body = $item['body'];
1377 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1378 $body = self::prependMentions($body, $item['uri-id']);
1381 if ($type == 'Note') {
1382 $body = self::removePictures($body);
1383 } elseif (($type == 'Article') && empty($data['summary'])) {
1384 $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
1387 if ($type == 'Event') {
1388 $data = array_merge($data, self::createEvent($item));
1390 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1391 $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1393 $data['content'] = BBCode::convert($body, false, BBCode::ACTIVITYPUB);
1396 // The regular "content" field does contain a minimized HTML. This is done since systems like
1397 // Mastodon has got problems with - for example - embedded pictures.
1398 // The contentMap does contain the unmodified HTML.
1399 $language = self::getLanguage($item);
1400 if (!empty($language)) {
1401 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1402 $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
1403 $richbody = BBCode::removeAttachment($richbody);
1405 $data['contentMap'][$language] = BBCode::convert($richbody, false);
1408 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1410 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1411 $data['diaspora:comment'] = $item['signed_text'];
1414 $data['attachment'] = self::createAttachmentList($item, $type);
1415 $data['tag'] = self::createTagList($item);
1417 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1418 $data['location'] = self::createLocation($item);
1421 if (!empty($item['app'])) {
1422 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1425 $data = array_merge($data, $permission_block);
1431 * Fetches the language from the post, the user or the system.
1433 * @param array $item
1435 * @return string language string
1437 private static function getLanguage(array $item)
1439 // Try to fetch the language from the post itself
1440 if (!empty($item['language'])) {
1441 $languages = array_keys(json_decode($item['language'], true));
1442 if (!empty($languages[0])) {
1443 return $languages[0];
1447 // Otherwise use the user's language
1448 if (!empty($item['uid'])) {
1449 $user = DBA::selectFirst('user', ['language'], ['uid' => $item['uid']]);
1450 if (!empty($user['language'])) {
1451 return $user['language'];
1455 // And finally just use the system language
1456 return DI::config()->get('system', 'language');
1460 * Creates an an "add tag" entry
1462 * @param array $item
1463 * @param array $data activity data
1465 * @return array with activity data for adding tags
1466 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1467 * @throws \ImagickException
1469 private static function createAddTag($item, $data)
1471 $object = XML::parseString($item['object']);
1472 $target = XML::parseString($item["target"]);
1474 $data['diaspora:guid'] = $item['guid'];
1475 $data['actor'] = $item['author-link'];
1476 $data['target'] = (string)$target->id;
1477 $data['summary'] = BBCode::toPlaintext($item['body']);
1478 $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1484 * Creates an announce object entry
1486 * @param array $item
1487 * @param array $data activity data
1489 * @return array with activity data
1490 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1491 * @throws \ImagickException
1493 private static function createAnnounce($item, $data)
1495 $orig_body = $item['body'];
1496 $announce = self::getAnnounceArray($item);
1497 if (empty($announce)) {
1498 $data['type'] = 'Create';
1499 $data['object'] = self::createNote($item);
1503 if (empty($announce['comment'])) {
1504 // Pure announce, without a quote
1505 $data['type'] = 'Announce';
1506 $data['object'] = $announce['object']['uri'];
1511 $data['type'] = 'Create';
1512 $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
1513 $data['object'] = self::createNote($item);
1515 /// @todo Finally descide how to implement this in AP. This is a possible way:
1516 $data['object']['attachment'][] = self::createNote($announce['object']);
1518 $data['object']['source']['content'] = $orig_body;
1523 * Return announce related data if the item is an annunce
1525 * @param array $item
1529 public static function getAnnounceArray($item)
1531 $reshared = Item::getShareArray($item);
1532 if (empty($reshared['guid'])) {
1536 $reshared_item = Item::selectFirst([], ['guid' => $reshared['guid']]);
1537 if (!DBA::isResult($reshared_item)) {
1541 if (!in_array($reshared_item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
1545 $profile = APContact::getByURL($reshared_item['author-link'], false);
1546 if (empty($profile)) {
1550 return ['object' => $reshared_item, 'actor' => $profile, 'comment' => $reshared['comment']];
1554 * Checks if the provided item array is an announce
1556 * @param array $item
1560 public static function isAnnounce($item)
1562 if (!empty($item['verb']) && ($item['verb'] == Activity::ANNOUNCE)) {
1566 $announce = self::getAnnounceArray($item);
1567 if (empty($announce)) {
1571 return empty($announce['comment']);
1575 * Creates an activity id for a given contact id
1577 * @param integer $cid Contact ID of target
1579 * @return bool|string activity id
1581 public static function activityIDFromContact($cid)
1583 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1584 if (!DBA::isResult($contact)) {
1588 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1589 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1590 return DI::baseUrl() . '/activity/' . $uuid;
1594 * Transmits a contact suggestion to a given inbox
1596 * @param integer $uid User ID
1597 * @param string $inbox Target inbox
1598 * @param integer $suggestion_id Suggestion ID
1600 * @return boolean was the transmission successful?
1601 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1603 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1605 $owner = User::getOwnerDataById($uid);
1607 $suggestion = DI::fsuggest()->getById($suggestion_id);
1609 $data = ['@context' => ActivityPub::CONTEXT,
1610 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1611 'type' => 'Announce',
1612 'actor' => $owner['url'],
1613 'object' => $suggestion->url,
1614 'content' => $suggestion->note,
1615 'instrument' => self::getService(),
1616 'to' => [ActivityPub::PUBLIC_COLLECTION],
1619 $signed = LDSignature::sign($data, $owner);
1621 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1622 return HTTPSignature::transmit($signed, $inbox, $uid);
1626 * Transmits a profile relocation to a given inbox
1628 * @param integer $uid User ID
1629 * @param string $inbox Target inbox
1631 * @return boolean was the transmission successful?
1632 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1634 public static function sendProfileRelocation($uid, $inbox)
1636 $owner = User::getOwnerDataById($uid);
1638 $data = ['@context' => ActivityPub::CONTEXT,
1639 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1640 'type' => 'dfrn:relocate',
1641 'actor' => $owner['url'],
1642 'object' => $owner['url'],
1643 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1644 'instrument' => self::getService(),
1645 'to' => [ActivityPub::PUBLIC_COLLECTION],
1648 $signed = LDSignature::sign($data, $owner);
1650 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1651 return HTTPSignature::transmit($signed, $inbox, $uid);
1655 * Transmits a profile deletion to a given inbox
1657 * @param integer $uid User ID
1658 * @param string $inbox Target inbox
1660 * @return boolean was the transmission successful?
1661 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1663 public static function sendProfileDeletion($uid, $inbox)
1665 $owner = User::getOwnerDataById($uid);
1667 if (empty($owner)) {
1668 Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
1672 if (empty($owner['uprvkey'])) {
1673 Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
1677 $data = ['@context' => ActivityPub::CONTEXT,
1678 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1680 'actor' => $owner['url'],
1681 'object' => $owner['url'],
1682 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1683 'instrument' => self::getService(),
1684 'to' => [ActivityPub::PUBLIC_COLLECTION],
1687 $signed = LDSignature::sign($data, $owner);
1689 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1690 return HTTPSignature::transmit($signed, $inbox, $uid);
1694 * Transmits a profile change to a given inbox
1696 * @param integer $uid User ID
1697 * @param string $inbox Target inbox
1699 * @return boolean was the transmission successful?
1700 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1701 * @throws \ImagickException
1703 public static function sendProfileUpdate($uid, $inbox)
1705 $owner = User::getOwnerDataById($uid);
1706 $profile = APContact::getByURL($owner['url']);
1708 $data = ['@context' => ActivityPub::CONTEXT,
1709 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1711 'actor' => $owner['url'],
1712 'object' => self::getProfile($uid),
1713 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1714 'instrument' => self::getService(),
1715 'to' => [$profile['followers']],
1718 $signed = LDSignature::sign($data, $owner);
1720 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1721 return HTTPSignature::transmit($signed, $inbox, $uid);
1725 * Transmits a given activity to a target
1727 * @param string $activity Type name
1728 * @param string $target Target profile
1729 * @param integer $uid User ID
1731 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1732 * @throws \ImagickException
1733 * @throws \Exception
1735 public static function sendActivity($activity, $target, $uid, $id = '')
1737 $profile = APContact::getByURL($target);
1738 if (empty($profile['inbox'])) {
1739 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1743 $owner = User::getOwnerDataById($uid);
1746 $id = DI::baseUrl() . '/activity/' . System::createGUID();
1749 $data = ['@context' => ActivityPub::CONTEXT,
1751 'type' => $activity,
1752 'actor' => $owner['url'],
1753 'object' => $profile['url'],
1754 'instrument' => self::getService(),
1755 'to' => [$profile['url']]];
1757 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1759 $signed = LDSignature::sign($data, $owner);
1760 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1764 * Transmits a "follow object" activity to a target
1765 * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
1767 * @param string $object Object URL
1768 * @param string $target Target profile
1769 * @param integer $uid User ID
1771 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1772 * @throws \ImagickException
1773 * @throws \Exception
1775 public static function sendFollowObject($object, $target, $uid = 0)
1777 $profile = APContact::getByURL($target);
1778 if (empty($profile['inbox'])) {
1779 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1784 // Fetch the list of administrators
1785 $admin_mail = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
1787 // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
1788 $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
1789 $first_user = DBA::selectFirst('user', ['uid'], $condition);
1790 $uid = $first_user['uid'];
1793 $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
1794 'author-id' => Contact::getPublicIdByUserId($uid)];
1795 if (Item::exists($condition)) {
1796 Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
1800 $owner = User::getOwnerDataById($uid);
1802 $data = ['@context' => ActivityPub::CONTEXT,
1803 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1805 'actor' => $owner['url'],
1806 'object' => $object,
1807 'instrument' => self::getService(),
1808 'to' => [$profile['url']]];
1810 Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1812 $signed = LDSignature::sign($data, $owner);
1813 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1817 * Transmit a message that the contact request had been accepted
1819 * @param string $target Target profile
1821 * @param integer $uid User ID
1822 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1823 * @throws \ImagickException
1825 public static function sendContactAccept($target, $id, $uid)
1827 $profile = APContact::getByURL($target);
1828 if (empty($profile['inbox'])) {
1829 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1833 $owner = User::getOwnerDataById($uid);
1834 $data = ['@context' => ActivityPub::CONTEXT,
1835 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1837 'actor' => $owner['url'],
1839 'id' => (string)$id,
1841 'actor' => $profile['url'],
1842 'object' => $owner['url']
1844 'instrument' => self::getService(),
1845 'to' => [$profile['url']]];
1847 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1849 $signed = LDSignature::sign($data, $owner);
1850 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1854 * Reject a contact request or terminates the contact relation
1856 * @param string $target Target profile
1858 * @param integer $uid User ID
1859 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1860 * @throws \ImagickException
1862 public static function sendContactReject($target, $id, $uid)
1864 $profile = APContact::getByURL($target);
1865 if (empty($profile['inbox'])) {
1866 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1870 $owner = User::getOwnerDataById($uid);
1871 $data = ['@context' => ActivityPub::CONTEXT,
1872 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1874 'actor' => $owner['url'],
1876 'id' => (string)$id,
1878 'actor' => $profile['url'],
1879 'object' => $owner['url']
1881 'instrument' => self::getService(),
1882 'to' => [$profile['url']]];
1884 Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1886 $signed = LDSignature::sign($data, $owner);
1887 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1891 * Transmits a message that we don't want to follow this contact anymore
1893 * @param string $target Target profile
1894 * @param integer $uid User ID
1895 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1896 * @throws \ImagickException
1897 * @throws \Exception
1899 public static function sendContactUndo($target, $cid, $uid)
1901 $profile = APContact::getByURL($target);
1902 if (empty($profile['inbox'])) {
1903 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1907 $object_id = self::activityIDFromContact($cid);
1908 if (empty($object_id)) {
1912 $id = DI::baseUrl() . '/activity/' . System::createGUID();
1914 $owner = User::getOwnerDataById($uid);
1915 $data = ['@context' => ActivityPub::CONTEXT,
1918 'actor' => $owner['url'],
1919 'object' => ['id' => $object_id, 'type' => 'Follow',
1920 'actor' => $owner['url'],
1921 'object' => $profile['url']],
1922 'instrument' => self::getService(),
1923 'to' => [$profile['url']]];
1925 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1927 $signed = LDSignature::sign($data, $owner);
1928 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1931 private static function prependMentions($body, int $uriid)
1935 foreach (Tag::getByURIId($uriid, [Tag::IMPLICIT_MENTION]) as $tag) {
1936 $profile = Contact::getByURL($tag['url'], false, ['addr', 'contact-type', 'nick']);
1937 if (!empty($profile['addr'])
1938 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
1939 && !strstr($body, $profile['addr'])
1940 && !strstr($body, $tag['url'])
1942 $mentions[] = '@[url=' . $tag['url'] . ']' . $profile['nick'] . '[/url]';
1946 $mentions[] = $body;
1948 return implode(' ', $mentions);