3 * @file src/Protocol/ActivityPub.php
5 namespace Friendica\Protocol;
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Crypto;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\HTML;
24 use Friendica\Util\JsonLD;
25 use Friendica\Util\LDSignature;
26 use Friendica\Core\Config;
29 * @brief ActivityPub Protocol class
30 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
31 * https://www.w3.org/TR/activitypub/
32 * https://www.w3.org/TR/activitystreams-core/
33 * https://www.w3.org/TR/activitystreams-vocabulary/
35 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
36 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
38 * Digest: https://tools.ietf.org/html/rfc5843
39 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
41 * Mastodon implementation of supported activities:
42 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
47 * - Update (Image, Video, Article, Note)
51 * Check what this is meant to do:
57 * - Undo Accept (Problem: This could invert a contact accept or an event accept)
60 * - Delete (Application, Group, Organization, Person, Service)
70 * - Queueing unsucessful deliveries
71 * - Polling the outboxes for missing content?
72 * - Possibly using the LD-JSON parser
76 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
77 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
78 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
79 'diaspora' => 'https://diasporafoundation.org/ns/',
80 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
81 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
82 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
83 const CONTENT_TYPES = ['Note', 'Article', 'Video', 'Image'];
84 const ACTIVITY_TYPES = ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'];
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 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
247 'account_removed' => false, 'verified' => true];
248 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
249 $user = DBA::selectFirst('user', $fields, $condition);
250 if (!DBA::isResult($user)) {
254 $fields = ['locality', 'region', 'country-name'];
255 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
256 if (!DBA::isResult($profile)) {
260 $fields = ['name', 'url', 'location', 'about', 'avatar'];
261 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
262 if (!DBA::isResult($contact)) {
266 $data = ['@context' => self::CONTEXT];
267 $data['id'] = $contact['url'];
268 $data['diaspora:guid'] = $user['guid'];
269 $data['type'] = self::ACCOUNT_TYPES[$user['account-type']];
270 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
271 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
272 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
273 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
274 $data['preferredUsername'] = $user['nickname'];
275 $data['name'] = $contact['name'];
276 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
277 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
278 $data['summary'] = $contact['about'];
279 $data['url'] = $contact['url'];
280 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
281 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
282 'owner' => $contact['url'],
283 'publicKeyPem' => $user['pubkey']];
284 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
285 $data['icon'] = ['type' => 'Image',
286 'url' => $contact['avatar']];
288 // tags: https://kitty.town/@inmysocks/100656097926961126.json
293 * @brief Returns an array with permissions of a given item array
297 * @return array with permissions
299 private static function fetchPermissionBlockFromConversation($item)
301 if (empty($item['thr-parent'])) {
305 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
306 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
307 if (!DBA::isResult($conversation)) {
311 $activity = json_decode($conversation['source'], true);
313 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
314 $profile = APContact::getByURL($actor);
316 $item_profile = APContact::getByURL($item['author-link']);
317 $exclude[] = $item['author-link'];
319 if ($item['gravity'] == GRAVITY_PARENT) {
320 $exclude[] = $item['owner-link'];
323 $permissions['to'][] = $actor;
325 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
326 if (empty($activity[$element])) {
329 if (is_string($activity[$element])) {
330 $activity[$element] = [$activity[$element]];
333 foreach ($activity[$element] as $receiver) {
334 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
335 $receiver = $item_profile['followers'];
337 if (!in_array($receiver, $exclude)) {
338 $permissions[$element][] = $receiver;
346 * @brief Creates an array of permissions from an item thread
350 * @return permission array
352 public static function createPermissionBlockForItem($item)
354 $data = ['to' => [], 'cc' => []];
356 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
358 $actor_profile = APContact::getByURL($item['author-link']);
360 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
362 $contacts[$item['author-link']] = $item['author-link'];
364 if (!$item['private']) {
365 $data['to'][] = self::PUBLIC_COLLECTION;
366 if (!empty($actor_profile['followers'])) {
367 $data['cc'][] = $actor_profile['followers'];
370 foreach ($terms as $term) {
371 $profile = APContact::getByURL($term['url'], false);
372 if (!empty($profile) && empty($contacts[$profile['url']])) {
373 $data['cc'][] = $profile['url'];
374 $contacts[$profile['url']] = $profile['url'];
378 $receiver_list = Item::enumeratePermissions($item);
382 foreach ($terms as $term) {
383 $cid = Contact::getIdForURL($term['url'], $item['uid']);
384 if (!empty($cid) && in_array($cid, $receiver_list)) {
385 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
386 $data['to'][] = $contact['url'];
387 $contacts[$contact['url']] = $contact['url'];
391 foreach ($receiver_list as $receiver) {
392 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
393 if (empty($contacts[$contact['url']])) {
394 $data['cc'][] = $contact['url'];
395 $contacts[$contact['url']] = $contact['url'];
400 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
401 while ($parent = Item::fetch($parents)) {
402 // Don't include data from future posts
403 if ($parent['id'] >= $item['id']) {
407 $profile = APContact::getByURL($parent['author-link'], false);
408 if (!empty($profile) && empty($contacts[$profile['url']])) {
409 $data['cc'][] = $profile['url'];
410 $contacts[$profile['url']] = $profile['url'];
413 if ($item['gravity'] != GRAVITY_PARENT) {
417 $profile = APContact::getByURL($parent['owner-link'], false);
418 if (!empty($profile) && empty($contacts[$profile['url']])) {
419 $data['cc'][] = $profile['url'];
420 $contacts[$profile['url']] = $profile['url'];
423 DBA::close($parents);
425 if (empty($data['to'])) {
426 $data['to'] = $data['cc'];
434 * @brief Fetches a list of inboxes of followers of a given user
436 * @param integer $uid User ID
438 * @return array of follower inboxes
440 private static function fetchTargetInboxesforUser($uid)
444 $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
445 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB,
446 'archive' => false, 'pending' => false]);
447 while ($contact = DBA::fetch($contacts)) {
448 $contact = defaults($contact, 'batch', $contact['notify']);
449 $inboxes[$contact] = $contact;
451 DBA::close($contacts);
457 * @brief Fetches an array of inboxes for the given item and user
460 * @param integer $uid User ID
462 * @return array with inboxes
464 public static function fetchTargetInboxes($item, $uid)
466 $permissions = self::createPermissionBlockForItem($item);
467 if (empty($permissions)) {
473 if ($item['gravity'] == GRAVITY_ACTIVITY) {
474 $item_profile = APContact::getByURL($item['author-link']);
476 $item_profile = APContact::getByURL($item['owner-link']);
479 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
480 if (empty($permissions[$element])) {
484 foreach ($permissions[$element] as $receiver) {
485 if ($receiver == $item_profile['followers']) {
486 $inboxes = self::fetchTargetInboxesforUser($uid);
488 $profile = APContact::getByURL($receiver);
489 if (!empty($profile)) {
490 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
491 $inboxes[$target] = $target;
501 * @brief Returns the activity type of a given item
505 * @return activity type
507 public static function getTypeOfItem($item)
509 if ($item['verb'] == ACTIVITY_POST) {
510 if ($item['created'] == $item['edited']) {
515 } elseif ($item['verb'] == ACTIVITY_LIKE) {
517 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
519 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
521 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
523 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
524 $type = 'TentativeAccept';
533 * @brief Creates an activity array for a given item id
535 * @param integer $item_id
536 * @param boolean $object_mode Is the activity item is used inside another object?
538 * @return array of activity
540 public static function createActivityFromItem($item_id, $object_mode = false)
542 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
544 if (!DBA::isResult($item)) {
548 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
549 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
550 if (DBA::isResult($conversation)) {
551 $data = json_decode($conversation['source']);
557 $type = self::getTypeOfItem($item);
560 $data = ['@context' => self::CONTEXT];
562 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
564 } elseif ($item['deleted']) {
571 $data['id'] = $item['uri'] . '#' . $type;
572 $data['type'] = $type;
573 $data['actor'] = $item['author-link'];
575 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
577 if ($item["created"] != $item["edited"]) {
578 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
581 $data['context'] = self::fetchContextURLForItem($item);
583 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
585 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
586 $data['object'] = self::createNote($item);
587 } elseif ($data['type'] == 'Undo') {
588 $data['object'] = self::createActivityFromItem($item_id, true);
590 $data['object'] = $item['thr-parent'];
593 $owner = User::getOwnerDataById($item['uid']);
596 return LDSignature::sign($data, $owner);
603 * @brief Creates an object array for a given item id
605 * @param integer $item_id
607 * @return object array
609 public static function createObjectFromItemID($item_id)
611 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
613 if (!DBA::isResult($item)) {
617 $data = ['@context' => self::CONTEXT];
618 $data = array_merge($data, self::createNote($item));
624 * @brief Returns a tag array for a given item array
628 * @return array of tags
630 private static function createTagList($item)
634 $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
635 foreach ($terms as $term) {
636 $contact = Contact::getDetailsByURL($term['url']);
637 if (!empty($contact['addr'])) {
638 $mention = '@' . $contact['addr'];
640 $mention = '@' . $term['url'];
643 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
649 * @brief Fetches the "context" value for a givem item array from the "conversation" table
653 * @return string with context url
655 private static function fetchContextURLForItem($item)
657 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
658 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
659 $context_uri = $conversation['conversation-href'];
660 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
661 $context_uri = $conversation['conversation-uri'];
663 $context_uri = str_replace('/object/', '/context/', $item['parent-uri']);
669 * @brief Creates a note/article object array
673 * @return object array
675 private static function createNote($item)
677 if (!empty($item['title'])) {
683 if ($item['deleted']) {
688 $data['id'] = $item['uri'];
689 $data['type'] = $type;
691 if ($item['deleted']) {
695 $data['summary'] = null; // Ignore by now
697 if ($item['uri'] != $item['thr-parent']) {
698 $data['inReplyTo'] = $item['thr-parent'];
700 $data['inReplyTo'] = null;
703 $data['diaspora:guid'] = $item['guid'];
704 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
706 if ($item["created"] != $item["edited"]) {
707 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
710 $data['url'] = $item['plink'];
711 $data['attributedTo'] = $item['author-link'];
712 $data['actor'] = $item['author-link'];
713 $data['sensitive'] = false; // - Query NSFW
714 $data['context'] = self::fetchContextURLForItem($item);
716 if (!empty($item['title'])) {
717 $data['name'] = BBCode::convert($item['title'], false, 7);
720 $data['content'] = BBCode::convert($item['body'], false, 7);
721 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
723 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
724 $data['diaspora:comment'] = $item['signed_text'];
727 $data['attachment'] = []; // @ToDo
728 $data['tag'] = self::createTagList($item);
729 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
735 * @brief Transmits a profile change to the followers
737 * @param integer $uid User ID
739 public static function transmitProfileUpdate($uid)
741 $owner = User::getOwnerDataById($uid);
742 $profile = APContact::getByURL($owner['url']);
744 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
745 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
747 'actor' => $owner['url'],
748 'object' => self::profile($uid),
749 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
750 'to' => [$profile['followers']],
753 logger('Sending profile update to followers for user ' . $uid, LOGGER_DEBUG);
755 $signed = LDSignature::sign($data, $owner);
757 $inboxes = self::fetchTargetInboxesforUser($uid);
759 foreach ($inboxes as $inbox) {
760 logger('Deliver profile update for user ' . $uid . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
761 HTTPSignature::transmit($signed, $inbox, $uid);
765 * @brief Transmits a given activity to a target
767 * @param array $activity
768 * @param string $target Target profile
769 * @param integer $uid User ID
771 public static function transmitActivity($activity, $target, $uid)
773 $profile = APContact::getByURL($target);
775 $owner = User::getOwnerDataById($uid);
777 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
778 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
780 'actor' => $owner['url'],
781 'object' => $profile['url'],
782 'to' => $profile['url']];
784 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
786 $signed = LDSignature::sign($data, $owner);
787 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
791 * @brief Transmit a message that the contact request had been accepted
793 * @param string $target Target profile
795 * @param integer $uid User ID
797 public static function transmitContactAccept($target, $id, $uid)
799 $profile = APContact::getByURL($target);
801 $owner = User::getOwnerDataById($uid);
802 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
803 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
805 'actor' => $owner['url'],
806 'object' => ['id' => $id, 'type' => 'Follow',
807 'actor' => $profile['url'],
808 'object' => $owner['url']],
809 'to' => $profile['url']];
811 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
813 $signed = LDSignature::sign($data, $owner);
814 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
820 * @param string $target Target profile
822 * @param integer $uid User ID
824 public static function transmitContactReject($target, $id, $uid)
826 $profile = APContact::getByURL($target);
828 $owner = User::getOwnerDataById($uid);
829 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
830 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
832 'actor' => $owner['url'],
833 'object' => ['id' => $id, 'type' => 'Follow',
834 'actor' => $profile['url'],
835 'object' => $owner['url']],
836 'to' => $profile['url']];
838 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
840 $signed = LDSignature::sign($data, $owner);
841 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
847 * @param string $target Target profile
848 * @param integer $uid User ID
850 public static function transmitContactUndo($target, $uid)
852 $profile = APContact::getByURL($target);
854 $id = System::baseUrl() . '/activity/' . System::createGUID();
856 $owner = User::getOwnerDataById($uid);
857 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
860 'actor' => $owner['url'],
861 'object' => ['id' => $id, 'type' => 'Follow',
862 'actor' => $owner['url'],
863 'object' => $profile['url']],
864 'to' => $profile['url']];
866 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
868 $signed = LDSignature::sign($data, $owner);
869 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
873 * Fetches ActivityPub content from the given url
875 * @param string $url content url
878 public static function fetchContent($url)
880 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
881 if (!$ret['success'] || empty($ret['body'])) {
885 return json_decode($ret['body'], true);
889 * Fetches a profile from the given url into an array that is compatible to Probe::uri
891 * @param string $url profile url
894 public static function probeProfile($url)
896 $apcontact = APContact::getByURL($url, true);
897 if (empty($apcontact)) {
901 $profile = ['network' => Protocol::ACTIVITYPUB];
902 $profile['nick'] = $apcontact['nick'];
903 $profile['name'] = $apcontact['name'];
904 $profile['guid'] = $apcontact['uuid'];
905 $profile['url'] = $apcontact['url'];
906 $profile['addr'] = $apcontact['addr'];
907 $profile['alias'] = $apcontact['alias'];
908 $profile['photo'] = $apcontact['photo'];
909 // $profile['community']
910 // $profile['keywords']
911 // $profile['location']
912 $profile['about'] = $apcontact['about'];
913 $profile['batch'] = $apcontact['sharedinbox'];
914 $profile['notify'] = $apcontact['inbox'];
915 $profile['poll'] = $apcontact['outbox'];
916 $profile['pubkey'] = $apcontact['pubkey'];
917 $profile['baseurl'] = $apcontact['baseurl'];
919 // Remove all "null" fields
920 foreach ($profile as $field => $content) {
921 if (is_null($content)) {
922 unset($profile[$field]);
934 * @param integer $uid User ID
936 public static function processInbox($body, $header, $uid)
938 $http_signer = HTTPSignature::getSigner($body, $header);
939 if (empty($http_signer)) {
940 logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
943 logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
946 $activity = json_decode($body, true);
948 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
949 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
951 if (empty($activity)) {
952 logger('Invalid body.', LOGGER_DEBUG);
956 if (LDSignature::isSigned($activity)) {
957 $ld_signer = LDSignature::getSigner($activity);
958 if (empty($ld_signer)) {
959 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
961 if (!empty($ld_signer && ($actor == $http_signer))) {
962 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
963 $trust_source = true;
964 } elseif (!empty($ld_signer)) {
965 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
966 $trust_source = true;
967 } elseif ($actor == $http_signer) {
968 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
969 $trust_source = true;
971 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
972 $trust_source = false;
974 } elseif ($actor == $http_signer) {
975 logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
976 $trust_source = true;
978 logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
979 $trust_source = false;
982 self::processActivity($activity, $body, $uid, $trust_source);
989 * @param integer $uid User ID
991 public static function fetchOutbox($url, $uid)
993 $data = self::fetchContent($url);
998 if (!empty($data['orderedItems'])) {
999 $items = $data['orderedItems'];
1000 } elseif (!empty($data['first']['orderedItems'])) {
1001 $items = $data['first']['orderedItems'];
1002 } elseif (!empty($data['first'])) {
1003 self::fetchOutbox($data['first'], $uid);
1009 foreach ($items as $activity) {
1010 self::processActivity($activity, '', $uid, true);
1017 * @param array $activity
1018 * @param integer $uid User ID
1019 * @param $trust_source
1023 private static function prepareObjectData($activity, $uid, &$trust_source)
1025 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
1026 if (empty($actor)) {
1027 logger('Empty actor', LOGGER_DEBUG);
1031 // Fetch all receivers from to, cc, bto and bcc
1032 $receivers = self::getReceivers($activity, $actor);
1034 // When it is a delivery to a personal inbox we add that user to the receivers
1036 $owner = User::getOwnerDataById($uid);
1037 $additional = ['uid:' . $uid => $uid];
1038 $receivers = array_merge($receivers, $additional);
1041 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
1043 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
1044 if (empty($object_id)) {
1045 logger('No object found', LOGGER_DEBUG);
1049 // Fetch the content only on activities where this matters
1050 if (in_array($activity['type'], ['Create', 'Announce'])) {
1051 $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
1052 if (empty($object_data)) {
1053 logger("Object data couldn't be processed", LOGGER_DEBUG);
1056 // We had been able to retrieve the object data - so we can trust the source
1057 $trust_source = true;
1058 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
1059 // Create a mostly empty array out of the activity data (instead of the object).
1060 // This way we later don't have to check for the existence of ech individual array element.
1061 $object_data = self::processObject($activity);
1062 $object_data['name'] = $activity['type'];
1063 $object_data['author'] = $activity['actor'];
1064 $object_data['object'] = $object_id;
1065 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
1068 $object_data['id'] = $activity['id'];
1069 $object_data['object'] = $activity['object'];
1070 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
1073 $object_data = self::addActivityFields($object_data, $activity);
1075 $object_data['type'] = $activity['type'];
1076 $object_data['owner'] = $actor;
1077 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
1079 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
1081 return $object_data;
1087 * @param array $activity
1089 * @param integer $uid User ID
1090 * @param $trust_source
1092 private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
1094 if (empty($activity['type'])) {
1095 logger('Empty type', LOGGER_DEBUG);
1099 if (empty($activity['object'])) {
1100 logger('Empty object', LOGGER_DEBUG);
1104 if (empty($activity['actor'])) {
1105 logger('Empty actor', LOGGER_DEBUG);
1110 // $trust_source is called by reference and is set to true if the content was retrieved successfully
1111 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
1112 if (empty($object_data)) {
1113 logger('No object data found', LOGGER_DEBUG);
1117 if (!$trust_source) {
1118 logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
1121 switch ($activity['type']) {
1124 self::createItem($object_data, $body);
1128 self::likeItem($object_data, $body);
1132 self::dislikeItem($object_data, $body);
1136 if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
1138 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
1139 self::updatePerson($object_data, $body);
1144 if ($object_data['object_type'] == 'Tombstone') {
1145 self::deleteItem($object_data, $body);
1146 } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
1147 self::deletePerson($object_data, $body);
1152 self::followUser($object_data);
1156 if ($object_data['object_type'] == 'Follow') {
1157 self::acceptFollowUser($object_data);
1162 if ($object_data['object_type'] == 'Follow') {
1163 self::rejectFollowUser($object_data);
1168 if ($object_data['object_type'] == 'Follow') {
1169 self::undoFollowUser($object_data);
1170 } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES)) {
1171 self::undoActivity($object_data);
1176 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
1184 * @param array $activity
1189 private static function getReceivers($activity, $actor)
1193 // When it is an answer, we inherite the receivers from the parent
1194 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1195 if (!empty($replyto)) {
1196 $parents = Item::select(['uid'], ['uri' => $replyto]);
1197 while ($parent = Item::fetch($parents)) {
1198 $receivers['uid:' . $parent['uid']] = $parent['uid'];
1202 if (!empty($actor)) {
1203 $profile = APContact::getByURL($actor);
1204 $followers = defaults($profile, 'followers', '');
1206 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
1208 logger('Empty actor', LOGGER_DEBUG);
1212 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
1213 if (empty($activity[$element])) {
1217 // The receiver can be an array or a string
1218 if (is_string($activity[$element])) {
1219 $activity[$element] = [$activity[$element]];
1222 foreach ($activity[$element] as $receiver) {
1223 if ($receiver == self::PUBLIC_COLLECTION) {
1224 $receivers['uid:0'] = 0;
1227 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
1228 // This will most likely catch all OStatus connections to Mastodon
1229 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
1230 , 'archive' => false, 'pending' => false];
1231 $contacts = DBA::select('contact', ['uid'], $condition);
1232 while ($contact = DBA::fetch($contacts)) {
1233 if ($contact['uid'] != 0) {
1234 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1237 DBA::close($contacts);
1240 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
1241 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1242 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
1243 $contacts = DBA::select('contact', ['uid'], $condition);
1244 while ($contact = DBA::fetch($contacts)) {
1245 if ($contact['uid'] != 0) {
1246 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1249 DBA::close($contacts);
1253 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1254 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1255 if (!DBA::isResult($contact)) {
1258 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1262 self::switchContacts($receivers, $actor);
1271 * @param integer $uid User ID
1274 private static function switchContact($cid, $uid, $url)
1276 $profile = ActivityPub::probeProfile($url);
1277 if (empty($profile)) {
1281 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
1283 $photo = $profile['photo'];
1284 unset($profile['photo']);
1285 unset($profile['baseurl']);
1287 $profile['nurl'] = normalise_link($profile['url']);
1288 DBA::update('contact', $profile, ['id' => $cid]);
1290 Contact::updateAvatar($photo, $uid, $cid);
1299 private static function switchContacts($receivers, $actor)
1301 if (empty($actor)) {
1305 foreach ($receivers as $receiver) {
1306 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
1307 if (DBA::isResult($contact)) {
1308 self::switchContact($contact['id'], $receiver, $actor);
1311 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
1312 if (DBA::isResult($contact)) {
1313 self::switchContact($contact['id'], $receiver, $actor);
1321 * @param $object_data
1322 * @param array $activity
1326 private static function addActivityFields($object_data, $activity)
1328 if (!empty($activity['published']) && empty($object_data['published'])) {
1329 $object_data['published'] = $activity['published'];
1332 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1333 $object_data['updated'] = $activity['updated'];
1336 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1337 $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1340 if (!empty($activity['instrument'])) {
1341 $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1343 return $object_data;
1351 * @param $trust_source
1355 private static function fetchObject($object_id, $object = [], $trust_source = false)
1357 if (!$trust_source || is_string($object)) {
1358 $data = self::fetchContent($object_id);
1360 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
1363 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
1366 logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
1370 if (is_string($data)) {
1371 $item = Item::selectFirst([], ['uri' => $data]);
1372 if (!DBA::isResult($item)) {
1373 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1376 logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
1377 $data = self::createNote($item);
1380 if (empty($data['type'])) {
1381 logger('Empty type', LOGGER_DEBUG);
1385 if (in_array($data['type'], self::CONTENT_TYPES)) {
1386 return self::processObject($data);
1389 if ($data['type'] == 'Announce') {
1390 if (empty($data['object'])) {
1393 return self::fetchObject($data['object']);
1396 logger('Unhandled object type: ' . $data['type'], LOGGER_DEBUG);
1406 private static function processObject(&$object)
1408 if (empty($object['id'])) {
1413 $object_data['object_type'] = $object['type'];
1414 $object_data['id'] = $object['id'];
1416 if (!empty($object['inReplyTo'])) {
1417 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1419 $object_data['reply-to-id'] = $object_data['id'];
1422 $object_data['published'] = defaults($object, 'published', null);
1423 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1425 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1426 $object_data['published'] = $object_data['updated'];
1429 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
1430 if (empty($actor)) {
1431 $actor = defaults($object, 'actor', null);
1434 $object_data['diaspora:guid'] = defaults($object, 'diaspora:guid', null);
1435 $object_data['owner'] = $object_data['author'] = $actor;
1436 $object_data['context'] = defaults($object, 'context', null);
1437 $object_data['conversation'] = defaults($object, 'conversation', null);
1438 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1439 $object_data['name'] = defaults($object, 'title', null);
1440 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1441 $object_data['summary'] = defaults($object, 'summary', null);
1442 $object_data['content'] = defaults($object, 'content', null);
1443 $object_data['source'] = defaults($object, 'source', null);
1444 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1445 $object_data['attachments'] = defaults($object, 'attachment', null);
1446 $object_data['tags'] = defaults($object, 'tag', null);
1447 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1448 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1449 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1451 // Common object data:
1454 // @context, type, actor, signature, mediaType, duration, replies, icon
1456 // Also missing: (Defined in the standard, but currently unused)
1457 // audience, preview, endTime, startTime, generator, image
1462 // contentMap, announcement_count, announcements, context_id, likes, like_count
1463 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1468 // category, licence, language, commentsEnabled
1471 // views, waitTranscoding, state, support, subtitleLanguage
1472 // likes, dislikes, shares, comments
1474 return $object_data;
1478 * @brief Converts mentions from Pleroma into the Friendica format
1480 * @param string $body
1482 * @return converted body
1484 private static function convertMentions($body)
1486 $URLSearchString = "^\[\]";
1487 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1493 * @brief Constructs a string with tags for a given tag array
1495 * @param array $tags
1496 * @param boolean $sensitive
1498 * @return string with tags
1500 private static function constructTagList($tags, $sensitive)
1507 foreach ($tags as $tag) {
1508 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1509 if (!empty($tag_text)) {
1513 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1517 /// @todo add nsfw for $sensitive
1525 * @param $attachments
1526 * @param array $item
1528 * @return item array
1530 private static function constructAttachList($attachments, $item)
1532 if (empty($attachments)) {
1536 foreach ($attachments as $attach) {
1537 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1538 if ($filetype == 'image') {
1539 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1541 if (!empty($item["attach"])) {
1542 $item["attach"] .= ',';
1544 $item["attach"] = '';
1546 if (!isset($attach['length'])) {
1547 $attach['length'] = "0";
1549 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1559 * @param array $activity
1562 private static function createItem($activity, $body)
1565 $item['verb'] = ACTIVITY_POST;
1566 $item['parent-uri'] = $activity['reply-to-id'];
1568 if ($activity['reply-to-id'] == $activity['id']) {
1569 $item['gravity'] = GRAVITY_PARENT;
1570 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1572 $item['gravity'] = GRAVITY_COMMENT;
1573 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1576 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
1577 logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
1578 self::fetchMissingActivity($activity['reply-to-id'], $activity);
1581 self::postItem($activity, $item, $body);
1587 * @param array $activity
1590 private static function likeItem($activity, $body)
1593 $item['verb'] = ACTIVITY_LIKE;
1594 $item['parent-uri'] = $activity['object'];
1595 $item['gravity'] = GRAVITY_ACTIVITY;
1596 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1598 self::postItem($activity, $item, $body);
1602 * @brief Delete items
1604 * @param array $activity
1607 private static function deleteItem($activity)
1609 $owner = Contact::getIdForURL($activity['owner']);
1610 $object = JsonLD::fetchElement($activity, 'object', 'id');
1611 logger('Deleting item ' . $object . ' from ' . $owner, LOGGER_DEBUG);
1612 Item::delete(['uri' => $object, 'owner-id' => $owner]);
1618 * @param array $activity
1621 private static function dislikeItem($activity, $body)
1624 $item['verb'] = ACTIVITY_DISLIKE;
1625 $item['parent-uri'] = $activity['object'];
1626 $item['gravity'] = GRAVITY_ACTIVITY;
1627 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1629 self::postItem($activity, $item, $body);
1635 * @param array $activity
1636 * @param array $item
1639 private static function postItem($activity, $item, $body)
1641 /// @todo What to do with $activity['context']?
1643 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
1644 logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
1648 $item['network'] = Protocol::ACTIVITYPUB;
1649 $item['private'] = !in_array(0, $activity['receiver']);
1650 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1651 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1652 $item['uri'] = $activity['id'];
1653 $item['created'] = $activity['published'];
1654 $item['edited'] = $activity['updated'];
1655 $item['guid'] = $activity['diaspora:guid'];
1656 $item['title'] = HTML::toBBCode($activity['name']);
1657 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1658 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1659 $item['location'] = $activity['location'];
1660 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1661 $item['app'] = $activity['service'];
1662 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1664 $item = self::constructAttachList($activity['attachments'], $item);
1666 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1667 if (!empty($source)) {
1668 $item['body'] = $source;
1671 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1672 $item['source'] = $body;
1673 $item['conversation-href'] = $activity['context'];
1674 $item['conversation-uri'] = $activity['conversation'];
1676 foreach ($activity['receiver'] as $receiver) {
1677 $item['uid'] = $receiver;
1678 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1680 if (($receiver != 0) && empty($item['contact-id'])) {
1681 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1684 $item_id = Item::insert($item);
1685 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1695 private static function fetchMissingActivity($url, $child)
1697 if (Config::get('system', 'ostatus_full_threads')) {
1701 $object = ActivityPub::fetchContent($url);
1702 if (empty($object)) {
1703 logger('Activity ' . $url . ' was not fetchable, aborting.');
1708 $activity['@context'] = $object['@context'];
1709 unset($object['@context']);
1710 $activity['id'] = $object['id'];
1711 $activity['to'] = defaults($object, 'to', []);
1712 $activity['cc'] = defaults($object, 'cc', []);
1713 $activity['actor'] = $child['author'];
1714 $activity['object'] = $object;
1715 $activity['published'] = $object['published'];
1716 $activity['type'] = 'Create';
1718 self::processActivity($activity);
1719 logger('Activity ' . $url . ' had been fetched and processed.');
1723 * @brief perform a "follow" request
1725 * @param array $activity
1727 private static function followUser($activity)
1729 $actor = JsonLD::fetchElement($activity, 'object', 'id');
1730 $uid = User::getIdForURL($actor);
1735 $owner = User::getOwnerDataById($uid);
1737 $cid = Contact::getIdForURL($activity['owner'], $uid);
1739 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1744 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1745 'author-link' => $activity['owner']];
1747 Contact::addRelationship($owner, $contact, $item);
1748 $cid = Contact::getIdForURL($activity['owner'], $uid);
1753 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1754 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1755 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1758 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1759 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1763 * @brief Update the given profile
1765 * @param array $activity
1767 private static function updatePerson($activity)
1769 if (empty($activity['object']['id'])) {
1773 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
1774 APContact::getByURL($activity['object']['id'], true);
1778 * @brief Delete the given profile
1780 * @param array $activity
1782 private static function deletePerson($activity)
1784 if (empty($activity['object']['id']) || empty($activity['object']['actor'])) {
1785 logger('Empty object id or actor.', LOGGER_DEBUG);
1789 if ($activity['object']['id'] != $activity['object']['actor']) {
1790 logger('Object id does not match actor.', LOGGER_DEBUG);
1794 $contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($activity['object']['id'])]);
1795 while ($contact = DBA::fetch($contacts)) {
1796 Contact::remove($contact["id"]);
1798 DBA::close($contacts);
1800 logger('Deleted contact ' . $activity['object']['id'], LOGGER_DEBUG);
1804 * @brief Accept a follow request
1806 * @param array $activity
1808 private static function acceptFollowUser($activity)
1810 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1811 $uid = User::getIdForURL($actor);
1816 $owner = User::getOwnerDataById($uid);
1818 $cid = Contact::getIdForURL($activity['owner'], $uid);
1820 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1824 $fields = ['pending' => false];
1826 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1827 if ($contact['rel'] == Contact::FOLLOWER) {
1828 $fields['rel'] = Contact::FRIEND;
1831 $condition = ['id' => $cid];
1832 DBA::update('contact', $fields, $condition);
1833 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1837 * @brief Reject a follow request
1839 * @param array $activity
1841 private static function rejectFollowUser($activity)
1843 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1844 $uid = User::getIdForURL($actor);
1849 $owner = User::getOwnerDataById($uid);
1851 $cid = Contact::getIdForURL($activity['owner'], $uid);
1853 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1857 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING, 'pending' => true])) {
1858 Contact::remove($cid);
1859 logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', LOGGER_DEBUG);
1861 logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', LOGGER_DEBUG);
1866 * @brief Undo activity like "like" or "dislike"
1868 * @param array $activity
1870 private static function undoActivity($activity)
1872 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
1873 if (empty($activity_url)) {
1877 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1878 if (empty($actor)) {
1882 $author_id = Contact::getIdForURL($actor);
1883 if (empty($author_id)) {
1887 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1891 * @brief Activity to remove a follower
1893 * @param array $activity
1895 private static function undoFollowUser($activity)
1897 $object = JsonLD::fetchElement($activity, 'object', 'object');
1898 $uid = User::getIdForURL($object);
1903 $owner = User::getOwnerDataById($uid);
1905 $cid = Contact::getIdForURL($activity['owner'], $uid);
1907 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1911 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1912 if (!DBA::isResult($contact)) {
1916 Contact::removeFollower($owner, $contact);
1917 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);