3 * @file src/Protocol/ActivityPub/Transmitter.php
5 namespace Friendica\Protocol\ActivityPub;
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\Util\HTTPSignature;
10 use Friendica\Core\Protocol;
11 use Friendica\Model\Conversation;
12 use Friendica\Model\Contact;
13 use Friendica\Model\APContact;
14 use Friendica\Model\Item;
15 use Friendica\Model\Term;
16 use Friendica\Model\User;
17 use Friendica\Util\DateTimeFormat;
18 use Friendica\Content\Text\BBCode;
19 use Friendica\Util\JsonLD;
20 use Friendica\Util\LDSignature;
21 use Friendica\Model\Profile;
22 use Friendica\Core\Config;
23 use Friendica\Object\Image;
24 use Friendica\Protocol\ActivityPub;
25 use Friendica\Core\Cache;
28 * @brief ActivityPub Transmitter Protocol class
32 * Missing object types:
35 * Complicated object types:
40 * - Queueing unsucessful deliveries
45 * @brief collects the lost of followers of the given owner
47 * @param array $owner Owner array
48 * @param integer $page Page number
50 * @return array of owners
52 public static function getFollowers($owner, $page = null)
54 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
55 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
56 $count = DBA::count('contact', $condition);
58 $data = ['@context' => ActivityPub::CONTEXT];
59 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
60 $data['type'] = 'OrderedCollection';
61 $data['totalItems'] = $count;
63 // When we hide our friends we will only show the pure number but don't allow more.
64 $profile = Profile::getByUID($owner['uid']);
65 if (!empty($profile['hide-friends'])) {
70 $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
74 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
75 while ($contact = DBA::fetch($contacts)) {
76 $list[] = $contact['url'];
80 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
83 $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
85 $data['orderedItems'] = $list;
92 * @brief Create list of following contacts
94 * @param array $owner Owner array
95 * @param integer $page Page numbe
97 * @return array of following contacts
99 public static function getFollowing($owner, $page = null)
101 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
102 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
103 $count = DBA::count('contact', $condition);
105 $data = ['@context' => ActivityPub::CONTEXT];
106 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
107 $data['type'] = 'OrderedCollection';
108 $data['totalItems'] = $count;
110 // When we hide our friends we will only show the pure number but don't allow more.
111 $profile = Profile::getByUID($owner['uid']);
112 if (!empty($profile['hide-friends'])) {
117 $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
121 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
122 while ($contact = DBA::fetch($contacts)) {
123 $list[] = $contact['url'];
127 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
130 $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
132 $data['orderedItems'] = $list;
139 * @brief Public posts for the given owner
141 * @param array $owner Owner array
142 * @param integer $page Page numbe
144 * @return array of posts
146 public static function getOutbox($owner, $page = null)
148 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
150 $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact,
151 'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
152 'deleted' => false, 'visible' => true];
153 $count = DBA::count('item', $condition);
155 $data = ['@context' => ActivityPub::CONTEXT];
156 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
157 $data['type'] = 'OrderedCollection';
158 $data['totalItems'] = $count;
161 $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
165 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
167 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
168 while ($item = Item::fetch($items)) {
169 $object = self::createObjectFromItemID($item['id']);
170 unset($object['@context']);
175 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
178 $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
180 $data['orderedItems'] = $list;
187 * Return the ActivityPub profile of the given user
189 * @param integer $uid User ID
190 * @return array with profile data
192 public static function getProfile($uid)
194 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
195 'account_removed' => false, 'verified' => true];
196 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
197 $user = DBA::selectFirst('user', $fields, $condition);
198 if (!DBA::isResult($user)) {
202 $fields = ['locality', 'region', 'country-name'];
203 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
204 if (!DBA::isResult($profile)) {
208 $fields = ['name', 'url', 'location', 'about', 'avatar'];
209 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
210 if (!DBA::isResult($contact)) {
214 $data = ['@context' => ActivityPub::CONTEXT];
215 $data['id'] = $contact['url'];
216 $data['diaspora:guid'] = $user['guid'];
217 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
218 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
219 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
220 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
221 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
222 $data['preferredUsername'] = $user['nickname'];
223 $data['name'] = $contact['name'];
224 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
225 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
226 $data['summary'] = $contact['about'];
227 $data['url'] = $contact['url'];
228 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
229 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
230 'owner' => $contact['url'],
231 'publicKeyPem' => $user['pubkey']];
232 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
233 $data['icon'] = ['type' => 'Image',
234 'url' => $contact['avatar']];
236 // tags: https://kitty.town/@inmysocks/100656097926961126.json
241 * @brief Returns an array with permissions of a given item array
245 * @return array with permissions
247 private static function fetchPermissionBlockFromConversation($item)
249 if (empty($item['thr-parent'])) {
253 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
254 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
255 if (!DBA::isResult($conversation)) {
259 $activity = json_decode($conversation['source'], true);
261 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
262 $profile = APContact::getByURL($actor);
264 $item_profile = APContact::getByURL($item['author-link']);
265 $exclude[] = $item['author-link'];
267 if ($item['gravity'] == GRAVITY_PARENT) {
268 $exclude[] = $item['owner-link'];
271 $permissions['to'][] = $actor;
273 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
274 if (empty($activity[$element])) {
277 if (is_string($activity[$element])) {
278 $activity[$element] = [$activity[$element]];
281 foreach ($activity[$element] as $receiver) {
282 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
283 $receiver = $item_profile['followers'];
285 if (!in_array($receiver, $exclude)) {
286 $permissions[$element][] = $receiver;
294 * @brief Creates an array of permissions from an item thread
298 * @return array with permission data
300 private static function createPermissionBlockForItem($item)
302 $data = ['to' => [], 'cc' => []];
304 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
306 $actor_profile = APContact::getByURL($item['author-link']);
308 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
310 $contacts[$item['author-link']] = $item['author-link'];
312 if (!$item['private']) {
313 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
314 if (!empty($actor_profile['followers'])) {
315 $data['cc'][] = $actor_profile['followers'];
318 foreach ($terms as $term) {
319 $profile = APContact::getByURL($term['url'], false);
320 if (!empty($profile) && empty($contacts[$profile['url']])) {
321 $data['cc'][] = $profile['url'];
322 $contacts[$profile['url']] = $profile['url'];
326 $receiver_list = Item::enumeratePermissions($item);
330 foreach ($terms as $term) {
331 $cid = Contact::getIdForURL($term['url'], $item['uid']);
332 if (!empty($cid) && in_array($cid, $receiver_list)) {
333 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
334 $data['to'][] = $contact['url'];
335 $contacts[$contact['url']] = $contact['url'];
339 foreach ($receiver_list as $receiver) {
340 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
341 if (empty($contacts[$contact['url']])) {
342 $data['cc'][] = $contact['url'];
343 $contacts[$contact['url']] = $contact['url'];
348 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
349 while ($parent = Item::fetch($parents)) {
350 // Don't include data from future posts
351 if ($parent['id'] >= $item['id']) {
355 $profile = APContact::getByURL($parent['author-link'], false);
356 if (!empty($profile) && empty($contacts[$profile['url']])) {
357 $data['cc'][] = $profile['url'];
358 $contacts[$profile['url']] = $profile['url'];
361 if ($item['gravity'] != GRAVITY_PARENT) {
365 $profile = APContact::getByURL($parent['owner-link'], false);
366 if (!empty($profile) && empty($contacts[$profile['url']])) {
367 $data['cc'][] = $profile['url'];
368 $contacts[$profile['url']] = $profile['url'];
371 DBA::close($parents);
373 if (empty($data['to'])) {
374 $data['to'] = $data['cc'];
382 * @brief Fetches a list of inboxes of followers of a given user
384 * @param integer $uid User ID
386 * @return array of follower inboxes
388 public static function fetchTargetInboxesforUser($uid)
392 $condition = ['uid' => $uid, 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
395 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
398 $contacts = DBA::select('contact', ['notify', 'batch'], $condition);
399 while ($contact = DBA::fetch($contacts)) {
400 $contact = defaults($contact, 'batch', $contact['notify']);
401 $inboxes[$contact] = $contact;
403 DBA::close($contacts);
409 * @brief Fetches an array of inboxes for the given item and user
412 * @param integer $uid User ID
414 * @return array with inboxes
416 public static function fetchTargetInboxes($item, $uid)
418 $permissions = self::createPermissionBlockForItem($item);
419 if (empty($permissions)) {
425 if ($item['gravity'] == GRAVITY_ACTIVITY) {
426 $item_profile = APContact::getByURL($item['author-link']);
428 $item_profile = APContact::getByURL($item['owner-link']);
431 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
432 if (empty($permissions[$element])) {
436 foreach ($permissions[$element] as $receiver) {
437 if ($receiver == $item_profile['followers']) {
438 $inboxes = self::fetchTargetInboxesforUser($uid);
440 $profile = APContact::getByURL($receiver);
441 if (!empty($profile)) {
442 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
443 $inboxes[$target] = $target;
453 * @brief Returns the activity type of a given item
457 * @return string with activity type
459 private static function getTypeOfItem($item)
461 if ($item['verb'] == ACTIVITY_POST) {
462 if ($item['created'] == $item['edited']) {
467 } elseif ($item['verb'] == ACTIVITY_LIKE) {
469 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
471 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
473 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
475 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
476 $type = 'TentativeAccept';
485 * @brief Creates the activity or fetches it from the cache
487 * @param integer $item_id
489 * @return array with the activity
491 public static function createCachedActivityFromItem($item_id)
493 $cachekey = "APDelivery:createActivity:".$item_id;
494 $data = Cache::get($cachekey);
495 if (!is_null($data)) {
499 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
501 Cache::set($cachekey, $data, CACHE_QUARTER_HOUR);
506 * @brief Creates an activity array for a given item id
508 * @param integer $item_id
509 * @param boolean $object_mode Is the activity item is used inside another object?
511 * @return array of activity
513 public static function createActivityFromItem($item_id, $object_mode = false)
515 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
517 if (!DBA::isResult($item)) {
521 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
522 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
523 if (DBA::isResult($conversation)) {
524 $data = json_decode($conversation['source']);
530 $type = self::getTypeOfItem($item);
533 $data = ['@context' => ActivityPub::CONTEXT];
535 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
537 } elseif ($item['deleted']) {
544 $data['id'] = $item['uri'] . '#' . $type;
545 $data['type'] = $type;
546 $data['actor'] = $item['owner-link'];
548 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
550 $data = array_merge($data, self::createPermissionBlockForItem($item));
552 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
553 $data['object'] = self::createNote($item);
554 } elseif ($data['type'] == 'Undo') {
555 $data['object'] = self::createActivityFromItem($item_id, true);
557 $data['object'] = $item['thr-parent'];
560 $owner = User::getOwnerDataById($item['uid']);
563 return LDSignature::sign($data, $owner);
568 /// @todo Create "conversation" entry
572 * @brief Creates an object array for a given item id
574 * @param integer $item_id
576 * @return array with the object data
578 public static function createObjectFromItemID($item_id)
580 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
582 if (!DBA::isResult($item)) {
586 $data = ['@context' => ActivityPub::CONTEXT];
587 $data = array_merge($data, self::createNote($item));
593 * @brief Returns a tag array for a given item array
597 * @return array of tags
599 private static function createTagList($item)
603 $terms = Term::tagArrayFromItemId($item['id']);
604 foreach ($terms as $term) {
605 if ($term['type'] == TERM_HASHTAG) {
606 $tags[] = ['type' => 'Hashtag', 'href' => $term['url'], 'name' => '#' . $term['term']];
607 } elseif ($term['type'] == TERM_MENTION) {
608 $contact = Contact::getDetailsByURL($term['url']);
609 if (!empty($contact['addr'])) {
610 $mention = '@' . $contact['addr'];
612 $mention = '@' . $term['url'];
615 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
622 * @brief Adds attachment data to the JSON document
624 * @param array $item Data of the item that is to be posted
625 * @param text $type Object type
627 * @return array with attachment data
629 private static function createAttachmentList($item, $type)
633 $arr = explode('[/attach],', $item['attach']);
635 foreach ($arr as $r) {
637 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
639 $attributes = ['type' => 'Document',
640 'mediaType' => $matches[3],
641 'url' => $matches[1],
644 if (trim($matches[4]) != '') {
645 $attributes['name'] = trim($matches[4]);
648 $attachments[] = $attributes;
653 if ($type != 'Note') {
657 // Simplify image codes
658 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
660 // Grab all pictures and create attachments out of them
661 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
662 foreach ($pictures[1] as $picture) {
663 $imgdata = Image::getInfoFromURL($picture);
665 $attachments[] = ['type' => 'Document',
666 'mediaType' => $imgdata['mime'],
677 * @brief Remove image elements and replaces them with links to the image
679 * @param string $body
681 * @return string with replaced elements
683 private static function removePictures($body)
685 // Simplify image codes
686 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
688 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
689 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
695 * @brief Fetches the "context" value for a givem item array from the "conversation" table
699 * @return string with context url
701 private static function fetchContextURLForItem($item)
703 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
704 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
705 $context_uri = $conversation['conversation-href'];
706 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
707 $context_uri = $conversation['conversation-uri'];
709 $context_uri = $item['parent-uri'] . '#context';
715 * @brief Returns if the post contains sensitive content ("nsfw")
717 * @param integer $item_id
721 private static function isSensitive($item_id)
723 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
724 return DBA::exists('term', $condition);
728 * @brief Creates a note/article object array
732 * @return array with the object data
734 public static function createNote($item)
736 if (!empty($item['title'])) {
742 if ($item['deleted']) {
747 $data['id'] = $item['uri'];
748 $data['type'] = $type;
750 if ($item['deleted']) {
754 $data['summary'] = null; // Ignore by now
756 if ($item['uri'] != $item['thr-parent']) {
757 $data['inReplyTo'] = $item['thr-parent'];
759 $data['inReplyTo'] = null;
762 $data['diaspora:guid'] = $item['guid'];
763 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
765 if ($item['created'] != $item['edited']) {
766 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
769 $data['url'] = $item['plink'];
770 $data['attributedTo'] = $item['author-link'];
771 $data['actor'] = $item['author-link'];
772 $data['sensitive'] = self::isSensitive($item['id']);
773 $data['context'] = self::fetchContextURLForItem($item);
775 if (!empty($item['title'])) {
776 $data['name'] = BBCode::convert($item['title'], false, 7);
779 $body = $item['body'];
781 if ($type == 'Note') {
782 $body = self::removePictures($body);
785 $data['content'] = BBCode::convert($body, false, 7);
786 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
788 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
789 $data['diaspora:comment'] = $item['signed_text'];
792 $data['attachment'] = self::createAttachmentList($item, $type);
793 $data['tag'] = self::createTagList($item);
794 $data = array_merge($data, self::createPermissionBlockForItem($item));
800 * @brief Transmits a profile deletion to a given inbox
802 * @param integer $uid User ID
803 * @param string $inbox Target inbox
805 public static function sendProfileDeletion($uid, $inbox)
807 $owner = User::getOwnerDataById($uid);
808 $profile = APContact::getByURL($owner['url']);
810 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
811 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
813 'actor' => $owner['url'],
814 'object' => $owner['url'],
815 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
816 'to' => [ActivityPub::PUBLIC_COLLECTION],
819 $signed = LDSignature::sign($data, $owner);
821 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
822 HTTPSignature::transmit($signed, $inbox, $uid);
826 * @brief Transmits a profile change to a given inbox
828 * @param integer $uid User ID
829 * @param string $inbox Target inbox
831 public static function sendProfileUpdate($uid, $inbox)
833 $owner = User::getOwnerDataById($uid);
834 $profile = APContact::getByURL($owner['url']);
836 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
837 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
839 'actor' => $owner['url'],
840 'object' => self::getProfile($uid),
841 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
842 'to' => [$profile['followers']],
845 $signed = LDSignature::sign($data, $owner);
847 logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
848 HTTPSignature::transmit($signed, $inbox, $uid);
852 * @brief Transmits a given activity to a target
854 * @param array $activity
855 * @param string $target Target profile
856 * @param integer $uid User ID
858 public static function sendActivity($activity, $target, $uid)
860 $profile = APContact::getByURL($target);
862 $owner = User::getOwnerDataById($uid);
864 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
865 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
867 'actor' => $owner['url'],
868 'object' => $profile['url'],
869 'to' => $profile['url']];
871 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
873 $signed = LDSignature::sign($data, $owner);
874 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
878 * @brief Transmit a message that the contact request had been accepted
880 * @param string $target Target profile
882 * @param integer $uid User ID
884 public static function sendContactAccept($target, $id, $uid)
886 $profile = APContact::getByURL($target);
888 $owner = User::getOwnerDataById($uid);
889 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
890 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
892 'actor' => $owner['url'],
893 'object' => ['id' => $id, 'type' => 'Follow',
894 'actor' => $profile['url'],
895 'object' => $owner['url']],
896 'to' => $profile['url']];
898 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
900 $signed = LDSignature::sign($data, $owner);
901 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
905 * @brief Reject a contact request or terminates the contact relation
907 * @param string $target Target profile
909 * @param integer $uid User ID
911 public static function sendContactReject($target, $id, $uid)
913 $profile = APContact::getByURL($target);
915 $owner = User::getOwnerDataById($uid);
916 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
917 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
919 'actor' => $owner['url'],
920 'object' => ['id' => $id, 'type' => 'Follow',
921 'actor' => $profile['url'],
922 'object' => $owner['url']],
923 'to' => $profile['url']];
925 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
927 $signed = LDSignature::sign($data, $owner);
928 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
932 * @brief Transmits a message that we don't want to follow this contact anymore
934 * @param string $target Target profile
935 * @param integer $uid User ID
937 public static function sendContactUndo($target, $uid)
939 $profile = APContact::getByURL($target);
941 $id = System::baseUrl() . '/activity/' . System::createGUID();
943 $owner = User::getOwnerDataById($uid);
944 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
947 'actor' => $owner['url'],
948 'object' => ['id' => $id, 'type' => 'Follow',
949 'actor' => $owner['url'],
950 'object' => $profile['url']],
951 'to' => $profile['url']];
953 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
955 $signed = LDSignature::sign($data, $owner);
956 HTTPSignature::transmit($signed, $profile['inbox'], $uid);