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\Profile;
38 use Friendica\Model\Photo;
39 use Friendica\Model\Term;
40 use Friendica\Model\User;
41 use Friendica\Protocol\Activity;
42 use Friendica\Protocol\ActivityPub;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\HTTPSignature;
45 use Friendica\Util\Images;
46 use Friendica\Util\JsonLD;
47 use Friendica\Util\LDSignature;
48 use Friendica\Util\Map;
49 use Friendica\Util\Network;
50 use Friendica\Util\XML;
52 require_once 'include/api.php';
53 require_once 'mod/share.php';
56 * ActivityPub Transmitter Protocol class
64 * collects the lost of followers of the given owner
66 * @param array $owner Owner array
67 * @param integer $page Page number
69 * @return array of owners
70 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
72 public static function getFollowers($owner, $page = null)
74 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::FEDERATED, 'uid' => $owner['uid'],
75 'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
76 $count = DBA::count('contact', $condition);
78 $data = ['@context' => ActivityPub::CONTEXT];
79 $data['id'] = DI::baseUrl() . '/followers/' . $owner['nickname'];
80 $data['type'] = 'OrderedCollection';
81 $data['totalItems'] = $count;
83 // When we hide our friends we will only show the pure number but don't allow more.
84 $profile = Profile::getByUID($owner['uid']);
85 if (!empty($profile['hide-friends'])) {
90 $data['first'] = DI::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
92 $data['type'] = 'OrderedCollectionPage';
95 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
96 while ($contact = DBA::fetch($contacts)) {
97 $list[] = $contact['url'];
101 $data['next'] = DI::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
104 $data['partOf'] = DI::baseUrl() . '/followers/' . $owner['nickname'];
106 $data['orderedItems'] = $list;
113 * Create list of following contacts
115 * @param array $owner Owner array
116 * @param integer $page Page numbe
118 * @return array of following contacts
119 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
121 public static function getFollowing($owner, $page = null)
123 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::FEDERATED, 'uid' => $owner['uid'],
124 'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
125 $count = DBA::count('contact', $condition);
127 $data = ['@context' => ActivityPub::CONTEXT];
128 $data['id'] = DI::baseUrl() . '/following/' . $owner['nickname'];
129 $data['type'] = 'OrderedCollection';
130 $data['totalItems'] = $count;
132 // When we hide our friends we will only show the pure number but don't allow more.
133 $profile = Profile::getByUID($owner['uid']);
134 if (!empty($profile['hide-friends'])) {
139 $data['first'] = DI::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
141 $data['type'] = 'OrderedCollectionPage';
144 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
145 while ($contact = DBA::fetch($contacts)) {
146 $list[] = $contact['url'];
150 $data['next'] = DI::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
153 $data['partOf'] = DI::baseUrl() . '/following/' . $owner['nickname'];
155 $data['orderedItems'] = $list;
162 * Public posts for the given owner
164 * @param array $owner Owner array
165 * @param integer $page Page numbe
167 * @return array of posts
168 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
169 * @throws \ImagickException
171 public static function getOutbox($owner, $page = null)
173 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
175 $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
176 'private' => [Item::PUBLIC, Item::UNLISTED], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
177 'deleted' => false, 'visible' => true, 'moderated' => false];
178 $count = DBA::count('item', $condition);
180 $data = ['@context' => ActivityPub::CONTEXT];
181 $data['id'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
182 $data['type'] = 'OrderedCollection';
183 $data['totalItems'] = $count;
186 $data['first'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
188 $data['type'] = 'OrderedCollectionPage';
191 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
193 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
194 while ($item = Item::fetch($items)) {
195 $activity = self::createActivityFromItem($item['id'], true);
196 $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
198 // Only list "Create" activity objects here, no reshares
199 if (is_array($activity['object']) && ($activity['type'] == 'Create')) {
200 $list[] = $activity['object'];
205 $data['next'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
208 $data['partOf'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
210 $data['orderedItems'] = $list;
217 * Return the service array containing information the used software and it's url
219 * @return array with service data
221 private static function getService()
223 return ['type' => 'Service',
224 'name' => FRIENDICA_PLATFORM . " '" . FRIENDICA_CODENAME . "' " . FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
225 'url' => DI::baseUrl()->get()];
229 * Return the ActivityPub profile of the given user
231 * @param integer $uid User ID
232 * @return array with profile data
233 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
235 public static function getProfile($uid)
237 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
238 'account_removed' => false, 'verified' => true];
239 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
240 $user = DBA::selectFirst('user', $fields, $condition);
241 if (!DBA::isResult($user)) {
245 $fields = ['locality', 'region', 'country-name'];
246 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
247 if (!DBA::isResult($profile)) {
251 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
252 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
253 if (!DBA::isResult($contact)) {
257 $data = ['@context' => ActivityPub::CONTEXT];
258 $data['id'] = $contact['url'];
259 $data['diaspora:guid'] = $user['guid'];
260 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
261 $data['following'] = DI::baseUrl() . '/following/' . $user['nickname'];
262 $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname'];
263 $data['inbox'] = DI::baseUrl() . '/inbox/' . $user['nickname'];
264 $data['outbox'] = DI::baseUrl() . '/outbox/' . $user['nickname'];
265 $data['preferredUsername'] = $user['nickname'];
266 $data['name'] = $contact['name'];
267 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
268 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
269 $data['summary'] = BBCode::convert($contact['about'], false);
270 $data['url'] = $contact['url'];
271 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
272 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
273 'owner' => $contact['url'],
274 'publicKeyPem' => $user['pubkey']];
275 $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
276 $data['icon'] = ['type' => 'Image',
277 'url' => $contact['photo']];
279 $data['generator'] = self::getService();
281 // tags: https://kitty.town/@inmysocks/100656097926961126.json
286 * @param string $username
288 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
290 public static function getDeletedUser($username)
293 '@context' => ActivityPub::CONTEXT,
294 'id' => DI::baseUrl() . '/profile/' . $username,
295 'type' => 'Tombstone',
296 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
297 'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
298 'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
303 * Returns an array with permissions of a given item array
307 * @return array with permissions
308 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
309 * @throws \ImagickException
311 private static function fetchPermissionBlockFromConversation($item)
313 if (empty($item['thr-parent'])) {
317 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
318 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
319 if (!DBA::isResult($conversation)) {
323 $activity = json_decode($conversation['source'], true);
325 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
326 $profile = APContact::getByURL($actor);
328 $item_profile = APContact::getByURL($item['author-link']);
329 $exclude[] = $item['author-link'];
331 if ($item['gravity'] == GRAVITY_PARENT) {
332 $exclude[] = $item['owner-link'];
335 $permissions['to'][] = $actor;
337 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
338 if (empty($activity[$element])) {
341 if (is_string($activity[$element])) {
342 $activity[$element] = [$activity[$element]];
345 foreach ($activity[$element] as $receiver) {
346 if (empty($receiver)) {
350 if (!empty($profile['followers']) && $receiver == $profile['followers'] && !empty($item_profile['followers'])) {
351 $permissions[$element][] = $item_profile['followers'];
352 } elseif (!in_array($receiver, $exclude)) {
353 $permissions[$element][] = $receiver;
361 * Creates an array of permissions from an item thread
363 * @param array $item Item array
364 * @param boolean $blindcopy addressing via "bcc" or "cc"?
365 * @param integer $last_id Last item id for adding receivers
366 * @param boolean $forum_mode "true" means that we are sending content to a forum
368 * @return array with permission data
369 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
370 * @throws \ImagickException
372 private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false)
375 $last_id = $item['id'];
380 // Check if we should always deliver our stuff via BCC
381 if (!empty($item['uid'])) {
382 $profile = Profile::getByUID($item['uid']);
383 if (!empty($profile)) {
384 $always_bcc = $profile['hide-friends'];
388 if (DI::config()->get('system', 'ap_always_bcc')) {
392 if (self::isAnnounce($item) || DI::config()->get('debug', 'total_ap_delivery')) {
393 // Will be activated in a later step
394 $networks = Protocol::FEDERATED;
396 // For now only send to these contacts:
397 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
400 $data = ['to' => [], 'cc' => [], 'bcc' => []];
402 if ($item['gravity'] == GRAVITY_PARENT) {
403 $actor_profile = APContact::getByURL($item['owner-link']);
405 $actor_profile = APContact::getByURL($item['author-link']);
408 $terms = Term::tagArrayFromItemId($item['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
410 if ($item['private'] != Item::PRIVATE) {
411 // Directly mention the original author upon a quoted reshare.
412 // Else just ensure that the original author receives the reshare.
413 $announce = self::getAnnounceArray($item);
414 if (!empty($announce['comment'])) {
415 $data['to'][] = $announce['actor']['url'];
416 } elseif (!empty($announce)) {
417 $data['cc'][] = $announce['actor']['url'];
420 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
422 // Check if the item is completely public or unlisted
423 if ($item['private'] == Item::PUBLIC) {
424 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
426 $data['cc'][] = ActivityPub::PUBLIC_COLLECTION;
429 foreach ($terms as $term) {
430 $profile = APContact::getByURL($term['url'], false);
431 if (!empty($profile)) {
432 $data['to'][] = $profile['url'];
436 $receiver_list = Item::enumeratePermissions($item, true);
438 foreach ($terms as $term) {
439 $cid = Contact::getIdForURL($term['url'], $item['uid']);
440 if (!empty($cid) && in_array($cid, $receiver_list)) {
441 $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
442 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
446 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
447 $data['to'][] = $profile['url'];
452 foreach ($receiver_list as $receiver) {
453 $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
454 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
458 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
459 if ($contact['hidden'] || $always_bcc) {
460 $data['bcc'][] = $profile['url'];
462 $data['cc'][] = $profile['url'];
468 if (!empty($item['parent'])) {
469 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
470 while ($parent = Item::fetch($parents)) {
471 if ($parent['gravity'] == GRAVITY_PARENT) {
472 $profile = APContact::getByURL($parent['owner-link'], false);
473 if (!empty($profile)) {
474 if ($item['gravity'] != GRAVITY_PARENT) {
475 // Comments to forums are directed to the forum
476 // But comments to forums aren't directed to the followers collection
477 if ($profile['type'] == 'Group') {
478 $data['to'][] = $profile['url'];
480 $data['cc'][] = $profile['url'];
481 if (($item['private'] != Item::PRIVATE) && $item['private'] && !empty($actor_profile['followers'])) {
482 $data['cc'][] = $actor_profile['followers'];
486 // Public thread parent post always are directed to the followers
487 if (($item['private'] != Item::PRIVATE) && !$forum_mode) {
488 $data['cc'][] = $actor_profile['followers'];
494 // Don't include data from future posts
495 if ($parent['id'] >= $last_id) {
499 $profile = APContact::getByURL($parent['author-link'], false);
500 if (!empty($profile)) {
501 if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
502 $data['to'][] = $profile['url'];
504 $data['cc'][] = $profile['url'];
508 DBA::close($parents);
511 $data['to'] = array_unique($data['to']);
512 $data['cc'] = array_unique($data['cc']);
513 $data['bcc'] = array_unique($data['bcc']);
515 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
516 unset($data['to'][$key]);
519 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
520 unset($data['cc'][$key]);
523 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
524 unset($data['bcc'][$key]);
527 foreach ($data['to'] as $to) {
528 if (($key = array_search($to, $data['cc'])) !== false) {
529 unset($data['cc'][$key]);
532 if (($key = array_search($to, $data['bcc'])) !== false) {
533 unset($data['bcc'][$key]);
537 foreach ($data['cc'] as $cc) {
538 if (($key = array_search($cc, $data['bcc'])) !== false) {
539 unset($data['bcc'][$key]);
543 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
546 unset($receivers['bcc']);
553 * Check if an inbox is archived
555 * @param string $url Inbox url
557 * @return boolean "true" if inbox is archived
559 private static function archivedInbox($url)
561 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
565 * Fetches a list of inboxes of followers of a given user
567 * @param integer $uid User ID
568 * @param boolean $personal fetch personal inboxes
570 * @return array of follower inboxes
571 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
572 * @throws \ImagickException
574 public static function fetchTargetInboxesforUser($uid, $personal = false)
578 if (DI::config()->get('debug', 'total_ap_delivery')) {
579 // Will be activated in a later step
580 $networks = Protocol::FEDERATED;
582 // For now only send to these contacts:
583 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
586 $condition = ['uid' => $uid, 'archive' => false, 'pending' => false];
589 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
592 $contacts = DBA::select('contact', ['url', 'network', 'protocol'], $condition);
593 while ($contact = DBA::fetch($contacts)) {
594 if (Contact::isLocal($contact['url'])) {
598 if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
602 if (Network::isUrlBlocked($contact['url'])) {
606 $profile = APContact::getByURL($contact['url'], false);
607 if (!empty($profile)) {
608 if (empty($profile['sharedinbox']) || $personal) {
609 $target = $profile['inbox'];
611 $target = $profile['sharedinbox'];
613 if (!self::archivedInbox($target)) {
614 $inboxes[$target] = $target;
618 DBA::close($contacts);
624 * Fetches an array of inboxes for the given item and user
626 * @param array $item Item array
627 * @param integer $uid User ID
628 * @param boolean $personal fetch personal inboxes
629 * @param integer $last_id Last item id for adding receivers
630 * @param boolean $forum_mode "true" means that we are sending content to a forum
631 * @return array with inboxes
632 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
633 * @throws \ImagickException
635 public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
637 $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
638 if (empty($permissions)) {
644 if ($item['gravity'] == GRAVITY_ACTIVITY) {
645 $item_profile = APContact::getByURL($item['author-link'], false);
647 $item_profile = APContact::getByURL($item['owner-link'], false);
650 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
651 if (empty($permissions[$element])) {
655 $blindcopy = in_array($element, ['bto', 'bcc']);
657 foreach ($permissions[$element] as $receiver) {
658 if (empty($receiver) || Network::isUrlBlocked($receiver)) {
662 if ($receiver == $item_profile['followers']) {
663 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
665 if (Contact::isLocal($receiver)) {
669 $profile = APContact::getByURL($receiver, false);
670 if (!empty($profile)) {
671 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
672 $target = $profile['inbox'];
674 $target = $profile['sharedinbox'];
676 if (!self::archivedInbox($target)) {
677 $inboxes[$target] = $target;
688 * Creates an array in the structure of the item table for a given mail id
690 * @param integer $mail_id
695 public static function ItemArrayFromMail($mail_id)
697 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
698 if (!DBA::isResult($mail)) {
702 $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
704 // Making the post more compatible for Mastodon by:
705 // - Making it a note and not an article (no title)
706 // - Moving the title into the "summary" field that is used as a "content warning"
707 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
710 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
711 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
712 $mail['allow_gid'] = '';
713 $mail['deny_cid'] = '';
714 $mail['deny_gid'] = '';
715 $mail['private'] = true;
716 $mail['deleted'] = false;
717 $mail['edited'] = $mail['created'];
718 $mail['plink'] = $mail['uri'];
719 $mail['thr-parent'] = $reply['uri'];
720 $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
722 $mail['event-type'] = '';
723 $mail['attach'] = '';
731 * Creates an activity array for a given mail id
733 * @param integer $mail_id
734 * @param boolean $object_mode Is the activity item is used inside another object?
736 * @return array of activity
739 public static function createActivityFromMail($mail_id, $object_mode = false)
741 $mail = self::ItemArrayFromMail($mail_id);
742 $object = self::createNote($mail);
745 $data = ['@context' => ActivityPub::CONTEXT];
750 $data['id'] = $mail['uri'] . '#Create';
751 $data['type'] = 'Create';
752 $data['actor'] = $mail['author-link'];
753 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
754 $data['instrument'] = self::getService();
755 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
757 if (empty($data['to']) && !empty($data['cc'])) {
758 $data['to'] = $data['cc'];
761 if (empty($data['to']) && !empty($data['bcc'])) {
762 $data['to'] = $data['bcc'];
768 $object['to'] = $data['to'];
769 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
771 unset($object['cc']);
772 unset($object['bcc']);
774 $data['directMessage'] = true;
776 $data['object'] = $object;
778 $owner = User::getOwnerDataById($mail['uid']);
780 if (!$object_mode && !empty($owner)) {
781 return LDSignature::sign($data, $owner);
788 * Returns the activity type of a given item
792 * @return string with activity type
793 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
794 * @throws \ImagickException
796 private static function getTypeOfItem($item)
800 // Only check for a reshare, if it is a real reshare and no quoted reshare
801 if (strpos($item['body'], "[share") === 0) {
802 $announce = self::getAnnounceArray($item);
803 $reshared = !empty($announce);
808 } elseif ($item['verb'] == Activity::POST) {
809 if ($item['created'] == $item['edited']) {
814 } elseif ($item['verb'] == Activity::LIKE) {
816 } elseif ($item['verb'] == Activity::DISLIKE) {
818 } elseif ($item['verb'] == Activity::ATTEND) {
820 } elseif ($item['verb'] == Activity::ATTENDNO) {
822 } elseif ($item['verb'] == Activity::ATTENDMAYBE) {
823 $type = 'TentativeAccept';
824 } elseif ($item['verb'] == Activity::FOLLOW) {
826 } elseif ($item['verb'] == Activity::TAG) {
836 * Creates the activity or fetches it from the cache
838 * @param integer $item_id
839 * @param boolean $force Force new cache entry
841 * @return array with the activity
844 public static function createCachedActivityFromItem($item_id, $force = false)
846 $cachekey = 'APDelivery:createActivity:' . $item_id;
849 $data = DI::cache()->get($cachekey);
850 if (!is_null($data)) {
855 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
857 DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
862 * Creates an activity array for a given item id
864 * @param integer $item_id
865 * @param boolean $object_mode Is the activity item is used inside another object?
867 * @return array of activity
870 public static function createActivityFromItem($item_id, $object_mode = false)
872 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
874 if (!DBA::isResult($item)) {
878 if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
879 $owner = User::getOwnerDataById($item['uid']);
880 if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
883 // Disguise forum posts as reshares. Will later be converted to a real announce
884 $item['body'] = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
885 $item['guid'], $item['created'], $item['plink']) . $item['body'] . '[/share]';
890 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
891 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
892 if (DBA::isResult($conversation)) {
893 $data = json_decode($conversation['source'], true);
899 $type = self::getTypeOfItem($item);
903 $data = ['@context' => ActivityPub::CONTEXT];
905 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
907 } elseif ($item['deleted']) {
914 $data['id'] = $item['uri'] . '#' . $type;
915 $data['type'] = $type;
917 if (Item::isForumPost($item) && ($type != 'Announce')) {
918 $data['actor'] = $item['author-link'];
920 $data['actor'] = $item['owner-link'];
923 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
925 $data['instrument'] = self::getService();
927 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
929 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
930 $data['object'] = self::createNote($item);
931 } elseif ($data['type'] == 'Add') {
932 $data = self::createAddTag($item, $data);
933 } elseif ($data['type'] == 'Announce') {
934 $data = self::createAnnounce($item, $data);
935 } elseif ($data['type'] == 'Follow') {
936 $data['object'] = $item['parent-uri'];
937 } elseif ($data['type'] == 'Undo') {
938 $data['object'] = self::createActivityFromItem($item_id, true);
940 $data['diaspora:guid'] = $item['guid'];
941 if (!empty($item['signed_text'])) {
942 $data['diaspora:like'] = $item['signed_text'];
944 $data['object'] = $item['thr-parent'];
947 if (!empty($item['contact-uid'])) {
948 $uid = $item['contact-uid'];
953 $owner = User::getOwnerDataById($uid);
955 if (!$object_mode && !empty($owner)) {
956 return LDSignature::sign($data, $owner);
961 /// @todo Create "conversation" entry
965 * Creates a location entry for a given item array
969 * @return array with location array
971 private static function createLocation($item)
973 $location = ['type' => 'Place'];
975 if (!empty($item['location'])) {
976 $location['name'] = $item['location'];
981 if (empty($item['coord'])) {
982 $coord = Map::getCoordinates($item['location']);
984 $coords = explode(' ', $item['coord']);
985 if (count($coords) == 2) {
986 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
990 if (!empty($coord['lat']) && !empty($coord['lon'])) {
991 $location['latitude'] = $coord['lat'];
992 $location['longitude'] = $coord['lon'];
999 * Returns a tag array for a given item array
1001 * @param array $item
1003 * @return array of tags
1004 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1006 private static function createTagList($item)
1010 $terms = Term::tagArrayFromItemId($item['id'], [Term::HASHTAG, Term::MENTION, Term::IMPLICIT_MENTION]);
1011 foreach ($terms as $term) {
1012 if ($term['type'] == Term::HASHTAG) {
1013 $url = DI::baseUrl() . '/search?tag=' . urlencode($term['term']);
1014 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
1015 } elseif ($term['type'] == Term::MENTION || $term['type'] == Term::IMPLICIT_MENTION) {
1016 $contact = Contact::getDetailsByURL($term['url']);
1017 if (!empty($contact['addr'])) {
1018 $mention = '@' . $contact['addr'];
1020 $mention = '@' . $term['url'];
1023 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
1027 $announce = self::getAnnounceArray($item);
1028 // Mention the original author upon commented reshares
1029 if (!empty($announce['comment'])) {
1030 $tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']];
1037 * Adds attachment data to the JSON document
1039 * @param array $item Data of the item that is to be posted
1040 * @param string $type Object type
1042 * @return array with attachment data
1043 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1045 private static function createAttachmentList($item, $type)
1049 // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
1050 // It will be reactivated, once this cleared.
1052 $attach_data = BBCode::getAttachmentData($item['body']);
1053 if (!empty($attach_data['url'])) {
1054 $attachment = ['type' => 'Page',
1055 'mediaType' => 'text/html',
1056 'url' => $attach_data['url']];
1058 if (!empty($attach_data['title'])) {
1059 $attachment['name'] = $attach_data['title'];
1062 if (!empty($attach_data['description'])) {
1063 $attachment['summary'] = $attach_data['description'];
1066 if (!empty($attach_data['image'])) {
1067 $imgdata = Images::getInfoFromURLCached($attach_data['image']);
1069 $attachment['icon'] = ['type' => 'Image',
1070 'mediaType' => $imgdata['mime'],
1071 'width' => $imgdata[0],
1072 'height' => $imgdata[1],
1073 'url' => $attach_data['image']];
1077 $attachments[] = $attachment;
1080 $arr = explode('[/attach],', $item['attach']);
1082 foreach ($arr as $r) {
1084 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1086 $attributes = ['type' => 'Document',
1087 'mediaType' => $matches[3],
1088 'url' => $matches[1],
1091 if (trim($matches[4]) != '') {
1092 $attributes['name'] = trim($matches[4]);
1095 $attachments[] = $attributes;
1100 if ($type != 'Note') {
1101 return $attachments;
1104 // Simplify image codes
1105 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
1107 // Grab all pictures without alternative descriptions and create attachments out of them
1108 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
1109 foreach ($pictures[1] as $picture) {
1110 $imgdata = Images::getInfoFromURLCached($picture);
1112 $attachments[] = ['type' => 'Document',
1113 'mediaType' => $imgdata['mime'],
1120 // Grab all pictures with alternative description and create attachments out of them
1121 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
1122 foreach ($pictures as $picture) {
1123 $imgdata = Images::getInfoFromURLCached($picture[1]);
1125 $attachments[] = ['type' => 'Document',
1126 'mediaType' => $imgdata['mime'],
1127 'url' => $picture[1],
1128 'name' => $picture[2]];
1133 return $attachments;
1137 * Callback function to replace a Friendica style mention in a mention that is used on AP
1139 * @param array $match Matching values for the callback
1140 * @return string Replaced mention
1141 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1143 private static function mentionCallback($match)
1145 if (empty($match[1])) {
1149 $data = Contact::getDetailsByURL($match[1]);
1150 if (empty($data['nick'])) {
1154 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
1158 * Remove image elements since they are added as attachment
1160 * @param string $body
1162 * @return string with removed images
1164 private static function removePictures($body)
1166 // Simplify image codes
1167 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
1168 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $body);
1170 // Now remove local links
1171 $body = preg_replace_callback(
1172 '/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
1174 // We remove the link when it is a link to a local photo page
1175 if (Photo::isLocalPage($match[1])) {
1178 // otherwise we just return the link
1179 return '[url]' . $match[1] . '[/url]';
1184 // Remove all pictures
1185 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $body);
1191 * Fetches the "context" value for a givem item array from the "conversation" table
1193 * @param array $item
1195 * @return string with context url
1196 * @throws \Exception
1198 private static function fetchContextURLForItem($item)
1200 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
1201 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
1202 $context_uri = $conversation['conversation-href'];
1203 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
1204 $context_uri = $conversation['conversation-uri'];
1206 $context_uri = $item['parent-uri'] . '#context';
1208 return $context_uri;
1212 * Returns if the post contains sensitive content ("nsfw")
1214 * @param integer $item_id
1217 * @throws \Exception
1219 private static function isSensitive($item_id)
1221 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
1222 return DBA::exists('term', $condition);
1226 * Creates event data
1228 * @param array $item
1230 * @return array with the event data
1231 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1233 public static function createEvent($item)
1236 $event['name'] = $item['event-summary'];
1237 $event['content'] = BBCode::convert($item['event-desc'], false, 9);
1238 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
1240 if (!$item['event-nofinish']) {
1241 $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
1244 if (!empty($item['event-location'])) {
1245 $item['location'] = $item['event-location'];
1246 $event['location'] = self::createLocation($item);
1253 * Creates a note/article object array
1255 * @param array $item
1257 * @return array with the object data
1258 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1259 * @throws \ImagickException
1261 public static function createNote($item)
1267 if ($item['event-type'] == 'event') {
1269 } elseif (!empty($item['title'])) {
1275 if ($item['deleted']) {
1276 $type = 'Tombstone';
1280 $data['id'] = $item['uri'];
1281 $data['type'] = $type;
1283 if ($item['deleted']) {
1287 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1289 if ($item['uri'] != $item['thr-parent']) {
1290 $data['inReplyTo'] = $item['thr-parent'];
1292 $data['inReplyTo'] = null;
1295 $data['diaspora:guid'] = $item['guid'];
1296 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1298 if ($item['created'] != $item['edited']) {
1299 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1302 $data['url'] = $item['plink'];
1303 $data['attributedTo'] = $item['author-link'];
1304 $data['sensitive'] = self::isSensitive($item['id']);
1305 $data['context'] = self::fetchContextURLForItem($item);
1307 if (!empty($item['title'])) {
1308 $data['name'] = BBCode::toPlaintext($item['title'], false);
1311 $permission_block = self::createPermissionBlockForItem($item, false);
1313 $body = $item['body'];
1315 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1316 $body = self::prependMentions($body, $permission_block);
1319 if ($type == 'Note') {
1320 $body = self::removePictures($body);
1321 } elseif (($type == 'Article') && empty($data['summary'])) {
1322 $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
1325 if ($type == 'Event') {
1326 $data = array_merge($data, self::createEvent($item));
1328 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1329 $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1331 $data['content'] = BBCode::convert($body, false, 9);
1334 // The regular "content" field does contain a minimized HTML. This is done since systems like
1335 // Mastodon has got problems with - for example - embedded pictures.
1336 // The contentMap does contain the unmodified HTML.
1337 $language = self::getLanguage($item);
1338 if (!empty($language)) {
1339 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1340 $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
1341 $richbody = BBCode::removeAttachment($richbody);
1343 $data['contentMap'][$language] = BBCode::convert($richbody, false);
1346 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1348 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1349 $data['diaspora:comment'] = $item['signed_text'];
1352 $data['attachment'] = self::createAttachmentList($item, $type);
1353 $data['tag'] = self::createTagList($item);
1355 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1356 $data['location'] = self::createLocation($item);
1359 if (!empty($item['app'])) {
1360 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1363 $data = array_merge($data, $permission_block);
1369 * Fetches the language from the post, the user or the system.
1371 * @param array $item
1373 * @return string language string
1375 private static function getLanguage(array $item)
1377 // Try to fetch the language from the post itself
1378 if (!empty($item['language'])) {
1379 $languages = array_keys(json_decode($item['language'], true));
1380 if (!empty($languages[0])) {
1381 return $languages[0];
1385 // Otherwise use the user's language
1386 if (!empty($item['uid'])) {
1387 $user = DBA::selectFirst('user', ['language'], ['uid' => $item['uid']]);
1388 if (!empty($user['language'])) {
1389 return $user['language'];
1393 // And finally just use the system language
1394 return DI::config()->get('system', 'language');
1398 * Creates an an "add tag" entry
1400 * @param array $item
1401 * @param array $data activity data
1403 * @return array with activity data for adding tags
1404 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1405 * @throws \ImagickException
1407 private static function createAddTag($item, $data)
1409 $object = XML::parseString($item['object'], false);
1410 $target = XML::parseString($item["target"], false);
1412 $data['diaspora:guid'] = $item['guid'];
1413 $data['actor'] = $item['author-link'];
1414 $data['target'] = (string)$target->id;
1415 $data['summary'] = BBCode::toPlaintext($item['body']);
1416 $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1422 * Creates an announce object entry
1424 * @param array $item
1425 * @param array $data activity data
1427 * @return array with activity data
1428 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1429 * @throws \ImagickException
1431 private static function createAnnounce($item, $data)
1433 $orig_body = $item['body'];
1434 $announce = self::getAnnounceArray($item);
1435 if (empty($announce)) {
1436 $data['type'] = 'Create';
1437 $data['object'] = self::createNote($item);
1441 if (empty($announce['comment'])) {
1442 // Pure announce, without a quote
1443 $data['type'] = 'Announce';
1444 $data['object'] = $announce['object']['uri'];
1449 $data['type'] = 'Create';
1450 $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
1451 $data['object'] = self::createNote($item);
1453 /// @todo Finally descide how to implement this in AP. This is a possible way:
1454 $data['object']['attachment'][] = self::createNote($announce['object']);
1456 $data['object']['source']['content'] = $orig_body;
1461 * Return announce related data if the item is an annunce
1463 * @param array $item
1467 public static function getAnnounceArray($item)
1469 $reshared = Item::getShareArray($item);
1470 if (empty($reshared['guid'])) {
1474 $reshared_item = Item::selectFirst([], ['guid' => $reshared['guid']]);
1475 if (!DBA::isResult($reshared_item)) {
1479 if (!in_array($reshared_item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
1483 $profile = APContact::getByURL($reshared_item['author-link'], false);
1484 if (empty($profile)) {
1488 return ['object' => $reshared_item, 'actor' => $profile, 'comment' => $reshared['comment']];
1492 * Checks if the provided item array is an announce
1494 * @param array $item
1498 public static function isAnnounce($item)
1500 $announce = self::getAnnounceArray($item);
1501 if (empty($announce)) {
1505 return empty($announce['comment']);
1509 * Creates an activity id for a given contact id
1511 * @param integer $cid Contact ID of target
1513 * @return bool|string activity id
1515 public static function activityIDFromContact($cid)
1517 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1518 if (!DBA::isResult($contact)) {
1522 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1523 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1524 return DI::baseUrl() . '/activity/' . $uuid;
1528 * Transmits a contact suggestion to a given inbox
1530 * @param integer $uid User ID
1531 * @param string $inbox Target inbox
1532 * @param integer $suggestion_id Suggestion ID
1534 * @return boolean was the transmission successful?
1535 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1537 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1539 $owner = User::getOwnerDataById($uid);
1541 $suggestion = DI::fsuggest()->getById($suggestion_id);
1543 $data = ['@context' => ActivityPub::CONTEXT,
1544 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1545 'type' => 'Announce',
1546 'actor' => $owner['url'],
1547 'object' => $suggestion->url,
1548 'content' => $suggestion->note,
1549 'instrument' => self::getService(),
1550 'to' => [ActivityPub::PUBLIC_COLLECTION],
1553 $signed = LDSignature::sign($data, $owner);
1555 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1556 return HTTPSignature::transmit($signed, $inbox, $uid);
1560 * Transmits a profile relocation to a given inbox
1562 * @param integer $uid User ID
1563 * @param string $inbox Target inbox
1565 * @return boolean was the transmission successful?
1566 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1568 public static function sendProfileRelocation($uid, $inbox)
1570 $owner = User::getOwnerDataById($uid);
1572 $data = ['@context' => ActivityPub::CONTEXT,
1573 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1574 'type' => 'dfrn:relocate',
1575 'actor' => $owner['url'],
1576 'object' => $owner['url'],
1577 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1578 'instrument' => self::getService(),
1579 'to' => [ActivityPub::PUBLIC_COLLECTION],
1582 $signed = LDSignature::sign($data, $owner);
1584 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1585 return HTTPSignature::transmit($signed, $inbox, $uid);
1589 * Transmits a profile deletion to a given inbox
1591 * @param integer $uid User ID
1592 * @param string $inbox Target inbox
1594 * @return boolean was the transmission successful?
1595 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1597 public static function sendProfileDeletion($uid, $inbox)
1599 $owner = User::getOwnerDataById($uid);
1601 if (empty($owner)) {
1602 Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
1606 if (empty($owner['uprvkey'])) {
1607 Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
1611 $data = ['@context' => ActivityPub::CONTEXT,
1612 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1614 'actor' => $owner['url'],
1615 'object' => $owner['url'],
1616 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1617 'instrument' => self::getService(),
1618 'to' => [ActivityPub::PUBLIC_COLLECTION],
1621 $signed = LDSignature::sign($data, $owner);
1623 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1624 return HTTPSignature::transmit($signed, $inbox, $uid);
1628 * Transmits a profile change to a given inbox
1630 * @param integer $uid User ID
1631 * @param string $inbox Target inbox
1633 * @return boolean was the transmission successful?
1634 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1635 * @throws \ImagickException
1637 public static function sendProfileUpdate($uid, $inbox)
1639 $owner = User::getOwnerDataById($uid);
1640 $profile = APContact::getByURL($owner['url']);
1642 $data = ['@context' => ActivityPub::CONTEXT,
1643 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1645 'actor' => $owner['url'],
1646 'object' => self::getProfile($uid),
1647 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1648 'instrument' => self::getService(),
1649 'to' => [$profile['followers']],
1652 $signed = LDSignature::sign($data, $owner);
1654 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1655 return HTTPSignature::transmit($signed, $inbox, $uid);
1659 * Transmits a given activity to a target
1661 * @param string $activity Type name
1662 * @param string $target Target profile
1663 * @param integer $uid User ID
1665 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1666 * @throws \ImagickException
1667 * @throws \Exception
1669 public static function sendActivity($activity, $target, $uid, $id = '')
1671 $profile = APContact::getByURL($target);
1672 if (empty($profile['inbox'])) {
1673 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1677 $owner = User::getOwnerDataById($uid);
1680 $id = DI::baseUrl() . '/activity/' . System::createGUID();
1683 $data = ['@context' => ActivityPub::CONTEXT,
1685 'type' => $activity,
1686 'actor' => $owner['url'],
1687 'object' => $profile['url'],
1688 'instrument' => self::getService(),
1689 'to' => [$profile['url']]];
1691 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1693 $signed = LDSignature::sign($data, $owner);
1694 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1698 * Transmits a "follow object" activity to a target
1699 * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
1701 * @param string $object Object URL
1702 * @param string $target Target profile
1703 * @param integer $uid User ID
1705 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1706 * @throws \ImagickException
1707 * @throws \Exception
1709 public static function sendFollowObject($object, $target, $uid = 0)
1711 $profile = APContact::getByURL($target);
1712 if (empty($profile['inbox'])) {
1713 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1718 // Fetch the list of administrators
1719 $admin_mail = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
1721 // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
1722 $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
1723 $first_user = DBA::selectFirst('user', ['uid'], $condition);
1724 $uid = $first_user['uid'];
1727 $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
1728 'author-id' => Contact::getPublicIdByUserId($uid)];
1729 if (Item::exists($condition)) {
1730 Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
1734 $owner = User::getOwnerDataById($uid);
1736 $data = ['@context' => ActivityPub::CONTEXT,
1737 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1739 'actor' => $owner['url'],
1740 'object' => $object,
1741 'instrument' => self::getService(),
1742 'to' => [$profile['url']]];
1744 Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1746 $signed = LDSignature::sign($data, $owner);
1747 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1751 * Transmit a message that the contact request had been accepted
1753 * @param string $target Target profile
1755 * @param integer $uid User ID
1756 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1757 * @throws \ImagickException
1759 public static function sendContactAccept($target, $id, $uid)
1761 $profile = APContact::getByURL($target);
1762 if (empty($profile['inbox'])) {
1763 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1767 $owner = User::getOwnerDataById($uid);
1768 $data = ['@context' => ActivityPub::CONTEXT,
1769 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1771 'actor' => $owner['url'],
1773 'id' => (string)$id,
1775 'actor' => $profile['url'],
1776 'object' => $owner['url']
1778 'instrument' => self::getService(),
1779 'to' => [$profile['url']]];
1781 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1783 $signed = LDSignature::sign($data, $owner);
1784 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1788 * Reject a contact request or terminates the contact relation
1790 * @param string $target Target profile
1792 * @param integer $uid User ID
1793 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1794 * @throws \ImagickException
1796 public static function sendContactReject($target, $id, $uid)
1798 $profile = APContact::getByURL($target);
1799 if (empty($profile['inbox'])) {
1800 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1804 $owner = User::getOwnerDataById($uid);
1805 $data = ['@context' => ActivityPub::CONTEXT,
1806 'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1808 'actor' => $owner['url'],
1810 'id' => (string)$id,
1812 'actor' => $profile['url'],
1813 'object' => $owner['url']
1815 'instrument' => self::getService(),
1816 'to' => [$profile['url']]];
1818 Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1820 $signed = LDSignature::sign($data, $owner);
1821 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1825 * Transmits a message that we don't want to follow this contact anymore
1827 * @param string $target Target profile
1828 * @param integer $uid User ID
1829 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1830 * @throws \ImagickException
1831 * @throws \Exception
1833 public static function sendContactUndo($target, $cid, $uid)
1835 $profile = APContact::getByURL($target);
1836 if (empty($profile['inbox'])) {
1837 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1841 $object_id = self::activityIDFromContact($cid);
1842 if (empty($object_id)) {
1846 $id = DI::baseUrl() . '/activity/' . System::createGUID();
1848 $owner = User::getOwnerDataById($uid);
1849 $data = ['@context' => ActivityPub::CONTEXT,
1852 'actor' => $owner['url'],
1853 'object' => ['id' => $object_id, 'type' => 'Follow',
1854 'actor' => $owner['url'],
1855 'object' => $profile['url']],
1856 'instrument' => self::getService(),
1857 'to' => [$profile['url']]];
1859 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1861 $signed = LDSignature::sign($data, $owner);
1862 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1865 private static function prependMentions($body, array $permission_block)
1867 if (DI::config()->get('system', 'disable_implicit_mentions')) {
1873 foreach ($permission_block['to'] as $profile_url) {
1874 $profile = Contact::getDetailsByURL($profile_url);
1875 if (!empty($profile['addr'])
1876 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
1877 && !strstr($body, $profile['addr'])
1878 && !strstr($body, $profile_url)
1880 $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]';
1884 $mentions[] = $body;
1886 return implode(' ', $mentions);