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
303 * @param boolean $blindcopy
305 * @return array with permission data
307 private static function createPermissionBlockForItem($item, $blindcopy)
309 // Will be activated in a later step
310 // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
312 // For now only send to these contacts:
313 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
315 $data = ['to' => [], 'cc' => [], 'bcc' => []];
317 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
319 $actor_profile = APContact::getByURL($item['author-link']);
321 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
323 if (!$item['private']) {
324 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
325 if (!empty($actor_profile['followers'])) {
326 $data['cc'][] = $actor_profile['followers'];
329 foreach ($terms as $term) {
330 $profile = APContact::getByURL($term['url'], false);
331 if (!empty($profile)) {
332 $data['to'][] = $profile['url'];
336 $receiver_list = Item::enumeratePermissions($item);
340 foreach ($terms as $term) {
341 $cid = Contact::getIdForURL($term['url'], $item['uid']);
342 if (!empty($cid) && in_array($cid, $receiver_list)) {
343 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => $networks]);
344 if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) {
345 $data['to'][] = $profile['url'];
350 foreach ($receiver_list as $receiver) {
351 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => $networks]);
352 if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) {
353 // BCC is currently deactivated, due to Pleroma and Mastodon not reacting like expected
354 // $data['bcc'][] = $profile['url'];
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']);
389 $data['bcc'] = array_unique($data['bcc']);
391 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
392 unset($data['to'][$key]);
395 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
396 unset($data['cc'][$key]);
399 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
400 unset($data['bcc'][$key]);
403 foreach ($data['to'] as $to) {
404 if (($key = array_search($to, $data['cc'])) !== false) {
405 unset($data['cc'][$key]);
408 if (($key = array_search($to, $data['bcc'])) !== false) {
409 unset($data['bcc'][$key]);
413 foreach ($data['cc'] as $cc) {
414 if (($key = array_search($cc, $data['bcc'])) !== false) {
415 unset($data['bcc'][$key]);
419 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
422 unset($receivers['bcc']);
429 * Fetches a list of inboxes of followers of a given user
431 * @param integer $uid User ID
432 * @param boolean $personal fetch personal inboxes
434 * @return array of follower inboxes
436 public static function fetchTargetInboxesforUser($uid, $personal = false)
440 // Will be activated in a later step
441 // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
443 // For now only send to these contacts:
444 $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
446 $condition = ['uid' => $uid, 'network' => $networks, 'archive' => false, 'pending' => false];
449 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
452 $contacts = DBA::select('contact', ['url'], $condition);
453 while ($contact = DBA::fetch($contacts)) {
454 $profile = APContact::getByURL($contact['url'], false);
455 if (!empty($profile)) {
456 if (empty($profile['sharedinbox']) || $personal) {
457 $target = $profile['inbox'];
459 $target = $profile['sharedinbox'];
461 $inboxes[$target] = $target;
464 DBA::close($contacts);
470 * Fetches an array of inboxes for the given item and user
473 * @param integer $uid User ID
474 * @param boolean $personal fetch personal inboxes
476 * @return array with inboxes
478 public static function fetchTargetInboxes($item, $uid, $personal = false)
480 $permissions = self::createPermissionBlockForItem($item, true);
481 if (empty($permissions)) {
487 if ($item['gravity'] == GRAVITY_ACTIVITY) {
488 $item_profile = APContact::getByURL($item['author-link'], false);
490 $item_profile = APContact::getByURL($item['owner-link'], false);
493 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
494 if (empty($permissions[$element])) {
498 $blindcopy = in_array($element, ['bto', 'bcc']);
500 foreach ($permissions[$element] as $receiver) {
501 if ($receiver == $item_profile['followers']) {
502 $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
504 $profile = APContact::getByURL($receiver, false);
505 if (!empty($profile)) {
506 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
507 $target = $profile['inbox'];
509 $target = $profile['sharedinbox'];
511 $inboxes[$target] = $target;
521 * Returns the activity type of a given item
525 * @return string with activity type
527 private static function getTypeOfItem($item)
529 if (!empty(Diaspora::isReshare($item['body'], false))) {
531 } elseif ($item['verb'] == ACTIVITY_POST) {
532 if ($item['created'] == $item['edited']) {
537 } elseif ($item['verb'] == ACTIVITY_LIKE) {
539 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
541 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
543 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
545 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
546 $type = 'TentativeAccept';
555 * Creates the activity or fetches it from the cache
557 * @param integer $item_id
559 * @return array with the activity
561 public static function createCachedActivityFromItem($item_id)
563 $cachekey = 'APDelivery:createActivity:' . $item_id;
564 $data = Cache::get($cachekey);
565 if (!is_null($data)) {
569 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
571 Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
576 * Creates an activity array for a given item id
578 * @param integer $item_id
579 * @param boolean $object_mode Is the activity item is used inside another object?
581 * @return array of activity
583 public static function createActivityFromItem($item_id, $object_mode = false)
585 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
587 if (!DBA::isResult($item)) {
591 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
592 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
593 if (DBA::isResult($conversation)) {
594 $data = json_decode($conversation['source']);
600 $type = self::getTypeOfItem($item);
603 $data = ['@context' => ActivityPub::CONTEXT];
605 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
607 } elseif ($item['deleted']) {
614 $data['id'] = $item['uri'] . '#' . $type;
615 $data['type'] = $type;
616 $data['actor'] = $item['owner-link'];
618 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
620 $data['instrument'] = ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()];
622 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
624 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
625 $data['object'] = self::createNote($item);
626 } elseif ($data['type'] == 'Announce') {
627 $data['object'] = self::createAnnounce($item);
628 } elseif ($data['type'] == 'Undo') {
629 $data['object'] = self::createActivityFromItem($item_id, true);
631 $data['diaspora:guid'] = $item['guid'];
632 $data['object'] = $item['thr-parent'];
635 $owner = User::getOwnerDataById($item['uid']);
638 return LDSignature::sign($data, $owner);
643 /// @todo Create "conversation" entry
647 * Creates an object array for a given item id
649 * @param integer $item_id
651 * @return array with the object data
653 public static function createObjectFromItemID($item_id)
655 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
657 if (!DBA::isResult($item)) {
661 $data = ['@context' => ActivityPub::CONTEXT];
662 $data = array_merge($data, self::createNote($item));
668 * Creates a location entry for a given item array
672 * @return array with location array
674 private static function createLocation($item)
676 $location = ['type' => 'Place'];
678 if (!empty($item['location'])) {
679 $location['name'] = $item['location'];
684 if (empty($item['coord'])) {
685 $coord = Map::getCoordinates($item['location']);
687 $coords = explode(' ', $item['coord']);
688 if (count($coords) == 2) {
689 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
693 if (!empty($coord['lat']) && !empty($coord['lon'])) {
694 $location['latitude'] = $coord['lat'];
695 $location['longitude'] = $coord['lon'];
702 * Returns a tag array for a given item array
706 * @return array of tags
708 private static function createTagList($item)
712 $terms = Term::tagArrayFromItemId($item['id']);
713 foreach ($terms as $term) {
714 if ($term['type'] == TERM_HASHTAG) {
715 $tags[] = ['type' => 'Hashtag', 'href' => $term['url'], 'name' => '#' . $term['term']];
716 } elseif ($term['type'] == TERM_MENTION) {
717 $contact = Contact::getDetailsByURL($term['url']);
718 if (!empty($contact['addr'])) {
719 $mention = '@' . $contact['addr'];
721 $mention = '@' . $term['url'];
724 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
731 * Adds attachment data to the JSON document
733 * @param array $item Data of the item that is to be posted
734 * @param text $type Object type
736 * @return array with attachment data
738 private static function createAttachmentList($item, $type)
742 $arr = explode('[/attach],', $item['attach']);
744 foreach ($arr as $r) {
746 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
748 $attributes = ['type' => 'Document',
749 'mediaType' => $matches[3],
750 'url' => $matches[1],
753 if (trim($matches[4]) != '') {
754 $attributes['name'] = trim($matches[4]);
757 $attachments[] = $attributes;
762 if ($type != 'Note') {
766 // Simplify image codes
767 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
769 // Grab all pictures and create attachments out of them
770 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
771 foreach ($pictures[1] as $picture) {
772 $imgdata = Image::getInfoFromURL($picture);
774 $attachments[] = ['type' => 'Document',
775 'mediaType' => $imgdata['mime'],
786 * @brief Callback function to replace a Friendica style mention in a mention that is used on AP
788 * @param array $match Matching values for the callback
789 * @return string Replaced mention
791 private static function mentionCallback($match)
793 if (empty($match[1])) {
797 $data = Contact::getDetailsByURL($match[1]);
798 if (empty($data) || empty($data['nick'])) {
802 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
806 * Remove image elements and replaces them with links to the image
808 * @param string $body
810 * @return string with replaced elements
812 private static function removePictures($body)
814 // Simplify image codes
815 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
817 $body = preg_replace("/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi", '[url]$1[/url]', $body);
818 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '[url]$1[/url]', $body);
824 * Fetches the "context" value for a givem item array from the "conversation" table
828 * @return string with context url
830 private static function fetchContextURLForItem($item)
832 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
833 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
834 $context_uri = $conversation['conversation-href'];
835 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
836 $context_uri = $conversation['conversation-uri'];
838 $context_uri = $item['parent-uri'] . '#context';
844 * Returns if the post contains sensitive content ("nsfw")
846 * @param integer $item_id
850 private static function isSensitive($item_id)
852 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $item_id, 'type' => TERM_HASHTAG, 'term' => 'nsfw'];
853 return DBA::exists('term', $condition);
857 * Creates a note/article object array
861 * @return array with the object data
863 public static function createNote($item)
865 if (!empty($item['title'])) {
871 if ($item['deleted']) {
876 $data['id'] = $item['uri'];
877 $data['type'] = $type;
879 if ($item['deleted']) {
883 $data['summary'] = null; // Ignore by now
885 if ($item['uri'] != $item['thr-parent']) {
886 $data['inReplyTo'] = $item['thr-parent'];
888 $data['inReplyTo'] = null;
891 $data['diaspora:guid'] = $item['guid'];
892 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
894 if ($item['created'] != $item['edited']) {
895 $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
898 $data['url'] = $item['plink'];
899 $data['attributedTo'] = $item['author-link'];
900 $data['sensitive'] = self::isSensitive($item['id']);
901 $data['context'] = self::fetchContextURLForItem($item);
903 if (!empty($item['title'])) {
904 $data['name'] = BBCode::toPlaintext($item['title'], false);
907 $body = $item['body'];
909 if ($type == 'Note') {
910 $body = self::removePictures($body);
913 $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
914 $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
916 $data['content'] = BBCode::convert($body, false, 7);
917 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
919 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
920 $data['diaspora:comment'] = $item['signed_text'];
923 $data['attachment'] = self::createAttachmentList($item, $type);
924 $data['tag'] = self::createTagList($item);
926 if (!empty($item['coord']) || !empty($item['location'])) {
927 $data['location'] = self::createLocation($item);
930 if (!empty($item['app'])) {
931 $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
934 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
940 * Creates an announce object entry
944 * @return string with announced object url
946 public static function createAnnounce($item)
948 $announce = api_share_as_retweet($item);
949 if (empty($announce['plink'])) {
950 return self::createNote($item);
953 return $announce['plink'];
957 * Transmits a contact suggestion to a given inbox
959 * @param integer $uid User ID
960 * @param string $inbox Target inbox
961 * @param integer $suggestion_id Suggestion ID
963 * @return boolean was the transmission successful?
965 public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
967 $owner = User::getOwnerDataById($uid);
968 $profile = APContact::getByURL($owner['url']);
970 $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
972 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
973 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
974 'type' => 'Announce',
975 'actor' => $owner['url'],
976 'object' => $suggestion['url'],
977 'content' => $suggestion['note'],
978 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
979 'to' => [ActivityPub::PUBLIC_COLLECTION],
982 $signed = LDSignature::sign($data, $owner);
984 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
985 return HTTPSignature::transmit($signed, $inbox, $uid);
989 * Transmits a profile deletion to a given inbox
991 * @param integer $uid User ID
992 * @param string $inbox Target inbox
994 * @return boolean was the transmission successful?
996 public static function sendProfileDeletion($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' => $owner['url'],
1006 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1007 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1008 'to' => [ActivityPub::PUBLIC_COLLECTION],
1011 $signed = LDSignature::sign($data, $owner);
1013 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
1014 return HTTPSignature::transmit($signed, $inbox, $uid);
1018 * Transmits a profile change to a given inbox
1020 * @param integer $uid User ID
1021 * @param string $inbox Target inbox
1023 * @return boolean was the transmission successful?
1025 public static function sendProfileUpdate($uid, $inbox)
1027 $owner = User::getOwnerDataById($uid);
1028 $profile = APContact::getByURL($owner['url']);
1030 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1031 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1033 'actor' => $owner['url'],
1034 'object' => self::getProfile($uid),
1035 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1036 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1037 'to' => [$profile['followers']],
1040 $signed = LDSignature::sign($data, $owner);
1042 logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
1043 return HTTPSignature::transmit($signed, $inbox, $uid);
1047 * Transmits a given activity to a target
1049 * @param array $activity
1050 * @param string $target Target profile
1051 * @param integer $uid User ID
1053 public static function sendActivity($activity, $target, $uid)
1055 $profile = APContact::getByURL($target);
1057 $owner = User::getOwnerDataById($uid);
1059 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1060 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1061 'type' => $activity,
1062 'actor' => $owner['url'],
1063 'object' => $profile['url'],
1064 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1065 'to' => $profile['url']];
1067 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
1069 $signed = LDSignature::sign($data, $owner);
1070 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1074 * Transmit a message that the contact request had been accepted
1076 * @param string $target Target profile
1078 * @param integer $uid User ID
1080 public static function sendContactAccept($target, $id, $uid)
1082 $profile = APContact::getByURL($target);
1084 $owner = User::getOwnerDataById($uid);
1085 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1086 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1088 'actor' => $owner['url'],
1089 'object' => ['id' => $id, 'type' => 'Follow',
1090 'actor' => $profile['url'],
1091 'object' => $owner['url']],
1092 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1093 'to' => $profile['url']];
1095 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
1097 $signed = LDSignature::sign($data, $owner);
1098 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1102 * Reject a contact request or terminates the contact relation
1104 * @param string $target Target profile
1106 * @param integer $uid User ID
1108 public static function sendContactReject($target, $id, $uid)
1110 $profile = APContact::getByURL($target);
1112 $owner = User::getOwnerDataById($uid);
1113 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1114 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
1116 'actor' => $owner['url'],
1117 'object' => ['id' => $id, 'type' => 'Follow',
1118 'actor' => $profile['url'],
1119 'object' => $owner['url']],
1120 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1121 'to' => $profile['url']];
1123 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
1125 $signed = LDSignature::sign($data, $owner);
1126 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1130 * Transmits a message that we don't want to follow this contact anymore
1132 * @param string $target Target profile
1133 * @param integer $uid User ID
1135 public static function sendContactUndo($target, $uid)
1137 $profile = APContact::getByURL($target);
1139 $id = System::baseUrl() . '/activity/' . System::createGUID();
1141 $owner = User::getOwnerDataById($uid);
1142 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
1145 'actor' => $owner['url'],
1146 'object' => ['id' => $id, 'type' => 'Follow',
1147 'actor' => $owner['url'],
1148 'object' => $profile['url']],
1149 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
1150 'to' => $profile['url']];
1152 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
1154 $signed = LDSignature::sign($data, $owner);
1155 HTTPSignature::transmit($signed, $profile['inbox'], $uid);