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::getProfileForUser($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::getProfileForUser($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 $elements = ['to', 'cc', 'bto', 'bcc'];
327 foreach ($elements as $element) {
328 if (empty($activity[$element])) {
331 if (is_string($activity[$element])) {
332 $activity[$element] = [$activity[$element]];
335 foreach ($activity[$element] as $receiver) {
336 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
337 $receiver = $item_profile['followers'];
339 if (!in_array($receiver, $exclude)) {
340 $permissions[$element][] = $receiver;
348 * @brief Creates an array of permissions from an item thread
352 * @return permission array
354 public static function createPermissionBlockForItem($item)
356 $data = ['to' => [], 'cc' => []];
358 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
360 $actor_profile = APContact::getProfileByURL($item['author-link']);
362 $terms = Term::tagArrayFromItemId($item['id']);
364 $contacts[$item['author-link']] = $item['author-link'];
366 if (!$item['private']) {
367 $data['to'][] = self::PUBLIC_COLLECTION;
368 if (!empty($actor_profile['followers'])) {
369 $data['cc'][] = $actor_profile['followers'];
372 foreach ($terms as $term) {
373 if ($term['type'] != TERM_MENTION) {
376 $profile = APContact::getProfileByURL($term['url'], false);
377 if (!empty($profile) && empty($contacts[$profile['url']])) {
378 $data['cc'][] = $profile['url'];
379 $contacts[$profile['url']] = $profile['url'];
383 $receiver_list = Item::enumeratePermissions($item);
387 foreach ($terms as $term) {
388 if ($term['type'] != TERM_MENTION) {
391 $cid = Contact::getIdForURL($term['url'], $item['uid']);
392 if (!empty($cid) && in_array($cid, $receiver_list)) {
393 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
394 $data['to'][] = $contact['url'];
395 $contacts[$contact['url']] = $contact['url'];
399 foreach ($receiver_list as $receiver) {
400 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
401 if (empty($contacts[$contact['url']])) {
402 $data['cc'][] = $contact['url'];
403 $contacts[$contact['url']] = $contact['url'];
408 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
409 while ($parent = Item::fetch($parents)) {
410 // Don't include data from future posts
411 if ($parent['id'] >= $item['id']) {
415 $profile = APContact::getProfileByURL($parent['author-link'], false);
416 if (!empty($profile) && empty($contacts[$profile['url']])) {
417 $data['cc'][] = $profile['url'];
418 $contacts[$profile['url']] = $profile['url'];
421 if ($item['gravity'] != GRAVITY_PARENT) {
425 $profile = APContact::getProfileByURL($parent['owner-link'], false);
426 if (!empty($profile) && empty($contacts[$profile['url']])) {
427 $data['cc'][] = $profile['url'];
428 $contacts[$profile['url']] = $profile['url'];
431 DBA::close($parents);
433 if (empty($data['to'])) {
434 $data['to'] = $data['cc'];
442 * @brief Fetches an array of inboxes for the given item and user
445 * @param integer $uid User ID
447 * @return array with inboxes
449 public static function fetchTargetInboxes($item, $uid)
451 $permissions = self::createPermissionBlockForItem($item);
452 if (empty($permissions)) {
458 if ($item['gravity'] == GRAVITY_ACTIVITY) {
459 $item_profile = APContact::getProfileByURL($item['author-link']);
461 $item_profile = APContact::getProfileByURL($item['owner-link']);
464 $elements = ['to', 'cc', 'bto', 'bcc'];
465 foreach ($elements as $element) {
466 if (empty($permissions[$element])) {
470 foreach ($permissions[$element] as $receiver) {
471 if ($receiver == $item_profile['followers']) {
472 $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
473 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB,
474 'archive' => false, 'pending' => false]);
475 while ($contact = DBA::fetch($contacts)) {
476 $contact = defaults($contact, 'batch', $contact['notify']);
477 $inboxes[$contact] = $contact;
479 DBA::close($contacts);
481 $profile = APContact::getProfileByURL($receiver);
482 if (!empty($profile)) {
483 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
484 $inboxes[$target] = $target;
494 * @brief Returns the activity type of a given item
498 * @return activity type
500 public static function getTypeOfItem($item)
502 if ($item['verb'] == ACTIVITY_POST) {
503 if ($item['created'] == $item['edited']) {
508 } elseif ($item['verb'] == ACTIVITY_LIKE) {
510 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
512 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
514 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
516 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
517 $type = 'TentativeAccept';
526 * @brief Creates an activity array for a given item id
528 * @param integer $item_id
529 * @param boolean $object_mode Is the activity item is used inside another object?
531 * @return array of activity
533 public static function createActivityFromItem($item_id, $object_mode = false)
535 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
537 if (!DBA::isResult($item)) {
541 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
542 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
543 if (DBA::isResult($conversation)) {
544 $data = json_decode($conversation['source']);
550 $type = self::getTypeOfItem($item);
553 $data = ['@context' => self::CONTEXT];
555 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
557 } elseif ($item['deleted']) {
564 $data['id'] = $item['uri'] . '#' . $type;
565 $data['type'] = $type;
566 $data['actor'] = $item['author-link'];
568 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
570 if ($item["created"] != $item["edited"]) {
571 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
574 $data['context'] = self::fetchContextURLForItem($item);
576 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
578 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
579 $data['object'] = self::createNote($item);
580 } elseif ($data['type'] == 'Undo') {
581 $data['object'] = self::createActivityFromItem($item_id, true);
583 $data['object'] = $item['thr-parent'];
586 $owner = User::getOwnerDataById($item['uid']);
589 return LDSignature::sign($data, $owner);
596 * @brief Creates an object array for a given item id
598 * @param integer $item_id
600 * @return object array
602 public static function createObjectFromItemID($item_id)
604 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
606 if (!DBA::isResult($item)) {
610 $data = ['@context' => self::CONTEXT];
611 $data = array_merge($data, self::createNote($item));
617 * @brief Returns a tag array for a given item array
621 * @return array of tags
623 private static function createTagList($item)
627 $terms = Term::tagArrayFromItemId($item['id']);
628 foreach ($terms as $term) {
629 if ($term['type'] == TERM_MENTION) {
630 $contact = Contact::getDetailsByURL($term['url']);
631 if (!empty($contact['addr'])) {
632 $mention = '@' . $contact['addr'];
634 $mention = '@' . $term['url'];
637 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
644 * @brief Fetches the "context" value for a givem item array from the "conversation" table
648 * @return string with context url
650 private static function fetchContextURLForItem($item)
652 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
653 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
654 $context_uri = $conversation['conversation-href'];
655 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
656 $context_uri = $conversation['conversation-uri'];
658 $context_uri = str_replace('/object/', '/context/', $item['parent-uri']);
664 * @brief Creates a note/article object array
668 * @return object array
670 private static function createNote($item)
672 if (!empty($item['title'])) {
678 if ($item['deleted']) {
683 $data['id'] = $item['uri'];
684 $data['type'] = $type;
686 if ($item['deleted']) {
690 $data['summary'] = null; // Ignore by now
692 if ($item['uri'] != $item['thr-parent']) {
693 $data['inReplyTo'] = $item['thr-parent'];
695 $data['inReplyTo'] = null;
698 $data['diaspora:guid'] = $item['guid'];
699 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
701 if ($item["created"] != $item["edited"]) {
702 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
705 $data['url'] = $item['plink'];
706 $data['attributedTo'] = $item['author-link'];
707 $data['actor'] = $item['author-link'];
708 $data['sensitive'] = false; // - Query NSFW
709 $data['context'] = self::fetchContextURLForItem($item);
711 if (!empty($item['title'])) {
712 $data['name'] = BBCode::convert($item['title'], false, 7);
715 $data['content'] = BBCode::convert($item['body'], false, 7);
716 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
718 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
719 $data['diaspora:comment'] = $item['signed_text'];
722 $data['attachment'] = []; // @ToDo
723 $data['tag'] = self::createTagList($item);
724 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
730 * @brief Transmits a given activity to a target
732 * @param array $activity
733 * @param string $target Target profile
734 * @param integer $uid User ID
736 public static function transmitActivity($activity, $target, $uid)
738 $profile = APContact::getProfileByURL($target);
740 $owner = User::getOwnerDataById($uid);
742 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
743 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
745 'actor' => $owner['url'],
746 'object' => $profile['url'],
747 'to' => $profile['url']];
749 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
751 $signed = LDSignature::sign($data, $owner);
752 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
756 * @brief Transmit a message that the contact request had been accepted
758 * @param string $target Target profile
760 * @param integer $uid User ID
762 public static function transmitContactAccept($target, $id, $uid)
764 $profile = APContact::getProfileByURL($target);
766 $owner = User::getOwnerDataById($uid);
767 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
768 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
770 'actor' => $owner['url'],
771 'object' => ['id' => $id, 'type' => 'Follow',
772 'actor' => $profile['url'],
773 'object' => $owner['url']],
774 'to' => $profile['url']];
776 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
778 $signed = LDSignature::sign($data, $owner);
779 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
785 * @param string $target Target profile
787 * @param integer $uid User ID
789 public static function transmitContactReject($target, $id, $uid)
791 $profile = APContact::getProfileByURL($target);
793 $owner = User::getOwnerDataById($uid);
794 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
795 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
797 'actor' => $owner['url'],
798 'object' => ['id' => $id, 'type' => 'Follow',
799 'actor' => $profile['url'],
800 'object' => $owner['url']],
801 'to' => $profile['url']];
803 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
805 $signed = LDSignature::sign($data, $owner);
806 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
812 * @param string $target Target profile
813 * @param integer $uid User ID
815 public static function transmitContactUndo($target, $uid)
817 $profile = APContact::getProfileByURL($target);
819 $id = System::baseUrl() . '/activity/' . System::createGUID();
821 $owner = User::getOwnerDataById($uid);
822 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
825 'actor' => $owner['url'],
826 'object' => ['id' => $id, 'type' => 'Follow',
827 'actor' => $owner['url'],
828 'object' => $profile['url']],
829 'to' => $profile['url']];
831 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
833 $signed = LDSignature::sign($data, $owner);
834 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
838 * Fetches ActivityPub content from the given url
840 * @param string $url content url
843 public static function fetchContent($url)
845 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
846 if (!$ret['success'] || empty($ret['body'])) {
849 return json_decode($ret['body'], true);
853 * Fetches a profile from the given url into an array that is compatible to Probe::uri
855 * @param string $url profile url
858 public static function probeProfile($url)
860 $apcontact = APContact::getProfileByURL($url, true);
861 if (empty($apcontact)) {
865 $profile = ['network' => Protocol::ACTIVITYPUB];
866 $profile['nick'] = $apcontact['nick'];
867 $profile['name'] = $apcontact['name'];
868 $profile['guid'] = $apcontact['uuid'];
869 $profile['url'] = $apcontact['url'];
870 $profile['addr'] = $apcontact['addr'];
871 $profile['alias'] = $apcontact['alias'];
872 $profile['photo'] = $apcontact['photo'];
873 // $profile['community']
874 // $profile['keywords']
875 // $profile['location']
876 $profile['about'] = $apcontact['about'];
877 $profile['batch'] = $apcontact['sharedinbox'];
878 $profile['notify'] = $apcontact['inbox'];
879 $profile['poll'] = $apcontact['outbox'];
880 $profile['pubkey'] = $apcontact['pubkey'];
881 $profile['baseurl'] = $apcontact['baseurl'];
883 // Remove all "null" fields
884 foreach ($profile as $field => $content) {
885 if (is_null($content)) {
886 unset($profile[$field]);
898 * @param integer $uid User ID
900 public static function processInbox($body, $header, $uid)
902 $http_signer = HTTPSignature::getSigner($body, $header);
903 if (empty($http_signer)) {
904 logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
907 logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
910 $activity = json_decode($body, true);
912 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
913 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
915 if (empty($activity)) {
916 logger('Invalid body.', LOGGER_DEBUG);
920 if (LDSignature::isSigned($activity)) {
921 $ld_signer = LDSignature::getSigner($activity);
922 if (empty($ld_signer)) {
923 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
925 if (!empty($ld_signer && ($actor == $http_signer))) {
926 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
927 $trust_source = true;
928 } elseif (!empty($ld_signer)) {
929 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
930 $trust_source = true;
931 } elseif ($actor == $http_signer) {
932 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
933 $trust_source = true;
935 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
936 $trust_source = false;
938 } elseif ($actor == $http_signer) {
939 logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
940 $trust_source = true;
942 logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
943 $trust_source = false;
946 self::processActivity($activity, $body, $uid, $trust_source);
953 * @param integer $uid User ID
955 public static function fetchOutbox($url, $uid)
957 $data = self::fetchContent($url);
962 if (!empty($data['orderedItems'])) {
963 $items = $data['orderedItems'];
964 } elseif (!empty($data['first']['orderedItems'])) {
965 $items = $data['first']['orderedItems'];
966 } elseif (!empty($data['first'])) {
967 self::fetchOutbox($data['first'], $uid);
973 foreach ($items as $activity) {
974 self::processActivity($activity, '', $uid, true);
981 * @param array $activity
982 * @param integer $uid User ID
983 * @param $trust_source
987 private static function prepareObjectData($activity, $uid, &$trust_source)
989 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
991 logger('Empty actor', LOGGER_DEBUG);
995 // Fetch all receivers from to, cc, bto and bcc
996 $receivers = self::getReceivers($activity, $actor);
998 // When it is a delivery to a personal inbox we add that user to the receivers
1000 $owner = User::getOwnerDataById($uid);
1001 $additional = ['uid:' . $uid => $uid];
1002 $receivers = array_merge($receivers, $additional);
1005 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
1007 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
1008 if (empty($object_id)) {
1009 logger('No object found', LOGGER_DEBUG);
1013 // Fetch the content only on activities where this matters
1014 if (in_array($activity['type'], ['Create', 'Announce'])) {
1015 $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
1016 if (empty($object_data)) {
1017 logger("Object data couldn't be processed", LOGGER_DEBUG);
1020 // We had been able to retrieve the object data - so we can trust the source
1021 $trust_source = true;
1022 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
1023 // Create a mostly empty array out of the activity data (instead of the object).
1024 // This way we later don't have to check for the existence of ech individual array element.
1025 $object_data = self::processObject($activity);
1026 $object_data['name'] = $activity['type'];
1027 $object_data['author'] = $activity['actor'];
1028 $object_data['object'] = $object_id;
1029 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
1032 $object_data['id'] = $activity['id'];
1033 $object_data['object'] = $activity['object'];
1034 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
1037 $object_data = self::addActivityFields($object_data, $activity);
1039 $object_data['type'] = $activity['type'];
1040 $object_data['owner'] = $actor;
1041 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
1043 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
1045 return $object_data;
1051 * @param array $activity
1053 * @param integer $uid User ID
1054 * @param $trust_source
1056 private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
1058 if (empty($activity['type'])) {
1059 logger('Empty type', LOGGER_DEBUG);
1063 if (empty($activity['object'])) {
1064 logger('Empty object', LOGGER_DEBUG);
1068 if (empty($activity['actor'])) {
1069 logger('Empty actor', LOGGER_DEBUG);
1074 // $trust_source is called by reference and is set to true if the content was retrieved successfully
1075 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
1076 if (empty($object_data)) {
1077 logger('No object data found', LOGGER_DEBUG);
1081 if (!$trust_source) {
1082 logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
1085 switch ($activity['type']) {
1088 self::createItem($object_data, $body);
1092 self::likeItem($object_data, $body);
1096 self::dislikeItem($object_data, $body);
1100 if (in_array($object_data['object_type'], ['Person', 'Organization', 'Service', 'Group', 'Application'])) {
1101 self::updatePerson($object_data, $body);
1109 self::followUser($object_data);
1113 if ($object_data['object_type'] == 'Follow') {
1114 self::acceptFollowUser($object_data);
1119 if ($object_data['object_type'] == 'Follow') {
1120 self::undoFollowUser($object_data);
1121 } elseif (in_array($object_data['object_type'], ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'])) {
1122 self::undoActivity($object_data);
1127 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
1135 * @param array $activity
1140 private static function getReceivers($activity, $actor)
1144 // When it is an answer, we inherite the receivers from the parent
1145 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1146 if (!empty($replyto)) {
1147 $parents = Item::select(['uid'], ['uri' => $replyto]);
1148 while ($parent = Item::fetch($parents)) {
1149 $receivers['uid:' . $parent['uid']] = $parent['uid'];
1153 if (!empty($actor)) {
1154 $profile = APContact::getProfileByURL($actor);
1155 $followers = defaults($profile, 'followers', '');
1157 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
1159 logger('Empty actor', LOGGER_DEBUG);
1163 $elements = ['to', 'cc', 'bto', 'bcc'];
1164 foreach ($elements as $element) {
1165 if (empty($activity[$element])) {
1169 // The receiver can be an arror or a string
1170 if (is_string($activity[$element])) {
1171 $activity[$element] = [$activity[$element]];
1174 foreach ($activity[$element] as $receiver) {
1175 if ($receiver == self::PUBLIC_COLLECTION) {
1176 $receivers['uid:0'] = 0;
1179 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
1180 // This will most likely catch all OStatus connections to Mastodon
1181 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
1182 , 'archive' => false, 'pending' => false];
1183 $contacts = DBA::select('contact', ['uid'], $condition);
1184 while ($contact = DBA::fetch($contacts)) {
1185 if ($contact['uid'] != 0) {
1186 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1189 DBA::close($contacts);
1192 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
1193 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1194 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
1195 $contacts = DBA::select('contact', ['uid'], $condition);
1196 while ($contact = DBA::fetch($contacts)) {
1197 if ($contact['uid'] != 0) {
1198 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1201 DBA::close($contacts);
1205 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1206 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1207 if (!DBA::isResult($contact)) {
1210 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1214 self::switchContacts($receivers, $actor);
1223 * @param integer $uid User ID
1226 private static function switchContact($cid, $uid, $url)
1228 $profile = ActivityPub::probeProfile($url);
1229 if (empty($profile)) {
1233 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
1235 $photo = $profile['photo'];
1236 unset($profile['photo']);
1237 unset($profile['baseurl']);
1239 $profile['nurl'] = normalise_link($profile['url']);
1240 DBA::update('contact', $profile, ['id' => $cid]);
1242 Contact::updateAvatar($photo, $uid, $cid);
1251 private static function switchContacts($receivers, $actor)
1253 if (empty($actor)) {
1257 foreach ($receivers as $receiver) {
1258 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
1259 if (DBA::isResult($contact)) {
1260 self::switchContact($contact['id'], $receiver, $actor);
1263 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
1264 if (DBA::isResult($contact)) {
1265 self::switchContact($contact['id'], $receiver, $actor);
1273 * @param $object_data
1274 * @param array $activity
1278 private static function addActivityFields($object_data, $activity)
1280 if (!empty($activity['published']) && empty($object_data['published'])) {
1281 $object_data['published'] = $activity['published'];
1284 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1285 $object_data['updated'] = $activity['updated'];
1288 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1289 $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1292 if (!empty($activity['instrument'])) {
1293 $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1295 return $object_data;
1303 * @param $trust_source
1307 private static function fetchObject($object_id, $object = [], $trust_source = false)
1309 if (!$trust_source || is_string($object)) {
1310 $data = self::fetchContent($object_id);
1312 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
1315 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
1318 logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
1322 if (is_string($data)) {
1323 $item = Item::selectFirst([], ['uri' => $data]);
1324 if (!DBA::isResult($item)) {
1325 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1328 logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
1329 $data = self::createNote($item);
1332 if (empty($data['type'])) {
1333 logger('Empty type', LOGGER_DEBUG);
1337 switch ($data['type']) {
1341 return self::processObject($data);
1344 if (empty($data['object'])) {
1347 return self::fetchObject($data['object']);
1354 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
1366 private static function processObject(&$object)
1368 if (empty($object['id'])) {
1373 $object_data['object_type'] = $object['type'];
1374 $object_data['id'] = $object['id'];
1376 if (!empty($object['inReplyTo'])) {
1377 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1379 $object_data['reply-to-id'] = $object_data['id'];
1382 $object_data['published'] = defaults($object, 'published', null);
1383 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1385 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1386 $object_data['published'] = $object_data['updated'];
1389 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
1390 if (empty($actor)) {
1391 $actor = defaults($object, 'actor', null);
1394 $object_data['diaspora:guid'] = defaults($object, 'diaspora:guid', null);
1395 $object_data['owner'] = $object_data['author'] = $actor;
1396 $object_data['context'] = defaults($object, 'context', null);
1397 $object_data['conversation'] = defaults($object, 'conversation', null);
1398 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1399 $object_data['name'] = defaults($object, 'title', null);
1400 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1401 $object_data['summary'] = defaults($object, 'summary', null);
1402 $object_data['content'] = defaults($object, 'content', null);
1403 $object_data['source'] = defaults($object, 'source', null);
1404 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1405 $object_data['attachments'] = defaults($object, 'attachment', null);
1406 $object_data['tags'] = defaults($object, 'tag', null);
1407 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1408 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1409 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1411 // Common object data:
1414 // @context, type, actor, signature, mediaType, duration, replies, icon
1416 // Also missing: (Defined in the standard, but currently unused)
1417 // audience, preview, endTime, startTime, generator, image
1422 // contentMap, announcement_count, announcements, context_id, likes, like_count
1423 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1428 // category, licence, language, commentsEnabled
1431 // views, waitTranscoding, state, support, subtitleLanguage
1432 // likes, dislikes, shares, comments
1434 return $object_data;
1438 * @brief Converts mentions from Pleroma into the Friendica format
1440 * @param string $body
1442 * @return converted body
1444 private static function convertMentions($body)
1446 $URLSearchString = "^\[\]";
1447 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1453 * @brief Constructs a string with tags for a given tag array
1455 * @param array $tags
1456 * @param boolean $sensitive
1458 * @return string with tags
1460 private static function constructTagList($tags, $sensitive)
1467 foreach ($tags as $tag) {
1468 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1469 if (!empty($tag_text)) {
1473 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1477 /// @todo add nsfw for $sensitive
1485 * @param $attachments
1486 * @param array $item
1488 * @return item array
1490 private static function constructAttachList($attachments, $item)
1492 if (empty($attachments)) {
1496 foreach ($attachments as $attach) {
1497 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1498 if ($filetype == 'image') {
1499 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1501 if (!empty($item["attach"])) {
1502 $item["attach"] .= ',';
1504 $item["attach"] = '';
1506 if (!isset($attach['length'])) {
1507 $attach['length'] = "0";
1509 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1519 * @param array $activity
1522 private static function createItem($activity, $body)
1525 $item['verb'] = ACTIVITY_POST;
1526 $item['parent-uri'] = $activity['reply-to-id'];
1528 if ($activity['reply-to-id'] == $activity['id']) {
1529 $item['gravity'] = GRAVITY_PARENT;
1530 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1532 $item['gravity'] = GRAVITY_COMMENT;
1533 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1536 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
1537 logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
1538 self::fetchMissingActivity($activity['reply-to-id'], $activity);
1541 self::postItem($activity, $item, $body);
1547 * @param array $activity
1550 private static function likeItem($activity, $body)
1553 $item['verb'] = ACTIVITY_LIKE;
1554 $item['parent-uri'] = $activity['object'];
1555 $item['gravity'] = GRAVITY_ACTIVITY;
1556 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1558 self::postItem($activity, $item, $body);
1564 * @param array $activity
1567 private static function dislikeItem($activity, $body)
1570 $item['verb'] = ACTIVITY_DISLIKE;
1571 $item['parent-uri'] = $activity['object'];
1572 $item['gravity'] = GRAVITY_ACTIVITY;
1573 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1575 self::postItem($activity, $item, $body);
1581 * @param array $activity
1582 * @param array $item
1585 private static function postItem($activity, $item, $body)
1587 /// @todo What to do with $activity['context']?
1589 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
1590 logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
1594 $item['network'] = Protocol::ACTIVITYPUB;
1595 $item['private'] = !in_array(0, $activity['receiver']);
1596 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1597 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1598 $item['uri'] = $activity['id'];
1599 $item['created'] = $activity['published'];
1600 $item['edited'] = $activity['updated'];
1601 $item['guid'] = $activity['diaspora:guid'];
1602 $item['title'] = HTML::toBBCode($activity['name']);
1603 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1604 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1605 $item['location'] = $activity['location'];
1606 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1607 $item['app'] = $activity['service'];
1608 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1610 $item = self::constructAttachList($activity['attachments'], $item);
1612 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1613 if (!empty($source)) {
1614 $item['body'] = $source;
1617 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1618 $item['source'] = $body;
1619 $item['conversation-href'] = $activity['context'];
1620 $item['conversation-uri'] = $activity['conversation'];
1622 foreach ($activity['receiver'] as $receiver) {
1623 $item['uid'] = $receiver;
1624 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1626 if (($receiver != 0) && empty($item['contact-id'])) {
1627 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1630 $item_id = Item::insert($item);
1631 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1641 private static function fetchMissingActivity($url, $child)
1643 if (Config::get('system', 'ostatus_full_threads')) {
1647 $object = ActivityPub::fetchContent($url);
1648 if (empty($object)) {
1649 logger('Activity ' . $url . ' was not fetchable, aborting.');
1654 $activity['@context'] = $object['@context'];
1655 unset($object['@context']);
1656 $activity['id'] = $object['id'];
1657 $activity['to'] = defaults($object, 'to', []);
1658 $activity['cc'] = defaults($object, 'cc', []);
1659 $activity['actor'] = $child['author'];
1660 $activity['object'] = $object;
1661 $activity['published'] = $object['published'];
1662 $activity['type'] = 'Create';
1664 self::processActivity($activity);
1665 logger('Activity ' . $url . ' had been fetched and processed.');
1669 * @brief perform a "follow" request
1671 * @param array $activity
1673 private static function followUser($activity)
1675 $actor = JsonLD::fetchElement($activity, 'object', 'id');
1676 $uid = User::getIdForURL($actor);
1681 $owner = User::getOwnerDataById($uid);
1683 $cid = Contact::getIdForURL($activity['owner'], $uid);
1685 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1690 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1691 'author-link' => $activity['owner']];
1693 Contact::addRelationship($owner, $contact, $item);
1694 $cid = Contact::getIdForURL($activity['owner'], $uid);
1699 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1700 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1701 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1704 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1705 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1709 * @brief Update the given profile
1711 * @param array $activity
1713 private static function updatePerson($activity)
1715 if (empty($activity['object']['id'])) {
1719 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
1720 APContact::getProfileByURL($activity['object']['id'], true);
1724 * @brief Accept a follow request
1726 * @param array $activity
1728 private static function acceptFollowUser($activity)
1730 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1731 $uid = User::getIdForURL($actor);
1736 $owner = User::getOwnerDataById($uid);
1738 $cid = Contact::getIdForURL($activity['owner'], $uid);
1740 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1744 $fields = ['pending' => false];
1746 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1747 if ($contact['rel'] == Contact::FOLLOWER) {
1748 $fields['rel'] = Contact::FRIEND;
1751 $condition = ['id' => $cid];
1752 DBA::update('contact', $fields, $condition);
1753 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1757 * @brief Undo activity like "like" or "dislike"
1759 * @param array $activity
1761 private static function undoActivity($activity)
1763 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
1764 if (empty($activity_url)) {
1768 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1769 if (empty($actor)) {
1773 $author_id = Contact::getIdForURL($actor);
1774 if (empty($author_id)) {
1778 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1782 * @brief Activity to remove a follower
1784 * @param array $activity
1786 private static function undoFollowUser($activity)
1788 $object = JsonLD::fetchElement($activity, 'object', 'object');
1789 $uid = User::getIdForURL($object);
1794 $owner = User::getOwnerDataById($uid);
1796 $cid = Contact::getIdForURL($activity['owner'], $uid);
1798 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1802 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1803 if (!DBA::isResult($contact)) {
1807 Contact::removeFollower($owner, $contact);
1808 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);