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 fields:
36 * Missing object types:
39 * Complicated object types:
44 * - Queueing unsucessful deliveries
49 * collects the lost of followers of the given owner
51 * @param array $owner Owner array
52 * @param integer $page Page number
54 * @return array of owners
56 public static function getFollowers($owner, $page = null)
58 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
59 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
60 $count = DBA::count('contact', $condition);
62 $data = ['@context' => ActivityPub::CONTEXT];
63 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
64 $data['type'] = 'OrderedCollection';
65 $data['totalItems'] = $count;
67 // When we hide our friends we will only show the pure number but don't allow more.
68 $profile = Profile::getByUID($owner['uid']);
69 if (!empty($profile['hide-friends'])) {
74 $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
78 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
79 while ($contact = DBA::fetch($contacts)) {
80 $list[] = $contact['url'];
84 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
87 $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
89 $data['orderedItems'] = $list;
96 * Create list of following contacts
98 * @param array $owner Owner array
99 * @param integer $page Page numbe
101 * @return array of following contacts
103 public static function getFollowing($owner, $page = null)
105 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
106 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
107 $count = DBA::count('contact', $condition);
109 $data = ['@context' => ActivityPub::CONTEXT];
110 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
111 $data['type'] = 'OrderedCollection';
112 $data['totalItems'] = $count;
114 // When we hide our friends we will only show the pure number but don't allow more.
115 $profile = Profile::getByUID($owner['uid']);
116 if (!empty($profile['hide-friends'])) {
121 $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
125 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
126 while ($contact = DBA::fetch($contacts)) {
127 $list[] = $contact['url'];
131 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
134 $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
136 $data['orderedItems'] = $list;
143 * Public posts for the given owner
145 * @param array $owner Owner array
146 * @param integer $page Page numbe
148 * @return array of posts
150 public static function getOutbox($owner, $page = null)
152 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
154 $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact,
155 'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
156 'deleted' => false, 'visible' => true];
157 $count = DBA::count('item', $condition);
159 $data = ['@context' => ActivityPub::CONTEXT];
160 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
161 $data['type'] = 'OrderedCollection';
162 $data['totalItems'] = $count;
165 $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
169 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
171 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
172 while ($item = Item::fetch($items)) {
173 $object = self::createObjectFromItemID($item['id']);
174 unset($object['@context']);
179 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
182 $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
184 $data['orderedItems'] = $list;
191 * Return the ActivityPub profile of the given user
193 * @param integer $uid User ID
194 * @return array with profile data
196 public static function getProfile($uid)
198 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
199 'account_removed' => false, 'verified' => true];
200 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
201 $user = DBA::selectFirst('user', $fields, $condition);
202 if (!DBA::isResult($user)) {
206 $fields = ['locality', 'region', 'country-name'];
207 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
208 if (!DBA::isResult($profile)) {
212 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
213 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
214 if (!DBA::isResult($contact)) {
218 // On old installations and never changed contacts this might not be filled
219 if (empty($contact['avatar'])) {
220 $contact['avatar'] = $contact['photo'];
223 $data = ['@context' => ActivityPub::CONTEXT];
224 $data['id'] = $contact['url'];
225 $data['diaspora:guid'] = $user['guid'];
226 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
227 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
228 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
229 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
230 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
231 $data['preferredUsername'] = $user['nickname'];
232 $data['name'] = $contact['name'];
233 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
234 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
235 $data['summary'] = $contact['about'];
236 $data['url'] = $contact['url'];
237 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
238 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
239 'owner' => $contact['url'],
240 'publicKeyPem' => $user['pubkey']];
241 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
242 $data['icon'] = ['type' => 'Image',
243 'url' => $contact['avatar']];
245 // tags: https://kitty.town/@inmysocks/100656097926961126.json
250 * Returns an array with permissions of a given item array
254 * @return array with permissions
256 private static function fetchPermissionBlockFromConversation($item)
258 if (empty($item['thr-parent'])) {
262 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
263 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
264 if (!DBA::isResult($conversation)) {
268 $activity = json_decode($conversation['source'], true);
270 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
271 $profile = APContact::getByURL($actor);
273 $item_profile = APContact::getByURL($item['author-link']);
274 $exclude[] = $item['author-link'];
276 if ($item['gravity'] == GRAVITY_PARENT) {
277 $exclude[] = $item['owner-link'];
280 $permissions['to'][] = $actor;
282 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
283 if (empty($activity[$element])) {
286 if (is_string($activity[$element])) {
287 $activity[$element] = [$activity[$element]];
290 foreach ($activity[$element] as $receiver) {
291 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
292 $receiver = $item_profile['followers'];
294 if (!in_array($receiver, $exclude)) {
295 $permissions[$element][] = $receiver;
303 * Creates an array of permissions from an item thread
307 * @return array with permission data
309 private static function createPermissionBlockForItem($item)
311 $data = ['to' => [], 'cc' => []];
313 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
315 $actor_profile = APContact::getByURL($item['author-link']);
317 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
319 $contacts[$item['author-link']] = $item['author-link'];
321 if (!$item['private']) {
322 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
323 if (!empty($actor_profile['followers'])) {
324 $data['cc'][] = $actor_profile['followers'];
327 foreach ($terms as $term) {
328 $profile = APContact::getByURL($term['url'], false);
329 if (!empty($profile) && empty($contacts[$profile['url']])) {
330 $data['cc'][] = $profile['url'];
331 $contacts[$profile['url']] = $profile['url'];
335 $receiver_list = Item::enumeratePermissions($item);
339 foreach ($terms as $term) {
340 $cid = Contact::getIdForURL($term['url'], $item['uid']);
341 if (!empty($cid) && in_array($cid, $receiver_list)) {
342 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
343 $data['to'][] = $contact['url'];
344 $contacts[$contact['url']] = $contact['url'];
348 foreach ($receiver_list as $receiver) {
349 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
350 if (empty($contacts[$contact['url']])) {
351 $data['cc'][] = $contact['url'];
352 $contacts[$contact['url']] = $contact['url'];
357 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
358 while ($parent = Item::fetch($parents)) {
359 // Don't include data from future posts
360 if ($parent['id'] >= $item['id']) {
364 $profile = APContact::getByURL($parent['author-link'], false);
365 if (!empty($profile) && empty($contacts[$profile['url']])) {
366 $data['cc'][] = $profile['url'];
367 $contacts[$profile['url']] = $profile['url'];
370 if ($item['gravity'] != GRAVITY_PARENT) {
374 $profile = APContact::getByURL($parent['owner-link'], false);
375 if (!empty($profile) && empty($contacts[$profile['url']])) {
376 $data['cc'][] = $profile['url'];
377 $contacts[$profile['url']] = $profile['url'];
380 DBA::close($parents);
382 if (empty($data['to'])) {
383 $data['to'] = $data['cc'];
391 * Fetches a list of inboxes of followers of a given user
393 * @param integer $uid User ID
395 * @return array of follower inboxes
397 public static function fetchTargetInboxesforUser($uid)
401 $condition = ['uid' => $uid, 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
404 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
407 $contacts = DBA::select('contact', ['notify', 'batch'], $condition);
408 while ($contact = DBA::fetch($contacts)) {
409 $contact = defaults($contact, 'batch', $contact['notify']);
410 $inboxes[$contact] = $contact;
412 DBA::close($contacts);
418 * Fetches an array of inboxes for the given item and user
421 * @param integer $uid User ID
423 * @return array with inboxes
425 public static function fetchTargetInboxes($item, $uid)
427 $permissions = self::createPermissionBlockForItem($item);
428 if (empty($permissions)) {
434 if ($item['gravity'] == GRAVITY_ACTIVITY) {
435 $item_profile = APContact::getByURL($item['author-link']);
437 $item_profile = APContact::getByURL($item['owner-link']);
440 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
441 if (empty($permissions[$element])) {
445 foreach ($permissions[$element] as $receiver) {
446 if ($receiver == $item_profile['followers']) {
447 $inboxes = self::fetchTargetInboxesforUser($uid);
449 $profile = APContact::getByURL($receiver);
450 if (!empty($profile)) {
451 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
452 $inboxes[$target] = $target;
462 * Returns the activity type of a given item
466 * @return string with activity type
468 private static function getTypeOfItem($item)
470 if ($item['verb'] == ACTIVITY_POST) {
471 if ($item['created'] == $item['edited']) {
476 } elseif ($item['verb'] == ACTIVITY_LIKE) {
478 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
480 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
482 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
484 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
485 $type = 'TentativeAccept';
494 * Creates the activity or fetches it from the cache
496 * @param integer $item_id
498 * @return array with the activity
500 public static function createCachedActivityFromItem($item_id)
502 $cachekey = 'APDelivery:createActivity:' . $item_id;
503 $data = Cache::get($cachekey);
504 if (!is_null($data)) {
508 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
510 Cache::set($cachekey, $data, CACHE_QUARTER_HOUR);
515 * Creates an activity array for a given item id
517 * @param integer $item_id
518 * @param boolean $object_mode Is the activity item is used inside another object?
520 * @return array of activity
522 public static function createActivityFromItem($item_id, $object_mode = false)
524 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
526 if (!DBA::isResult($item)) {
530 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
531 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
532 if (DBA::isResult($conversation)) {
533 $data = json_decode($conversation['source']);
539 $type = self::getTypeOfItem($item);
542 $data = ['@context' => ActivityPub::CONTEXT];
544 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
546 } elseif ($item['deleted']) {
553 $data['id'] = $item['uri'] . '#' . $type;
554 $data['type'] = $type;
555 $data['actor'] = $item['owner-link'];
557 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
559 $data = array_merge($data, self::createPermissionBlockForItem($item));
561 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
562 $data['object'] = self::createNote($item);
563 } elseif ($data['type'] == 'Undo') {
564 $data['object'] = self::createActivityFromItem($item_id, true);
566 $data['diaspora:guid'] = $item['guid'];
567 $data['object'] = $item['thr-parent'];
570 $owner = User::getOwnerDataById($item['uid']);
573 return LDSignature::sign($data, $owner);
578 /// @todo Create "conversation" entry
582 * Creates an object array for a given item id
584 * @param integer $item_id
586 * @return array with the object data
588 public static function createObjectFromItemID($item_id)
590 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
592 if (!DBA::isResult($item)) {
596 $data = ['@context' => ActivityPub::CONTEXT];
597 $data = array_merge($data, self::createNote($item));
603 * Returns a tag array for a given item array
607 * @return array of tags
609 private static function createTagList($item)
613 $terms = Term::tagArrayFromItemId($item['id']);
614 foreach ($terms as $term) {
615 if ($term['type'] == TERM_HASHTAG) {
616 $tags[] = ['type' => 'Hashtag', 'href' => $term['url'], 'name' => '#' . $term['term']];
617 } elseif ($term['type'] == TERM_MENTION) {
618 $contact = Contact::getDetailsByURL($term['url']);
619 if (!empty($contact['addr'])) {
620 $mention = '@' . $contact['addr'];
622 $mention = '@' . $term['url'];
625 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
632 * Adds attachment data to the JSON document
634 * @param array $item Data of the item that is to be posted
635 * @param text $type Object type
637 * @return array with attachment data
639 private static function createAttachmentList($item, $type)
643 $arr = explode('[/attach],', $item['attach']);
645 foreach ($arr as $r) {
647 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
649 $attributes = ['type' => 'Document',
650 'mediaType' => $matches[3],
651 'url' => $matches[1],
654 if (trim($matches[4]) != '') {
655 $attributes['name'] = trim($matches[4]);
658 $attachments[] = $attributes;
663 if ($type != 'Note') {
667 // Simplify image codes
668 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
670 // Grab all pictures and create attachments out of them
671 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
672 foreach ($pictures[1] as $picture) {
673 $imgdata = Image::getInfoFromURL($picture);
675 $attachments[] = ['type' => 'Document',
676 'mediaType' => $imgdata['mime'],
687 * Remove image elements and replaces them with links to the image
689 * @param string $body
691 * @return string with replaced elements
693 private static function removePictures($body)
695 // Simplify image codes
696 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
698 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
699 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
705 * Fetches the "context" value for a givem item array from the "conversation" table
709 * @return string with context url
711 private static function fetchContextURLForItem($item)
713 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
714 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
715 $context_uri = $conversation['conversation-href'];
716 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
717 $context_uri = $conversation['conversation-uri'];
719 $context_uri = $item['parent-uri'] . '#context';
725 * Returns if the post contains sensitive content ("nsfw")
727 * @param integer $item_id
731 private static function isSensitive($item_id)
733 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
734 return DBA::exists('term', $condition);
738 * Creates a note/article object array
742 * @return array with the object data
744 public static function createNote($item)
746 if (!empty($item['title'])) {
752 if ($item['deleted']) {
757 $data['id'] = $item['uri'];
758 $data['type'] = $type;
760 if ($item['deleted']) {
764 $data['summary'] = null; // Ignore by now
766 if ($item['uri'] != $item['thr-parent']) {
767 $data['inReplyTo'] = $item['thr-parent'];
769 $data['inReplyTo'] = null;
772 $data['diaspora:guid'] = $item['guid'];
773 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
775 if ($item['created'] != $item['edited']) {
776 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
779 $data['url'] = $item['plink'];
780 $data['attributedTo'] = $item['author-link'];
781 $data['actor'] = $item['author-link'];
782 $data['sensitive'] = self::isSensitive($item['id']);
783 $data['context'] = self::fetchContextURLForItem($item);
785 if (!empty($item['title'])) {
786 $data['name'] = BBCode::toPlaintext($item['title'], false);
789 $body = $item['body'];
791 if ($type == 'Note') {
792 $body = self::removePictures($body);
795 $data['content'] = BBCode::convert($body, false, 7);
796 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
798 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
799 $data['diaspora:comment'] = $item['signed_text'];
802 $data['attachment'] = self::createAttachmentList($item, $type);
803 $data['tag'] = self::createTagList($item);
804 $data = array_merge($data, self::createPermissionBlockForItem($item));
810 * Transmits a contact suggestion to a given inbox
812 * @param integer $uid User ID
813 * @param string $inbox Target inbox
814 * @param integer $suggestion_id Suggestion ID
816 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
818 $owner = User::getOwnerDataById($uid);
819 $profile = APContact::getByURL($owner['url']);
821 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
823 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
824 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
825 'type' => 'Announce',
826 'actor' => $owner['url'],
827 'object' => $suggestion['url'],
828 'content' => $suggestion['note'],
829 'published' => DateTimeFormat::utc($suggestion['created'] . '+00:00', DateTimeFormat::ATOM),
830 'to' => [ActivityPub::PUBLIC_COLLECTION],
833 $signed = LDSignature::sign($data, $owner);
835 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
836 HTTPSignature::transmit($signed, $inbox, $uid);
840 * Transmits a profile deletion to a given inbox
842 * @param integer $uid User ID
843 * @param string $inbox Target inbox
845 public static function sendProfileDeletion($uid, $inbox)
847 $owner = User::getOwnerDataById($uid);
848 $profile = APContact::getByURL($owner['url']);
850 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
851 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
853 'actor' => $owner['url'],
854 'object' => $owner['url'],
855 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
856 'to' => [ActivityPub::PUBLIC_COLLECTION],
859 $signed = LDSignature::sign($data, $owner);
861 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
862 HTTPSignature::transmit($signed, $inbox, $uid);
866 * Transmits a profile change to a given inbox
868 * @param integer $uid User ID
869 * @param string $inbox Target inbox
871 public static function sendProfileUpdate($uid, $inbox)
873 $owner = User::getOwnerDataById($uid);
874 $profile = APContact::getByURL($owner['url']);
876 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
877 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
879 'actor' => $owner['url'],
880 'object' => self::getProfile($uid),
881 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
882 'to' => [$profile['followers']],
885 $signed = LDSignature::sign($data, $owner);
887 logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
888 HTTPSignature::transmit($signed, $inbox, $uid);
892 * Transmits a given activity to a target
894 * @param array $activity
895 * @param string $target Target profile
896 * @param integer $uid User ID
898 public static function sendActivity($activity, $target, $uid)
900 $profile = APContact::getByURL($target);
902 $owner = User::getOwnerDataById($uid);
904 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
905 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
907 'actor' => $owner['url'],
908 'object' => $profile['url'],
909 'to' => $profile['url']];
911 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
913 $signed = LDSignature::sign($data, $owner);
914 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
918 * Transmit a message that the contact request had been accepted
920 * @param string $target Target profile
922 * @param integer $uid User ID
924 public static function sendContactAccept($target, $id, $uid)
926 $profile = APContact::getByURL($target);
928 $owner = User::getOwnerDataById($uid);
929 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
930 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
932 'actor' => $owner['url'],
933 'object' => ['id' => $id, 'type' => 'Follow',
934 'actor' => $profile['url'],
935 'object' => $owner['url']],
936 'to' => $profile['url']];
938 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
940 $signed = LDSignature::sign($data, $owner);
941 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
945 * Reject a contact request or terminates the contact relation
947 * @param string $target Target profile
949 * @param integer $uid User ID
951 public static function sendContactReject($target, $id, $uid)
953 $profile = APContact::getByURL($target);
955 $owner = User::getOwnerDataById($uid);
956 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
957 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
959 'actor' => $owner['url'],
960 'object' => ['id' => $id, 'type' => 'Follow',
961 'actor' => $profile['url'],
962 'object' => $owner['url']],
963 'to' => $profile['url']];
965 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
967 $signed = LDSignature::sign($data, $owner);
968 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
972 * Transmits a message that we don't want to follow this contact anymore
974 * @param string $target Target profile
975 * @param integer $uid User ID
977 public static function sendContactUndo($target, $uid)
979 $profile = APContact::getByURL($target);
981 $id = System::baseUrl() . '/activity/' . System::createGUID();
983 $owner = User::getOwnerDataById($uid);
984 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
987 'actor' => $owner['url'],
988 'object' => ['id' => $id, 'type' => 'Follow',
989 'actor' => $owner['url'],
990 'object' => $profile['url']],
991 'to' => $profile['url']];
993 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
995 $signed = LDSignature::sign($data, $owner);
996 HTTPSignature::transmit($signed, $profile['inbox'], $uid);