3 * @file src/Protocol/ActivityPub.php
5 namespace Friendica\Protocol;
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Crypto;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\HTML;
24 use Friendica\Util\JsonLD;
25 use Friendica\Util\LDSignature;
26 use Friendica\Core\Config;
29 * @brief ActivityPub Protocol class
30 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
31 * https://www.w3.org/TR/activitypub/
32 * https://www.w3.org/TR/activitystreams-core/
33 * https://www.w3.org/TR/activitystreams-vocabulary/
35 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
36 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
38 * Digest: https://tools.ietf.org/html/rfc5843
39 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
41 * Mastodon implementation of supported activities:
42 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
72 * - Queueing unsucessful deliveries
73 * - Polling the outboxes for missing content?
74 * - Possibly using the LD-JSON parser
78 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
79 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
80 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
81 'diaspora' => 'https://diasporafoundation.org/ns/',
82 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
83 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
86 * @brief Checks if the web request is done for the AP protocol
90 public static function isRequest()
92 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
93 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
97 * @brief collects the lost of followers of the given owner
99 * @param array $owner Owner array
100 * @param integer $page Page number
102 * @return array of owners
104 public static function getFollowers($owner, $page = null)
106 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
107 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
108 $count = DBA::count('contact', $condition);
110 $data = ['@context' => self::CONTEXT];
111 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
112 $data['type'] = 'OrderedCollection';
113 $data['totalItems'] = $count;
115 // When we hide our friends we will only show the pure number but don't allow more.
116 $profile = Profile::getByUID($owner['uid']);
117 if (!empty($profile['hide-friends'])) {
122 $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
126 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
127 while ($contact = DBA::fetch($contacts)) {
128 $list[] = $contact['url'];
132 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
135 $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
137 $data['orderedItems'] = $list;
144 * @brief Create list of following contacts
146 * @param array $owner Owner array
147 * @param integer $page Page numbe
149 * @return array of following contacts
151 public static function getFollowing($owner, $page = null)
153 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
154 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
155 $count = DBA::count('contact', $condition);
157 $data = ['@context' => self::CONTEXT];
158 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
159 $data['type'] = 'OrderedCollection';
160 $data['totalItems'] = $count;
162 // When we hide our friends we will only show the pure number but don't allow more.
163 $profile = Profile::getByUID($owner['uid']);
164 if (!empty($profile['hide-friends'])) {
169 $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
173 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
174 while ($contact = DBA::fetch($contacts)) {
175 $list[] = $contact['url'];
179 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
182 $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
184 $data['orderedItems'] = $list;
191 * @brief Public posts for the given owner
193 * @param array $owner Owner array
194 * @param integer $page Page numbe
196 * @return array of posts
198 public static function getOutbox($owner, $page = null)
200 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
202 $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact,
203 'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
204 'deleted' => false, 'visible' => true];
205 $count = DBA::count('item', $condition);
207 $data = ['@context' => self::CONTEXT];
208 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
209 $data['type'] = 'OrderedCollection';
210 $data['totalItems'] = $count;
213 $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
217 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
219 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
220 while ($item = Item::fetch($items)) {
221 $object = self::createObjectFromItemID($item['id']);
222 unset($object['@context']);
227 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
230 $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
232 $data['orderedItems'] = $list;
239 * Return the ActivityPub profile of the given user
241 * @param integer $uid User ID
242 * @return profile array
244 public static function profile($uid)
246 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
247 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
248 'account_removed' => false, 'verified' => true];
249 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
250 $user = DBA::selectFirst('user', $fields, $condition);
251 if (!DBA::isResult($user)) {
255 $fields = ['locality', 'region', 'country-name'];
256 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
257 if (!DBA::isResult($profile)) {
261 $fields = ['name', 'url', 'location', 'about', 'avatar'];
262 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
263 if (!DBA::isResult($contact)) {
267 $data = ['@context' => self::CONTEXT];
268 $data['id'] = $contact['url'];
269 $data['diaspora:guid'] = $user['guid'];
270 $data['type'] = $accounttype[$user['account-type']];
271 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
272 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
273 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
274 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
275 $data['preferredUsername'] = $user['nickname'];
276 $data['name'] = $contact['name'];
277 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
278 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
279 $data['summary'] = $contact['about'];
280 $data['url'] = $contact['url'];
281 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
282 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
283 'owner' => $contact['url'],
284 'publicKeyPem' => $user['pubkey']];
285 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
286 $data['icon'] = ['type' => 'Image',
287 'url' => $contact['avatar']];
289 // tags: https://kitty.town/@inmysocks/100656097926961126.json
294 * @brief Returns an array with permissions of a given item array
298 * @return array with permissions
300 private static function fetchPermissionBlockFromConversation($item)
302 if (empty($item['thr-parent'])) {
306 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
307 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
308 if (!DBA::isResult($conversation)) {
312 $activity = json_decode($conversation['source'], true);
314 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
315 $profile = APContact::getProfileByURL($actor);
317 $item_profile = APContact::getProfileByURL($item['author-link']);
318 $exclude[] = $item['author-link'];
320 if ($item['gravity'] == GRAVITY_PARENT) {
321 $exclude[] = $item['owner-link'];
324 $permissions['to'][] = $actor;
326 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
327 if (empty($activity[$element])) {
330 if (is_string($activity[$element])) {
331 $activity[$element] = [$activity[$element]];
334 foreach ($activity[$element] as $receiver) {
335 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
336 $receiver = $item_profile['followers'];
338 if (!in_array($receiver, $exclude)) {
339 $permissions[$element][] = $receiver;
347 * @brief Creates an array of permissions from an item thread
351 * @return permission array
353 public static function createPermissionBlockForItem($item)
355 $data = ['to' => [], 'cc' => []];
357 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
359 $actor_profile = APContact::getProfileByURL($item['author-link']);
361 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
363 $contacts[$item['author-link']] = $item['author-link'];
365 if (!$item['private']) {
366 $data['to'][] = self::PUBLIC_COLLECTION;
367 if (!empty($actor_profile['followers'])) {
368 $data['cc'][] = $actor_profile['followers'];
371 foreach ($terms as $term) {
372 if ($term['type'] != TERM_MENTION) {
375 $profile = APContact::getProfileByURL($term['url'], false);
376 if (!empty($profile) && empty($contacts[$profile['url']])) {
377 $data['cc'][] = $profile['url'];
378 $contacts[$profile['url']] = $profile['url'];
382 $receiver_list = Item::enumeratePermissions($item);
386 foreach ($terms as $term) {
387 if ($term['type'] != TERM_MENTION) {
390 $cid = Contact::getIdForURL($term['url'], $item['uid']);
391 if (!empty($cid) && in_array($cid, $receiver_list)) {
392 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
393 $data['to'][] = $contact['url'];
394 $contacts[$contact['url']] = $contact['url'];
398 foreach ($receiver_list as $receiver) {
399 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
400 if (empty($contacts[$contact['url']])) {
401 $data['cc'][] = $contact['url'];
402 $contacts[$contact['url']] = $contact['url'];
407 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
408 while ($parent = Item::fetch($parents)) {
409 // Don't include data from future posts
410 if ($parent['id'] >= $item['id']) {
414 $profile = APContact::getProfileByURL($parent['author-link'], false);
415 if (!empty($profile) && empty($contacts[$profile['url']])) {
416 $data['cc'][] = $profile['url'];
417 $contacts[$profile['url']] = $profile['url'];
420 if ($item['gravity'] != GRAVITY_PARENT) {
424 $profile = APContact::getProfileByURL($parent['owner-link'], false);
425 if (!empty($profile) && empty($contacts[$profile['url']])) {
426 $data['cc'][] = $profile['url'];
427 $contacts[$profile['url']] = $profile['url'];
430 DBA::close($parents);
432 if (empty($data['to'])) {
433 $data['to'] = $data['cc'];
441 * @brief Fetches an array of inboxes for the given item and user
444 * @param integer $uid User ID
446 * @return array with inboxes
448 public static function fetchTargetInboxes($item, $uid)
450 $permissions = self::createPermissionBlockForItem($item);
451 if (empty($permissions)) {
457 if ($item['gravity'] == GRAVITY_ACTIVITY) {
458 $item_profile = APContact::getProfileByURL($item['author-link']);
460 $item_profile = APContact::getProfileByURL($item['owner-link']);
463 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
464 if (empty($permissions[$element])) {
468 foreach ($permissions[$element] as $receiver) {
469 if ($receiver == $item_profile['followers']) {
470 $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
471 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB,
472 'archive' => false, 'pending' => false]);
473 while ($contact = DBA::fetch($contacts)) {
474 $contact = defaults($contact, 'batch', $contact['notify']);
475 $inboxes[$contact] = $contact;
477 DBA::close($contacts);
479 $profile = APContact::getProfileByURL($receiver);
480 if (!empty($profile)) {
481 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
482 $inboxes[$target] = $target;
492 * @brief Returns the activity type of a given item
496 * @return activity type
498 public static function getTypeOfItem($item)
500 if ($item['verb'] == ACTIVITY_POST) {
501 if ($item['created'] == $item['edited']) {
506 } elseif ($item['verb'] == ACTIVITY_LIKE) {
508 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
510 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
512 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
514 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
515 $type = 'TentativeAccept';
524 * @brief Creates an activity array for a given item id
526 * @param integer $item_id
527 * @param boolean $object_mode Is the activity item is used inside another object?
529 * @return array of activity
531 public static function createActivityFromItem($item_id, $object_mode = false)
533 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
535 if (!DBA::isResult($item)) {
539 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
540 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
541 if (DBA::isResult($conversation)) {
542 $data = json_decode($conversation['source']);
548 $type = self::getTypeOfItem($item);
551 $data = ['@context' => self::CONTEXT];
553 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
555 } elseif ($item['deleted']) {
562 $data['id'] = $item['uri'] . '#' . $type;
563 $data['type'] = $type;
564 $data['actor'] = $item['author-link'];
566 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
568 if ($item["created"] != $item["edited"]) {
569 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
572 $data['context'] = self::fetchContextURLForItem($item);
574 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
576 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
577 $data['object'] = self::createNote($item);
578 } elseif ($data['type'] == 'Undo') {
579 $data['object'] = self::createActivityFromItem($item_id, true);
581 $data['object'] = $item['thr-parent'];
584 $owner = User::getOwnerDataById($item['uid']);
587 return LDSignature::sign($data, $owner);
594 * @brief Creates an object array for a given item id
596 * @param integer $item_id
598 * @return object array
600 public static function createObjectFromItemID($item_id)
602 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
604 if (!DBA::isResult($item)) {
608 $data = ['@context' => self::CONTEXT];
609 $data = array_merge($data, self::createNote($item));
615 * @brief Returns a tag array for a given item array
619 * @return array of tags
621 private static function createTagList($item)
625 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
626 foreach ($terms as $term) {
627 if ($term['type'] == TERM_MENTION) {
628 $contact = Contact::getDetailsByURL($term['url']);
629 if (!empty($contact['addr'])) {
630 $mention = '@' . $contact['addr'];
632 $mention = '@' . $term['url'];
635 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
642 * @brief Fetches the "context" value for a givem item array from the "conversation" table
646 * @return string with context url
648 private static function fetchContextURLForItem($item)
650 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
651 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
652 $context_uri = $conversation['conversation-href'];
653 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
654 $context_uri = $conversation['conversation-uri'];
656 $context_uri = str_replace('/object/', '/context/', $item['parent-uri']);
662 * @brief Creates a note/article object array
666 * @return object array
668 private static function createNote($item)
670 if (!empty($item['title'])) {
676 if ($item['deleted']) {
681 $data['id'] = $item['uri'];
682 $data['type'] = $type;
684 if ($item['deleted']) {
688 $data['summary'] = null; // Ignore by now
690 if ($item['uri'] != $item['thr-parent']) {
691 $data['inReplyTo'] = $item['thr-parent'];
693 $data['inReplyTo'] = null;
696 $data['diaspora:guid'] = $item['guid'];
697 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
699 if ($item["created"] != $item["edited"]) {
700 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
703 $data['url'] = $item['plink'];
704 $data['attributedTo'] = $item['author-link'];
705 $data['actor'] = $item['author-link'];
706 $data['sensitive'] = false; // - Query NSFW
707 $data['context'] = self::fetchContextURLForItem($item);
709 if (!empty($item['title'])) {
710 $data['name'] = BBCode::convert($item['title'], false, 7);
713 $data['content'] = BBCode::convert($item['body'], false, 7);
714 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
716 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
717 $data['diaspora:comment'] = $item['signed_text'];
720 $data['attachment'] = []; // @ToDo
721 $data['tag'] = self::createTagList($item);
722 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
728 * @brief Transmits a given activity to a target
730 * @param array $activity
731 * @param string $target Target profile
732 * @param integer $uid User ID
734 public static function transmitActivity($activity, $target, $uid)
736 $profile = APContact::getProfileByURL($target);
738 $owner = User::getOwnerDataById($uid);
740 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
741 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
743 'actor' => $owner['url'],
744 'object' => $profile['url'],
745 'to' => $profile['url']];
747 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
749 $signed = LDSignature::sign($data, $owner);
750 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
754 * @brief Transmit a message that the contact request had been accepted
756 * @param string $target Target profile
758 * @param integer $uid User ID
760 public static function transmitContactAccept($target, $id, $uid)
762 $profile = APContact::getProfileByURL($target);
764 $owner = User::getOwnerDataById($uid);
765 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
766 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
768 'actor' => $owner['url'],
769 'object' => ['id' => $id, 'type' => 'Follow',
770 'actor' => $profile['url'],
771 'object' => $owner['url']],
772 'to' => $profile['url']];
774 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
776 $signed = LDSignature::sign($data, $owner);
777 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
783 * @param string $target Target profile
785 * @param integer $uid User ID
787 public static function transmitContactReject($target, $id, $uid)
789 $profile = APContact::getProfileByURL($target);
791 $owner = User::getOwnerDataById($uid);
792 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
793 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
795 'actor' => $owner['url'],
796 'object' => ['id' => $id, 'type' => 'Follow',
797 'actor' => $profile['url'],
798 'object' => $owner['url']],
799 'to' => $profile['url']];
801 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
803 $signed = LDSignature::sign($data, $owner);
804 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
810 * @param string $target Target profile
811 * @param integer $uid User ID
813 public static function transmitContactUndo($target, $uid)
815 $profile = APContact::getProfileByURL($target);
817 $id = System::baseUrl() . '/activity/' . System::createGUID();
819 $owner = User::getOwnerDataById($uid);
820 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
823 'actor' => $owner['url'],
824 'object' => ['id' => $id, 'type' => 'Follow',
825 'actor' => $owner['url'],
826 'object' => $profile['url']],
827 'to' => $profile['url']];
829 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
831 $signed = LDSignature::sign($data, $owner);
832 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
836 * Fetches ActivityPub content from the given url
838 * @param string $url content url
841 public static function fetchContent($url)
843 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
844 if (!$ret['success'] || empty($ret['body'])) {
847 return json_decode($ret['body'], true);
851 * Fetches a profile from the given url into an array that is compatible to Probe::uri
853 * @param string $url profile url
856 public static function probeProfile($url)
858 $apcontact = APContact::getProfileByURL($url, true);
859 if (empty($apcontact)) {
863 $profile = ['network' => Protocol::ACTIVITYPUB];
864 $profile['nick'] = $apcontact['nick'];
865 $profile['name'] = $apcontact['name'];
866 $profile['guid'] = $apcontact['uuid'];
867 $profile['url'] = $apcontact['url'];
868 $profile['addr'] = $apcontact['addr'];
869 $profile['alias'] = $apcontact['alias'];
870 $profile['photo'] = $apcontact['photo'];
871 // $profile['community']
872 // $profile['keywords']
873 // $profile['location']
874 $profile['about'] = $apcontact['about'];
875 $profile['batch'] = $apcontact['sharedinbox'];
876 $profile['notify'] = $apcontact['inbox'];
877 $profile['poll'] = $apcontact['outbox'];
878 $profile['pubkey'] = $apcontact['pubkey'];
879 $profile['baseurl'] = $apcontact['baseurl'];
881 // Remove all "null" fields
882 foreach ($profile as $field => $content) {
883 if (is_null($content)) {
884 unset($profile[$field]);
896 * @param integer $uid User ID
898 public static function processInbox($body, $header, $uid)
900 $http_signer = HTTPSignature::getSigner($body, $header);
901 if (empty($http_signer)) {
902 logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
905 logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
908 $activity = json_decode($body, true);
910 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
911 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
913 if (empty($activity)) {
914 logger('Invalid body.', LOGGER_DEBUG);
918 if (LDSignature::isSigned($activity)) {
919 $ld_signer = LDSignature::getSigner($activity);
920 if (empty($ld_signer)) {
921 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
923 if (!empty($ld_signer && ($actor == $http_signer))) {
924 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
925 $trust_source = true;
926 } elseif (!empty($ld_signer)) {
927 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
928 $trust_source = true;
929 } elseif ($actor == $http_signer) {
930 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
931 $trust_source = true;
933 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
934 $trust_source = false;
936 } elseif ($actor == $http_signer) {
937 logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
938 $trust_source = true;
940 logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
941 $trust_source = false;
944 self::processActivity($activity, $body, $uid, $trust_source);
951 * @param integer $uid User ID
953 public static function fetchOutbox($url, $uid)
955 $data = self::fetchContent($url);
960 if (!empty($data['orderedItems'])) {
961 $items = $data['orderedItems'];
962 } elseif (!empty($data['first']['orderedItems'])) {
963 $items = $data['first']['orderedItems'];
964 } elseif (!empty($data['first'])) {
965 self::fetchOutbox($data['first'], $uid);
971 foreach ($items as $activity) {
972 self::processActivity($activity, '', $uid, true);
979 * @param array $activity
980 * @param integer $uid User ID
981 * @param $trust_source
985 private static function prepareObjectData($activity, $uid, &$trust_source)
987 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
989 logger('Empty actor', LOGGER_DEBUG);
993 // Fetch all receivers from to, cc, bto and bcc
994 $receivers = self::getReceivers($activity, $actor);
996 // When it is a delivery to a personal inbox we add that user to the receivers
998 $owner = User::getOwnerDataById($uid);
999 $additional = ['uid:' . $uid => $uid];
1000 $receivers = array_merge($receivers, $additional);
1003 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
1005 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
1006 if (empty($object_id)) {
1007 logger('No object found', LOGGER_DEBUG);
1011 // Fetch the content only on activities where this matters
1012 if (in_array($activity['type'], ['Create', 'Announce'])) {
1013 $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
1014 if (empty($object_data)) {
1015 logger("Object data couldn't be processed", LOGGER_DEBUG);
1018 // We had been able to retrieve the object data - so we can trust the source
1019 $trust_source = true;
1020 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
1021 // Create a mostly empty array out of the activity data (instead of the object).
1022 // This way we later don't have to check for the existence of ech individual array element.
1023 $object_data = self::processObject($activity);
1024 $object_data['name'] = $activity['type'];
1025 $object_data['author'] = $activity['actor'];
1026 $object_data['object'] = $object_id;
1027 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
1030 $object_data['id'] = $activity['id'];
1031 $object_data['object'] = $activity['object'];
1032 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
1035 $object_data = self::addActivityFields($object_data, $activity);
1037 $object_data['type'] = $activity['type'];
1038 $object_data['owner'] = $actor;
1039 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
1041 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
1043 return $object_data;
1049 * @param array $activity
1051 * @param integer $uid User ID
1052 * @param $trust_source
1054 private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
1056 if (empty($activity['type'])) {
1057 logger('Empty type', LOGGER_DEBUG);
1061 if (empty($activity['object'])) {
1062 logger('Empty object', LOGGER_DEBUG);
1066 if (empty($activity['actor'])) {
1067 logger('Empty actor', LOGGER_DEBUG);
1072 // $trust_source is called by reference and is set to true if the content was retrieved successfully
1073 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
1074 if (empty($object_data)) {
1075 logger('No object data found', LOGGER_DEBUG);
1079 if (!$trust_source) {
1080 logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
1083 switch ($activity['type']) {
1086 self::createItem($object_data, $body);
1090 self::likeItem($object_data, $body);
1094 self::dislikeItem($object_data, $body);
1098 if (in_array($object_data['object_type'], ['Person', 'Organization', 'Service', 'Group', 'Application'])) {
1099 self::updatePerson($object_data, $body);
1107 self::followUser($object_data);
1111 if ($object_data['object_type'] == 'Follow') {
1112 self::acceptFollowUser($object_data);
1117 if ($object_data['object_type'] == 'Follow') {
1118 self::undoFollowUser($object_data);
1119 } elseif (in_array($object_data['object_type'], ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'])) {
1120 self::undoActivity($object_data);
1125 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
1133 * @param array $activity
1138 private static function getReceivers($activity, $actor)
1142 // When it is an answer, we inherite the receivers from the parent
1143 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1144 if (!empty($replyto)) {
1145 $parents = Item::select(['uid'], ['uri' => $replyto]);
1146 while ($parent = Item::fetch($parents)) {
1147 $receivers['uid:' . $parent['uid']] = $parent['uid'];
1151 if (!empty($actor)) {
1152 $profile = APContact::getProfileByURL($actor);
1153 $followers = defaults($profile, 'followers', '');
1155 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
1157 logger('Empty actor', LOGGER_DEBUG);
1161 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
1162 if (empty($activity[$element])) {
1166 // The receiver can be an array or a string
1167 if (is_string($activity[$element])) {
1168 $activity[$element] = [$activity[$element]];
1171 foreach ($activity[$element] as $receiver) {
1172 if ($receiver == self::PUBLIC_COLLECTION) {
1173 $receivers['uid:0'] = 0;
1176 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
1177 // This will most likely catch all OStatus connections to Mastodon
1178 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
1179 , 'archive' => false, 'pending' => false];
1180 $contacts = DBA::select('contact', ['uid'], $condition);
1181 while ($contact = DBA::fetch($contacts)) {
1182 if ($contact['uid'] != 0) {
1183 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1186 DBA::close($contacts);
1189 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
1190 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1191 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
1192 $contacts = DBA::select('contact', ['uid'], $condition);
1193 while ($contact = DBA::fetch($contacts)) {
1194 if ($contact['uid'] != 0) {
1195 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1198 DBA::close($contacts);
1202 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1203 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1204 if (!DBA::isResult($contact)) {
1207 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1211 self::switchContacts($receivers, $actor);
1220 * @param integer $uid User ID
1223 private static function switchContact($cid, $uid, $url)
1225 $profile = ActivityPub::probeProfile($url);
1226 if (empty($profile)) {
1230 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
1232 $photo = $profile['photo'];
1233 unset($profile['photo']);
1234 unset($profile['baseurl']);
1236 $profile['nurl'] = normalise_link($profile['url']);
1237 DBA::update('contact', $profile, ['id' => $cid]);
1239 Contact::updateAvatar($photo, $uid, $cid);
1248 private static function switchContacts($receivers, $actor)
1250 if (empty($actor)) {
1254 foreach ($receivers as $receiver) {
1255 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
1256 if (DBA::isResult($contact)) {
1257 self::switchContact($contact['id'], $receiver, $actor);
1260 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
1261 if (DBA::isResult($contact)) {
1262 self::switchContact($contact['id'], $receiver, $actor);
1270 * @param $object_data
1271 * @param array $activity
1275 private static function addActivityFields($object_data, $activity)
1277 if (!empty($activity['published']) && empty($object_data['published'])) {
1278 $object_data['published'] = $activity['published'];
1281 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1282 $object_data['updated'] = $activity['updated'];
1285 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1286 $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1289 if (!empty($activity['instrument'])) {
1290 $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1292 return $object_data;
1300 * @param $trust_source
1304 private static function fetchObject($object_id, $object = [], $trust_source = false)
1306 if (!$trust_source || is_string($object)) {
1307 $data = self::fetchContent($object_id);
1309 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
1312 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
1315 logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
1319 if (is_string($data)) {
1320 $item = Item::selectFirst([], ['uri' => $data]);
1321 if (!DBA::isResult($item)) {
1322 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1325 logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
1326 $data = self::createNote($item);
1329 if (empty($data['type'])) {
1330 logger('Empty type', LOGGER_DEBUG);
1334 switch ($data['type']) {
1338 return self::processObject($data);
1341 if (empty($data['object'])) {
1344 return self::fetchObject($data['object']);
1351 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
1363 private static function processObject(&$object)
1365 if (empty($object['id'])) {
1370 $object_data['object_type'] = $object['type'];
1371 $object_data['id'] = $object['id'];
1373 if (!empty($object['inReplyTo'])) {
1374 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1376 $object_data['reply-to-id'] = $object_data['id'];
1379 $object_data['published'] = defaults($object, 'published', null);
1380 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1382 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1383 $object_data['published'] = $object_data['updated'];
1386 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
1387 if (empty($actor)) {
1388 $actor = defaults($object, 'actor', null);
1391 $object_data['diaspora:guid'] = defaults($object, 'diaspora:guid', null);
1392 $object_data['owner'] = $object_data['author'] = $actor;
1393 $object_data['context'] = defaults($object, 'context', null);
1394 $object_data['conversation'] = defaults($object, 'conversation', null);
1395 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1396 $object_data['name'] = defaults($object, 'title', null);
1397 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1398 $object_data['summary'] = defaults($object, 'summary', null);
1399 $object_data['content'] = defaults($object, 'content', null);
1400 $object_data['source'] = defaults($object, 'source', null);
1401 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1402 $object_data['attachments'] = defaults($object, 'attachment', null);
1403 $object_data['tags'] = defaults($object, 'tag', null);
1404 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1405 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1406 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1408 // Common object data:
1411 // @context, type, actor, signature, mediaType, duration, replies, icon
1413 // Also missing: (Defined in the standard, but currently unused)
1414 // audience, preview, endTime, startTime, generator, image
1419 // contentMap, announcement_count, announcements, context_id, likes, like_count
1420 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1425 // category, licence, language, commentsEnabled
1428 // views, waitTranscoding, state, support, subtitleLanguage
1429 // likes, dislikes, shares, comments
1431 return $object_data;
1435 * @brief Converts mentions from Pleroma into the Friendica format
1437 * @param string $body
1439 * @return converted body
1441 private static function convertMentions($body)
1443 $URLSearchString = "^\[\]";
1444 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1450 * @brief Constructs a string with tags for a given tag array
1452 * @param array $tags
1453 * @param boolean $sensitive
1455 * @return string with tags
1457 private static function constructTagList($tags, $sensitive)
1464 foreach ($tags as $tag) {
1465 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1466 if (!empty($tag_text)) {
1470 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1474 /// @todo add nsfw for $sensitive
1482 * @param $attachments
1483 * @param array $item
1485 * @return item array
1487 private static function constructAttachList($attachments, $item)
1489 if (empty($attachments)) {
1493 foreach ($attachments as $attach) {
1494 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1495 if ($filetype == 'image') {
1496 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1498 if (!empty($item["attach"])) {
1499 $item["attach"] .= ',';
1501 $item["attach"] = '';
1503 if (!isset($attach['length'])) {
1504 $attach['length'] = "0";
1506 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1516 * @param array $activity
1519 private static function createItem($activity, $body)
1522 $item['verb'] = ACTIVITY_POST;
1523 $item['parent-uri'] = $activity['reply-to-id'];
1525 if ($activity['reply-to-id'] == $activity['id']) {
1526 $item['gravity'] = GRAVITY_PARENT;
1527 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1529 $item['gravity'] = GRAVITY_COMMENT;
1530 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1533 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
1534 logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
1535 self::fetchMissingActivity($activity['reply-to-id'], $activity);
1538 self::postItem($activity, $item, $body);
1544 * @param array $activity
1547 private static function likeItem($activity, $body)
1550 $item['verb'] = ACTIVITY_LIKE;
1551 $item['parent-uri'] = $activity['object'];
1552 $item['gravity'] = GRAVITY_ACTIVITY;
1553 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1555 self::postItem($activity, $item, $body);
1561 * @param array $activity
1564 private static function dislikeItem($activity, $body)
1567 $item['verb'] = ACTIVITY_DISLIKE;
1568 $item['parent-uri'] = $activity['object'];
1569 $item['gravity'] = GRAVITY_ACTIVITY;
1570 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1572 self::postItem($activity, $item, $body);
1578 * @param array $activity
1579 * @param array $item
1582 private static function postItem($activity, $item, $body)
1584 /// @todo What to do with $activity['context']?
1586 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
1587 logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
1591 $item['network'] = Protocol::ACTIVITYPUB;
1592 $item['private'] = !in_array(0, $activity['receiver']);
1593 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1594 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1595 $item['uri'] = $activity['id'];
1596 $item['created'] = $activity['published'];
1597 $item['edited'] = $activity['updated'];
1598 $item['guid'] = $activity['diaspora:guid'];
1599 $item['title'] = HTML::toBBCode($activity['name']);
1600 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1601 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1602 $item['location'] = $activity['location'];
1603 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1604 $item['app'] = $activity['service'];
1605 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1607 $item = self::constructAttachList($activity['attachments'], $item);
1609 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1610 if (!empty($source)) {
1611 $item['body'] = $source;
1614 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1615 $item['source'] = $body;
1616 $item['conversation-href'] = $activity['context'];
1617 $item['conversation-uri'] = $activity['conversation'];
1619 foreach ($activity['receiver'] as $receiver) {
1620 $item['uid'] = $receiver;
1621 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1623 if (($receiver != 0) && empty($item['contact-id'])) {
1624 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1627 $item_id = Item::insert($item);
1628 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1638 private static function fetchMissingActivity($url, $child)
1640 if (Config::get('system', 'ostatus_full_threads')) {
1644 $object = ActivityPub::fetchContent($url);
1645 if (empty($object)) {
1646 logger('Activity ' . $url . ' was not fetchable, aborting.');
1651 $activity['@context'] = $object['@context'];
1652 unset($object['@context']);
1653 $activity['id'] = $object['id'];
1654 $activity['to'] = defaults($object, 'to', []);
1655 $activity['cc'] = defaults($object, 'cc', []);
1656 $activity['actor'] = $child['author'];
1657 $activity['object'] = $object;
1658 $activity['published'] = $object['published'];
1659 $activity['type'] = 'Create';
1661 self::processActivity($activity);
1662 logger('Activity ' . $url . ' had been fetched and processed.');
1666 * @brief perform a "follow" request
1668 * @param array $activity
1670 private static function followUser($activity)
1672 $actor = JsonLD::fetchElement($activity, 'object', 'id');
1673 $uid = User::getIdForURL($actor);
1678 $owner = User::getOwnerDataById($uid);
1680 $cid = Contact::getIdForURL($activity['owner'], $uid);
1682 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1687 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1688 'author-link' => $activity['owner']];
1690 Contact::addRelationship($owner, $contact, $item);
1691 $cid = Contact::getIdForURL($activity['owner'], $uid);
1696 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1697 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1698 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1701 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1702 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1706 * @brief Update the given profile
1708 * @param array $activity
1710 private static function updatePerson($activity)
1712 if (empty($activity['object']['id'])) {
1716 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
1717 APContact::getProfileByURL($activity['object']['id'], true);
1721 * @brief Accept a follow request
1723 * @param array $activity
1725 private static function acceptFollowUser($activity)
1727 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1728 $uid = User::getIdForURL($actor);
1733 $owner = User::getOwnerDataById($uid);
1735 $cid = Contact::getIdForURL($activity['owner'], $uid);
1737 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1741 $fields = ['pending' => false];
1743 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1744 if ($contact['rel'] == Contact::FOLLOWER) {
1745 $fields['rel'] = Contact::FRIEND;
1748 $condition = ['id' => $cid];
1749 DBA::update('contact', $fields, $condition);
1750 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1754 * @brief Undo activity like "like" or "dislike"
1756 * @param array $activity
1758 private static function undoActivity($activity)
1760 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
1761 if (empty($activity_url)) {
1765 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1766 if (empty($actor)) {
1770 $author_id = Contact::getIdForURL($actor);
1771 if (empty($author_id)) {
1775 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1779 * @brief Activity to remove a follower
1781 * @param array $activity
1783 private static function undoFollowUser($activity)
1785 $object = JsonLD::fetchElement($activity, 'object', 'object');
1786 $uid = User::getIdForURL($object);
1791 $owner = User::getOwnerDataById($uid);
1793 $cid = Contact::getIdForURL($activity['owner'], $uid);
1795 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1799 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1800 if (!DBA::isResult($contact)) {
1804 Contact::removeFollower($owner, $contact);
1805 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);