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);
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 (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
551 if (Network::isUrlBlocked($contact['url'])) {
555 $profile = APContact::getByURL($contact['url'], false);
556 if (!empty($profile)) {
557 if (empty($profile['sharedinbox']) || $personal) {
558 $target = $profile['inbox'];
560 $target = $profile['sharedinbox'];
562 if (!self::archivedInbox($target)) {
563 $inboxes[$target] = $target;
567 DBA::close($contacts);
573 * Fetches an array of inboxes for the given item and user
575 * @param array $item Item array
576 * @param integer $uid User ID
577 * @param boolean $personal fetch personal inboxes
578 * @param integer $last_id Last item id for adding receivers
579 * @param boolean $forum_mode "true" means that we are sending content to a forum
580 * @return array with inboxes
581 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
582 * @throws \ImagickException
584 public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
586 $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
587 if (empty($permissions)) {
593 if ($item['gravity'] == GRAVITY_ACTIVITY) {
594 $item_profile = APContact::getByURL($item['author-link'], false);
596 $item_profile = APContact::getByURL($item['owner-link'], false);
599 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
600 if (empty($permissions[$element])) {
604 $blindcopy = in_array($element, ['bto', 'bcc']);
606 foreach ($permissions[$element] as $receiver) {
607 if (Network::isUrlBlocked($receiver)) {
611 if ($receiver == $item_profile['followers']) {
612 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
614 $profile = APContact::getByURL($receiver, false);
615 if (!empty($profile)) {
616 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
617 $target = $profile['inbox'];
619 $target = $profile['sharedinbox'];
621 if (!self::archivedInbox($target)) {
622 $inboxes[$target] = $target;
633 * Creates an array in the structure of the item table for a given mail id
635 * @param integer $mail_id
640 public static function ItemArrayFromMail($mail_id)
642 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
644 $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
646 // Making the post more compatible for Mastodon by:
647 // - Making it a note and not an article (no title)
648 // - Moving the title into the "summary" field that is used as a "content warning"
649 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
652 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
653 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
654 $mail['allow_gid'] = '';
655 $mail['deny_cid'] = '';
656 $mail['deny_gid'] = '';
657 $mail['private'] = true;
658 $mail['deleted'] = false;
659 $mail['edited'] = $mail['created'];
660 $mail['plink'] = $mail['uri'];
661 $mail['thr-parent'] = $reply['uri'];
662 $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
664 $mail['event-type'] = '';
665 $mail['attach'] = '';
673 * Creates an activity array for a given mail id
675 * @param integer $mail_id
676 * @param boolean $object_mode Is the activity item is used inside another object?
678 * @return array of activity
681 public static function createActivityFromMail($mail_id, $object_mode = false)
683 $mail = self::ItemArrayFromMail($mail_id);
684 $object = self::createNote($mail);
686 $object['to'] = $object['cc'];
687 unset($object['cc']);
689 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => 'test']];
692 $data = ['@context' => ActivityPub::CONTEXT];
697 $data['id'] = $mail['uri'] . '#Create';
698 $data['type'] = 'Create';
699 $data['actor'] = $mail['author-link'];
700 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
701 $data['instrument'] = self::getService();
702 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
704 if (empty($data['to']) && !empty($data['cc'])) {
705 $data['to'] = $data['cc'];
708 if (empty($data['to']) && !empty($data['bcc'])) {
709 $data['to'] = $data['bcc'];
715 $object['to'] = $data['to'];
716 unset($object['cc']);
717 unset($object['bcc']);
719 $data['directMessage'] = true;
721 $data['object'] = $object;
723 $owner = User::getOwnerDataById($mail['uid']);
725 if (!$object_mode && !empty($owner)) {
726 return LDSignature::sign($data, $owner);
733 * Returns the activity type of a given item
737 * @return string with activity type
738 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
739 * @throws \ImagickException
741 private static function getTypeOfItem($item)
745 // Only check for a reshare, if it is a real reshare and no quoted reshare
746 if (strpos($item['body'], "[share") === 0) {
747 $announce = api_share_as_retweet($item);
748 $reshared = !empty($announce['plink']);
753 } elseif ($item['verb'] == ACTIVITY_POST) {
754 if ($item['created'] == $item['edited']) {
759 } elseif ($item['verb'] == ACTIVITY_LIKE) {
761 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
763 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
765 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
767 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
768 $type = 'TentativeAccept';
769 } elseif ($item['verb'] == ACTIVITY_FOLLOW) {
771 } elseif ($item['verb'] == ACTIVITY_TAG) {
781 * Creates the activity or fetches it from the cache
783 * @param integer $item_id
784 * @param boolean $force Force new cache entry
786 * @return array with the activity
789 public static function createCachedActivityFromItem($item_id, $force = false)
791 $cachekey = 'APDelivery:createActivity:' . $item_id;
794 $data = Cache::get($cachekey);
795 if (!is_null($data)) {
800 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
802 Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
807 * Creates an activity array for a given item id
809 * @param integer $item_id
810 * @param boolean $object_mode Is the activity item is used inside another object?
812 * @return array of activity
815 public static function createActivityFromItem($item_id, $object_mode = false)
817 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
819 if (!DBA::isResult($item)) {
823 if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
824 $owner = User::getOwnerDataById($item['uid']);
825 if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
828 // Disguise forum posts as reshares. Will later be converted to a real announce
829 $item['body'] = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
830 $item['guid'], $item['created'], $item['plink']) . $item['body'] . '[/share]';
835 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
836 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
837 if (DBA::isResult($conversation)) {
838 $data = json_decode($conversation['source'], true);
844 $type = self::getTypeOfItem($item);
848 $data = ['@context' => ActivityPub::CONTEXT];
850 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
852 } elseif ($item['deleted']) {
859 $data['id'] = $item['uri'] . '#' . $type;
860 $data['type'] = $type;
862 if (Item::isForumPost($item) && ($type != 'Announce')) {
863 $data['actor'] = $item['author-link'];
865 $data['actor'] = $item['owner-link'];
868 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
870 $data['instrument'] = self::getService();
872 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
874 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
875 $data['object'] = self::createNote($item);
876 } elseif ($data['type'] == 'Add') {
877 $data = self::createAddTag($item, $data);
878 } elseif ($data['type'] == 'Announce') {
879 $data = self::createAnnounce($item, $data);
880 } elseif ($data['type'] == 'Follow') {
881 $data['object'] = $item['parent-uri'];
882 } elseif ($data['type'] == 'Undo') {
883 $data['object'] = self::createActivityFromItem($item_id, true);
885 $data['diaspora:guid'] = $item['guid'];
886 if (!empty($item['signed_text'])) {
887 $data['diaspora:like'] = $item['signed_text'];
889 $data['object'] = $item['thr-parent'];
892 if (!empty($item['contact-uid'])) {
893 $uid = $item['contact-uid'];
898 $owner = User::getOwnerDataById($uid);
900 if (!$object_mode && !empty($owner)) {
901 return LDSignature::sign($data, $owner);
906 /// @todo Create "conversation" entry
910 * Creates an object array for a given item id
912 * @param integer $item_id
914 * @return array with the object data
915 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
916 * @throws \ImagickException
918 public static function createObjectFromItemID($item_id)
920 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
922 if (!DBA::isResult($item)) {
926 $data = ['@context' => ActivityPub::CONTEXT];
927 $data = array_merge($data, self::createNote($item));
933 * Creates a location entry for a given item array
937 * @return array with location array
939 private static function createLocation($item)
941 $location = ['type' => 'Place'];
943 if (!empty($item['location'])) {
944 $location['name'] = $item['location'];
949 if (empty($item['coord'])) {
950 $coord = Map::getCoordinates($item['location']);
952 $coords = explode(' ', $item['coord']);
953 if (count($coords) == 2) {
954 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
958 if (!empty($coord['lat']) && !empty($coord['lon'])) {
959 $location['latitude'] = $coord['lat'];
960 $location['longitude'] = $coord['lon'];
967 * Returns a tag array for a given item array
971 * @return array of tags
972 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
974 private static function createTagList($item)
978 $terms = Term::tagArrayFromItemId($item['id'], [Term::HASHTAG, Term::MENTION, Term::IMPLICIT_MENTION]);
979 foreach ($terms as $term) {
980 if ($term['type'] == Term::HASHTAG) {
981 $url = System::baseUrl() . '/search?tag=' . urlencode($term['term']);
982 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
983 } elseif ($term['type'] == Term::MENTION || $term['type'] == Term::IMPLICIT_MENTION) {
984 $contact = Contact::getDetailsByURL($term['url']);
985 if (!empty($contact['addr'])) {
986 $mention = '@' . $contact['addr'];
988 $mention = '@' . $term['url'];
991 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
998 * Adds attachment data to the JSON document
1000 * @param array $item Data of the item that is to be posted
1001 * @param string $type Object type
1003 * @return array with attachment data
1004 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1006 private static function createAttachmentList($item, $type)
1010 $arr = explode('[/attach],', $item['attach']);
1012 foreach ($arr as $r) {
1014 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1016 $attributes = ['type' => 'Document',
1017 'mediaType' => $matches[3],
1018 'url' => $matches[1],
1021 if (trim($matches[4]) != '') {
1022 $attributes['name'] = trim($matches[4]);
1025 $attachments[] = $attributes;
1030 if ($type != 'Note') {
1031 return $attachments;
1034 // Simplify image codes
1035 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
1037 // Grab all pictures without alternative descriptions and create attachments out of them
1038 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
1039 foreach ($pictures[1] as $picture) {
1040 $imgdata = Image::getInfoFromURL($picture);
1042 $attachments[] = ['type' => 'Document',
1043 'mediaType' => $imgdata['mime'],
1050 // Grab all pictures with alternative description and create attachments out of them
1051 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
1052 foreach ($pictures as $picture) {
1053 $imgdata = Image::getInfoFromURL($picture[1]);
1055 $attachments[] = ['type' => 'Document',
1056 'mediaType' => $imgdata['mime'],
1057 'url' => $picture[1],
1058 'name' => $picture[2]];
1063 return $attachments;
1067 * @brief Callback function to replace a Friendica style mention in a mention that is used on AP
1069 * @param array $match Matching values for the callback
1070 * @return string Replaced mention
1071 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1073 private static function mentionCallback($match)
1075 if (empty($match[1])) {
1079 $data = Contact::getDetailsByURL($match[1]);
1080 if (empty($data['nick'])) {
1084 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
1088 * Remove image elements and replaces them with links to the image
1090 * @param string $body
1092 * @return string with replaced elements
1094 private static function removePictures($body)
1096 // Simplify image codes
1097 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
1099 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
1100 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
1106 * Fetches the "context" value for a givem item array from the "conversation" table
1108 * @param array $item
1110 * @return string with context url
1111 * @throws \Exception
1113 private static function fetchContextURLForItem($item)
1115 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
1116 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
1117 $context_uri = $conversation['conversation-href'];
1118 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
1119 $context_uri = $conversation['conversation-uri'];
1121 $context_uri = $item['parent-uri'] . '#context';
1123 return $context_uri;
1127 * Returns if the post contains sensitive content ("nsfw")
1129 * @param integer $item_id
1132 * @throws \Exception
1134 private static function isSensitive($item_id)
1136 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
1137 return DBA::exists('term', $condition);
1141 * Creates event data
1143 * @param array $item
1145 * @return array with the event data
1146 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1148 public static function createEvent($item)
1151 $event['name'] = $item['event-summary'];
1152 $event['content'] = BBCode::convert($item['event-desc'], false, 9);
1153 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
1155 if (!$item['event-nofinish']) {
1156 $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
1159 if (!empty($item['event-location'])) {
1160 $item['location'] = $item['event-location'];
1161 $event['location'] = self::createLocation($item);
1168 * Creates a note/article object array
1170 * @param array $item
1172 * @return array with the object data
1173 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1174 * @throws \ImagickException
1176 public static function createNote($item)
1178 if ($item['event-type'] == 'event') {
1180 } elseif (!empty($item['title'])) {
1186 if ($item['deleted']) {
1187 $type = 'Tombstone';
1191 $data['id'] = $item['uri'];
1192 $data['type'] = $type;
1194 if ($item['deleted']) {
1198 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1200 if ($item['uri'] != $item['thr-parent']) {
1201 $data['inReplyTo'] = $item['thr-parent'];
1203 $data['inReplyTo'] = null;
1206 $data['diaspora:guid'] = $item['guid'];
1207 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1209 if ($item['created'] != $item['edited']) {
1210 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1213 $data['url'] = $item['plink'];
1214 $data['attributedTo'] = $item['author-link'];
1215 $data['sensitive'] = self::isSensitive($item['id']);
1216 $data['context'] = self::fetchContextURLForItem($item);
1218 if (!empty($item['title'])) {
1219 $data['name'] = BBCode::toPlaintext($item['title'], false);
1222 $permission_block = self::createPermissionBlockForItem($item, false);
1224 $body = $item['body'];
1226 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1227 $body = self::prependMentions($body, $permission_block);
1230 if ($type == 'Note') {
1231 $body = self::removePictures($body);
1232 } elseif (($type == 'Article') && empty($data['summary'])) {
1233 $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
1236 if ($type == 'Event') {
1237 $data = array_merge($data, self::createEvent($item));
1239 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1240 $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1242 $data['content'] = BBCode::convert($body, false, 9);
1245 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1247 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1248 $data['diaspora:comment'] = $item['signed_text'];
1251 $data['attachment'] = self::createAttachmentList($item, $type);
1252 $data['tag'] = self::createTagList($item);
1254 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1255 $data['location'] = self::createLocation($item);
1258 if (!empty($item['app'])) {
1259 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1262 $data = array_merge($data, $permission_block);
1268 * Creates an an "add tag" entry
1270 * @param array $item
1271 * @param array $data activity data
1273 * @return array with activity data for adding tags
1274 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1275 * @throws \ImagickException
1277 private static function createAddTag($item, $data)
1279 $object = XML::parseString($item['object'], false);
1280 $target = XML::parseString($item["target"], false);
1282 $data['diaspora:guid'] = $item['guid'];
1283 $data['actor'] = $item['author-link'];
1284 $data['target'] = (string)$target->id;
1285 $data['summary'] = BBCode::toPlaintext($item['body']);
1286 $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1292 * Creates an announce object entry
1294 * @param array $item
1295 * @param array $data activity data
1297 * @return array with activity data
1298 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1299 * @throws \ImagickException
1301 private static function createAnnounce($item, $data)
1303 $orig_body = $item['body'];
1304 $announce = api_share_as_retweet($item);
1305 if (empty($announce['plink'])) {
1306 $data['type'] = 'Create';
1307 $data['object'] = self::createNote($item);
1311 // Fetch the original id of the object
1312 $activity = ActivityPub::fetchContent($announce['plink'], $item['uid']);
1313 if (!empty($activity)) {
1314 $ldactivity = JsonLD::compact($activity);
1315 $id = JsonLD::fetchElement($ldactivity, '@id');
1316 $type = str_replace('as:', '', JsonLD::fetchElement($ldactivity, '@type'));
1318 if (empty($announce['share-pre-body'])) {
1319 // Pure announce, without a quote
1320 $data['type'] = 'Announce';
1321 $data['object'] = $id;
1326 $data['type'] = 'Create';
1327 $item['body'] = trim($announce['share-pre-body']) . "\n" . $id;
1328 $data['object'] = self::createNote($item);
1330 /// @todo Finally descide how to implement this in AP. This is a possible way:
1331 $data['object']['attachment'][] = ['type' => $type, 'id' => $id];
1333 $data['object']['source']['content'] = $orig_body;
1338 $item['body'] = $orig_body;
1339 $data['type'] = 'Create';
1340 $data['object'] = self::createNote($item);
1345 * Creates an activity id for a given contact id
1347 * @param integer $cid Contact ID of target
1349 * @return bool|string activity id
1351 public static function activityIDFromContact($cid)
1353 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1354 if (!DBA::isResult($contact)) {
1358 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1359 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1360 return System::baseUrl() . '/activity/' . $uuid;
1364 * Transmits a contact suggestion to a given inbox
1366 * @param integer $uid User ID
1367 * @param string $inbox Target inbox
1368 * @param integer $suggestion_id Suggestion ID
1370 * @return boolean was the transmission successful?
1371 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1373 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1375 $owner = User::getOwnerDataById($uid);
1377 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
1379 $data = ['@context' => ActivityPub::CONTEXT,
1380 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1381 'type' => 'Announce',
1382 'actor' => $owner['url'],
1383 'object' => $suggestion['url'],
1384 'content' => $suggestion['note'],
1385 'instrument' => self::getService(),
1386 'to' => [ActivityPub::PUBLIC_COLLECTION],
1389 $signed = LDSignature::sign($data, $owner);
1391 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1392 return HTTPSignature::transmit($signed, $inbox, $uid);
1396 * Transmits a profile relocation to a given inbox
1398 * @param integer $uid User ID
1399 * @param string $inbox Target inbox
1401 * @return boolean was the transmission successful?
1402 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1404 public static function sendProfileRelocation($uid, $inbox)
1406 $owner = User::getOwnerDataById($uid);
1408 $data = ['@context' => ActivityPub::CONTEXT,
1409 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1410 'type' => 'dfrn:relocate',
1411 'actor' => $owner['url'],
1412 'object' => $owner['url'],
1413 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1414 'instrument' => self::getService(),
1415 'to' => [ActivityPub::PUBLIC_COLLECTION],
1418 $signed = LDSignature::sign($data, $owner);
1420 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1421 return HTTPSignature::transmit($signed, $inbox, $uid);
1425 * Transmits a profile deletion to a given inbox
1427 * @param integer $uid User ID
1428 * @param string $inbox Target inbox
1430 * @return boolean was the transmission successful?
1431 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1433 public static function sendProfileDeletion($uid, $inbox)
1435 $owner = User::getOwnerDataById($uid);
1437 if (empty($owner)) {
1438 Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
1442 if (empty($owner['uprvkey'])) {
1443 Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
1447 $data = ['@context' => ActivityPub::CONTEXT,
1448 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1450 'actor' => $owner['url'],
1451 'object' => $owner['url'],
1452 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1453 'instrument' => self::getService(),
1454 'to' => [ActivityPub::PUBLIC_COLLECTION],
1457 $signed = LDSignature::sign($data, $owner);
1459 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1460 return HTTPSignature::transmit($signed, $inbox, $uid);
1464 * Transmits a profile change to a given inbox
1466 * @param integer $uid User ID
1467 * @param string $inbox Target inbox
1469 * @return boolean was the transmission successful?
1470 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1471 * @throws \ImagickException
1473 public static function sendProfileUpdate($uid, $inbox)
1475 $owner = User::getOwnerDataById($uid);
1476 $profile = APContact::getByURL($owner['url']);
1478 $data = ['@context' => ActivityPub::CONTEXT,
1479 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1481 'actor' => $owner['url'],
1482 'object' => self::getProfile($uid),
1483 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1484 'instrument' => self::getService(),
1485 'to' => [$profile['followers']],
1488 $signed = LDSignature::sign($data, $owner);
1490 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1491 return HTTPSignature::transmit($signed, $inbox, $uid);
1495 * Transmits a given activity to a target
1497 * @param string $activity Type name
1498 * @param string $target Target profile
1499 * @param integer $uid User ID
1501 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1502 * @throws \ImagickException
1503 * @throws \Exception
1505 public static function sendActivity($activity, $target, $uid, $id = '')
1507 $profile = APContact::getByURL($target);
1508 if (empty($profile['inbox'])) {
1509 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1513 $owner = User::getOwnerDataById($uid);
1516 $id = System::baseUrl() . '/activity/' . System::createGUID();
1519 $data = ['@context' => ActivityPub::CONTEXT,
1521 'type' => $activity,
1522 'actor' => $owner['url'],
1523 'object' => $profile['url'],
1524 'instrument' => self::getService(),
1525 'to' => [$profile['url']]];
1527 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1529 $signed = LDSignature::sign($data, $owner);
1530 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1534 * Transmits a "follow object" activity to a target
1535 * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
1537 * @param string $object Object URL
1538 * @param string $target Target profile
1539 * @param integer $uid User ID
1541 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1542 * @throws \ImagickException
1543 * @throws \Exception
1545 public static function sendFollowObject($object, $target, $uid = 0)
1547 $profile = APContact::getByURL($target);
1548 if (empty($profile['inbox'])) {
1549 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1554 // Fetch the list of administrators
1555 $admin_mail = explode(',', str_replace(' ', '', Config::get('config', 'admin_email')));
1557 // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
1558 $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
1559 $first_user = DBA::selectFirst('user', ['uid'], $condition);
1560 $uid = $first_user['uid'];
1563 $condition = ['verb' => ACTIVITY_FOLLOW, 'uid' => 0, 'parent-uri' => $object,
1564 'author-id' => Contact::getPublicIdByUserId($uid)];
1565 if (Item::exists($condition)) {
1566 Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
1570 $owner = User::getOwnerDataById($uid);
1572 $data = ['@context' => ActivityPub::CONTEXT,
1573 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1575 'actor' => $owner['url'],
1576 'object' => $object,
1577 'instrument' => self::getService(),
1578 'to' => [$profile['url']]];
1580 Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1582 $signed = LDSignature::sign($data, $owner);
1583 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1587 * Transmit a message that the contact request had been accepted
1589 * @param string $target Target profile
1591 * @param integer $uid User ID
1592 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1593 * @throws \ImagickException
1595 public static function sendContactAccept($target, $id, $uid)
1597 $profile = APContact::getByURL($target);
1598 if (empty($profile['inbox'])) {
1599 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1603 $owner = User::getOwnerDataById($uid);
1604 $data = ['@context' => ActivityPub::CONTEXT,
1605 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1607 'actor' => $owner['url'],
1609 'id' => (string)$id,
1611 'actor' => $profile['url'],
1612 'object' => $owner['url']
1614 'instrument' => self::getService(),
1615 'to' => [$profile['url']]];
1617 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1619 $signed = LDSignature::sign($data, $owner);
1620 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1624 * Reject a contact request or terminates the contact relation
1626 * @param string $target Target profile
1628 * @param integer $uid User ID
1629 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1630 * @throws \ImagickException
1632 public static function sendContactReject($target, $id, $uid)
1634 $profile = APContact::getByURL($target);
1635 if (empty($profile['inbox'])) {
1636 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1640 $owner = User::getOwnerDataById($uid);
1641 $data = ['@context' => ActivityPub::CONTEXT,
1642 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1644 'actor' => $owner['url'],
1646 'id' => (string)$id,
1648 'actor' => $profile['url'],
1649 'object' => $owner['url']
1651 'instrument' => self::getService(),
1652 'to' => [$profile['url']]];
1654 Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1656 $signed = LDSignature::sign($data, $owner);
1657 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1661 * Transmits a message that we don't want to follow this contact anymore
1663 * @param string $target Target profile
1664 * @param integer $uid User ID
1665 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1666 * @throws \ImagickException
1667 * @throws \Exception
1669 public static function sendContactUndo($target, $cid, $uid)
1671 $profile = APContact::getByURL($target);
1672 if (empty($profile['inbox'])) {
1673 Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1677 $object_id = self::activityIDFromContact($cid);
1678 if (empty($object_id)) {
1682 $id = System::baseUrl() . '/activity/' . System::createGUID();
1684 $owner = User::getOwnerDataById($uid);
1685 $data = ['@context' => ActivityPub::CONTEXT,
1688 'actor' => $owner['url'],
1689 'object' => ['id' => $object_id, 'type' => 'Follow',
1690 'actor' => $owner['url'],
1691 'object' => $profile['url']],
1692 'instrument' => self::getService(),
1693 'to' => [$profile['url']]];
1695 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1697 $signed = LDSignature::sign($data, $owner);
1698 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1701 private static function prependMentions($body, array $permission_block)
1703 if (Config::get('system', 'disable_implicit_mentions')) {
1709 foreach ($permission_block['to'] as $profile_url) {
1710 $profile = Contact::getDetailsByURL($profile_url);
1711 if (!empty($profile['addr'])
1712 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
1713 && !strstr($body, $profile['addr'])
1714 && !strstr($body, $profile_url)
1716 $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]';
1720 $mentions[] = $body;
1722 return implode(' ', $mentions);