3 * @file src/Protocol/ActivityPub.php
5 namespace Friendica\Protocol;
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Crypto;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\HTML;
24 use Friendica\Util\JsonLD;
25 use Friendica\Util\LDSignature;
26 use Friendica\Core\Config;
29 * @brief ActivityPub Protocol class
30 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
31 * https://www.w3.org/TR/activitypub/
32 * https://www.w3.org/TR/activitystreams-core/
33 * https://www.w3.org/TR/activitystreams-vocabulary/
35 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
36 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
38 * Digest: https://tools.ietf.org/html/rfc5843
39 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
41 * Mastodon implementation of supported activities:
42 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
72 * - Queueing unsucessful deliveries
73 * - Polling the outboxes for missing content?
74 * - Possibly using the LD-JSON parser
78 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
79 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
80 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
81 'diaspora' => 'https://diasporafoundation.org/ns/',
82 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
83 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
86 * @brief Checks if the web request is done for the AP protocol
90 public static function isRequest()
92 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
93 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
97 * @brief collects the lost of followers of the given owner
99 * @param array $owner Owner array
100 * @param integer $page Page number
102 * @return array of owners
104 public static function getFollowers($owner, $page = null)
106 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
107 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
108 $count = DBA::count('contact', $condition);
110 $data = ['@context' => self::CONTEXT];
111 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
112 $data['type'] = 'OrderedCollection';
113 $data['totalItems'] = $count;
115 // When we hide our friends we will only show the pure number but don't allow more.
116 $profile = Profile::getByUID($owner['uid']);
117 if (!empty($profile['hide-friends'])) {
122 $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
126 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
127 while ($contact = DBA::fetch($contacts)) {
128 $list[] = $contact['url'];
132 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
135 $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
137 $data['orderedItems'] = $list;
144 * @brief Create list of following contacts
146 * @param array $owner Owner array
147 * @param integer $page Page numbe
149 * @return array of following contacts
151 public static function getFollowing($owner, $page = null)
153 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
154 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
155 $count = DBA::count('contact', $condition);
157 $data = ['@context' => self::CONTEXT];
158 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
159 $data['type'] = 'OrderedCollection';
160 $data['totalItems'] = $count;
162 // When we hide our friends we will only show the pure number but don't allow more.
163 $profile = Profile::getByUID($owner['uid']);
164 if (!empty($profile['hide-friends'])) {
169 $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
173 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
174 while ($contact = DBA::fetch($contacts)) {
175 $list[] = $contact['url'];
179 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
182 $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
184 $data['orderedItems'] = $list;
191 * @brief Public posts for the given owner
193 * @param array $owner Owner array
194 * @param integer $page Page numbe
196 * @return array of posts
198 public static function getOutbox($owner, $page = null)
200 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
202 $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact,
203 'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
204 'deleted' => false, 'visible' => true];
205 $count = DBA::count('item', $condition);
207 $data = ['@context' => self::CONTEXT];
208 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
209 $data['type'] = 'OrderedCollection';
210 $data['totalItems'] = $count;
213 $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
217 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
219 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
220 while ($item = Item::fetch($items)) {
221 $object = self::createObjectFromItemID($item['id']);
222 unset($object['@context']);
227 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
230 $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
232 $data['orderedItems'] = $list;
239 * Return the ActivityPub profile of the given user
241 * @param integer $uid User ID
242 * @return profile array
244 public static function profile($uid)
246 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
247 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
248 'account_removed' => false, 'verified' => true];
249 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
250 $user = DBA::selectFirst('user', $fields, $condition);
251 if (!DBA::isResult($user)) {
255 $fields = ['locality', 'region', 'country-name'];
256 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
257 if (!DBA::isResult($profile)) {
261 $fields = ['name', 'url', 'location', 'about', 'avatar'];
262 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
263 if (!DBA::isResult($contact)) {
267 $data = ['@context' => self::CONTEXT];
268 $data['id'] = $contact['url'];
269 $data['diaspora:guid'] = $user['guid'];
270 $data['type'] = $accounttype[$user['account-type']];
271 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
272 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
273 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
274 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
275 $data['preferredUsername'] = $user['nickname'];
276 $data['name'] = $contact['name'];
277 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
278 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
279 $data['summary'] = $contact['about'];
280 $data['url'] = $contact['url'];
281 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
282 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
283 'owner' => $contact['url'],
284 'publicKeyPem' => $user['pubkey']];
285 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
286 $data['icon'] = ['type' => 'Image',
287 'url' => $contact['avatar']];
289 // tags: https://kitty.town/@inmysocks/100656097926961126.json
294 * @brief Returns an array with permissions of a given item array
298 * @return array with permissions
300 private static function fetchPermissionBlockFromConversation($item)
302 if (empty($item['thr-parent'])) {
306 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
307 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
308 if (!DBA::isResult($conversation)) {
312 $activity = json_decode($conversation['source'], true);
314 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
315 $profile = APContact::getProfileByURL($actor);
317 $item_profile = APContact::getProfileByURL($item['author-link']);
318 $exclude[] = $item['author-link'];
320 if ($item['gravity'] == GRAVITY_PARENT) {
321 $exclude[] = $item['owner-link'];
324 $permissions['to'][] = $actor;
326 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
327 if (empty($activity[$element])) {
330 if (is_string($activity[$element])) {
331 $activity[$element] = [$activity[$element]];
334 foreach ($activity[$element] as $receiver) {
335 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
336 $receiver = $item_profile['followers'];
338 if (!in_array($receiver, $exclude)) {
339 $permissions[$element][] = $receiver;
347 * @brief Creates an array of permissions from an item thread
351 * @return permission array
353 public static function createPermissionBlockForItem($item)
355 $data = ['to' => [], 'cc' => []];
357 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
359 $actor_profile = APContact::getProfileByURL($item['author-link']);
361 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
363 $contacts[$item['author-link']] = $item['author-link'];
365 if (!$item['private']) {
366 $data['to'][] = self::PUBLIC_COLLECTION;
367 if (!empty($actor_profile['followers'])) {
368 $data['cc'][] = $actor_profile['followers'];
371 foreach ($terms as $term) {
372 $profile = APContact::getProfileByURL($term['url'], false);
373 if (!empty($profile) && empty($contacts[$profile['url']])) {
374 $data['cc'][] = $profile['url'];
375 $contacts[$profile['url']] = $profile['url'];
379 $receiver_list = Item::enumeratePermissions($item);
383 foreach ($terms as $term) {
384 $cid = Contact::getIdForURL($term['url'], $item['uid']);
385 if (!empty($cid) && in_array($cid, $receiver_list)) {
386 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
387 $data['to'][] = $contact['url'];
388 $contacts[$contact['url']] = $contact['url'];
392 foreach ($receiver_list as $receiver) {
393 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
394 if (empty($contacts[$contact['url']])) {
395 $data['cc'][] = $contact['url'];
396 $contacts[$contact['url']] = $contact['url'];
401 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
402 while ($parent = Item::fetch($parents)) {
403 // Don't include data from future posts
404 if ($parent['id'] >= $item['id']) {
408 $profile = APContact::getProfileByURL($parent['author-link'], false);
409 if (!empty($profile) && empty($contacts[$profile['url']])) {
410 $data['cc'][] = $profile['url'];
411 $contacts[$profile['url']] = $profile['url'];
414 if ($item['gravity'] != GRAVITY_PARENT) {
418 $profile = APContact::getProfileByURL($parent['owner-link'], false);
419 if (!empty($profile) && empty($contacts[$profile['url']])) {
420 $data['cc'][] = $profile['url'];
421 $contacts[$profile['url']] = $profile['url'];
424 DBA::close($parents);
426 if (empty($data['to'])) {
427 $data['to'] = $data['cc'];
435 * @brief Fetches an array of inboxes for the given item and user
438 * @param integer $uid User ID
440 * @return array with inboxes
442 public static function fetchTargetInboxes($item, $uid)
444 $permissions = self::createPermissionBlockForItem($item);
445 if (empty($permissions)) {
451 if ($item['gravity'] == GRAVITY_ACTIVITY) {
452 $item_profile = APContact::getProfileByURL($item['author-link']);
454 $item_profile = APContact::getProfileByURL($item['owner-link']);
457 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
458 if (empty($permissions[$element])) {
462 foreach ($permissions[$element] as $receiver) {
463 if ($receiver == $item_profile['followers']) {
464 $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
465 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB,
466 'archive' => false, 'pending' => false]);
467 while ($contact = DBA::fetch($contacts)) {
468 $contact = defaults($contact, 'batch', $contact['notify']);
469 $inboxes[$contact] = $contact;
471 DBA::close($contacts);
473 $profile = APContact::getProfileByURL($receiver);
474 if (!empty($profile)) {
475 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
476 $inboxes[$target] = $target;
486 * @brief Returns the activity type of a given item
490 * @return activity type
492 public static function getTypeOfItem($item)
494 if ($item['verb'] == ACTIVITY_POST) {
495 if ($item['created'] == $item['edited']) {
500 } elseif ($item['verb'] == ACTIVITY_LIKE) {
502 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
504 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
506 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
508 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
509 $type = 'TentativeAccept';
518 * @brief Creates an activity array for a given item id
520 * @param integer $item_id
521 * @param boolean $object_mode Is the activity item is used inside another object?
523 * @return array of activity
525 public static function createActivityFromItem($item_id, $object_mode = false)
527 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
529 if (!DBA::isResult($item)) {
533 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
534 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
535 if (DBA::isResult($conversation)) {
536 $data = json_decode($conversation['source']);
542 $type = self::getTypeOfItem($item);
545 $data = ['@context' => self::CONTEXT];
547 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
549 } elseif ($item['deleted']) {
556 $data['id'] = $item['uri'] . '#' . $type;
557 $data['type'] = $type;
558 $data['actor'] = $item['author-link'];
560 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
562 if ($item["created"] != $item["edited"]) {
563 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
566 $data['context'] = self::fetchContextURLForItem($item);
568 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
570 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
571 $data['object'] = self::createNote($item);
572 } elseif ($data['type'] == 'Undo') {
573 $data['object'] = self::createActivityFromItem($item_id, true);
575 $data['object'] = $item['thr-parent'];
578 $owner = User::getOwnerDataById($item['uid']);
581 return LDSignature::sign($data, $owner);
588 * @brief Creates an object array for a given item id
590 * @param integer $item_id
592 * @return object array
594 public static function createObjectFromItemID($item_id)
596 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
598 if (!DBA::isResult($item)) {
602 $data = ['@context' => self::CONTEXT];
603 $data = array_merge($data, self::createNote($item));
609 * @brief Returns a tag array for a given item array
613 * @return array of tags
615 private static function createTagList($item)
619 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
620 foreach ($terms as $term) {
621 $contact = Contact::getDetailsByURL($term['url']);
622 if (!empty($contact['addr'])) {
623 $mention = '@' . $contact['addr'];
625 $mention = '@' . $term['url'];
628 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
634 * @brief Fetches the "context" value for a givem item array from the "conversation" table
638 * @return string with context url
640 private static function fetchContextURLForItem($item)
642 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
643 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
644 $context_uri = $conversation['conversation-href'];
645 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
646 $context_uri = $conversation['conversation-uri'];
648 $context_uri = str_replace('/object/', '/context/', $item['parent-uri']);
654 * @brief Creates a note/article object array
658 * @return object array
660 private static function createNote($item)
662 if (!empty($item['title'])) {
668 if ($item['deleted']) {
673 $data['id'] = $item['uri'];
674 $data['type'] = $type;
676 if ($item['deleted']) {
680 $data['summary'] = null; // Ignore by now
682 if ($item['uri'] != $item['thr-parent']) {
683 $data['inReplyTo'] = $item['thr-parent'];
685 $data['inReplyTo'] = null;
688 $data['diaspora:guid'] = $item['guid'];
689 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
691 if ($item["created"] != $item["edited"]) {
692 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
695 $data['url'] = $item['plink'];
696 $data['attributedTo'] = $item['author-link'];
697 $data['actor'] = $item['author-link'];
698 $data['sensitive'] = false; // - Query NSFW
699 $data['context'] = self::fetchContextURLForItem($item);
701 if (!empty($item['title'])) {
702 $data['name'] = BBCode::convert($item['title'], false, 7);
705 $data['content'] = BBCode::convert($item['body'], false, 7);
706 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
708 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
709 $data['diaspora:comment'] = $item['signed_text'];
712 $data['attachment'] = []; // @ToDo
713 $data['tag'] = self::createTagList($item);
714 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
720 * @brief Transmits a given activity to a target
722 * @param array $activity
723 * @param string $target Target profile
724 * @param integer $uid User ID
726 public static function transmitActivity($activity, $target, $uid)
728 $profile = APContact::getProfileByURL($target);
730 $owner = User::getOwnerDataById($uid);
732 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
733 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
735 'actor' => $owner['url'],
736 'object' => $profile['url'],
737 'to' => $profile['url']];
739 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
741 $signed = LDSignature::sign($data, $owner);
742 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
746 * @brief Transmit a message that the contact request had been accepted
748 * @param string $target Target profile
750 * @param integer $uid User ID
752 public static function transmitContactAccept($target, $id, $uid)
754 $profile = APContact::getProfileByURL($target);
756 $owner = User::getOwnerDataById($uid);
757 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
758 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
760 'actor' => $owner['url'],
761 'object' => ['id' => $id, 'type' => 'Follow',
762 'actor' => $profile['url'],
763 'object' => $owner['url']],
764 'to' => $profile['url']];
766 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
768 $signed = LDSignature::sign($data, $owner);
769 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
775 * @param string $target Target profile
777 * @param integer $uid User ID
779 public static function transmitContactReject($target, $id, $uid)
781 $profile = APContact::getProfileByURL($target);
783 $owner = User::getOwnerDataById($uid);
784 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
785 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
787 'actor' => $owner['url'],
788 'object' => ['id' => $id, 'type' => 'Follow',
789 'actor' => $profile['url'],
790 'object' => $owner['url']],
791 'to' => $profile['url']];
793 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
795 $signed = LDSignature::sign($data, $owner);
796 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
802 * @param string $target Target profile
803 * @param integer $uid User ID
805 public static function transmitContactUndo($target, $uid)
807 $profile = APContact::getProfileByURL($target);
809 $id = System::baseUrl() . '/activity/' . System::createGUID();
811 $owner = User::getOwnerDataById($uid);
812 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
815 'actor' => $owner['url'],
816 'object' => ['id' => $id, 'type' => 'Follow',
817 'actor' => $owner['url'],
818 'object' => $profile['url']],
819 'to' => $profile['url']];
821 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
823 $signed = LDSignature::sign($data, $owner);
824 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
828 * Fetches ActivityPub content from the given url
830 * @param string $url content url
833 public static function fetchContent($url)
835 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
836 if (!$ret['success'] || empty($ret['body'])) {
839 return json_decode($ret['body'], true);
843 * Fetches a profile from the given url into an array that is compatible to Probe::uri
845 * @param string $url profile url
848 public static function probeProfile($url)
850 $apcontact = APContact::getProfileByURL($url, true);
851 if (empty($apcontact)) {
855 $profile = ['network' => Protocol::ACTIVITYPUB];
856 $profile['nick'] = $apcontact['nick'];
857 $profile['name'] = $apcontact['name'];
858 $profile['guid'] = $apcontact['uuid'];
859 $profile['url'] = $apcontact['url'];
860 $profile['addr'] = $apcontact['addr'];
861 $profile['alias'] = $apcontact['alias'];
862 $profile['photo'] = $apcontact['photo'];
863 // $profile['community']
864 // $profile['keywords']
865 // $profile['location']
866 $profile['about'] = $apcontact['about'];
867 $profile['batch'] = $apcontact['sharedinbox'];
868 $profile['notify'] = $apcontact['inbox'];
869 $profile['poll'] = $apcontact['outbox'];
870 $profile['pubkey'] = $apcontact['pubkey'];
871 $profile['baseurl'] = $apcontact['baseurl'];
873 // Remove all "null" fields
874 foreach ($profile as $field => $content) {
875 if (is_null($content)) {
876 unset($profile[$field]);
888 * @param integer $uid User ID
890 public static function processInbox($body, $header, $uid)
892 $http_signer = HTTPSignature::getSigner($body, $header);
893 if (empty($http_signer)) {
894 logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
897 logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
900 $activity = json_decode($body, true);
902 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
903 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
905 if (empty($activity)) {
906 logger('Invalid body.', LOGGER_DEBUG);
910 if (LDSignature::isSigned($activity)) {
911 $ld_signer = LDSignature::getSigner($activity);
912 if (empty($ld_signer)) {
913 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
915 if (!empty($ld_signer && ($actor == $http_signer))) {
916 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
917 $trust_source = true;
918 } elseif (!empty($ld_signer)) {
919 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
920 $trust_source = true;
921 } elseif ($actor == $http_signer) {
922 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
923 $trust_source = true;
925 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
926 $trust_source = false;
928 } elseif ($actor == $http_signer) {
929 logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
930 $trust_source = true;
932 logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
933 $trust_source = false;
936 self::processActivity($activity, $body, $uid, $trust_source);
943 * @param integer $uid User ID
945 public static function fetchOutbox($url, $uid)
947 $data = self::fetchContent($url);
952 if (!empty($data['orderedItems'])) {
953 $items = $data['orderedItems'];
954 } elseif (!empty($data['first']['orderedItems'])) {
955 $items = $data['first']['orderedItems'];
956 } elseif (!empty($data['first'])) {
957 self::fetchOutbox($data['first'], $uid);
963 foreach ($items as $activity) {
964 self::processActivity($activity, '', $uid, true);
971 * @param array $activity
972 * @param integer $uid User ID
973 * @param $trust_source
977 private static function prepareObjectData($activity, $uid, &$trust_source)
979 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
981 logger('Empty actor', LOGGER_DEBUG);
985 // Fetch all receivers from to, cc, bto and bcc
986 $receivers = self::getReceivers($activity, $actor);
988 // When it is a delivery to a personal inbox we add that user to the receivers
990 $owner = User::getOwnerDataById($uid);
991 $additional = ['uid:' . $uid => $uid];
992 $receivers = array_merge($receivers, $additional);
995 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
997 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
998 if (empty($object_id)) {
999 logger('No object found', LOGGER_DEBUG);
1003 // Fetch the content only on activities where this matters
1004 if (in_array($activity['type'], ['Create', 'Announce'])) {
1005 $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
1006 if (empty($object_data)) {
1007 logger("Object data couldn't be processed", LOGGER_DEBUG);
1010 // We had been able to retrieve the object data - so we can trust the source
1011 $trust_source = true;
1012 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
1013 // Create a mostly empty array out of the activity data (instead of the object).
1014 // This way we later don't have to check for the existence of ech individual array element.
1015 $object_data = self::processObject($activity);
1016 $object_data['name'] = $activity['type'];
1017 $object_data['author'] = $activity['actor'];
1018 $object_data['object'] = $object_id;
1019 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
1022 $object_data['id'] = $activity['id'];
1023 $object_data['object'] = $activity['object'];
1024 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
1027 $object_data = self::addActivityFields($object_data, $activity);
1029 $object_data['type'] = $activity['type'];
1030 $object_data['owner'] = $actor;
1031 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
1033 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
1035 return $object_data;
1041 * @param array $activity
1043 * @param integer $uid User ID
1044 * @param $trust_source
1046 private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
1048 if (empty($activity['type'])) {
1049 logger('Empty type', LOGGER_DEBUG);
1053 if (empty($activity['object'])) {
1054 logger('Empty object', LOGGER_DEBUG);
1058 if (empty($activity['actor'])) {
1059 logger('Empty actor', LOGGER_DEBUG);
1064 // $trust_source is called by reference and is set to true if the content was retrieved successfully
1065 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
1066 if (empty($object_data)) {
1067 logger('No object data found', LOGGER_DEBUG);
1071 if (!$trust_source) {
1072 logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
1075 switch ($activity['type']) {
1078 self::createItem($object_data, $body);
1082 self::likeItem($object_data, $body);
1086 self::dislikeItem($object_data, $body);
1090 if (in_array($object_data['object_type'], ['Person', 'Organization', 'Service', 'Group', 'Application'])) {
1091 self::updatePerson($object_data, $body);
1099 self::followUser($object_data);
1103 if ($object_data['object_type'] == 'Follow') {
1104 self::acceptFollowUser($object_data);
1109 if ($object_data['object_type'] == 'Follow') {
1110 self::undoFollowUser($object_data);
1111 } elseif (in_array($object_data['object_type'], ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'])) {
1112 self::undoActivity($object_data);
1117 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
1125 * @param array $activity
1130 private static function getReceivers($activity, $actor)
1134 // When it is an answer, we inherite the receivers from the parent
1135 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1136 if (!empty($replyto)) {
1137 $parents = Item::select(['uid'], ['uri' => $replyto]);
1138 while ($parent = Item::fetch($parents)) {
1139 $receivers['uid:' . $parent['uid']] = $parent['uid'];
1143 if (!empty($actor)) {
1144 $profile = APContact::getProfileByURL($actor);
1145 $followers = defaults($profile, 'followers', '');
1147 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
1149 logger('Empty actor', LOGGER_DEBUG);
1153 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
1154 if (empty($activity[$element])) {
1158 // The receiver can be an array or a string
1159 if (is_string($activity[$element])) {
1160 $activity[$element] = [$activity[$element]];
1163 foreach ($activity[$element] as $receiver) {
1164 if ($receiver == self::PUBLIC_COLLECTION) {
1165 $receivers['uid:0'] = 0;
1168 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
1169 // This will most likely catch all OStatus connections to Mastodon
1170 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
1171 , 'archive' => false, 'pending' => false];
1172 $contacts = DBA::select('contact', ['uid'], $condition);
1173 while ($contact = DBA::fetch($contacts)) {
1174 if ($contact['uid'] != 0) {
1175 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1178 DBA::close($contacts);
1181 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
1182 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1183 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
1184 $contacts = DBA::select('contact', ['uid'], $condition);
1185 while ($contact = DBA::fetch($contacts)) {
1186 if ($contact['uid'] != 0) {
1187 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1190 DBA::close($contacts);
1194 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1195 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1196 if (!DBA::isResult($contact)) {
1199 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1203 self::switchContacts($receivers, $actor);
1212 * @param integer $uid User ID
1215 private static function switchContact($cid, $uid, $url)
1217 $profile = ActivityPub::probeProfile($url);
1218 if (empty($profile)) {
1222 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
1224 $photo = $profile['photo'];
1225 unset($profile['photo']);
1226 unset($profile['baseurl']);
1228 $profile['nurl'] = normalise_link($profile['url']);
1229 DBA::update('contact', $profile, ['id' => $cid]);
1231 Contact::updateAvatar($photo, $uid, $cid);
1240 private static function switchContacts($receivers, $actor)
1242 if (empty($actor)) {
1246 foreach ($receivers as $receiver) {
1247 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
1248 if (DBA::isResult($contact)) {
1249 self::switchContact($contact['id'], $receiver, $actor);
1252 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
1253 if (DBA::isResult($contact)) {
1254 self::switchContact($contact['id'], $receiver, $actor);
1262 * @param $object_data
1263 * @param array $activity
1267 private static function addActivityFields($object_data, $activity)
1269 if (!empty($activity['published']) && empty($object_data['published'])) {
1270 $object_data['published'] = $activity['published'];
1273 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1274 $object_data['updated'] = $activity['updated'];
1277 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1278 $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1281 if (!empty($activity['instrument'])) {
1282 $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1284 return $object_data;
1292 * @param $trust_source
1296 private static function fetchObject($object_id, $object = [], $trust_source = false)
1298 if (!$trust_source || is_string($object)) {
1299 $data = self::fetchContent($object_id);
1301 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
1304 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
1307 logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
1311 if (is_string($data)) {
1312 $item = Item::selectFirst([], ['uri' => $data]);
1313 if (!DBA::isResult($item)) {
1314 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1317 logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
1318 $data = self::createNote($item);
1321 if (empty($data['type'])) {
1322 logger('Empty type', LOGGER_DEBUG);
1326 switch ($data['type']) {
1330 return self::processObject($data);
1333 if (empty($data['object'])) {
1336 return self::fetchObject($data['object']);
1343 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
1355 private static function processObject(&$object)
1357 if (empty($object['id'])) {
1362 $object_data['object_type'] = $object['type'];
1363 $object_data['id'] = $object['id'];
1365 if (!empty($object['inReplyTo'])) {
1366 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1368 $object_data['reply-to-id'] = $object_data['id'];
1371 $object_data['published'] = defaults($object, 'published', null);
1372 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1374 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1375 $object_data['published'] = $object_data['updated'];
1378 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
1379 if (empty($actor)) {
1380 $actor = defaults($object, 'actor', null);
1383 $object_data['diaspora:guid'] = defaults($object, 'diaspora:guid', null);
1384 $object_data['owner'] = $object_data['author'] = $actor;
1385 $object_data['context'] = defaults($object, 'context', null);
1386 $object_data['conversation'] = defaults($object, 'conversation', null);
1387 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1388 $object_data['name'] = defaults($object, 'title', null);
1389 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1390 $object_data['summary'] = defaults($object, 'summary', null);
1391 $object_data['content'] = defaults($object, 'content', null);
1392 $object_data['source'] = defaults($object, 'source', null);
1393 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1394 $object_data['attachments'] = defaults($object, 'attachment', null);
1395 $object_data['tags'] = defaults($object, 'tag', null);
1396 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1397 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1398 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1400 // Common object data:
1403 // @context, type, actor, signature, mediaType, duration, replies, icon
1405 // Also missing: (Defined in the standard, but currently unused)
1406 // audience, preview, endTime, startTime, generator, image
1411 // contentMap, announcement_count, announcements, context_id, likes, like_count
1412 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1417 // category, licence, language, commentsEnabled
1420 // views, waitTranscoding, state, support, subtitleLanguage
1421 // likes, dislikes, shares, comments
1423 return $object_data;
1427 * @brief Converts mentions from Pleroma into the Friendica format
1429 * @param string $body
1431 * @return converted body
1433 private static function convertMentions($body)
1435 $URLSearchString = "^\[\]";
1436 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1442 * @brief Constructs a string with tags for a given tag array
1444 * @param array $tags
1445 * @param boolean $sensitive
1447 * @return string with tags
1449 private static function constructTagList($tags, $sensitive)
1456 foreach ($tags as $tag) {
1457 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1458 if (!empty($tag_text)) {
1462 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1466 /// @todo add nsfw for $sensitive
1474 * @param $attachments
1475 * @param array $item
1477 * @return item array
1479 private static function constructAttachList($attachments, $item)
1481 if (empty($attachments)) {
1485 foreach ($attachments as $attach) {
1486 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1487 if ($filetype == 'image') {
1488 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1490 if (!empty($item["attach"])) {
1491 $item["attach"] .= ',';
1493 $item["attach"] = '';
1495 if (!isset($attach['length'])) {
1496 $attach['length'] = "0";
1498 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1508 * @param array $activity
1511 private static function createItem($activity, $body)
1514 $item['verb'] = ACTIVITY_POST;
1515 $item['parent-uri'] = $activity['reply-to-id'];
1517 if ($activity['reply-to-id'] == $activity['id']) {
1518 $item['gravity'] = GRAVITY_PARENT;
1519 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1521 $item['gravity'] = GRAVITY_COMMENT;
1522 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1525 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
1526 logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
1527 self::fetchMissingActivity($activity['reply-to-id'], $activity);
1530 self::postItem($activity, $item, $body);
1536 * @param array $activity
1539 private static function likeItem($activity, $body)
1542 $item['verb'] = ACTIVITY_LIKE;
1543 $item['parent-uri'] = $activity['object'];
1544 $item['gravity'] = GRAVITY_ACTIVITY;
1545 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1547 self::postItem($activity, $item, $body);
1553 * @param array $activity
1556 private static function dislikeItem($activity, $body)
1559 $item['verb'] = ACTIVITY_DISLIKE;
1560 $item['parent-uri'] = $activity['object'];
1561 $item['gravity'] = GRAVITY_ACTIVITY;
1562 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1564 self::postItem($activity, $item, $body);
1570 * @param array $activity
1571 * @param array $item
1574 private static function postItem($activity, $item, $body)
1576 /// @todo What to do with $activity['context']?
1578 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
1579 logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
1583 $item['network'] = Protocol::ACTIVITYPUB;
1584 $item['private'] = !in_array(0, $activity['receiver']);
1585 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1586 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1587 $item['uri'] = $activity['id'];
1588 $item['created'] = $activity['published'];
1589 $item['edited'] = $activity['updated'];
1590 $item['guid'] = $activity['diaspora:guid'];
1591 $item['title'] = HTML::toBBCode($activity['name']);
1592 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1593 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1594 $item['location'] = $activity['location'];
1595 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1596 $item['app'] = $activity['service'];
1597 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1599 $item = self::constructAttachList($activity['attachments'], $item);
1601 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1602 if (!empty($source)) {
1603 $item['body'] = $source;
1606 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1607 $item['source'] = $body;
1608 $item['conversation-href'] = $activity['context'];
1609 $item['conversation-uri'] = $activity['conversation'];
1611 foreach ($activity['receiver'] as $receiver) {
1612 $item['uid'] = $receiver;
1613 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1615 if (($receiver != 0) && empty($item['contact-id'])) {
1616 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1619 $item_id = Item::insert($item);
1620 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1630 private static function fetchMissingActivity($url, $child)
1632 if (Config::get('system', 'ostatus_full_threads')) {
1636 $object = ActivityPub::fetchContent($url);
1637 if (empty($object)) {
1638 logger('Activity ' . $url . ' was not fetchable, aborting.');
1643 $activity['@context'] = $object['@context'];
1644 unset($object['@context']);
1645 $activity['id'] = $object['id'];
1646 $activity['to'] = defaults($object, 'to', []);
1647 $activity['cc'] = defaults($object, 'cc', []);
1648 $activity['actor'] = $child['author'];
1649 $activity['object'] = $object;
1650 $activity['published'] = $object['published'];
1651 $activity['type'] = 'Create';
1653 self::processActivity($activity);
1654 logger('Activity ' . $url . ' had been fetched and processed.');
1658 * @brief perform a "follow" request
1660 * @param array $activity
1662 private static function followUser($activity)
1664 $actor = JsonLD::fetchElement($activity, 'object', 'id');
1665 $uid = User::getIdForURL($actor);
1670 $owner = User::getOwnerDataById($uid);
1672 $cid = Contact::getIdForURL($activity['owner'], $uid);
1674 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1679 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1680 'author-link' => $activity['owner']];
1682 Contact::addRelationship($owner, $contact, $item);
1683 $cid = Contact::getIdForURL($activity['owner'], $uid);
1688 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1689 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1690 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1693 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1694 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1698 * @brief Update the given profile
1700 * @param array $activity
1702 private static function updatePerson($activity)
1704 if (empty($activity['object']['id'])) {
1708 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
1709 APContact::getProfileByURL($activity['object']['id'], true);
1713 * @brief Accept a follow request
1715 * @param array $activity
1717 private static function acceptFollowUser($activity)
1719 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1720 $uid = User::getIdForURL($actor);
1725 $owner = User::getOwnerDataById($uid);
1727 $cid = Contact::getIdForURL($activity['owner'], $uid);
1729 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1733 $fields = ['pending' => false];
1735 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1736 if ($contact['rel'] == Contact::FOLLOWER) {
1737 $fields['rel'] = Contact::FRIEND;
1740 $condition = ['id' => $cid];
1741 DBA::update('contact', $fields, $condition);
1742 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1746 * @brief Undo activity like "like" or "dislike"
1748 * @param array $activity
1750 private static function undoActivity($activity)
1752 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
1753 if (empty($activity_url)) {
1757 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1758 if (empty($actor)) {
1762 $author_id = Contact::getIdForURL($actor);
1763 if (empty($author_id)) {
1767 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1771 * @brief Activity to remove a follower
1773 * @param array $activity
1775 private static function undoFollowUser($activity)
1777 $object = JsonLD::fetchElement($activity, 'object', 'object');
1778 $uid = User::getIdForURL($object);
1783 $owner = User::getOwnerDataById($uid);
1785 $cid = Contact::getIdForURL($activity['owner'], $uid);
1787 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1791 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1792 if (!DBA::isResult($contact)) {
1796 Contact::removeFollower($owner, $contact);
1797 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);