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 * 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 * 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 * 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 * 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 * 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 * 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 * 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 * 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 * 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 * 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['diaspora:guid'] = $item['guid'];
558 $data['object'] = $item['thr-parent'];
561 $owner = User::getOwnerDataById($item['uid']);
564 return LDSignature::sign($data, $owner);
569 /// @todo Create "conversation" entry
573 * Creates an object array for a given item id
575 * @param integer $item_id
577 * @return array with the object data
579 public static function createObjectFromItemID($item_id)
581 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
583 if (!DBA::isResult($item)) {
587 $data = ['@context' => ActivityPub::CONTEXT];
588 $data = array_merge($data, self::createNote($item));
594 * Returns a tag array for a given item array
598 * @return array of tags
600 private static function createTagList($item)
604 $terms = Term::tagArrayFromItemId($item['id']);
605 foreach ($terms as $term) {
606 if ($term['type'] == TERM_HASHTAG) {
607 $tags[] = ['type' => 'Hashtag', 'href' => $term['url'], 'name' => '#' . $term['term']];
608 } elseif ($term['type'] == TERM_MENTION) {
609 $contact = Contact::getDetailsByURL($term['url']);
610 if (!empty($contact['addr'])) {
611 $mention = '@' . $contact['addr'];
613 $mention = '@' . $term['url'];
616 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
623 * Adds attachment data to the JSON document
625 * @param array $item Data of the item that is to be posted
626 * @param text $type Object type
628 * @return array with attachment data
630 private static function createAttachmentList($item, $type)
634 $arr = explode('[/attach],', $item['attach']);
636 foreach ($arr as $r) {
638 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
640 $attributes = ['type' => 'Document',
641 'mediaType' => $matches[3],
642 'url' => $matches[1],
645 if (trim($matches[4]) != '') {
646 $attributes['name'] = trim($matches[4]);
649 $attachments[] = $attributes;
654 if ($type != 'Note') {
658 // Simplify image codes
659 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
661 // Grab all pictures and create attachments out of them
662 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
663 foreach ($pictures[1] as $picture) {
664 $imgdata = Image::getInfoFromURL($picture);
666 $attachments[] = ['type' => 'Document',
667 'mediaType' => $imgdata['mime'],
678 * Remove image elements and replaces them with links to the image
680 * @param string $body
682 * @return string with replaced elements
684 private static function removePictures($body)
686 // Simplify image codes
687 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
689 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
690 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
696 * Fetches the "context" value for a givem item array from the "conversation" table
700 * @return string with context url
702 private static function fetchContextURLForItem($item)
704 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
705 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
706 $context_uri = $conversation['conversation-href'];
707 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
708 $context_uri = $conversation['conversation-uri'];
710 $context_uri = $item['parent-uri'] . '#context';
716 * Returns if the post contains sensitive content ("nsfw")
718 * @param integer $item_id
722 private static function isSensitive($item_id)
724 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
725 return DBA::exists('term', $condition);
729 * Creates a note/article object array
733 * @return array with the object data
735 public static function createNote($item)
737 if (!empty($item['title'])) {
743 if ($item['deleted']) {
748 $data['id'] = $item['uri'];
749 $data['type'] = $type;
751 if ($item['deleted']) {
755 $data['summary'] = null; // Ignore by now
757 if ($item['uri'] != $item['thr-parent']) {
758 $data['inReplyTo'] = $item['thr-parent'];
760 $data['inReplyTo'] = null;
763 $data['diaspora:guid'] = $item['guid'];
764 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
766 if ($item['created'] != $item['edited']) {
767 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
770 $data['url'] = $item['plink'];
771 $data['attributedTo'] = $item['author-link'];
772 $data['actor'] = $item['author-link'];
773 $data['sensitive'] = self::isSensitive($item['id']);
774 $data['context'] = self::fetchContextURLForItem($item);
776 if (!empty($item['title'])) {
777 $data['name'] = BBCode::toPlaintext($item['title'], false);
780 $body = $item['body'];
782 if ($type == 'Note') {
783 $body = self::removePictures($body);
786 $data['content'] = BBCode::convert($body, false, 7);
787 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
789 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
790 $data['diaspora:comment'] = $item['signed_text'];
793 $data['attachment'] = self::createAttachmentList($item, $type);
794 $data['tag'] = self::createTagList($item);
795 $data = array_merge($data, self::createPermissionBlockForItem($item));
801 * Transmits a profile deletion to a given inbox
803 * @param integer $uid User ID
804 * @param string $inbox Target inbox
806 public static function sendProfileDeletion($uid, $inbox)
808 $owner = User::getOwnerDataById($uid);
809 $profile = APContact::getByURL($owner['url']);
811 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
812 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
814 'actor' => $owner['url'],
815 'object' => $owner['url'],
816 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
817 'to' => [ActivityPub::PUBLIC_COLLECTION],
820 $signed = LDSignature::sign($data, $owner);
822 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
823 HTTPSignature::transmit($signed, $inbox, $uid);
827 * Transmits a profile change to a given inbox
829 * @param integer $uid User ID
830 * @param string $inbox Target inbox
832 public static function sendProfileUpdate($uid, $inbox)
834 $owner = User::getOwnerDataById($uid);
835 $profile = APContact::getByURL($owner['url']);
837 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
838 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
840 'actor' => $owner['url'],
841 'object' => self::getProfile($uid),
842 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
843 'to' => [$profile['followers']],
846 $signed = LDSignature::sign($data, $owner);
848 logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
849 HTTPSignature::transmit($signed, $inbox, $uid);
853 * Transmits a given activity to a target
855 * @param array $activity
856 * @param string $target Target profile
857 * @param integer $uid User ID
859 public static function sendActivity($activity, $target, $uid)
861 $profile = APContact::getByURL($target);
863 $owner = User::getOwnerDataById($uid);
865 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
866 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
868 'actor' => $owner['url'],
869 'object' => $profile['url'],
870 'to' => $profile['url']];
872 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
874 $signed = LDSignature::sign($data, $owner);
875 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
879 * Transmit a message that the contact request had been accepted
881 * @param string $target Target profile
883 * @param integer $uid User ID
885 public static function sendContactAccept($target, $id, $uid)
887 $profile = APContact::getByURL($target);
889 $owner = User::getOwnerDataById($uid);
890 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
891 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
893 'actor' => $owner['url'],
894 'object' => ['id' => $id, 'type' => 'Follow',
895 'actor' => $profile['url'],
896 'object' => $owner['url']],
897 'to' => $profile['url']];
899 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
901 $signed = LDSignature::sign($data, $owner);
902 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
906 * Reject a contact request or terminates the contact relation
908 * @param string $target Target profile
910 * @param integer $uid User ID
912 public static function sendContactReject($target, $id, $uid)
914 $profile = APContact::getByURL($target);
916 $owner = User::getOwnerDataById($uid);
917 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
918 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
920 'actor' => $owner['url'],
921 'object' => ['id' => $id, 'type' => 'Follow',
922 'actor' => $profile['url'],
923 'object' => $owner['url']],
924 'to' => $profile['url']];
926 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
928 $signed = LDSignature::sign($data, $owner);
929 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
933 * Transmits a message that we don't want to follow this contact anymore
935 * @param string $target Target profile
936 * @param integer $uid User ID
938 public static function sendContactUndo($target, $uid)
940 $profile = APContact::getByURL($target);
942 $id = System::baseUrl() . '/activity/' . System::createGUID();
944 $owner = User::getOwnerDataById($uid);
945 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
948 'actor' => $owner['url'],
949 'object' => ['id' => $id, 'type' => 'Follow',
950 'actor' => $owner['url'],
951 'object' => $profile['url']],
952 'to' => $profile['url']];
954 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
956 $signed = LDSignature::sign($data, $owner);
957 HTTPSignature::transmit($signed, $profile['inbox'], $uid);