3 * @file src/Protocol/ActivityPub/Transmitter.php
5 namespace Friendica\Protocol\ActivityPub;
7 use Friendica\BaseObject;
8 use Friendica\Content\Feature;
9 use Friendica\Database\DBA;
10 use Friendica\Core\Config;
11 use Friendica\Core\Logger;
12 use Friendica\Core\System;
13 use Friendica\Util\HTTPSignature;
14 use Friendica\Core\Protocol;
15 use Friendica\Model\Conversation;
16 use Friendica\Model\Contact;
17 use Friendica\Model\APContact;
18 use Friendica\Model\Item;
19 use Friendica\Model\Term;
20 use Friendica\Model\User;
21 use Friendica\Util\DateTimeFormat;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\Plaintext;
24 use Friendica\Util\XML;
25 use Friendica\Util\JsonLD;
26 use Friendica\Util\LDSignature;
27 use Friendica\Model\Profile;
28 use Friendica\Object\Image;
29 use Friendica\Protocol\ActivityPub;
30 use Friendica\Core\Cache;
31 use Friendica\Util\Map;
32 use Friendica\Util\Network;
34 require_once 'include/api.php';
35 require_once 'mod/share.php';
38 * @brief ActivityPub Transmitter Protocol class
46 * collects the lost of followers of the given owner
48 * @param array $owner Owner array
49 * @param integer $page Page number
51 * @return array of owners
52 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
54 public static function getFollowers($owner, $page = null)
56 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
57 'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
58 $count = DBA::count('contact', $condition);
60 $data = ['@context' => ActivityPub::CONTEXT];
61 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
62 $data['type'] = 'OrderedCollection';
63 $data['totalItems'] = $count;
65 // When we hide our friends we will only show the pure number but don't allow more.
66 $profile = Profile::getByUID($owner['uid']);
67 if (!empty($profile['hide-friends'])) {
72 $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
76 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
77 while ($contact = DBA::fetch($contacts)) {
78 $list[] = $contact['url'];
82 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
85 $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
87 $data['orderedItems'] = $list;
94 * Create list of following contacts
96 * @param array $owner Owner array
97 * @param integer $page Page numbe
99 * @return array of following contacts
100 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
102 public static function getFollowing($owner, $page = null)
104 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
105 'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
106 $count = DBA::count('contact', $condition);
108 $data = ['@context' => ActivityPub::CONTEXT];
109 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
110 $data['type'] = 'OrderedCollection';
111 $data['totalItems'] = $count;
113 // When we hide our friends we will only show the pure number but don't allow more.
114 $profile = Profile::getByUID($owner['uid']);
115 if (!empty($profile['hide-friends'])) {
120 $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
124 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
125 while ($contact = DBA::fetch($contacts)) {
126 $list[] = $contact['url'];
130 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
133 $data['partOf'] = System::baseUrl() . '/following/' . $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'], 0, true);
155 $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
156 'private' => false, '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'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
162 $data['type'] = 'OrderedCollection';
163 $data['totalItems'] = $count;
166 $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
170 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
172 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
173 while ($item = Item::fetch($items)) {
174 $object = self::createObjectFromItemID($item['id']);
175 unset($object['@context']);
180 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
183 $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
185 $data['orderedItems'] = $list;
192 * Return the service array containing information the used software and it's url
194 * @return array with service data
196 private static function getService()
198 return ['type' => 'Service',
199 'name' => FRIENDICA_PLATFORM . " '" . FRIENDICA_CODENAME . "' " . FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
200 'url' => BaseObject::getApp()->getBaseURL()];
204 * Return the ActivityPub profile of the given user
206 * @param integer $uid User ID
207 * @return array with profile data
208 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
210 public static function getProfile($uid)
212 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
213 'account_removed' => false, 'verified' => true];
214 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
215 $user = DBA::selectFirst('user', $fields, $condition);
216 if (!DBA::isResult($user)) {
220 $fields = ['locality', 'region', 'country-name'];
221 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
222 if (!DBA::isResult($profile)) {
226 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
227 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
228 if (!DBA::isResult($contact)) {
232 $data = ['@context' => ActivityPub::CONTEXT];
233 $data['id'] = $contact['url'];
234 $data['diaspora:guid'] = $user['guid'];
235 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
236 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
237 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
238 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
239 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
240 $data['preferredUsername'] = $user['nickname'];
241 $data['name'] = $contact['name'];
242 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
243 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
244 $data['summary'] = $contact['about'];
245 $data['url'] = $contact['url'];
246 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
247 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
248 'owner' => $contact['url'],
249 'publicKeyPem' => $user['pubkey']];
250 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
251 $data['icon'] = ['type' => 'Image',
252 'url' => $contact['photo']];
254 $data['generator'] = self::getService();
256 // tags: https://kitty.town/@inmysocks/100656097926961126.json
261 * @param string $username
263 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
265 public static function getDeletedUser($username)
268 '@context' => ActivityPub::CONTEXT,
269 'id' => System::baseUrl() . '/profile/' . $username,
270 'type' => 'Tombstone',
271 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
272 'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
273 'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
278 * Returns an array with permissions of a given item array
282 * @return array with permissions
283 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
284 * @throws \ImagickException
286 private static function fetchPermissionBlockFromConversation($item)
288 if (empty($item['thr-parent'])) {
292 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
293 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
294 if (!DBA::isResult($conversation)) {
298 $activity = json_decode($conversation['source'], true);
300 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
301 $profile = APContact::getByURL($actor);
303 $item_profile = APContact::getByURL($item['author-link']);
304 $exclude[] = $item['author-link'];
306 if ($item['gravity'] == GRAVITY_PARENT) {
307 $exclude[] = $item['owner-link'];
310 $permissions['to'][] = $actor;
312 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
313 if (empty($activity[$element])) {
316 if (is_string($activity[$element])) {
317 $activity[$element] = [$activity[$element]];
320 foreach ($activity[$element] as $receiver) {
321 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
322 $permissions[$element][] = $item_profile['followers'];
323 } elseif (!in_array($receiver, $exclude)) {
324 $permissions[$element][] = $receiver;
332 * Creates an array of permissions from an item thread
334 * @param array $item Item array
335 * @param boolean $blindcopy addressing via "bcc" or "cc"?
336 * @param integer $last_id Last item id for adding receivers
337 * @param boolean $forum_mode "true" means that we are sending content to a forum
339 * @return array with permission data
340 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
341 * @throws \ImagickException
343 private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false)
346 $last_id = $item['id'];
351 // Check if we should always deliver our stuff via BCC
352 if (!empty($item['uid'])) {
353 $profile = Profile::getByUID($item['uid']);
354 if (!empty($profile)) {
355 $always_bcc = $profile['hide-friends'];
359 if (Config::get('debug', 'total_ap_delivery')) {
360 // Will be activated in a later step
361 $networks = Protocol::FEDERATED;
363 // For now only send to these contacts:
364 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
367 $data = ['to' => [], 'cc' => [], 'bcc' => []];
369 if ($item['gravity'] == GRAVITY_PARENT) {
370 $actor_profile = APContact::getByURL($item['owner-link']);
372 $actor_profile = APContact::getByURL($item['author-link']);
375 $terms = Term::tagArrayFromItemId($item['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
377 if (!$item['private']) {
378 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
380 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
382 foreach ($terms as $term) {
383 $profile = APContact::getByURL($term['url'], false);
384 if (!empty($profile)) {
385 $data['to'][] = $profile['url'];
389 $receiver_list = Item::enumeratePermissions($item, true);
391 foreach ($terms as $term) {
392 $cid = Contact::getIdForURL($term['url'], $item['uid']);
393 if (!empty($cid) && in_array($cid, $receiver_list)) {
394 $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
395 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
399 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
400 $data['to'][] = $profile['url'];
405 foreach ($receiver_list as $receiver) {
406 $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
407 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
411 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
412 if ($contact['hidden'] || $always_bcc) {
413 $data['bcc'][] = $profile['url'];
415 $data['cc'][] = $profile['url'];
421 if (!empty($item['parent'])) {
422 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
423 while ($parent = Item::fetch($parents)) {
424 if ($parent['gravity'] == GRAVITY_PARENT) {
425 $profile = APContact::getByURL($parent['owner-link'], false);
426 if (!empty($profile)) {
427 if ($item['gravity'] != GRAVITY_PARENT) {
428 // Comments to forums are directed to the forum
429 // But comments to forums aren't directed to the followers collection
430 if ($profile['type'] == 'Group') {
431 $data['to'][] = $profile['url'];
433 $data['cc'][] = $profile['url'];
434 if (!$item['private'] && !empty($actor_profile['followers'])) {
435 $data['cc'][] = $actor_profile['followers'];
439 // Public thread parent post always are directed to the followers
440 if (!$item['private'] && !$forum_mode) {
441 $data['cc'][] = $actor_profile['followers'];
447 // Don't include data from future posts
448 if ($parent['id'] >= $last_id) {
452 $profile = APContact::getByURL($parent['author-link'], false);
453 if (!empty($profile)) {
454 if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
455 $data['to'][] = $profile['url'];
457 $data['cc'][] = $profile['url'];
461 DBA::close($parents);
464 $data['to'] = array_unique($data['to']);
465 $data['cc'] = array_unique($data['cc']);
466 $data['bcc'] = array_unique($data['bcc']);
468 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
469 unset($data['to'][$key]);
472 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
473 unset($data['cc'][$key]);
476 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
477 unset($data['bcc'][$key]);
480 foreach ($data['to'] as $to) {
481 if (($key = array_search($to, $data['cc'])) !== false) {
482 unset($data['cc'][$key]);
485 if (($key = array_search($to, $data['bcc'])) !== false) {
486 unset($data['bcc'][$key]);
490 foreach ($data['cc'] as $cc) {
491 if (($key = array_search($cc, $data['bcc'])) !== false) {
492 unset($data['bcc'][$key]);
496 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
499 unset($receivers['bcc']);
506 * Check if an inbox is archived
508 * @param string $url Inbox url
510 * @return boolean "true" if inbox is archived
512 private static function archivedInbox($url)
514 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
518 * Fetches a list of inboxes of followers of a given user
520 * @param integer $uid User ID
521 * @param boolean $personal fetch personal inboxes
523 * @return array of follower inboxes
524 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
525 * @throws \ImagickException
527 public static function fetchTargetInboxesforUser($uid, $personal = false)
531 if (Config::get('debug', 'total_ap_delivery')) {
532 // Will be activated in a later step
533 $networks = Protocol::FEDERATED;
535 // For now only send to these contacts:
536 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
539 $condition = ['uid' => $uid, 'archive' => false, 'pending' => false];
542 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
545 $contacts = DBA::select('contact', ['url', 'network', 'protocol'], $condition);
546 while ($contact = DBA::fetch($contacts)) {
547 if (Contact::isLocal($contact['url'])) {
551 if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
555 if (Network::isUrlBlocked($contact['url'])) {
559 $profile = APContact::getByURL($contact['url'], false);
560 if (!empty($profile)) {
561 if (empty($profile['sharedinbox']) || $personal) {
562 $target = $profile['inbox'];
564 $target = $profile['sharedinbox'];
566 if (!self::archivedInbox($target)) {
567 $inboxes[$target] = $target;
571 DBA::close($contacts);
577 * Fetches an array of inboxes for the given item and user
579 * @param array $item Item array
580 * @param integer $uid User ID
581 * @param boolean $personal fetch personal inboxes
582 * @param integer $last_id Last item id for adding receivers
583 * @param boolean $forum_mode "true" means that we are sending content to a forum
584 * @return array with inboxes
585 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
586 * @throws \ImagickException
588 public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
590 $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
591 if (empty($permissions)) {
597 if ($item['gravity'] == GRAVITY_ACTIVITY) {
598 $item_profile = APContact::getByURL($item['author-link'], false);
600 $item_profile = APContact::getByURL($item['owner-link'], false);
603 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
604 if (empty($permissions[$element])) {
608 $blindcopy = in_array($element, ['bto', 'bcc']);
610 foreach ($permissions[$element] as $receiver) {
611 if (Network::isUrlBlocked($receiver)) {
615 if ($receiver == $item_profile['followers']) {
616 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
618 if (Contact::isLocal($receiver)) {
622 $profile = APContact::getByURL($receiver, false);
623 if (!empty($profile)) {
624 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
625 $target = $profile['inbox'];
627 $target = $profile['sharedinbox'];
629 if (!self::archivedInbox($target)) {
630 $inboxes[$target] = $target;
641 * Creates an array in the structure of the item table for a given mail id
643 * @param integer $mail_id
648 public static function ItemArrayFromMail($mail_id)
650 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
652 $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
654 // Making the post more compatible for Mastodon by:
655 // - Making it a note and not an article (no title)
656 // - Moving the title into the "summary" field that is used as a "content warning"
657 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
660 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
661 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
662 $mail['allow_gid'] = '';
663 $mail['deny_cid'] = '';
664 $mail['deny_gid'] = '';
665 $mail['private'] = true;
666 $mail['deleted'] = false;
667 $mail['edited'] = $mail['created'];
668 $mail['plink'] = $mail['uri'];
669 $mail['thr-parent'] = $reply['uri'];
670 $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
672 $mail['event-type'] = '';
673 $mail['attach'] = '';
681 * Creates an activity array for a given mail id
683 * @param integer $mail_id
684 * @param boolean $object_mode Is the activity item is used inside another object?
686 * @return array of activity
689 public static function createActivityFromMail($mail_id, $object_mode = false)
691 $mail = self::ItemArrayFromMail($mail_id);
692 $object = self::createNote($mail);
694 $object['to'] = $object['cc'];
695 unset($object['cc']);
697 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => 'test']];
700 $data = ['@context' => ActivityPub::CONTEXT];
705 $data['id'] = $mail['uri'] . '#Create';
706 $data['type'] = 'Create';
707 $data['actor'] = $mail['author-link'];
708 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
709 $data['instrument'] = self::getService();
710 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
712 if (empty($data['to']) && !empty($data['cc'])) {
713 $data['to'] = $data['cc'];
716 if (empty($data['to']) && !empty($data['bcc'])) {
717 $data['to'] = $data['bcc'];
723 $object['to'] = $data['to'];
724 unset($object['cc']);
725 unset($object['bcc']);
727 $data['directMessage'] = true;
729 $data['object'] = $object;
731 $owner = User::getOwnerDataById($mail['uid']);
733 if (!$object_mode && !empty($owner)) {
734 return LDSignature::sign($data, $owner);
741 * Returns the activity type of a given item
745 * @return string with activity type
746 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
747 * @throws \ImagickException
749 private static function getTypeOfItem($item)
753 // Only check for a reshare, if it is a real reshare and no quoted reshare
754 if (strpos($item['body'], "[share") === 0) {
755 $announce = api_share_as_retweet($item);
756 $reshared = !empty($announce['plink']);
761 } elseif ($item['verb'] == ACTIVITY_POST) {
762 if ($item['created'] == $item['edited']) {
767 } elseif ($item['verb'] == ACTIVITY_LIKE) {
769 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
771 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
773 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
775 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
776 $type = 'TentativeAccept';
777 } elseif ($item['verb'] == ACTIVITY_FOLLOW) {
779 } elseif ($item['verb'] == ACTIVITY_TAG) {
789 * Creates the activity or fetches it from the cache
791 * @param integer $item_id
792 * @param boolean $force Force new cache entry
794 * @return array with the activity
797 public static function createCachedActivityFromItem($item_id, $force = false)
799 $cachekey = 'APDelivery:createActivity:' . $item_id;
802 $data = Cache::get($cachekey);
803 if (!is_null($data)) {
808 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
810 Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
815 * Creates an activity array for a given item id
817 * @param integer $item_id
818 * @param boolean $object_mode Is the activity item is used inside another object?
820 * @return array of activity
823 public static function createActivityFromItem($item_id, $object_mode = false)
825 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
827 if (!DBA::isResult($item)) {
831 if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
832 $owner = User::getOwnerDataById($item['uid']);
833 if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
836 // Disguise forum posts as reshares. Will later be converted to a real announce
837 $item['body'] = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
838 $item['guid'], $item['created'], $item['plink']) . $item['body'] . '[/share]';
843 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
844 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
845 if (DBA::isResult($conversation)) {
846 $data = json_decode($conversation['source'], true);
852 $type = self::getTypeOfItem($item);
856 $data = ['@context' => ActivityPub::CONTEXT];
858 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
860 } elseif ($item['deleted']) {
867 $data['id'] = $item['uri'] . '#' . $type;
868 $data['type'] = $type;
870 if (Item::isForumPost($item) && ($type != 'Announce')) {
871 $data['actor'] = $item['author-link'];
873 $data['actor'] = $item['owner-link'];
876 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
878 $data['instrument'] = self::getService();
880 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
882 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
883 $data['object'] = self::createNote($item);
884 } elseif ($data['type'] == 'Add') {
885 $data = self::createAddTag($item, $data);
886 } elseif ($data['type'] == 'Announce') {
887 $data = self::createAnnounce($item, $data);
888 } elseif ($data['type'] == 'Follow') {
889 $data['object'] = $item['parent-uri'];
890 } elseif ($data['type'] == 'Undo') {
891 $data['object'] = self::createActivityFromItem($item_id, true);
893 $data['diaspora:guid'] = $item['guid'];
894 if (!empty($item['signed_text'])) {
895 $data['diaspora:like'] = $item['signed_text'];
897 $data['object'] = $item['thr-parent'];
900 if (!empty($item['contact-uid'])) {
901 $uid = $item['contact-uid'];
906 $owner = User::getOwnerDataById($uid);
908 if (!$object_mode && !empty($owner)) {
909 return LDSignature::sign($data, $owner);
914 /// @todo Create "conversation" entry
918 * Creates an object array for a given item id
920 * @param integer $item_id
922 * @return array with the object data
923 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
924 * @throws \ImagickException
926 public static function createObjectFromItemID($item_id)
928 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
930 if (!DBA::isResult($item)) {
934 $data = ['@context' => ActivityPub::CONTEXT];
935 $data = array_merge($data, self::createNote($item));
941 * Creates a location entry for a given item array
945 * @return array with location array
947 private static function createLocation($item)
949 $location = ['type' => 'Place'];
951 if (!empty($item['location'])) {
952 $location['name'] = $item['location'];
957 if (empty($item['coord'])) {
958 $coord = Map::getCoordinates($item['location']);
960 $coords = explode(' ', $item['coord']);
961 if (count($coords) == 2) {
962 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
966 if (!empty($coord['lat']) && !empty($coord['lon'])) {
967 $location['latitude'] = $coord['lat'];
968 $location['longitude'] = $coord['lon'];
975 * Returns a tag array for a given item array
979 * @return array of tags
980 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
982 private static function createTagList($item)
986 $terms = Term::tagArrayFromItemId($item['id'], [Term::HASHTAG, Term::MENTION, Term::IMPLICIT_MENTION]);
987 foreach ($terms as $term) {
988 if ($term['type'] == Term::HASHTAG) {
989 $url = System::baseUrl() . '/search?tag=' . urlencode($term['term']);
990 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
991 } elseif ($term['type'] == Term::MENTION || $term['type'] == Term::IMPLICIT_MENTION) {
992 $contact = Contact::getDetailsByURL($term['url']);
993 if (!empty($contact['addr'])) {
994 $mention = '@' . $contact['addr'];
996 $mention = '@' . $term['url'];
999 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
1006 * Adds attachment data to the JSON document
1008 * @param array $item Data of the item that is to be posted
1009 * @param string $type Object type
1011 * @return array with attachment data
1012 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1014 private static function createAttachmentList($item, $type)
1018 $arr = explode('[/attach],', $item['attach']);
1020 foreach ($arr as $r) {
1022 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1024 $attributes = ['type' => 'Document',
1025 'mediaType' => $matches[3],
1026 'url' => $matches[1],
1029 if (trim($matches[4]) != '') {
1030 $attributes['name'] = trim($matches[4]);
1033 $attachments[] = $attributes;
1038 if ($type != 'Note') {
1039 return $attachments;
1042 // Simplify image codes
1043 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
1045 // Grab all pictures without alternative descriptions and create attachments out of them
1046 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
1047 foreach ($pictures[1] as $picture) {
1048 $imgdata = Image::getInfoFromURL($picture);
1050 $attachments[] = ['type' => 'Document',
1051 'mediaType' => $imgdata['mime'],
1058 // Grab all pictures with alternative description and create attachments out of them
1059 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
1060 foreach ($pictures as $picture) {
1061 $imgdata = Image::getInfoFromURL($picture[1]);
1063 $attachments[] = ['type' => 'Document',
1064 'mediaType' => $imgdata['mime'],
1065 'url' => $picture[1],
1066 'name' => $picture[2]];
1071 return $attachments;
1075 * @brief Callback function to replace a Friendica style mention in a mention that is used on AP
1077 * @param array $match Matching values for the callback
1078 * @return string Replaced mention
1079 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1081 private static function mentionCallback($match)
1083 if (empty($match[1])) {
1087 $data = Contact::getDetailsByURL($match[1]);
1088 if (empty($data['nick'])) {
1092 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
1096 * Remove image elements and replaces them with links to the image
1098 * @param string $body
1100 * @return string with replaced elements
1102 private static function removePictures($body)
1104 // Simplify image codes
1105 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
1107 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
1108 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
1114 * Fetches the "context" value for a givem item array from the "conversation" table
1116 * @param array $item
1118 * @return string with context url
1119 * @throws \Exception
1121 private static function fetchContextURLForItem($item)
1123 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
1124 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
1125 $context_uri = $conversation['conversation-href'];
1126 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
1127 $context_uri = $conversation['conversation-uri'];
1129 $context_uri = $item['parent-uri'] . '#context';
1131 return $context_uri;
1135 * Returns if the post contains sensitive content ("nsfw")
1137 * @param integer $item_id
1140 * @throws \Exception
1142 private static function isSensitive($item_id)
1144 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
1145 return DBA::exists('term', $condition);
1149 * Creates event data
1151 * @param array $item
1153 * @return array with the event data
1154 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1156 public static function createEvent($item)
1159 $event['name'] = $item['event-summary'];
1160 $event['content'] = BBCode::convert($item['event-desc'], false, 9);
1161 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
1163 if (!$item['event-nofinish']) {
1164 $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
1167 if (!empty($item['event-location'])) {
1168 $item['location'] = $item['event-location'];
1169 $event['location'] = self::createLocation($item);
1176 * Creates a note/article object array
1178 * @param array $item
1180 * @return array with the object data
1181 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1182 * @throws \ImagickException
1184 public static function createNote($item)
1186 if ($item['event-type'] == 'event') {
1188 } elseif (!empty($item['title'])) {
1194 if ($item['deleted']) {
1195 $type = 'Tombstone';
1199 $data['id'] = $item['uri'];
1200 $data['type'] = $type;
1202 if ($item['deleted']) {
1206 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1208 if ($item['uri'] != $item['thr-parent']) {
1209 $data['inReplyTo'] = $item['thr-parent'];
1211 $data['inReplyTo'] = null;
1214 $data['diaspora:guid'] = $item['guid'];
1215 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1217 if ($item['created'] != $item['edited']) {
1218 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1221 $data['url'] = $item['plink'];
1222 $data['attributedTo'] = $item['author-link'];
1223 $data['sensitive'] = self::isSensitive($item['id']);
1224 $data['context'] = self::fetchContextURLForItem($item);
1226 if (!empty($item['title'])) {
1227 $data['name'] = BBCode::toPlaintext($item['title'], false);
1230 $permission_block = self::createPermissionBlockForItem($item, false);
1232 $body = $item['body'];
1234 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1235 $body = self::prependMentions($body, $permission_block);
1238 if ($type == 'Note') {
1239 $body = self::removePictures($body);
1240 } elseif (($type == 'Article') && empty($data['summary'])) {
1241 $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
1244 if ($type == 'Event') {
1245 $data = array_merge($data, self::createEvent($item));
1247 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1248 $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1250 $data['content'] = BBCode::convert($body, false, 9);
1253 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1255 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1256 $data['diaspora:comment'] = $item['signed_text'];
1259 $data['attachment'] = self::createAttachmentList($item, $type);
1260 $data['tag'] = self::createTagList($item);
1262 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1263 $data['location'] = self::createLocation($item);
1266 if (!empty($item['app'])) {
1267 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1270 $data = array_merge($data, $permission_block);
1276 * Creates an an "add tag" entry
1278 * @param array $item
1279 * @param array $data activity data
1281 * @return array with activity data for adding tags
1282 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1283 * @throws \ImagickException
1285 private static function createAddTag($item, $data)
1287 $object = XML::parseString($item['object'], false);
1288 $target = XML::parseString($item["target"], false);
1290 $data['diaspora:guid'] = $item['guid'];
1291 $data['actor'] = $item['author-link'];
1292 $data['target'] = (string)$target->id;
1293 $data['summary'] = BBCode::toPlaintext($item['body']);
1294 $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1300 * Creates an announce object entry
1302 * @param array $item
1303 * @param array $data activity data
1305 * @return array with activity data
1306 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1307 * @throws \ImagickException
1309 private static function createAnnounce($item, $data)
1311 $orig_body = $item['body'];
1312 $announce = api_share_as_retweet($item);
1313 if (empty($announce['plink'])) {
1314 $data['type'] = 'Create';
1315 $data['object'] = self::createNote($item);
1319 // Fetch the original id of the object
1320 $activity = ActivityPub::fetchContent($announce['plink'], $item['uid']);
1321 if (!empty($activity)) {
1322 $ldactivity = JsonLD::compact($activity);
1323 $id = JsonLD::fetchElement($ldactivity, '@id');
1324 $type = str_replace('as:', '', JsonLD::fetchElement($ldactivity, '@type'));
1326 if (empty($announce['share-pre-body'])) {
1327 // Pure announce, without a quote
1328 $data['type'] = 'Announce';
1329 $data['object'] = $id;
1334 $data['type'] = 'Create';
1335 $item['body'] = trim($announce['share-pre-body']) . "\n" . $id;
1336 $data['object'] = self::createNote($item);
1338 /// @todo Finally descide how to implement this in AP. This is a possible way:
1339 $data['object']['attachment'][] = ['type' => $type, 'id' => $id];
1341 $data['object']['source']['content'] = $orig_body;
1346 $item['body'] = $orig_body;
1347 $data['type'] = 'Create';
1348 $data['object'] = self::createNote($item);
1353 * Creates an activity id for a given contact id
1355 * @param integer $cid Contact ID of target
1357 * @return bool|string activity id
1359 public static function activityIDFromContact($cid)
1361 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1362 if (!DBA::isResult($contact)) {
1366 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1367 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1368 return System::baseUrl() . '/activity/' . $uuid;
1372 * Transmits a contact suggestion to a given inbox
1374 * @param integer $uid User ID
1375 * @param string $inbox Target inbox
1376 * @param integer $suggestion_id Suggestion ID
1378 * @return boolean was the transmission successful?
1379 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1381 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1383 $owner = User::getOwnerDataById($uid);
1385 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
1387 $data = ['@context' => ActivityPub::CONTEXT,
1388 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1389 'type' => 'Announce',
1390 'actor' => $owner['url'],
1391 'object' => $suggestion['url'],
1392 'content' => $suggestion['note'],
1393 'instrument' => self::getService(),
1394 'to' => [ActivityPub::PUBLIC_COLLECTION],
1397 $signed = LDSignature::sign($data, $owner);
1399 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1400 return HTTPSignature::transmit($signed, $inbox, $uid);
1404 * Transmits a profile relocation to a given inbox
1406 * @param integer $uid User ID
1407 * @param string $inbox Target inbox
1409 * @return boolean was the transmission successful?
1410 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1412 public static function sendProfileRelocation($uid, $inbox)
1414 $owner = User::getOwnerDataById($uid);
1416 $data = ['@context' => ActivityPub::CONTEXT,
1417 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1418 'type' => 'dfrn:relocate',
1419 'actor' => $owner['url'],
1420 'object' => $owner['url'],
1421 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1422 'instrument' => self::getService(),
1423 'to' => [ActivityPub::PUBLIC_COLLECTION],
1426 $signed = LDSignature::sign($data, $owner);
1428 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1429 return HTTPSignature::transmit($signed, $inbox, $uid);
1433 * Transmits a profile deletion to a given inbox
1435 * @param integer $uid User ID
1436 * @param string $inbox Target inbox
1438 * @return boolean was the transmission successful?
1439 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1441 public static function sendProfileDeletion($uid, $inbox)
1443 $owner = User::getOwnerDataById($uid);
1445 if (empty($owner)) {
1446 Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
1450 if (empty($owner['uprvkey'])) {
1451 Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
1455 $data = ['@context' => ActivityPub::CONTEXT,
1456 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1458 'actor' => $owner['url'],
1459 'object' => $owner['url'],
1460 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1461 'instrument' => self::getService(),
1462 'to' => [ActivityPub::PUBLIC_COLLECTION],
1465 $signed = LDSignature::sign($data, $owner);
1467 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1468 return HTTPSignature::transmit($signed, $inbox, $uid);
1472 * Transmits a profile change to a given inbox
1474 * @param integer $uid User ID
1475 * @param string $inbox Target inbox
1477 * @return boolean was the transmission successful?
1478 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1479 * @throws \ImagickException
1481 public static function sendProfileUpdate($uid, $inbox)
1483 $owner = User::getOwnerDataById($uid);
1484 $profile = APContact::getByURL($owner['url']);
1486 $data = ['@context' => ActivityPub::CONTEXT,
1487 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1489 'actor' => $owner['url'],
1490 'object' => self::getProfile($uid),
1491 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1492 'instrument' => self::getService(),
1493 'to' => [$profile['followers']],
1496 $signed = LDSignature::sign($data, $owner);
1498 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1499 return HTTPSignature::transmit($signed, $inbox, $uid);
1503 * Transmits a given activity to a target
1505 * @param string $activity Type name
1506 * @param string $target Target profile
1507 * @param integer $uid User ID
1509 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1510 * @throws \ImagickException
1511 * @throws \Exception
1513 public static function sendActivity($activity, $target, $uid, $id = '')
1515 $profile = APContact::getByURL($target);
1516 if (empty($profile['inbox'])) {
1517 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1521 $owner = User::getOwnerDataById($uid);
1524 $id = System::baseUrl() . '/activity/' . System::createGUID();
1527 $data = ['@context' => ActivityPub::CONTEXT,
1529 'type' => $activity,
1530 'actor' => $owner['url'],
1531 'object' => $profile['url'],
1532 'instrument' => self::getService(),
1533 'to' => [$profile['url']]];
1535 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1537 $signed = LDSignature::sign($data, $owner);
1538 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1542 * Transmits a "follow object" activity to a target
1543 * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
1545 * @param string $object Object URL
1546 * @param string $target Target profile
1547 * @param integer $uid User ID
1549 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1550 * @throws \ImagickException
1551 * @throws \Exception
1553 public static function sendFollowObject($object, $target, $uid = 0)
1555 $profile = APContact::getByURL($target);
1556 if (empty($profile['inbox'])) {
1557 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1562 // Fetch the list of administrators
1563 $admin_mail = explode(',', str_replace(' ', '', Config::get('config', 'admin_email')));
1565 // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
1566 $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
1567 $first_user = DBA::selectFirst('user', ['uid'], $condition);
1568 $uid = $first_user['uid'];
1571 $condition = ['verb' => ACTIVITY_FOLLOW, 'uid' => 0, 'parent-uri' => $object,
1572 'author-id' => Contact::getPublicIdByUserId($uid)];
1573 if (Item::exists($condition)) {
1574 Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
1578 $owner = User::getOwnerDataById($uid);
1580 $data = ['@context' => ActivityPub::CONTEXT,
1581 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1583 'actor' => $owner['url'],
1584 'object' => $object,
1585 'instrument' => self::getService(),
1586 'to' => [$profile['url']]];
1588 Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1590 $signed = LDSignature::sign($data, $owner);
1591 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1595 * Transmit a message that the contact request had been accepted
1597 * @param string $target Target profile
1599 * @param integer $uid User ID
1600 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1601 * @throws \ImagickException
1603 public static function sendContactAccept($target, $id, $uid)
1605 $profile = APContact::getByURL($target);
1606 if (empty($profile['inbox'])) {
1607 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1611 $owner = User::getOwnerDataById($uid);
1612 $data = ['@context' => ActivityPub::CONTEXT,
1613 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1615 'actor' => $owner['url'],
1617 'id' => (string)$id,
1619 'actor' => $profile['url'],
1620 'object' => $owner['url']
1622 'instrument' => self::getService(),
1623 'to' => [$profile['url']]];
1625 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1627 $signed = LDSignature::sign($data, $owner);
1628 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1632 * Reject a contact request or terminates the contact relation
1634 * @param string $target Target profile
1636 * @param integer $uid User ID
1637 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1638 * @throws \ImagickException
1640 public static function sendContactReject($target, $id, $uid)
1642 $profile = APContact::getByURL($target);
1643 if (empty($profile['inbox'])) {
1644 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1648 $owner = User::getOwnerDataById($uid);
1649 $data = ['@context' => ActivityPub::CONTEXT,
1650 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1652 'actor' => $owner['url'],
1654 'id' => (string)$id,
1656 'actor' => $profile['url'],
1657 'object' => $owner['url']
1659 'instrument' => self::getService(),
1660 'to' => [$profile['url']]];
1662 Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1664 $signed = LDSignature::sign($data, $owner);
1665 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1669 * Transmits a message that we don't want to follow this contact anymore
1671 * @param string $target Target profile
1672 * @param integer $uid User ID
1673 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1674 * @throws \ImagickException
1675 * @throws \Exception
1677 public static function sendContactUndo($target, $cid, $uid)
1679 $profile = APContact::getByURL($target);
1680 if (empty($profile['inbox'])) {
1681 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1685 $object_id = self::activityIDFromContact($cid);
1686 if (empty($object_id)) {
1690 $id = System::baseUrl() . '/activity/' . System::createGUID();
1692 $owner = User::getOwnerDataById($uid);
1693 $data = ['@context' => ActivityPub::CONTEXT,
1696 'actor' => $owner['url'],
1697 'object' => ['id' => $object_id, 'type' => 'Follow',
1698 'actor' => $owner['url'],
1699 'object' => $profile['url']],
1700 'instrument' => self::getService(),
1701 'to' => [$profile['url']]];
1703 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1705 $signed = LDSignature::sign($data, $owner);
1706 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1709 private static function prependMentions($body, array $permission_block)
1711 if (Config::get('system', 'disable_implicit_mentions')) {
1717 foreach ($permission_block['to'] as $profile_url) {
1718 $profile = Contact::getDetailsByURL($profile_url);
1719 if (!empty($profile['addr'])
1720 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
1721 && !strstr($body, $profile['addr'])
1722 && !strstr($body, $profile_url)
1724 $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]';
1728 $mentions[] = $body;
1730 return implode(' ', $mentions);