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 // On old installations and never changed contacts this might not be filled
233 if (empty($contact['avatar'])) {
234 $contact['avatar'] = $contact['photo'];
237 $data = ['@context' => ActivityPub::CONTEXT];
238 $data['id'] = $contact['url'];
239 $data['diaspora:guid'] = $user['guid'];
240 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
241 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
242 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
243 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
244 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
245 $data['preferredUsername'] = $user['nickname'];
246 $data['name'] = $contact['name'];
247 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
248 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
249 $data['summary'] = $contact['about'];
250 $data['url'] = $contact['url'];
251 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
252 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
253 'owner' => $contact['url'],
254 'publicKeyPem' => $user['pubkey']];
255 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
256 $data['icon'] = ['type' => 'Image',
257 'url' => $contact['avatar']];
259 $data['generator'] = self::getService();
261 // tags: https://kitty.town/@inmysocks/100656097926961126.json
266 * @param string $username
268 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
270 public static function getDeletedUser($username)
273 '@context' => ActivityPub::CONTEXT,
274 'id' => System::baseUrl() . '/profile/' . $username,
275 'type' => 'Tombstone',
276 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
277 'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
278 'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
283 * Returns an array with permissions of a given item array
287 * @return array with permissions
288 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
289 * @throws \ImagickException
291 private static function fetchPermissionBlockFromConversation($item)
293 if (empty($item['thr-parent'])) {
297 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
298 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
299 if (!DBA::isResult($conversation)) {
303 $activity = json_decode($conversation['source'], true);
305 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
306 $profile = APContact::getByURL($actor);
308 $item_profile = APContact::getByURL($item['author-link']);
309 $exclude[] = $item['author-link'];
311 if ($item['gravity'] == GRAVITY_PARENT) {
312 $exclude[] = $item['owner-link'];
315 $permissions['to'][] = $actor;
317 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
318 if (empty($activity[$element])) {
321 if (is_string($activity[$element])) {
322 $activity[$element] = [$activity[$element]];
325 foreach ($activity[$element] as $receiver) {
326 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
327 $permissions[$element][] = $item_profile['followers'];
328 } elseif (!in_array($receiver, $exclude)) {
329 $permissions[$element][] = $receiver;
337 * Creates an array of permissions from an item thread
339 * @param array $item Item array
340 * @param boolean $blindcopy addressing via "bcc" or "cc"?
341 * @param integer $last_id Last item id for adding receivers
342 * @param boolean $forum_mode "true" means that we are sending content to a forum
344 * @return array with permission data
345 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
346 * @throws \ImagickException
348 private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false)
351 $last_id = $item['id'];
356 // Check if we should always deliver our stuff via BCC
357 if (!empty($item['uid'])) {
358 $profile = Profile::getByUID($item['uid']);
359 if (!empty($profile)) {
360 $always_bcc = $profile['hide-friends'];
364 if (Config::get('debug', 'total_ap_delivery')) {
365 // Will be activated in a later step
366 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
368 // For now only send to these contacts:
369 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
372 $data = ['to' => [], 'cc' => [], 'bcc' => []];
374 if ($item['gravity'] == GRAVITY_PARENT) {
375 $actor_profile = APContact::getByURL($item['owner-link']);
377 $actor_profile = APContact::getByURL($item['author-link']);
380 $terms = Term::tagArrayFromItemId($item['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
382 if (!$item['private']) {
383 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
385 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
387 foreach ($terms as $term) {
388 $profile = APContact::getByURL($term['url'], false);
389 if (!empty($profile)) {
390 $data['to'][] = $profile['url'];
394 $receiver_list = Item::enumeratePermissions($item);
396 foreach ($terms as $term) {
397 $cid = Contact::getIdForURL($term['url'], $item['uid']);
398 if (!empty($cid) && in_array($cid, $receiver_list)) {
399 $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
400 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
404 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
405 $data['to'][] = $profile['url'];
410 foreach ($receiver_list as $receiver) {
411 $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
412 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
416 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
417 if ($contact['hidden'] || $always_bcc) {
418 $data['bcc'][] = $profile['url'];
420 $data['cc'][] = $profile['url'];
426 if (!empty($item['parent'])) {
427 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
428 while ($parent = Item::fetch($parents)) {
429 if ($parent['gravity'] == GRAVITY_PARENT) {
430 $profile = APContact::getByURL($parent['owner-link'], false);
431 if (!empty($profile)) {
432 if ($item['gravity'] != GRAVITY_PARENT) {
433 // Comments to forums are directed to the forum
434 // But comments to forums aren't directed to the followers collection
435 if ($profile['type'] == 'Group') {
436 $data['to'][] = $profile['url'];
438 $data['cc'][] = $profile['url'];
439 if (!$item['private'] && !empty($actor_profile['followers'])) {
440 $data['cc'][] = $actor_profile['followers'];
444 // Public thread parent post always are directed to the followers
445 if (!$item['private'] && !$forum_mode) {
446 $data['cc'][] = $actor_profile['followers'];
452 // Don't include data from future posts
453 if ($parent['id'] >= $last_id) {
457 $profile = APContact::getByURL($parent['author-link'], false);
458 if (!empty($profile)) {
459 if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
460 $data['to'][] = $profile['url'];
462 $data['cc'][] = $profile['url'];
466 DBA::close($parents);
469 $data['to'] = array_unique($data['to']);
470 $data['cc'] = array_unique($data['cc']);
471 $data['bcc'] = array_unique($data['bcc']);
473 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
474 unset($data['to'][$key]);
477 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
478 unset($data['cc'][$key]);
481 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
482 unset($data['bcc'][$key]);
485 foreach ($data['to'] as $to) {
486 if (($key = array_search($to, $data['cc'])) !== false) {
487 unset($data['cc'][$key]);
490 if (($key = array_search($to, $data['bcc'])) !== false) {
491 unset($data['bcc'][$key]);
495 foreach ($data['cc'] as $cc) {
496 if (($key = array_search($cc, $data['bcc'])) !== false) {
497 unset($data['bcc'][$key]);
501 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
504 unset($receivers['bcc']);
511 * Check if an inbox is archived
513 * @param string $url Inbox url
515 * @return boolean "true" if inbox is archived
517 private static function archivedInbox($url)
519 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
523 * Fetches a list of inboxes of followers of a given user
525 * @param integer $uid User ID
526 * @param boolean $personal fetch personal inboxes
528 * @return array of follower inboxes
529 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
530 * @throws \ImagickException
532 public static function fetchTargetInboxesforUser($uid, $personal = false)
536 if (Config::get('debug', 'total_ap_delivery')) {
537 // Will be activated in a later step
538 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
540 // For now only send to these contacts:
541 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
544 $condition = ['uid' => $uid, 'archive' => false, 'pending' => false];
547 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
550 $contacts = DBA::select('contact', ['url', 'network', 'protocol'], $condition);
551 while ($contact = DBA::fetch($contacts)) {
552 if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
556 if (Network::isUrlBlocked($contact['url'])) {
560 $profile = APContact::getByURL($contact['url'], false);
561 if (!empty($profile)) {
562 if (empty($profile['sharedinbox']) || $personal) {
563 $target = $profile['inbox'];
565 $target = $profile['sharedinbox'];
567 if (!self::archivedInbox($target)) {
568 $inboxes[$target] = $target;
572 DBA::close($contacts);
578 * Fetches an array of inboxes for the given item and user
580 * @param array $item Item array
581 * @param integer $uid User ID
582 * @param boolean $personal fetch personal inboxes
583 * @param integer $last_id Last item id for adding receivers
584 * @param boolean $forum_mode "true" means that we are sending content to a forum
585 * @return array with inboxes
586 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
587 * @throws \ImagickException
589 public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
591 $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
592 if (empty($permissions)) {
598 if ($item['gravity'] == GRAVITY_ACTIVITY) {
599 $item_profile = APContact::getByURL($item['author-link'], false);
601 $item_profile = APContact::getByURL($item['owner-link'], false);
604 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
605 if (empty($permissions[$element])) {
609 $blindcopy = in_array($element, ['bto', 'bcc']);
611 foreach ($permissions[$element] as $receiver) {
612 if (Network::isUrlBlocked($receiver)) {
616 if ($receiver == $item_profile['followers']) {
617 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
619 $profile = APContact::getByURL($receiver, false);
620 if (!empty($profile)) {
621 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
622 $target = $profile['inbox'];
624 $target = $profile['sharedinbox'];
626 if (!self::archivedInbox($target)) {
627 $inboxes[$target] = $target;
638 * Creates an array in the structure of the item table for a given mail id
640 * @param integer $mail_id
645 public static function ItemArrayFromMail($mail_id)
647 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
649 $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
651 // Making the post more compatible for Mastodon by:
652 // - Making it a note and not an article (no title)
653 // - Moving the title into the "summary" field that is used as a "content warning"
654 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
657 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
658 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
659 $mail['allow_gid'] = '';
660 $mail['deny_cid'] = '';
661 $mail['deny_gid'] = '';
662 $mail['private'] = true;
663 $mail['deleted'] = false;
664 $mail['edited'] = $mail['created'];
665 $mail['plink'] = $mail['uri'];
666 $mail['thr-parent'] = $reply['uri'];
667 $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
669 $mail['event-type'] = '';
670 $mail['attach'] = '';
678 * Creates an activity array for a given mail id
680 * @param integer $mail_id
681 * @param boolean $object_mode Is the activity item is used inside another object?
683 * @return array of activity
686 public static function createActivityFromMail($mail_id, $object_mode = false)
688 $mail = self::ItemArrayFromMail($mail_id);
689 $object = self::createNote($mail);
691 $object['to'] = $object['cc'];
692 unset($object['cc']);
694 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => 'test']];
697 $data = ['@context' => ActivityPub::CONTEXT];
702 $data['id'] = $mail['uri'] . '#Create';
703 $data['type'] = 'Create';
704 $data['actor'] = $mail['author-link'];
705 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
706 $data['instrument'] = self::getService();
707 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
709 if (empty($data['to']) && !empty($data['cc'])) {
710 $data['to'] = $data['cc'];
713 if (empty($data['to']) && !empty($data['bcc'])) {
714 $data['to'] = $data['bcc'];
720 $object['to'] = $data['to'];
721 unset($object['cc']);
722 unset($object['bcc']);
724 $data['directMessage'] = true;
726 $data['object'] = $object;
728 $owner = User::getOwnerDataById($mail['uid']);
730 if (!$object_mode && !empty($owner)) {
731 return LDSignature::sign($data, $owner);
738 * Returns the activity type of a given item
742 * @return string with activity type
743 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
744 * @throws \ImagickException
746 private static function getTypeOfItem($item)
750 // Only check for a reshare, if it is a real reshare and no quoted reshare
751 if (strpos($item['body'], "[share") === 0) {
752 $announce = api_share_as_retweet($item);
753 $reshared = !empty($announce['plink']);
758 } elseif ($item['verb'] == ACTIVITY_POST) {
759 if ($item['created'] == $item['edited']) {
764 } elseif ($item['verb'] == ACTIVITY_LIKE) {
766 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
768 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
770 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
772 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
773 $type = 'TentativeAccept';
774 } elseif ($item['verb'] == ACTIVITY_FOLLOW) {
776 } elseif ($item['verb'] == ACTIVITY_TAG) {
786 * Creates the activity or fetches it from the cache
788 * @param integer $item_id
789 * @param boolean $force Force new cache entry
791 * @return array with the activity
794 public static function createCachedActivityFromItem($item_id, $force = false)
796 $cachekey = 'APDelivery:createActivity:' . $item_id;
799 $data = Cache::get($cachekey);
800 if (!is_null($data)) {
805 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
807 Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
812 * Creates an activity array for a given item id
814 * @param integer $item_id
815 * @param boolean $object_mode Is the activity item is used inside another object?
817 * @return array of activity
820 public static function createActivityFromItem($item_id, $object_mode = false)
822 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
824 if (!DBA::isResult($item)) {
828 if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
829 $owner = User::getOwnerDataById($item['uid']);
830 if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
833 // Disguise forum posts as reshares. Will later be converted to a real announce
834 $item['body'] = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
835 $item['guid'], $item['created'], $item['plink']) . $item['body'] . '[/share]';
840 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
841 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
842 if (DBA::isResult($conversation)) {
843 $data = json_decode($conversation['source'], true);
849 $type = self::getTypeOfItem($item);
853 $data = ['@context' => ActivityPub::CONTEXT];
855 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
857 } elseif ($item['deleted']) {
864 $data['id'] = $item['uri'] . '#' . $type;
865 $data['type'] = $type;
867 if (Item::isForumPost($item) && ($type != 'Announce')) {
868 $data['actor'] = $item['author-link'];
870 $data['actor'] = $item['owner-link'];
873 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
875 $data['instrument'] = self::getService();
877 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
879 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
880 $data['object'] = self::createNote($item);
881 } elseif ($data['type'] == 'Add') {
882 $data = self::createAddTag($item, $data);
883 } elseif ($data['type'] == 'Announce') {
884 $data = self::createAnnounce($item, $data);
885 } elseif ($data['type'] == 'Follow') {
886 $data['object'] = $item['parent-uri'];
887 } elseif ($data['type'] == 'Undo') {
888 $data['object'] = self::createActivityFromItem($item_id, true);
890 $data['diaspora:guid'] = $item['guid'];
891 if (!empty($item['signed_text'])) {
892 $data['diaspora:like'] = $item['signed_text'];
894 $data['object'] = $item['thr-parent'];
897 if (!empty($item['contact-uid'])) {
898 $uid = $item['contact-uid'];
903 $owner = User::getOwnerDataById($uid);
905 if (!$object_mode && !empty($owner)) {
906 return LDSignature::sign($data, $owner);
911 /// @todo Create "conversation" entry
915 * Creates an object array for a given item id
917 * @param integer $item_id
919 * @return array with the object data
920 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
921 * @throws \ImagickException
923 public static function createObjectFromItemID($item_id)
925 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
927 if (!DBA::isResult($item)) {
931 $data = ['@context' => ActivityPub::CONTEXT];
932 $data = array_merge($data, self::createNote($item));
938 * Creates a location entry for a given item array
942 * @return array with location array
944 private static function createLocation($item)
946 $location = ['type' => 'Place'];
948 if (!empty($item['location'])) {
949 $location['name'] = $item['location'];
954 if (empty($item['coord'])) {
955 $coord = Map::getCoordinates($item['location']);
957 $coords = explode(' ', $item['coord']);
958 if (count($coords) == 2) {
959 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
963 if (!empty($coord['lat']) && !empty($coord['lon'])) {
964 $location['latitude'] = $coord['lat'];
965 $location['longitude'] = $coord['lon'];
972 * Returns a tag array for a given item array
976 * @return array of tags
977 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
979 private static function createTagList($item)
983 $terms = Term::tagArrayFromItemId($item['id'], [Term::HASHTAG, Term::MENTION, Term::IMPLICIT_MENTION]);
984 foreach ($terms as $term) {
985 if ($term['type'] == Term::HASHTAG) {
986 $url = System::baseUrl() . '/search?tag=' . urlencode($term['term']);
987 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
988 } elseif ($term['type'] == Term::MENTION || $term['type'] == Term::IMPLICIT_MENTION) {
989 $contact = Contact::getDetailsByURL($term['url']);
990 if (!empty($contact['addr'])) {
991 $mention = '@' . $contact['addr'];
993 $mention = '@' . $term['url'];
996 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
1003 * Adds attachment data to the JSON document
1005 * @param array $item Data of the item that is to be posted
1006 * @param string $type Object type
1008 * @return array with attachment data
1009 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1011 private static function createAttachmentList($item, $type)
1015 $arr = explode('[/attach],', $item['attach']);
1017 foreach ($arr as $r) {
1019 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1021 $attributes = ['type' => 'Document',
1022 'mediaType' => $matches[3],
1023 'url' => $matches[1],
1026 if (trim($matches[4]) != '') {
1027 $attributes['name'] = trim($matches[4]);
1030 $attachments[] = $attributes;
1035 if ($type != 'Note') {
1036 return $attachments;
1039 // Simplify image codes
1040 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
1042 // Grab all pictures and create attachments out of them
1043 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
1044 foreach ($pictures[1] as $picture) {
1045 $imgdata = Image::getInfoFromURL($picture);
1047 $attachments[] = ['type' => 'Document',
1048 'mediaType' => $imgdata['mime'],
1055 return $attachments;
1059 * @brief Callback function to replace a Friendica style mention in a mention that is used on AP
1061 * @param array $match Matching values for the callback
1062 * @return string Replaced mention
1063 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1065 private static function mentionCallback($match)
1067 if (empty($match[1])) {
1071 $data = Contact::getDetailsByURL($match[1]);
1072 if (empty($data['nick'])) {
1076 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
1080 * Remove image elements and replaces them with links to the image
1082 * @param string $body
1084 * @return string with replaced elements
1086 private static function removePictures($body)
1088 // Simplify image codes
1089 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
1091 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
1092 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
1098 * Fetches the "context" value for a givem item array from the "conversation" table
1100 * @param array $item
1102 * @return string with context url
1103 * @throws \Exception
1105 private static function fetchContextURLForItem($item)
1107 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
1108 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
1109 $context_uri = $conversation['conversation-href'];
1110 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
1111 $context_uri = $conversation['conversation-uri'];
1113 $context_uri = $item['parent-uri'] . '#context';
1115 return $context_uri;
1119 * Returns if the post contains sensitive content ("nsfw")
1121 * @param integer $item_id
1124 * @throws \Exception
1126 private static function isSensitive($item_id)
1128 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
1129 return DBA::exists('term', $condition);
1133 * Creates event data
1135 * @param array $item
1137 * @return array with the event data
1138 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1140 public static function createEvent($item)
1143 $event['name'] = $item['event-summary'];
1144 $event['content'] = BBCode::convert($item['event-desc'], false, 7);
1145 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
1147 if (!$item['event-nofinish']) {
1148 $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
1151 if (!empty($item['event-location'])) {
1152 $item['location'] = $item['event-location'];
1153 $event['location'] = self::createLocation($item);
1160 * Creates a note/article object array
1162 * @param array $item
1164 * @return array with the object data
1165 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1166 * @throws \ImagickException
1168 public static function createNote($item)
1170 if ($item['event-type'] == 'event') {
1172 } elseif (!empty($item['title'])) {
1178 if ($item['deleted']) {
1179 $type = 'Tombstone';
1183 $data['id'] = $item['uri'];
1184 $data['type'] = $type;
1186 if ($item['deleted']) {
1190 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1192 if ($item['uri'] != $item['thr-parent']) {
1193 $data['inReplyTo'] = $item['thr-parent'];
1195 $data['inReplyTo'] = null;
1198 $data['diaspora:guid'] = $item['guid'];
1199 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1201 if ($item['created'] != $item['edited']) {
1202 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1205 $data['url'] = $item['plink'];
1206 $data['attributedTo'] = $item['author-link'];
1207 $data['sensitive'] = self::isSensitive($item['id']);
1208 $data['context'] = self::fetchContextURLForItem($item);
1210 if (!empty($item['title'])) {
1211 $data['name'] = BBCode::toPlaintext($item['title'], false);
1214 $permission_block = self::createPermissionBlockForItem($item, false);
1216 $body = $item['body'];
1218 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1219 $body = self::prependMentions($body, $permission_block);
1222 if ($type == 'Note') {
1223 $body = self::removePictures($body);
1224 } elseif (($type == 'Article') && empty($data['summary'])) {
1225 $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
1228 if ($type == 'Event') {
1229 $data = array_merge($data, self::createEvent($item));
1231 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1232 $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1234 $data['content'] = BBCode::convert($body, false, 7);
1237 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1239 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1240 $data['diaspora:comment'] = $item['signed_text'];
1243 $data['attachment'] = self::createAttachmentList($item, $type);
1244 $data['tag'] = self::createTagList($item);
1246 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1247 $data['location'] = self::createLocation($item);
1250 if (!empty($item['app'])) {
1251 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1254 $data = array_merge($data, $permission_block);
1260 * Creates an an "add tag" entry
1262 * @param array $item
1263 * @param array $data activity data
1265 * @return array with activity data for adding tags
1266 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1267 * @throws \ImagickException
1269 private static function createAddTag($item, $data)
1271 $object = XML::parseString($item['object'], false);
1272 $target = XML::parseString($item["target"], false);
1274 $data['diaspora:guid'] = $item['guid'];
1275 $data['actor'] = $item['author-link'];
1276 $data['target'] = (string)$target->id;
1277 $data['summary'] = BBCode::toPlaintext($item['body']);
1278 $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1284 * Creates an announce object entry
1286 * @param array $item
1287 * @param array $data activity data
1289 * @return array with activity data
1290 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1291 * @throws \ImagickException
1293 private static function createAnnounce($item, $data)
1295 $announce = api_share_as_retweet($item);
1296 if (empty($announce['plink'])) {
1297 $data['type'] = 'Create';
1298 $data['object'] = self::createNote($item);
1302 // Fetch the original id of the object
1303 $activity = ActivityPub::fetchContent($announce['plink'], $item['uid']);
1304 if (!empty($activity)) {
1305 $ldactivity = JsonLD::compact($activity);
1306 $id = JsonLD::fetchElement($ldactivity, '@id');
1308 $data['object'] = $id;
1313 $data['type'] = 'Create';
1314 $data['object'] = self::createNote($item);
1319 * Creates an activity id for a given contact id
1321 * @param integer $cid Contact ID of target
1323 * @return bool|string activity id
1325 public static function activityIDFromContact($cid)
1327 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1328 if (!DBA::isResult($contact)) {
1332 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1333 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1334 return System::baseUrl() . '/activity/' . $uuid;
1338 * Transmits a contact suggestion to a given inbox
1340 * @param integer $uid User ID
1341 * @param string $inbox Target inbox
1342 * @param integer $suggestion_id Suggestion ID
1344 * @return boolean was the transmission successful?
1345 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1347 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1349 $owner = User::getOwnerDataById($uid);
1351 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
1353 $data = ['@context' => ActivityPub::CONTEXT,
1354 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1355 'type' => 'Announce',
1356 'actor' => $owner['url'],
1357 'object' => $suggestion['url'],
1358 'content' => $suggestion['note'],
1359 'instrument' => self::getService(),
1360 'to' => [ActivityPub::PUBLIC_COLLECTION],
1363 $signed = LDSignature::sign($data, $owner);
1365 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1366 return HTTPSignature::transmit($signed, $inbox, $uid);
1370 * Transmits a profile relocation to a given inbox
1372 * @param integer $uid User ID
1373 * @param string $inbox Target inbox
1375 * @return boolean was the transmission successful?
1376 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1378 public static function sendProfileRelocation($uid, $inbox)
1380 $owner = User::getOwnerDataById($uid);
1382 $data = ['@context' => ActivityPub::CONTEXT,
1383 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1384 'type' => 'dfrn:relocate',
1385 'actor' => $owner['url'],
1386 'object' => $owner['url'],
1387 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1388 'instrument' => self::getService(),
1389 'to' => [ActivityPub::PUBLIC_COLLECTION],
1392 $signed = LDSignature::sign($data, $owner);
1394 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1395 return HTTPSignature::transmit($signed, $inbox, $uid);
1399 * Transmits a profile deletion to a given inbox
1401 * @param integer $uid User ID
1402 * @param string $inbox Target inbox
1404 * @return boolean was the transmission successful?
1405 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1407 public static function sendProfileDeletion($uid, $inbox)
1409 $owner = User::getOwnerDataById($uid);
1411 if (empty($owner)) {
1412 Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
1416 if (empty($owner['uprvkey'])) {
1417 Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
1421 $data = ['@context' => ActivityPub::CONTEXT,
1422 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1424 'actor' => $owner['url'],
1425 'object' => $owner['url'],
1426 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1427 'instrument' => self::getService(),
1428 'to' => [ActivityPub::PUBLIC_COLLECTION],
1431 $signed = LDSignature::sign($data, $owner);
1433 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1434 return HTTPSignature::transmit($signed, $inbox, $uid);
1438 * Transmits a profile change to a given inbox
1440 * @param integer $uid User ID
1441 * @param string $inbox Target inbox
1443 * @return boolean was the transmission successful?
1444 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1445 * @throws \ImagickException
1447 public static function sendProfileUpdate($uid, $inbox)
1449 $owner = User::getOwnerDataById($uid);
1450 $profile = APContact::getByURL($owner['url']);
1452 $data = ['@context' => ActivityPub::CONTEXT,
1453 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1455 'actor' => $owner['url'],
1456 'object' => self::getProfile($uid),
1457 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1458 'instrument' => self::getService(),
1459 'to' => [$profile['followers']],
1462 $signed = LDSignature::sign($data, $owner);
1464 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1465 return HTTPSignature::transmit($signed, $inbox, $uid);
1469 * Transmits a given activity to a target
1471 * @param string $activity Type name
1472 * @param string $target Target profile
1473 * @param integer $uid User ID
1475 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1476 * @throws \ImagickException
1477 * @throws \Exception
1479 public static function sendActivity($activity, $target, $uid, $id = '')
1481 $profile = APContact::getByURL($target);
1483 $owner = User::getOwnerDataById($uid);
1486 $id = System::baseUrl() . '/activity/' . System::createGUID();
1489 $data = ['@context' => ActivityPub::CONTEXT,
1491 'type' => $activity,
1492 'actor' => $owner['url'],
1493 'object' => $profile['url'],
1494 'instrument' => self::getService(),
1495 'to' => [$profile['url']]];
1497 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1499 $signed = LDSignature::sign($data, $owner);
1500 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1504 * Transmits a "follow object" activity to a target
1505 * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
1507 * @param string $object Object URL
1508 * @param string $target Target profile
1509 * @param integer $uid User ID
1511 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1512 * @throws \ImagickException
1513 * @throws \Exception
1515 public static function sendFollowObject($object, $target, $uid = 0)
1517 $profile = APContact::getByURL($target);
1520 // Fetch the list of administrators
1521 $admin_mail = explode(',', str_replace(' ', '', Config::get('config', 'admin_email')));
1523 // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
1524 $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
1525 $first_user = DBA::selectFirst('user', ['uid'], $condition);
1526 $uid = $first_user['uid'];
1529 $condition = ['verb' => ACTIVITY_FOLLOW, 'uid' => 0, 'parent-uri' => $object,
1530 'author-id' => Contact::getPublicIdByUserId($uid)];
1531 if (Item::exists($condition)) {
1532 Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
1536 $owner = User::getOwnerDataById($uid);
1538 $data = ['@context' => ActivityPub::CONTEXT,
1539 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1541 'actor' => $owner['url'],
1542 'object' => $object,
1543 'instrument' => self::getService(),
1544 'to' => [$profile['url']]];
1546 Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1548 $signed = LDSignature::sign($data, $owner);
1549 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1553 * Transmit a message that the contact request had been accepted
1555 * @param string $target Target profile
1557 * @param integer $uid User ID
1558 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1559 * @throws \ImagickException
1561 public static function sendContactAccept($target, $id, $uid)
1563 $profile = APContact::getByURL($target);
1565 $owner = User::getOwnerDataById($uid);
1566 $data = ['@context' => ActivityPub::CONTEXT,
1567 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1569 'actor' => $owner['url'],
1571 'id' => (string)$id,
1573 'actor' => $profile['url'],
1574 'object' => $owner['url']
1576 'instrument' => self::getService(),
1577 'to' => [$profile['url']]];
1579 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1581 $signed = LDSignature::sign($data, $owner);
1582 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1586 * Reject a contact request or terminates the contact relation
1588 * @param string $target Target profile
1590 * @param integer $uid User ID
1591 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1592 * @throws \ImagickException
1594 public static function sendContactReject($target, $id, $uid)
1596 $profile = APContact::getByURL($target);
1598 $owner = User::getOwnerDataById($uid);
1599 $data = ['@context' => ActivityPub::CONTEXT,
1600 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1602 'actor' => $owner['url'],
1604 'id' => (string)$id,
1606 'actor' => $profile['url'],
1607 'object' => $owner['url']
1609 'instrument' => self::getService(),
1610 'to' => [$profile['url']]];
1612 Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1614 $signed = LDSignature::sign($data, $owner);
1615 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1619 * Transmits a message that we don't want to follow this contact anymore
1621 * @param string $target Target profile
1622 * @param integer $uid User ID
1623 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1624 * @throws \ImagickException
1625 * @throws \Exception
1627 public static function sendContactUndo($target, $cid, $uid)
1629 $profile = APContact::getByURL($target);
1631 $object_id = self::activityIDFromContact($cid);
1632 if (empty($object_id)) {
1636 $id = System::baseUrl() . '/activity/' . System::createGUID();
1638 $owner = User::getOwnerDataById($uid);
1639 $data = ['@context' => ActivityPub::CONTEXT,
1642 'actor' => $owner['url'],
1643 'object' => ['id' => $object_id, 'type' => 'Follow',
1644 'actor' => $owner['url'],
1645 'object' => $profile['url']],
1646 'instrument' => self::getService(),
1647 'to' => [$profile['url']]];
1649 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1651 $signed = LDSignature::sign($data, $owner);
1652 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1655 private static function prependMentions($body, array $permission_block)
1657 if (Config::get('system', 'disable_implicit_mentions')) {
1663 foreach ($permission_block['to'] as $profile_url) {
1664 $profile = Contact::getDetailsByURL($profile_url);
1665 if (!empty($profile['addr'])
1666 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
1667 && !strstr($body, $profile['addr'])
1668 && !strstr($body, $profile_url)
1670 $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]';
1674 $mentions[] = $body;
1676 return implode(' ', $mentions);