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
47 * - Update (Image, Video, Article, Note)
51 * Check what this is meant to do:
57 * - Undo Accept (Problem: This could invert a contact accept or an event accept)
69 * - Queueing unsucessful deliveries
70 * - Polling the outboxes for missing content?
71 * - Possibly using the LD-JSON parser
75 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
76 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
77 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
78 'diaspora' => 'https://diasporafoundation.org/ns/',
79 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
80 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
81 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
82 const CONTENT_TYPES = ['Note', 'Article', 'Video', 'Image'];
83 const ACTIVITY_TYPES = ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'];
85 * @brief Checks if the web request is done for the AP protocol
89 public static function isRequest()
91 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
92 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
96 * Return the ActivityPub profile of the given user
98 * @param integer $uid User ID
99 * @return profile array
101 public static function profile($uid)
103 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
104 'account_removed' => false, 'verified' => true];
105 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
106 $user = DBA::selectFirst('user', $fields, $condition);
107 if (!DBA::isResult($user)) {
111 $fields = ['locality', 'region', 'country-name'];
112 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
113 if (!DBA::isResult($profile)) {
117 $fields = ['name', 'url', 'location', 'about', 'avatar'];
118 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
119 if (!DBA::isResult($contact)) {
123 $data = ['@context' => self::CONTEXT];
124 $data['id'] = $contact['url'];
125 $data['diaspora:guid'] = $user['guid'];
126 $data['type'] = self::ACCOUNT_TYPES[$user['account-type']];
127 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
128 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
129 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
130 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
131 $data['preferredUsername'] = $user['nickname'];
132 $data['name'] = $contact['name'];
133 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
134 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
135 $data['summary'] = $contact['about'];
136 $data['url'] = $contact['url'];
137 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
138 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
139 'owner' => $contact['url'],
140 'publicKeyPem' => $user['pubkey']];
141 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
142 $data['icon'] = ['type' => 'Image',
143 'url' => $contact['avatar']];
145 // tags: https://kitty.town/@inmysocks/100656097926961126.json
150 * @brief Returns an array with permissions of a given item array
154 * @return array with permissions
156 private static function fetchPermissionBlockFromConversation($item)
158 if (empty($item['thr-parent'])) {
162 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
163 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
164 if (!DBA::isResult($conversation)) {
168 $activity = json_decode($conversation['source'], true);
170 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
171 $profile = APContact::getByURL($actor);
173 $item_profile = APContact::getByURL($item['author-link']);
174 $exclude[] = $item['author-link'];
176 if ($item['gravity'] == GRAVITY_PARENT) {
177 $exclude[] = $item['owner-link'];
180 $permissions['to'][] = $actor;
182 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
183 if (empty($activity[$element])) {
186 if (is_string($activity[$element])) {
187 $activity[$element] = [$activity[$element]];
190 foreach ($activity[$element] as $receiver) {
191 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
192 $receiver = $item_profile['followers'];
194 if (!in_array($receiver, $exclude)) {
195 $permissions[$element][] = $receiver;
203 * @brief Creates an array of permissions from an item thread
207 * @return permission array
209 public static function createPermissionBlockForItem($item)
211 $data = ['to' => [], 'cc' => []];
213 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
215 $actor_profile = APContact::getByURL($item['author-link']);
217 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
219 $contacts[$item['author-link']] = $item['author-link'];
221 if (!$item['private']) {
222 $data['to'][] = self::PUBLIC_COLLECTION;
223 if (!empty($actor_profile['followers'])) {
224 $data['cc'][] = $actor_profile['followers'];
227 foreach ($terms as $term) {
228 $profile = APContact::getByURL($term['url'], false);
229 if (!empty($profile) && empty($contacts[$profile['url']])) {
230 $data['cc'][] = $profile['url'];
231 $contacts[$profile['url']] = $profile['url'];
235 $receiver_list = Item::enumeratePermissions($item);
239 foreach ($terms as $term) {
240 $cid = Contact::getIdForURL($term['url'], $item['uid']);
241 if (!empty($cid) && in_array($cid, $receiver_list)) {
242 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
243 $data['to'][] = $contact['url'];
244 $contacts[$contact['url']] = $contact['url'];
248 foreach ($receiver_list as $receiver) {
249 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
250 if (empty($contacts[$contact['url']])) {
251 $data['cc'][] = $contact['url'];
252 $contacts[$contact['url']] = $contact['url'];
257 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
258 while ($parent = Item::fetch($parents)) {
259 // Don't include data from future posts
260 if ($parent['id'] >= $item['id']) {
264 $profile = APContact::getByURL($parent['author-link'], false);
265 if (!empty($profile) && empty($contacts[$profile['url']])) {
266 $data['cc'][] = $profile['url'];
267 $contacts[$profile['url']] = $profile['url'];
270 if ($item['gravity'] != GRAVITY_PARENT) {
274 $profile = APContact::getByURL($parent['owner-link'], false);
275 if (!empty($profile) && empty($contacts[$profile['url']])) {
276 $data['cc'][] = $profile['url'];
277 $contacts[$profile['url']] = $profile['url'];
280 DBA::close($parents);
282 if (empty($data['to'])) {
283 $data['to'] = $data['cc'];
291 * @brief Fetches a list of inboxes of followers of a given user
293 * @param integer $uid User ID
295 * @return array of follower inboxes
297 public static function fetchTargetInboxesforUser($uid)
301 $condition = ['uid' => $uid, 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
304 $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
307 $contacts = DBA::select('contact', ['notify', 'batch'], $condition);
308 while ($contact = DBA::fetch($contacts)) {
309 $contact = defaults($contact, 'batch', $contact['notify']);
310 $inboxes[$contact] = $contact;
312 DBA::close($contacts);
318 * @brief Fetches an array of inboxes for the given item and user
321 * @param integer $uid User ID
323 * @return array with inboxes
325 public static function fetchTargetInboxes($item, $uid)
327 $permissions = self::createPermissionBlockForItem($item);
328 if (empty($permissions)) {
334 if ($item['gravity'] == GRAVITY_ACTIVITY) {
335 $item_profile = APContact::getByURL($item['author-link']);
337 $item_profile = APContact::getByURL($item['owner-link']);
340 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
341 if (empty($permissions[$element])) {
345 foreach ($permissions[$element] as $receiver) {
346 if ($receiver == $item_profile['followers']) {
347 $inboxes = self::fetchTargetInboxesforUser($uid);
349 $profile = APContact::getByURL($receiver);
350 if (!empty($profile)) {
351 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
352 $inboxes[$target] = $target;
362 * @brief Returns the activity type of a given item
366 * @return activity type
368 public static function getTypeOfItem($item)
370 if ($item['verb'] == ACTIVITY_POST) {
371 if ($item['created'] == $item['edited']) {
376 } elseif ($item['verb'] == ACTIVITY_LIKE) {
378 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
380 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
382 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
384 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
385 $type = 'TentativeAccept';
394 * @brief Creates an activity array for a given item id
396 * @param integer $item_id
397 * @param boolean $object_mode Is the activity item is used inside another object?
399 * @return array of activity
401 public static function createActivityFromItem($item_id, $object_mode = false)
403 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
405 if (!DBA::isResult($item)) {
409 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
410 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
411 if (DBA::isResult($conversation)) {
412 $data = json_decode($conversation['source']);
418 $type = self::getTypeOfItem($item);
421 $data = ['@context' => self::CONTEXT];
423 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
425 } elseif ($item['deleted']) {
432 $data['id'] = $item['uri'] . '#' . $type;
433 $data['type'] = $type;
434 $data['actor'] = $item['author-link'];
436 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
438 if ($item["created"] != $item["edited"]) {
439 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
442 $data['context'] = self::fetchContextURLForItem($item);
444 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
446 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
447 $data['object'] = self::createNote($item);
448 } elseif ($data['type'] == 'Undo') {
449 $data['object'] = self::createActivityFromItem($item_id, true);
451 $data['object'] = $item['thr-parent'];
454 $owner = User::getOwnerDataById($item['uid']);
457 return LDSignature::sign($data, $owner);
464 * @brief Creates an object array for a given item id
466 * @param integer $item_id
468 * @return object array
470 public static function createObjectFromItemID($item_id)
472 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
474 if (!DBA::isResult($item)) {
478 $data = ['@context' => self::CONTEXT];
479 $data = array_merge($data, self::createNote($item));
485 * @brief Returns a tag array for a given item array
489 * @return array of tags
491 private static function createTagList($item)
495 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
496 foreach ($terms as $term) {
497 $contact = Contact::getDetailsByURL($term['url']);
498 if (!empty($contact['addr'])) {
499 $mention = '@' . $contact['addr'];
501 $mention = '@' . $term['url'];
504 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
510 * @brief Fetches the "context" value for a givem item array from the "conversation" table
514 * @return string with context url
516 private static function fetchContextURLForItem($item)
518 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
519 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
520 $context_uri = $conversation['conversation-href'];
521 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
522 $context_uri = $conversation['conversation-uri'];
524 $context_uri = str_replace('/objects/', '/context/', $item['parent-uri']);
530 * @brief Creates a note/article object array
534 * @return object array
536 private static function createNote($item)
538 if (!empty($item['title'])) {
544 if ($item['deleted']) {
549 $data['id'] = $item['uri'];
550 $data['type'] = $type;
552 if ($item['deleted']) {
556 $data['summary'] = null; // Ignore by now
558 if ($item['uri'] != $item['thr-parent']) {
559 $data['inReplyTo'] = $item['thr-parent'];
561 $data['inReplyTo'] = null;
564 $data['diaspora:guid'] = $item['guid'];
565 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
567 if ($item["created"] != $item["edited"]) {
568 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
571 $data['url'] = $item['plink'];
572 $data['attributedTo'] = $item['author-link'];
573 $data['actor'] = $item['author-link'];
574 $data['sensitive'] = false; // - Query NSFW
575 $data['context'] = self::fetchContextURLForItem($item);
577 if (!empty($item['title'])) {
578 $data['name'] = BBCode::convert($item['title'], false, 7);
581 $data['content'] = BBCode::convert($item['body'], false, 7);
582 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
584 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
585 $data['diaspora:comment'] = $item['signed_text'];
588 $data['attachment'] = []; // @ToDo
589 $data['tag'] = self::createTagList($item);
590 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
596 * @brief Transmits a profile deletion to a given inbox
598 * @param integer $uid User ID
599 * @param string $inbox Target inbox
601 public static function transmitProfileDeletion($uid, $inbox)
603 $owner = User::getOwnerDataById($uid);
604 $profile = APContact::getByURL($owner['url']);
606 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
607 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
609 'actor' => $owner['url'],
610 'object' => self::profile($uid),
611 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
612 'to' => [self::PUBLIC_COLLECTION],
615 $signed = LDSignature::sign($data, $owner);
617 logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
618 HTTPSignature::transmit($signed, $inbox, $uid);
622 * @brief Transmits a profile change to a given inbox
624 * @param integer $uid User ID
625 * @param string $inbox Target inbox
627 public static function transmitProfileUpdate($uid, $inbox)
629 $owner = User::getOwnerDataById($uid);
630 $profile = APContact::getByURL($owner['url']);
632 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
633 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
635 'actor' => $owner['url'],
636 'object' => self::profile($uid),
637 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
638 'to' => [$profile['followers']],
641 $signed = LDSignature::sign($data, $owner);
643 logger('Deliver profile update for user ' . $uid . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
644 HTTPSignature::transmit($signed, $inbox, $uid);
648 * @brief Transmits a given activity to a target
650 * @param array $activity
651 * @param string $target Target profile
652 * @param integer $uid User ID
654 public static function transmitActivity($activity, $target, $uid)
656 $profile = APContact::getByURL($target);
658 $owner = User::getOwnerDataById($uid);
660 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
661 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
663 'actor' => $owner['url'],
664 'object' => $profile['url'],
665 'to' => $profile['url']];
667 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
669 $signed = LDSignature::sign($data, $owner);
670 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
674 * @brief Transmit a message that the contact request had been accepted
676 * @param string $target Target profile
678 * @param integer $uid User ID
680 public static function transmitContactAccept($target, $id, $uid)
682 $profile = APContact::getByURL($target);
684 $owner = User::getOwnerDataById($uid);
685 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
686 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
688 'actor' => $owner['url'],
689 'object' => ['id' => $id, 'type' => 'Follow',
690 'actor' => $profile['url'],
691 'object' => $owner['url']],
692 'to' => $profile['url']];
694 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
696 $signed = LDSignature::sign($data, $owner);
697 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
703 * @param string $target Target profile
705 * @param integer $uid User ID
707 public static function transmitContactReject($target, $id, $uid)
709 $profile = APContact::getByURL($target);
711 $owner = User::getOwnerDataById($uid);
712 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
713 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
715 'actor' => $owner['url'],
716 'object' => ['id' => $id, 'type' => 'Follow',
717 'actor' => $profile['url'],
718 'object' => $owner['url']],
719 'to' => $profile['url']];
721 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
723 $signed = LDSignature::sign($data, $owner);
724 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
730 * @param string $target Target profile
731 * @param integer $uid User ID
733 public static function transmitContactUndo($target, $uid)
735 $profile = APContact::getByURL($target);
737 $id = System::baseUrl() . '/activity/' . System::createGUID();
739 $owner = User::getOwnerDataById($uid);
740 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
743 'actor' => $owner['url'],
744 'object' => ['id' => $id, 'type' => 'Follow',
745 'actor' => $owner['url'],
746 'object' => $profile['url']],
747 'to' => $profile['url']];
749 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
751 $signed = LDSignature::sign($data, $owner);
752 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
756 * Fetches ActivityPub content from the given url
758 * @param string $url content url
761 public static function fetchContent($url)
763 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
764 if (!$ret['success'] || empty($ret['body'])) {
768 return json_decode($ret['body'], true);
772 * Fetches a profile from the given url into an array that is compatible to Probe::uri
774 * @param string $url profile url
777 public static function probeProfile($url)
779 $apcontact = APContact::getByURL($url, true);
780 if (empty($apcontact)) {
784 $profile = ['network' => Protocol::ACTIVITYPUB];
785 $profile['nick'] = $apcontact['nick'];
786 $profile['name'] = $apcontact['name'];
787 $profile['guid'] = $apcontact['uuid'];
788 $profile['url'] = $apcontact['url'];
789 $profile['addr'] = $apcontact['addr'];
790 $profile['alias'] = $apcontact['alias'];
791 $profile['photo'] = $apcontact['photo'];
792 // $profile['community']
793 // $profile['keywords']
794 // $profile['location']
795 $profile['about'] = $apcontact['about'];
796 $profile['batch'] = $apcontact['sharedinbox'];
797 $profile['notify'] = $apcontact['inbox'];
798 $profile['poll'] = $apcontact['outbox'];
799 $profile['pubkey'] = $apcontact['pubkey'];
800 $profile['baseurl'] = $apcontact['baseurl'];
802 // Remove all "null" fields
803 foreach ($profile as $field => $content) {
804 if (is_null($content)) {
805 unset($profile[$field]);
817 * @param integer $uid User ID
819 public static function processInbox($body, $header, $uid)
821 $http_signer = HTTPSignature::getSigner($body, $header);
822 if (empty($http_signer)) {
823 logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
826 logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
829 $activity = json_decode($body, true);
831 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
832 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
834 if (empty($activity)) {
835 logger('Invalid body.', LOGGER_DEBUG);
839 if (LDSignature::isSigned($activity)) {
840 $ld_signer = LDSignature::getSigner($activity);
841 if (empty($ld_signer)) {
842 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
844 if (!empty($ld_signer && ($actor == $http_signer))) {
845 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
846 $trust_source = true;
847 } elseif (!empty($ld_signer)) {
848 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
849 $trust_source = true;
850 } elseif ($actor == $http_signer) {
851 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
852 $trust_source = true;
854 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
855 $trust_source = false;
857 } elseif ($actor == $http_signer) {
858 logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
859 $trust_source = true;
861 logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
862 $trust_source = false;
865 self::processActivity($activity, $body, $uid, $trust_source);
872 * @param integer $uid User ID
874 public static function fetchOutbox($url, $uid)
876 $data = self::fetchContent($url);
881 if (!empty($data['orderedItems'])) {
882 $items = $data['orderedItems'];
883 } elseif (!empty($data['first']['orderedItems'])) {
884 $items = $data['first']['orderedItems'];
885 } elseif (!empty($data['first'])) {
886 self::fetchOutbox($data['first'], $uid);
892 foreach ($items as $activity) {
893 self::processActivity($activity, '', $uid, true);
900 * @param array $activity
901 * @param integer $uid User ID
902 * @param $trust_source
906 private static function prepareObjectData($activity, $uid, &$trust_source)
908 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
910 logger('Empty actor', LOGGER_DEBUG);
914 // Fetch all receivers from to, cc, bto and bcc
915 $receivers = self::getReceivers($activity, $actor);
917 // When it is a delivery to a personal inbox we add that user to the receivers
919 $owner = User::getOwnerDataById($uid);
920 $additional = ['uid:' . $uid => $uid];
921 $receivers = array_merge($receivers, $additional);
924 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
926 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
927 if (empty($object_id)) {
928 logger('No object found', LOGGER_DEBUG);
932 // Fetch the content only on activities where this matters
933 if (in_array($activity['type'], ['Create', 'Announce'])) {
934 $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
935 if (empty($object_data)) {
936 logger("Object data couldn't be processed", LOGGER_DEBUG);
939 // We had been able to retrieve the object data - so we can trust the source
940 $trust_source = true;
941 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
942 // Create a mostly empty array out of the activity data (instead of the object).
943 // This way we later don't have to check for the existence of ech individual array element.
944 $object_data = self::processObject($activity);
945 $object_data['name'] = $activity['type'];
946 $object_data['author'] = $activity['actor'];
947 $object_data['object'] = $object_id;
948 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
951 $object_data['id'] = $activity['id'];
952 $object_data['object'] = $activity['object'];
953 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
956 $object_data = self::addActivityFields($object_data, $activity);
958 $object_data['type'] = $activity['type'];
959 $object_data['owner'] = $actor;
960 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
962 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
970 * @param array $activity
972 * @param integer $uid User ID
973 * @param $trust_source
975 private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
977 if (empty($activity['type'])) {
978 logger('Empty type', LOGGER_DEBUG);
982 if (empty($activity['object'])) {
983 logger('Empty object', LOGGER_DEBUG);
987 if (empty($activity['actor'])) {
988 logger('Empty actor', LOGGER_DEBUG);
993 // $trust_source is called by reference and is set to true if the content was retrieved successfully
994 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
995 if (empty($object_data)) {
996 logger('No object data found', LOGGER_DEBUG);
1000 if (!$trust_source) {
1001 logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
1004 switch ($activity['type']) {
1007 self::createItem($object_data, $body);
1011 self::likeItem($object_data, $body);
1015 self::dislikeItem($object_data, $body);
1019 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
1021 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
1022 self::updatePerson($object_data, $body);
1027 if ($object_data['object_type'] == 'Tombstone') {
1028 self::deleteItem($object_data, $body);
1029 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
1030 self::deletePerson($object_data, $body);
1035 self::followUser($object_data);
1039 if ($object_data['object_type'] == 'Follow') {
1040 self::acceptFollowUser($object_data);
1045 if ($object_data['object_type'] == 'Follow') {
1046 self::rejectFollowUser($object_data);
1051 if ($object_data['object_type'] == 'Follow') {
1052 self::undoFollowUser($object_data);
1053 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES)) {
1054 self::undoActivity($object_data);
1059 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
1067 * @param array $activity
1072 private static function getReceivers($activity, $actor)
1076 // When it is an answer, we inherite the receivers from the parent
1077 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1078 if (!empty($replyto)) {
1079 $parents = Item::select(['uid'], ['uri' => $replyto]);
1080 while ($parent = Item::fetch($parents)) {
1081 $receivers['uid:' . $parent['uid']] = $parent['uid'];
1085 if (!empty($actor)) {
1086 $profile = APContact::getByURL($actor);
1087 $followers = defaults($profile, 'followers', '');
1089 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
1091 logger('Empty actor', LOGGER_DEBUG);
1095 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
1096 if (empty($activity[$element])) {
1100 // The receiver can be an array or a string
1101 if (is_string($activity[$element])) {
1102 $activity[$element] = [$activity[$element]];
1105 foreach ($activity[$element] as $receiver) {
1106 if ($receiver == self::PUBLIC_COLLECTION) {
1107 $receivers['uid:0'] = 0;
1110 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
1111 // This will most likely catch all OStatus connections to Mastodon
1112 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
1113 , 'archive' => false, 'pending' => false];
1114 $contacts = DBA::select('contact', ['uid'], $condition);
1115 while ($contact = DBA::fetch($contacts)) {
1116 if ($contact['uid'] != 0) {
1117 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1120 DBA::close($contacts);
1123 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
1124 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1125 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
1126 $contacts = DBA::select('contact', ['uid'], $condition);
1127 while ($contact = DBA::fetch($contacts)) {
1128 if ($contact['uid'] != 0) {
1129 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1132 DBA::close($contacts);
1136 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1137 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1138 if (!DBA::isResult($contact)) {
1141 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1145 self::switchContacts($receivers, $actor);
1154 * @param integer $uid User ID
1157 private static function switchContact($cid, $uid, $url)
1159 $profile = ActivityPub::probeProfile($url);
1160 if (empty($profile)) {
1164 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
1166 $photo = $profile['photo'];
1167 unset($profile['photo']);
1168 unset($profile['baseurl']);
1170 $profile['nurl'] = normalise_link($profile['url']);
1171 DBA::update('contact', $profile, ['id' => $cid]);
1173 Contact::updateAvatar($photo, $uid, $cid);
1182 private static function switchContacts($receivers, $actor)
1184 if (empty($actor)) {
1188 foreach ($receivers as $receiver) {
1189 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
1190 if (DBA::isResult($contact)) {
1191 self::switchContact($contact['id'], $receiver, $actor);
1194 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
1195 if (DBA::isResult($contact)) {
1196 self::switchContact($contact['id'], $receiver, $actor);
1204 * @param $object_data
1205 * @param array $activity
1209 private static function addActivityFields($object_data, $activity)
1211 if (!empty($activity['published']) && empty($object_data['published'])) {
1212 $object_data['published'] = $activity['published'];
1215 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1216 $object_data['updated'] = $activity['updated'];
1219 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1220 $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1223 if (!empty($activity['instrument'])) {
1224 $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1226 return $object_data;
1234 * @param $trust_source
1238 private static function fetchObject($object_id, $object = [], $trust_source = false)
1240 if (!$trust_source || is_string($object)) {
1241 $data = self::fetchContent($object_id);
1243 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
1246 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
1249 logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
1253 if (is_string($data)) {
1254 $item = Item::selectFirst([], ['uri' => $data]);
1255 if (!DBA::isResult($item)) {
1256 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1259 logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
1260 $data = self::createNote($item);
1263 if (empty($data['type'])) {
1264 logger('Empty type', LOGGER_DEBUG);
1268 if (in_array($data['type'], self::CONTENT_TYPES)) {
1269 return self::processObject($data);
1272 if ($data['type'] == 'Announce') {
1273 if (empty($data['object'])) {
1276 return self::fetchObject($data['object']);
1279 logger('Unhandled object type: ' . $data['type'], LOGGER_DEBUG);
1289 private static function processObject($object)
1291 if (empty($object['id'])) {
1296 $object_data['object_type'] = $object['type'];
1297 $object_data['id'] = $object['id'];
1299 if (!empty($object['inReplyTo'])) {
1300 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1302 $object_data['reply-to-id'] = $object_data['id'];
1305 $object_data['published'] = defaults($object, 'published', null);
1306 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1308 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1309 $object_data['published'] = $object_data['updated'];
1312 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
1313 if (empty($actor)) {
1314 $actor = defaults($object, 'actor', null);
1317 $object_data['diaspora:guid'] = defaults($object, 'diaspora:guid', null);
1318 $object_data['owner'] = $object_data['author'] = $actor;
1319 $object_data['context'] = defaults($object, 'context', null);
1320 $object_data['conversation'] = defaults($object, 'conversation', null);
1321 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1322 $object_data['name'] = defaults($object, 'title', null);
1323 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1324 $object_data['summary'] = defaults($object, 'summary', null);
1325 $object_data['content'] = defaults($object, 'content', null);
1326 $object_data['source'] = defaults($object, 'source', null);
1327 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1328 $object_data['attachments'] = defaults($object, 'attachment', null);
1329 $object_data['tags'] = defaults($object, 'tag', null);
1330 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1331 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1332 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1334 // Common object data:
1337 // @context, type, actor, signature, mediaType, duration, replies, icon
1339 // Also missing: (Defined in the standard, but currently unused)
1340 // audience, preview, endTime, startTime, generator, image
1345 // contentMap, announcement_count, announcements, context_id, likes, like_count
1346 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1351 // category, licence, language, commentsEnabled
1354 // views, waitTranscoding, state, support, subtitleLanguage
1355 // likes, dislikes, shares, comments
1357 return $object_data;
1361 * @brief Converts mentions from Pleroma into the Friendica format
1363 * @param string $body
1365 * @return converted body
1367 private static function convertMentions($body)
1369 $URLSearchString = "^\[\]";
1370 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1376 * @brief Constructs a string with tags for a given tag array
1378 * @param array $tags
1379 * @param boolean $sensitive
1381 * @return string with tags
1383 private static function constructTagList($tags, $sensitive)
1390 foreach ($tags as $tag) {
1391 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1392 if (!empty($tag_text)) {
1396 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1400 /// @todo add nsfw for $sensitive
1408 * @param $attachments
1409 * @param array $item
1411 * @return item array
1413 private static function constructAttachList($attachments, $item)
1415 if (empty($attachments)) {
1419 foreach ($attachments as $attach) {
1420 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1421 if ($filetype == 'image') {
1422 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1424 if (!empty($item["attach"])) {
1425 $item["attach"] .= ',';
1427 $item["attach"] = '';
1429 if (!isset($attach['length'])) {
1430 $attach['length'] = "0";
1432 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1442 * @param array $activity
1445 private static function createItem($activity, $body)
1448 $item['verb'] = ACTIVITY_POST;
1449 $item['parent-uri'] = $activity['reply-to-id'];
1451 if ($activity['reply-to-id'] == $activity['id']) {
1452 $item['gravity'] = GRAVITY_PARENT;
1453 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1455 $item['gravity'] = GRAVITY_COMMENT;
1456 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1459 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
1460 logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
1461 self::fetchMissingActivity($activity['reply-to-id'], $activity);
1464 self::postItem($activity, $item, $body);
1470 * @param array $activity
1473 private static function likeItem($activity, $body)
1476 $item['verb'] = ACTIVITY_LIKE;
1477 $item['parent-uri'] = $activity['object'];
1478 $item['gravity'] = GRAVITY_ACTIVITY;
1479 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1481 self::postItem($activity, $item, $body);
1485 * @brief Delete items
1487 * @param array $activity
1490 private static function deleteItem($activity)
1492 $owner = Contact::getIdForURL($activity['owner']);
1493 $object = JsonLD::fetchElement($activity, 'object', 'id');
1494 logger('Deleting item ' . $object . ' from ' . $owner, LOGGER_DEBUG);
1495 Item::delete(['uri' => $object, 'owner-id' => $owner]);
1501 * @param array $activity
1504 private static function dislikeItem($activity, $body)
1507 $item['verb'] = ACTIVITY_DISLIKE;
1508 $item['parent-uri'] = $activity['object'];
1509 $item['gravity'] = GRAVITY_ACTIVITY;
1510 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1512 self::postItem($activity, $item, $body);
1518 * @param array $activity
1519 * @param array $item
1522 private static function postItem($activity, $item, $body)
1524 /// @todo What to do with $activity['context']?
1526 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
1527 logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
1531 $item['network'] = Protocol::ACTIVITYPUB;
1532 $item['private'] = !in_array(0, $activity['receiver']);
1533 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1534 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1535 $item['uri'] = $activity['id'];
1536 $item['created'] = $activity['published'];
1537 $item['edited'] = $activity['updated'];
1538 $item['guid'] = $activity['diaspora:guid'];
1539 $item['title'] = HTML::toBBCode($activity['name']);
1540 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1541 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1542 $item['location'] = $activity['location'];
1543 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1544 $item['app'] = $activity['service'];
1545 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1547 $item = self::constructAttachList($activity['attachments'], $item);
1549 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1550 if (!empty($source)) {
1551 $item['body'] = $source;
1554 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1555 $item['source'] = $body;
1556 $item['conversation-href'] = $activity['context'];
1557 $item['conversation-uri'] = $activity['conversation'];
1559 foreach ($activity['receiver'] as $receiver) {
1560 $item['uid'] = $receiver;
1561 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1563 if (($receiver != 0) && empty($item['contact-id'])) {
1564 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1567 $item_id = Item::insert($item);
1568 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1578 private static function fetchMissingActivity($url, $child)
1580 if (Config::get('system', 'ostatus_full_threads')) {
1584 $object = ActivityPub::fetchContent($url);
1585 if (empty($object)) {
1586 logger('Activity ' . $url . ' was not fetchable, aborting.');
1591 $activity['@context'] = $object['@context'];
1592 unset($object['@context']);
1593 $activity['id'] = $object['id'];
1594 $activity['to'] = defaults($object, 'to', []);
1595 $activity['cc'] = defaults($object, 'cc', []);
1596 $activity['actor'] = $child['author'];
1597 $activity['object'] = $object;
1598 $activity['published'] = $object['published'];
1599 $activity['type'] = 'Create';
1601 self::processActivity($activity);
1602 logger('Activity ' . $url . ' had been fetched and processed.');
1606 * @brief perform a "follow" request
1608 * @param array $activity
1610 private static function followUser($activity)
1612 $actor = JsonLD::fetchElement($activity, 'object', 'id');
1613 $uid = User::getIdForURL($actor);
1618 $owner = User::getOwnerDataById($uid);
1620 $cid = Contact::getIdForURL($activity['owner'], $uid);
1622 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1627 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1628 'author-link' => $activity['owner']];
1630 Contact::addRelationship($owner, $contact, $item);
1631 $cid = Contact::getIdForURL($activity['owner'], $uid);
1636 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1637 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1638 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1641 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1642 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1646 * @brief Update the given profile
1648 * @param array $activity
1650 private static function updatePerson($activity)
1652 if (empty($activity['object']['id'])) {
1656 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
1657 APContact::getByURL($activity['object']['id'], true);
1661 * @brief Delete the given profile
1663 * @param array $activity
1665 private static function deletePerson($activity)
1667 if (empty($activity['object']['id']) || empty($activity['object']['actor'])) {
1668 logger('Empty object id or actor.', LOGGER_DEBUG);
1672 if ($activity['object']['id'] != $activity['object']['actor']) {
1673 logger('Object id does not match actor.', LOGGER_DEBUG);
1677 $contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($activity['object']['id'])]);
1678 while ($contact = DBA::fetch($contacts)) {
1679 Contact::remove($contact["id"]);
1681 DBA::close($contacts);
1683 logger('Deleted contact ' . $activity['object']['id'], LOGGER_DEBUG);
1687 * @brief Accept a follow request
1689 * @param array $activity
1691 private static function acceptFollowUser($activity)
1693 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1694 $uid = User::getIdForURL($actor);
1699 $owner = User::getOwnerDataById($uid);
1701 $cid = Contact::getIdForURL($activity['owner'], $uid);
1703 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1707 $fields = ['pending' => false];
1709 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1710 if ($contact['rel'] == Contact::FOLLOWER) {
1711 $fields['rel'] = Contact::FRIEND;
1714 $condition = ['id' => $cid];
1715 DBA::update('contact', $fields, $condition);
1716 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1720 * @brief Reject a follow request
1722 * @param array $activity
1724 private static function rejectFollowUser($activity)
1726 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1727 $uid = User::getIdForURL($actor);
1732 $owner = User::getOwnerDataById($uid);
1734 $cid = Contact::getIdForURL($activity['owner'], $uid);
1736 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1740 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING, 'pending' => true])) {
1741 Contact::remove($cid);
1742 logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', LOGGER_DEBUG);
1744 logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', LOGGER_DEBUG);
1749 * @brief Undo activity like "like" or "dislike"
1751 * @param array $activity
1753 private static function undoActivity($activity)
1755 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
1756 if (empty($activity_url)) {
1760 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1761 if (empty($actor)) {
1765 $author_id = Contact::getIdForURL($actor);
1766 if (empty($author_id)) {
1770 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1774 * @brief Activity to remove a follower
1776 * @param array $activity
1778 private static function undoFollowUser($activity)
1780 $object = JsonLD::fetchElement($activity, 'object', 'object');
1781 $uid = User::getIdForURL($object);
1786 $owner = User::getOwnerDataById($uid);
1788 $cid = Contact::getIdForURL($activity['owner'], $uid);
1790 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1794 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1795 if (!DBA::isResult($contact)) {
1799 Contact::removeFollower($owner, $contact);
1800 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);