3 * @file src/Protocol/ActivityPub/Transmitter.php
5 namespace Friendica\Protocol\ActivityPub;
7 use Friendica\BaseObject;
8 use Friendica\Database\DBA;
9 use Friendica\Core\System;
10 use Friendica\Util\HTTPSignature;
11 use Friendica\Core\Protocol;
12 use Friendica\Model\Conversation;
13 use Friendica\Model\Contact;
14 use Friendica\Model\APContact;
15 use Friendica\Model\Item;
16 use Friendica\Model\Term;
17 use Friendica\Model\User;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Content\Text\BBCode;
20 use Friendica\Util\JsonLD;
21 use Friendica\Util\LDSignature;
22 use Friendica\Model\Profile;
23 use Friendica\Core\Config;
24 use Friendica\Object\Image;
25 use Friendica\Protocol\ActivityPub;
26 use Friendica\Protocol\Diaspora;
27 use Friendica\Core\Cache;
28 use Friendica\Util\Map;
30 require_once 'include/api.php';
33 * @brief ActivityPub Transmitter Protocol class
37 * Missing object types:
40 * Complicated object types:
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
53 public static function getFollowers($owner, $page = null)
55 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
56 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
57 $count = DBA::count('contact', $condition);
59 $data = ['@context' => ActivityPub::CONTEXT];
60 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
61 $data['type'] = 'OrderedCollection';
62 $data['totalItems'] = $count;
64 // When we hide our friends we will only show the pure number but don't allow more.
65 $profile = Profile::getByUID($owner['uid']);
66 if (!empty($profile['hide-friends'])) {
71 $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
75 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
76 while ($contact = DBA::fetch($contacts)) {
77 $list[] = $contact['url'];
81 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
84 $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
86 $data['orderedItems'] = $list;
93 * Create list of following contacts
95 * @param array $owner Owner array
96 * @param integer $page Page numbe
98 * @return array of following contacts
100 public static function getFollowing($owner, $page = null)
102 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
103 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
104 $count = DBA::count('contact', $condition);
106 $data = ['@context' => ActivityPub::CONTEXT];
107 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
108 $data['type'] = 'OrderedCollection';
109 $data['totalItems'] = $count;
111 // When we hide our friends we will only show the pure number but don't allow more.
112 $profile = Profile::getByUID($owner['uid']);
113 if (!empty($profile['hide-friends'])) {
118 $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
122 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
123 while ($contact = DBA::fetch($contacts)) {
124 $list[] = $contact['url'];
128 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
131 $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
133 $data['orderedItems'] = $list;
140 * Public posts for the given owner
142 * @param array $owner Owner array
143 * @param integer $page Page numbe
145 * @return array of posts
147 public static function getOutbox($owner, $page = null)
149 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
151 $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
152 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
153 'deleted' => false, 'visible' => true];
154 $count = DBA::count('item', $condition);
156 $data = ['@context' => ActivityPub::CONTEXT];
157 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
158 $data['type'] = 'OrderedCollection';
159 $data['totalItems'] = $count;
162 $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
166 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
168 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
169 while ($item = Item::fetch($items)) {
170 $object = self::createObjectFromItemID($item['id']);
171 unset($object['@context']);
176 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
179 $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
181 $data['orderedItems'] = $list;
188 * Return the ActivityPub profile of the given user
190 * @param integer $uid User ID
191 * @return array with profile data
193 public static function getProfile($uid)
195 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
196 'account_removed' => false, 'verified' => true];
197 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
198 $user = DBA::selectFirst('user', $fields, $condition);
199 if (!DBA::isResult($user)) {
203 $fields = ['locality', 'region', 'country-name'];
204 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
205 if (!DBA::isResult($profile)) {
209 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
210 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
211 if (!DBA::isResult($contact)) {
215 // On old installations and never changed contacts this might not be filled
216 if (empty($contact['avatar'])) {
217 $contact['avatar'] = $contact['photo'];
220 $data = ['@context' => ActivityPub::CONTEXT];
221 $data['id'] = $contact['url'];
222 $data['diaspora:guid'] = $user['guid'];
223 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
224 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
225 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
226 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
227 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
228 $data['preferredUsername'] = $user['nickname'];
229 $data['name'] = $contact['name'];
230 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
231 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
232 $data['summary'] = $contact['about'];
233 $data['url'] = $contact['url'];
234 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
235 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
236 'owner' => $contact['url'],
237 'publicKeyPem' => $user['pubkey']];
238 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
239 $data['icon'] = ['type' => 'Image',
240 'url' => $contact['avatar']];
242 // tags: https://kitty.town/@inmysocks/100656097926961126.json
247 * Returns an array with permissions of a given item array
251 * @return array with permissions
253 private static function fetchPermissionBlockFromConversation($item)
255 if (empty($item['thr-parent'])) {
259 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
260 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
261 if (!DBA::isResult($conversation)) {
265 $activity = json_decode($conversation['source'], true);
267 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
268 $profile = APContact::getByURL($actor);
270 $item_profile = APContact::getByURL($item['author-link']);
271 $exclude[] = $item['author-link'];
273 if ($item['gravity'] == GRAVITY_PARENT) {
274 $exclude[] = $item['owner-link'];
277 $permissions['to'][] = $actor;
279 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
280 if (empty($activity[$element])) {
283 if (is_string($activity[$element])) {
284 $activity[$element] = [$activity[$element]];
287 foreach ($activity[$element] as $receiver) {
288 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
289 $permissions[$element][] = $item_profile['followers'];
291 if (!in_array($receiver, $exclude)) {
292 $permissions[$element][] = $receiver;
300 * Creates an array of permissions from an item thread
304 * @return array with permission data
306 private static function createPermissionBlockForItem($item)
308 // Will be activated in a later step
309 // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
311 // For now only send to these contacts:
312 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
314 $data = ['to' => [], 'cc' => []];
316 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
318 $actor_profile = APContact::getByURL($item['author-link']);
320 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
322 if (!$item['private']) {
323 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
324 if (!empty($actor_profile['followers'])) {
325 $data['cc'][] = $actor_profile['followers'];
328 foreach ($terms as $term) {
329 $profile = APContact::getByURL($term['url'], false);
330 if (!empty($profile)) {
331 $data['to'][] = $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' => $networks]);
343 if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) {
344 $data['to'][] = $profile['url'];
349 foreach ($receiver_list as $receiver) {
350 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => $networks]);
351 if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) {
352 $data['cc'][] = $profile['url'];
357 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['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)) {
366 if ($parent['uri'] == $item['thr-parent']) {
367 $data['to'][] = $profile['url'];
369 $data['cc'][] = $profile['url'];
373 if ($item['gravity'] != GRAVITY_PARENT) {
377 $profile = APContact::getByURL($parent['owner-link'], false);
378 if (!empty($profile)) {
379 $data['cc'][] = $profile['url'];
382 DBA::close($parents);
384 $data['to'] = array_unique($data['to']);
385 $data['cc'] = array_unique($data['cc']);
387 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
388 unset($data['to'][$key]);
391 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
392 unset($data['cc'][$key]);
395 foreach ($data['to'] as $to) {
396 if (($key = array_search($to, $data['cc'])) !== false) {
397 unset($data['cc'][$key]);
401 return ['to' => array_values($data['to']), 'cc' => array_values($data['cc'])];
405 * Fetches a list of inboxes of followers of a given user
407 * @param integer $uid User ID
408 * @param boolean $personal fetch personal inboxes
410 * @return array of follower inboxes
412 public static function fetchTargetInboxesforUser($uid, $personal = false)
416 // Will be activated in a later step
417 // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
419 // For now only send to these contacts:
420 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
422 $condition = ['uid' => $uid, 'network' => $networks, 'archive' => false, 'pending' => false];
425 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
428 $contacts = DBA::select('contact', ['url'], $condition);
429 while ($contact = DBA::fetch($contacts)) {
430 $profile = APContact::getByURL($contact['url'], false);
431 if (!empty($profile)) {
432 if (empty($profile['sharedinbox']) || $personal) {
433 $target = $profile['inbox'];
435 $target = $profile['sharedinbox'];
437 $inboxes[$target] = $target;
440 DBA::close($contacts);
446 * Fetches an array of inboxes for the given item and user
449 * @param integer $uid User ID
450 * @param boolean $personal fetch personal inboxes
452 * @return array with inboxes
454 public static function fetchTargetInboxes($item, $uid, $personal = false)
456 $permissions = self::createPermissionBlockForItem($item);
457 if (empty($permissions)) {
463 if ($item['gravity'] == GRAVITY_ACTIVITY) {
464 $item_profile = APContact::getByURL($item['author-link'], false);
466 $item_profile = APContact::getByURL($item['owner-link'], false);
469 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
470 if (empty($permissions[$element])) {
474 foreach ($permissions[$element] as $receiver) {
475 if ($receiver == $item_profile['followers']) {
476 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
478 $profile = APContact::getByURL($receiver, false);
479 if (!empty($profile)) {
480 if (empty($profile['sharedinbox']) || $personal) {
481 $target = $profile['inbox'];
483 $target = $profile['sharedinbox'];
485 $inboxes[$target] = $target;
495 * Returns the activity type of a given item
499 * @return string with activity type
501 private static function getTypeOfItem($item)
503 if (!empty(Diaspora::isReshare($item['body'], false))) {
505 } elseif ($item['verb'] == ACTIVITY_POST) {
506 if ($item['created'] == $item['edited']) {
511 } elseif ($item['verb'] == ACTIVITY_LIKE) {
513 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
515 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
517 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
519 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
520 $type = 'TentativeAccept';
529 * Creates the activity or fetches it from the cache
531 * @param integer $item_id
533 * @return array with the activity
535 public static function createCachedActivityFromItem($item_id)
537 $cachekey = 'APDelivery:createActivity:' . $item_id;
538 $data = Cache::get($cachekey);
539 if (!is_null($data)) {
543 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
545 Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
550 * Creates an activity array for a given item id
552 * @param integer $item_id
553 * @param boolean $object_mode Is the activity item is used inside another object?
555 * @return array of activity
557 public static function createActivityFromItem($item_id, $object_mode = false)
559 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
561 if (!DBA::isResult($item)) {
565 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
566 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
567 if (DBA::isResult($conversation)) {
568 $data = json_decode($conversation['source']);
574 $type = self::getTypeOfItem($item);
577 $data = ['@context' => ActivityPub::CONTEXT];
579 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
581 } elseif ($item['deleted']) {
588 $data['id'] = $item['uri'] . '#' . $type;
589 $data['type'] = $type;
590 $data['actor'] = $item['owner-link'];
592 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
594 $data['instrument'] = ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()];
596 $data = array_merge($data, self::createPermissionBlockForItem($item));
598 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
599 $data['object'] = self::createNote($item);
600 } elseif ($data['type'] == 'Announce') {
601 $data['object'] = self::createAnnounce($item);
602 } elseif ($data['type'] == 'Undo') {
603 $data['object'] = self::createActivityFromItem($item_id, true);
605 $data['diaspora:guid'] = $item['guid'];
606 $data['object'] = $item['thr-parent'];
609 $owner = User::getOwnerDataById($item['uid']);
612 return LDSignature::sign($data, $owner);
617 /// @todo Create "conversation" entry
621 * Creates an object array for a given item id
623 * @param integer $item_id
625 * @return array with the object data
627 public static function createObjectFromItemID($item_id)
629 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
631 if (!DBA::isResult($item)) {
635 $data = ['@context' => ActivityPub::CONTEXT];
636 $data = array_merge($data, self::createNote($item));
642 * Creates a location entry for a given item array
646 * @return array with location array
648 private static function createLocation($item)
650 $location = ['type' => 'Place'];
652 if (!empty($item['location'])) {
653 $location['name'] = $item['location'];
658 if (empty($item['coord'])) {
659 $coord = Map::getCoordinates($item['location']);
661 $coords = explode(' ', $item['coord']);
662 if (count($coords) == 2) {
663 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
667 if (!empty($coord['lat']) && !empty($coord['lon'])) {
668 $location['latitude'] = $coord['lat'];
669 $location['longitude'] = $coord['lon'];
676 * Returns a tag array for a given item array
680 * @return array of tags
682 private static function createTagList($item)
686 $terms = Term::tagArrayFromItemId($item['id']);
687 foreach ($terms as $term) {
688 if ($term['type'] == TERM_HASHTAG) {
689 $tags[] = ['type' => 'Hashtag', 'href' => $term['url'], 'name' => '#' . $term['term']];
690 } elseif ($term['type'] == TERM_MENTION) {
691 $contact = Contact::getDetailsByURL($term['url']);
692 if (!empty($contact['addr'])) {
693 $mention = '@' . $contact['addr'];
695 $mention = '@' . $term['url'];
698 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
705 * Adds attachment data to the JSON document
707 * @param array $item Data of the item that is to be posted
708 * @param text $type Object type
710 * @return array with attachment data
712 private static function createAttachmentList($item, $type)
716 $arr = explode('[/attach],', $item['attach']);
718 foreach ($arr as $r) {
720 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
722 $attributes = ['type' => 'Document',
723 'mediaType' => $matches[3],
724 'url' => $matches[1],
727 if (trim($matches[4]) != '') {
728 $attributes['name'] = trim($matches[4]);
731 $attachments[] = $attributes;
736 if ($type != 'Note') {
740 // Simplify image codes
741 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
743 // Grab all pictures and create attachments out of them
744 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
745 foreach ($pictures[1] as $picture) {
746 $imgdata = Image::getInfoFromURL($picture);
748 $attachments[] = ['type' => 'Document',
749 'mediaType' => $imgdata['mime'],
760 * @brief Callback function to replace a Friendica style mention in a mention that is used on AP
762 * @param array $match Matching values for the callback
763 * @return string Replaced mention
765 private static function mentionCallback($match)
767 if (empty($match[1])) {
771 $data = Contact::getDetailsByURL($match[1]);
772 if (empty($data) || empty($data['nick'])) {
776 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
780 * Remove image elements and replaces them with links to the image
782 * @param string $body
784 * @return string with replaced elements
786 private static function removePictures($body)
788 // Simplify image codes
789 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
791 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
792 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
798 * Fetches the "context" value for a givem item array from the "conversation" table
802 * @return string with context url
804 private static function fetchContextURLForItem($item)
806 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
807 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
808 $context_uri = $conversation['conversation-href'];
809 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
810 $context_uri = $conversation['conversation-uri'];
812 $context_uri = $item['parent-uri'] . '#context';
818 * Returns if the post contains sensitive content ("nsfw")
820 * @param integer $item_id
824 private static function isSensitive($item_id)
826 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
827 return DBA::exists('term', $condition);
831 * Creates a note/article object array
835 * @return array with the object data
837 public static function createNote($item)
839 if (!empty($item['title'])) {
845 if ($item['deleted']) {
850 $data['id'] = $item['uri'];
851 $data['type'] = $type;
853 if ($item['deleted']) {
857 $data['summary'] = null; // Ignore by now
859 if ($item['uri'] != $item['thr-parent']) {
860 $data['inReplyTo'] = $item['thr-parent'];
862 $data['inReplyTo'] = null;
865 $data['diaspora:guid'] = $item['guid'];
866 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
868 if ($item['created'] != $item['edited']) {
869 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
872 $data['url'] = $item['plink'];
873 $data['attributedTo'] = $item['author-link'];
874 $data['sensitive'] = self::isSensitive($item['id']);
875 $data['context'] = self::fetchContextURLForItem($item);
877 if (!empty($item['title'])) {
878 $data['name'] = BBCode::toPlaintext($item['title'], false);
881 $body = $item['body'];
883 if ($type == 'Note') {
884 $body = self::removePictures($body);
887 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
888 $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
890 $data['content'] = BBCode::convert($body, false, 7);
891 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
893 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
894 $data['diaspora:comment'] = $item['signed_text'];
897 $data['attachment'] = self::createAttachmentList($item, $type);
898 $data['tag'] = self::createTagList($item);
900 if (!empty($item['coord']) || !empty($item['location'])) {
901 $data['location'] = self::createLocation($item);
904 if (!empty($item['app'])) {
905 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
908 $data = array_merge($data, self::createPermissionBlockForItem($item));
914 * Creates an announce object entry
918 * @return string with announced object url
920 public static function createAnnounce($item)
922 $announce = api_share_as_retweet($item);
923 if (empty($announce['plink'])) {
924 return self::createNote($item);
927 return $announce['plink'];
931 * Transmits a contact suggestion to a given inbox
933 * @param integer $uid User ID
934 * @param string $inbox Target inbox
935 * @param integer $suggestion_id Suggestion ID
937 * @return boolean was the transmission successful?
939 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
941 $owner = User::getOwnerDataById($uid);
942 $profile = APContact::getByURL($owner['url']);
944 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
946 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
947 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
948 'type' => 'Announce',
949 'actor' => $owner['url'],
950 'object' => $suggestion['url'],
951 'content' => $suggestion['note'],
952 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
953 'to' => [ActivityPub::PUBLIC_COLLECTION],
956 $signed = LDSignature::sign($data, $owner);
958 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
959 return HTTPSignature::transmit($signed, $inbox, $uid);
963 * Transmits a profile deletion to a given inbox
965 * @param integer $uid User ID
966 * @param string $inbox Target inbox
968 * @return boolean was the transmission successful?
970 public static function sendProfileDeletion($uid, $inbox)
972 $owner = User::getOwnerDataById($uid);
973 $profile = APContact::getByURL($owner['url']);
975 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
976 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
978 'actor' => $owner['url'],
979 'object' => $owner['url'],
980 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
981 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
982 'to' => [ActivityPub::PUBLIC_COLLECTION],
985 $signed = LDSignature::sign($data, $owner);
987 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
988 return HTTPSignature::transmit($signed, $inbox, $uid);
992 * Transmits a profile change to a given inbox
994 * @param integer $uid User ID
995 * @param string $inbox Target inbox
997 * @return boolean was the transmission successful?
999 public static function sendProfileUpdate($uid, $inbox)
1001 $owner = User::getOwnerDataById($uid);
1002 $profile = APContact::getByURL($owner['url']);
1004 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1005 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1007 'actor' => $owner['url'],
1008 'object' => self::getProfile($uid),
1009 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1010 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1011 'to' => [$profile['followers']],
1014 $signed = LDSignature::sign($data, $owner);
1016 logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
1017 return HTTPSignature::transmit($signed, $inbox, $uid);
1021 * Transmits a given activity to a target
1023 * @param array $activity
1024 * @param string $target Target profile
1025 * @param integer $uid User ID
1027 public static function sendActivity($activity, $target, $uid)
1029 $profile = APContact::getByURL($target);
1031 $owner = User::getOwnerDataById($uid);
1033 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1034 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1035 'type' => $activity,
1036 'actor' => $owner['url'],
1037 'object' => $profile['url'],
1038 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1039 'to' => $profile['url']];
1041 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
1043 $signed = LDSignature::sign($data, $owner);
1044 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1048 * Transmit a message that the contact request had been accepted
1050 * @param string $target Target profile
1052 * @param integer $uid User ID
1054 public static function sendContactAccept($target, $id, $uid)
1056 $profile = APContact::getByURL($target);
1058 $owner = User::getOwnerDataById($uid);
1059 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1060 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1062 'actor' => $owner['url'],
1063 'object' => ['id' => $id, 'type' => 'Follow',
1064 'actor' => $profile['url'],
1065 'object' => $owner['url']],
1066 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1067 'to' => $profile['url']];
1069 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
1071 $signed = LDSignature::sign($data, $owner);
1072 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1076 * Reject a contact request or terminates the contact relation
1078 * @param string $target Target profile
1080 * @param integer $uid User ID
1082 public static function sendContactReject($target, $id, $uid)
1084 $profile = APContact::getByURL($target);
1086 $owner = User::getOwnerDataById($uid);
1087 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1088 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1090 'actor' => $owner['url'],
1091 'object' => ['id' => $id, 'type' => 'Follow',
1092 'actor' => $profile['url'],
1093 'object' => $owner['url']],
1094 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1095 'to' => $profile['url']];
1097 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
1099 $signed = LDSignature::sign($data, $owner);
1100 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1104 * Transmits a message that we don't want to follow this contact anymore
1106 * @param string $target Target profile
1107 * @param integer $uid User ID
1109 public static function sendContactUndo($target, $uid)
1111 $profile = APContact::getByURL($target);
1113 $id = System::baseUrl() . '/activity/' . System::createGUID();
1115 $owner = User::getOwnerDataById($uid);
1116 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1119 'actor' => $owner['url'],
1120 'object' => ['id' => $id, 'type' => 'Follow',
1121 'actor' => $owner['url'],
1122 'object' => $profile['url']],
1123 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1124 'to' => $profile['url']];
1126 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
1128 $signed = LDSignature::sign($data, $owner);
1129 HTTPSignature::transmit($signed, $profile['inbox'], $uid);