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:
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' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
155 '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 $permissions[$element][] = $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 // Will be activated in a later step
312 // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
314 // For now only send to these contacts:
315 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
317 $data = ['to' => [], 'cc' => []];
319 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
321 $actor_profile = APContact::getByURL($item['author-link']);
323 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
325 if (!$item['private']) {
326 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
327 if (!empty($actor_profile['followers'])) {
328 $data['cc'][] = $actor_profile['followers'];
331 foreach ($terms as $term) {
332 $profile = APContact::getByURL($term['url'], false);
333 if (!empty($profile)) {
334 $data['to'][] = $profile['url'];
338 $receiver_list = Item::enumeratePermissions($item);
342 foreach ($terms as $term) {
343 $cid = Contact::getIdForURL($term['url'], $item['uid']);
344 if (!empty($cid) && in_array($cid, $receiver_list)) {
345 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => $networks]);
346 if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) {
347 $data['to'][] = $profile['url'];
352 foreach ($receiver_list as $receiver) {
353 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => $networks]);
354 if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) {
355 $data['cc'][] = $profile['url'];
360 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
361 while ($parent = Item::fetch($parents)) {
362 // Don't include data from future posts
363 if ($parent['id'] >= $item['id']) {
367 $profile = APContact::getByURL($parent['author-link'], false);
368 if (!empty($profile)) {
369 if ($parent['uri'] == $item['thr-parent']) {
370 $data['to'][] = $profile['url'];
372 $data['cc'][] = $profile['url'];
376 if ($item['gravity'] != GRAVITY_PARENT) {
380 $profile = APContact::getByURL($parent['owner-link'], false);
381 if (!empty($profile)) {
382 $data['cc'][] = $profile['url'];
385 DBA::close($parents);
387 $data['to'] = array_unique($data['to']);
388 $data['cc'] = array_unique($data['cc']);
390 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
391 unset($data['to'][$key]);
394 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
395 unset($data['cc'][$key]);
398 foreach ($data['to'] as $to) {
399 if (($key = array_search($to, $data['cc'])) !== false) {
400 unset($data['cc'][$key]);
404 return ['to' => array_values($data['to']), 'cc' => array_values($data['cc'])];
408 * Fetches a list of inboxes of followers of a given user
410 * @param integer $uid User ID
411 * @param boolean $personal fetch personal inboxes
413 * @return array of follower inboxes
415 public static function fetchTargetInboxesforUser($uid, $personal = false)
419 // Will be activated in a later step
420 // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
422 // For now only send to these contacts:
423 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
425 $condition = ['uid' => $uid, 'network' => $networks, 'archive' => false, 'pending' => false];
428 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
431 $contacts = DBA::select('contact', ['url'], $condition);
432 while ($contact = DBA::fetch($contacts)) {
433 $profile = APContact::getByURL($contact['url'], false);
434 if (!empty($profile)) {
435 if (empty($profile['sharedinbox']) || $personal) {
436 $target = $profile['inbox'];
438 $target = $profile['sharedinbox'];
440 $inboxes[$target] = $target;
443 DBA::close($contacts);
449 * Fetches an array of inboxes for the given item and user
452 * @param integer $uid User ID
453 * @param boolean $personal fetch personal inboxes
455 * @return array with inboxes
457 public static function fetchTargetInboxes($item, $uid, $personal = false)
459 $permissions = self::createPermissionBlockForItem($item);
460 if (empty($permissions)) {
466 if ($item['gravity'] == GRAVITY_ACTIVITY) {
467 $item_profile = APContact::getByURL($item['author-link'], false);
469 $item_profile = APContact::getByURL($item['owner-link'], false);
472 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
473 if (empty($permissions[$element])) {
477 foreach ($permissions[$element] as $receiver) {
478 if ($receiver == $item_profile['followers']) {
479 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
481 $profile = APContact::getByURL($receiver, false);
482 if (!empty($profile)) {
483 if (empty($profile['sharedinbox']) || $personal) {
484 $target = $profile['inbox'];
486 $target = $profile['sharedinbox'];
488 $inboxes[$target] = $target;
498 * Returns the activity type of a given item
502 * @return string with activity type
504 private static function getTypeOfItem($item)
506 if (!empty(Diaspora::isReshare($item['body'], false))) {
508 } elseif ($item['verb'] == ACTIVITY_POST) {
509 if ($item['created'] == $item['edited']) {
514 } elseif ($item['verb'] == ACTIVITY_LIKE) {
516 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
518 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
520 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
522 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
523 $type = 'TentativeAccept';
532 * Creates the activity or fetches it from the cache
534 * @param integer $item_id
536 * @return array with the activity
538 public static function createCachedActivityFromItem($item_id)
540 $cachekey = 'APDelivery:createActivity:' . $item_id;
541 $data = Cache::get($cachekey);
542 if (!is_null($data)) {
546 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
548 Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
553 * Creates an activity array for a given item id
555 * @param integer $item_id
556 * @param boolean $object_mode Is the activity item is used inside another object?
558 * @return array of activity
560 public static function createActivityFromItem($item_id, $object_mode = false)
562 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
564 if (!DBA::isResult($item)) {
568 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
569 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
570 if (DBA::isResult($conversation)) {
571 $data = json_decode($conversation['source']);
577 $type = self::getTypeOfItem($item);
580 $data = ['@context' => ActivityPub::CONTEXT];
582 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
584 } elseif ($item['deleted']) {
591 $data['id'] = $item['uri'] . '#' . $type;
592 $data['type'] = $type;
593 $data['actor'] = $item['owner-link'];
595 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
597 $data['instrument'] = ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()];
599 $data = array_merge($data, self::createPermissionBlockForItem($item));
601 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
602 $data['object'] = self::createNote($item);
603 } elseif ($data['type'] == 'Announce') {
604 $data['object'] = self::createAnnounce($item);
605 } elseif ($data['type'] == 'Undo') {
606 $data['object'] = self::createActivityFromItem($item_id, true);
608 $data['diaspora:guid'] = $item['guid'];
609 $data['object'] = $item['thr-parent'];
612 $owner = User::getOwnerDataById($item['uid']);
615 return LDSignature::sign($data, $owner);
620 /// @todo Create "conversation" entry
624 * Creates an object array for a given item id
626 * @param integer $item_id
628 * @return array with the object data
630 public static function createObjectFromItemID($item_id)
632 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
634 if (!DBA::isResult($item)) {
638 $data = ['@context' => ActivityPub::CONTEXT];
639 $data = array_merge($data, self::createNote($item));
645 * Creates a location entry for a given item array
649 * @return array with location array
651 private static function createLocation($item)
653 $location = ['type' => 'Place'];
655 if (!empty($item['location'])) {
656 $location['name'] = $item['location'];
661 if (empty($item['coord'])) {
662 $coord = Map::getCoordinates($item['location']);
664 $coords = explode(' ', $item['coord']);
665 if (count($coords) == 2) {
666 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
670 if (!empty($coord['lat']) && !empty($coord['lon'])) {
671 $location['latitude'] = $coord['lat'];
672 $location['longitude'] = $coord['lon'];
679 * Returns a tag array for a given item array
683 * @return array of tags
685 private static function createTagList($item)
689 $terms = Term::tagArrayFromItemId($item['id']);
690 foreach ($terms as $term) {
691 if ($term['type'] == TERM_HASHTAG) {
692 $tags[] = ['type' => 'Hashtag', 'href' => $term['url'], 'name' => '#' . $term['term']];
693 } elseif ($term['type'] == TERM_MENTION) {
694 $contact = Contact::getDetailsByURL($term['url']);
695 if (!empty($contact['addr'])) {
696 $mention = '@' . $contact['addr'];
698 $mention = '@' . $term['url'];
701 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
708 * Adds attachment data to the JSON document
710 * @param array $item Data of the item that is to be posted
711 * @param text $type Object type
713 * @return array with attachment data
715 private static function createAttachmentList($item, $type)
719 $arr = explode('[/attach],', $item['attach']);
721 foreach ($arr as $r) {
723 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
725 $attributes = ['type' => 'Document',
726 'mediaType' => $matches[3],
727 'url' => $matches[1],
730 if (trim($matches[4]) != '') {
731 $attributes['name'] = trim($matches[4]);
734 $attachments[] = $attributes;
739 if ($type != 'Note') {
743 // Simplify image codes
744 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
746 // Grab all pictures and create attachments out of them
747 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
748 foreach ($pictures[1] as $picture) {
749 $imgdata = Image::getInfoFromURL($picture);
751 $attachments[] = ['type' => 'Document',
752 'mediaType' => $imgdata['mime'],
763 * @brief Callback function to replace a Friendica style mention in a mention that is used on AP
765 * @param array $match Matching values for the callback
766 * @return string Replaced mention
768 private static function mentionCallback($match)
770 if (empty($match[1])) {
774 $data = Contact::getDetailsByURL($match[1]);
775 if (empty($data) || empty($data['nick'])) {
779 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
783 * Remove image elements and replaces them with links to the image
785 * @param string $body
787 * @return string with replaced elements
789 private static function removePictures($body)
791 // Simplify image codes
792 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
794 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
795 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
801 * Fetches the "context" value for a givem item array from the "conversation" table
805 * @return string with context url
807 private static function fetchContextURLForItem($item)
809 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
810 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
811 $context_uri = $conversation['conversation-href'];
812 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
813 $context_uri = $conversation['conversation-uri'];
815 $context_uri = $item['parent-uri'] . '#context';
821 * Returns if the post contains sensitive content ("nsfw")
823 * @param integer $item_id
827 private static function isSensitive($item_id)
829 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
830 return DBA::exists('term', $condition);
834 * Creates a note/article object array
838 * @return array with the object data
840 public static function createNote($item)
842 if (!empty($item['title'])) {
848 if ($item['deleted']) {
853 $data['id'] = $item['uri'];
854 $data['type'] = $type;
856 if ($item['deleted']) {
860 $data['summary'] = null; // Ignore by now
862 if ($item['uri'] != $item['thr-parent']) {
863 $data['inReplyTo'] = $item['thr-parent'];
865 $data['inReplyTo'] = null;
868 $data['diaspora:guid'] = $item['guid'];
869 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
871 if ($item['created'] != $item['edited']) {
872 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
875 $data['url'] = $item['plink'];
876 $data['attributedTo'] = $item['author-link'];
877 $data['sensitive'] = self::isSensitive($item['id']);
878 $data['context'] = self::fetchContextURLForItem($item);
880 if (!empty($item['title'])) {
881 $data['name'] = BBCode::toPlaintext($item['title'], false);
884 $body = $item['body'];
886 if ($type == 'Note') {
887 $body = self::removePictures($body);
890 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
891 $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
893 $data['content'] = BBCode::convert($body, false, 7);
894 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
896 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
897 $data['diaspora:comment'] = $item['signed_text'];
900 $data['attachment'] = self::createAttachmentList($item, $type);
901 $data['tag'] = self::createTagList($item);
903 if (!empty($item['coord']) || !empty($item['location'])) {
904 $data['location'] = self::createLocation($item);
907 if (!empty($item['app'])) {
908 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
911 $data = array_merge($data, self::createPermissionBlockForItem($item));
917 * Creates an announce object entry
921 * @return string with announced object url
923 public static function createAnnounce($item)
925 $announce = api_share_as_retweet($item);
926 if (empty($announce['plink'])) {
927 return self::createNote($item);
930 return $announce['plink'];
934 * Transmits a contact suggestion to a given inbox
936 * @param integer $uid User ID
937 * @param string $inbox Target inbox
938 * @param integer $suggestion_id Suggestion ID
940 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
942 $owner = User::getOwnerDataById($uid);
943 $profile = APContact::getByURL($owner['url']);
945 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
947 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
948 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
949 'type' => 'Announce',
950 'actor' => $owner['url'],
951 'object' => $suggestion['url'],
952 'content' => $suggestion['note'],
953 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
954 'to' => [ActivityPub::PUBLIC_COLLECTION],
957 $signed = LDSignature::sign($data, $owner);
959 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
960 HTTPSignature::transmit($signed, $inbox, $uid);
964 * Transmits a profile deletion to a given inbox
966 * @param integer $uid User ID
967 * @param string $inbox Target inbox
969 public static function sendProfileDeletion($uid, $inbox)
971 $owner = User::getOwnerDataById($uid);
972 $profile = APContact::getByURL($owner['url']);
974 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
975 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
977 'actor' => $owner['url'],
978 'object' => $owner['url'],
979 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
980 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
981 'to' => [ActivityPub::PUBLIC_COLLECTION],
984 $signed = LDSignature::sign($data, $owner);
986 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
987 HTTPSignature::transmit($signed, $inbox, $uid);
991 * Transmits a profile change to a given inbox
993 * @param integer $uid User ID
994 * @param string $inbox Target inbox
996 public static function sendProfileUpdate($uid, $inbox)
998 $owner = User::getOwnerDataById($uid);
999 $profile = APContact::getByURL($owner['url']);
1001 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1002 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1004 'actor' => $owner['url'],
1005 'object' => self::getProfile($uid),
1006 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1007 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1008 'to' => [$profile['followers']],
1011 $signed = LDSignature::sign($data, $owner);
1013 logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
1014 HTTPSignature::transmit($signed, $inbox, $uid);
1018 * Transmits a given activity to a target
1020 * @param array $activity
1021 * @param string $target Target profile
1022 * @param integer $uid User ID
1024 public static function sendActivity($activity, $target, $uid)
1026 $profile = APContact::getByURL($target);
1028 $owner = User::getOwnerDataById($uid);
1030 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1031 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1032 'type' => $activity,
1033 'actor' => $owner['url'],
1034 'object' => $profile['url'],
1035 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1036 'to' => $profile['url']];
1038 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
1040 $signed = LDSignature::sign($data, $owner);
1041 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1045 * Transmit a message that the contact request had been accepted
1047 * @param string $target Target profile
1049 * @param integer $uid User ID
1051 public static function sendContactAccept($target, $id, $uid)
1053 $profile = APContact::getByURL($target);
1055 $owner = User::getOwnerDataById($uid);
1056 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1057 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1059 'actor' => $owner['url'],
1060 'object' => ['id' => $id, 'type' => 'Follow',
1061 'actor' => $profile['url'],
1062 'object' => $owner['url']],
1063 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1064 'to' => $profile['url']];
1066 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
1068 $signed = LDSignature::sign($data, $owner);
1069 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1073 * Reject a contact request or terminates the contact relation
1075 * @param string $target Target profile
1077 * @param integer $uid User ID
1079 public static function sendContactReject($target, $id, $uid)
1081 $profile = APContact::getByURL($target);
1083 $owner = User::getOwnerDataById($uid);
1084 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1085 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1087 'actor' => $owner['url'],
1088 'object' => ['id' => $id, 'type' => 'Follow',
1089 'actor' => $profile['url'],
1090 'object' => $owner['url']],
1091 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1092 'to' => $profile['url']];
1094 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
1096 $signed = LDSignature::sign($data, $owner);
1097 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1101 * Transmits a message that we don't want to follow this contact anymore
1103 * @param string $target Target profile
1104 * @param integer $uid User ID
1106 public static function sendContactUndo($target, $uid)
1108 $profile = APContact::getByURL($target);
1110 $id = System::baseUrl() . '/activity/' . System::createGUID();
1112 $owner = User::getOwnerDataById($uid);
1113 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1116 'actor' => $owner['url'],
1117 'object' => ['id' => $id, 'type' => 'Follow',
1118 'actor' => $owner['url'],
1119 'object' => $profile['url']],
1120 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1121 'to' => $profile['url']];
1123 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
1125 $signed = LDSignature::sign($data, $owner);
1126 HTTPSignature::transmit($signed, $profile['inbox'], $uid);