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 = 'https://www.w3.org/ns/activitystreams#Public';
79 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
80 ['ostatus' => 'http://ostatus.org#', 'uuid' => 'http://schema.org/identifier',
81 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
82 'atomUri' => 'ostatus:atomUri', 'conversation' => 'ostatus:conversation',
83 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri']];
85 public static function isRequest()
87 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
88 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
91 public static function getFollowers($owner, $page = null)
93 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
94 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
95 $count = DBA::count('contact', $condition);
97 $data = ['@context' => self::CONTEXT];
98 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
99 $data['type'] = 'OrderedCollection';
100 $data['totalItems'] = $count;
102 // When we hide our friends we will only show the pure number but don't allow more.
103 $profile = Profile::getProfileForUser($owner['uid']);
104 if (!empty($profile['hide-friends'])) {
109 $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
113 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
114 while ($contact = DBA::fetch($contacts)) {
115 $list[] = $contact['url'];
119 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
122 $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
124 $data['orderedItems'] = $list;
130 public static function getFollowing($owner, $page = null)
132 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
133 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
134 $count = DBA::count('contact', $condition);
136 $data = ['@context' => self::CONTEXT];
137 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
138 $data['type'] = 'OrderedCollection';
139 $data['totalItems'] = $count;
141 // When we hide our friends we will only show the pure number but don't allow more.
142 $profile = Profile::getProfileForUser($owner['uid']);
143 if (!empty($profile['hide-friends'])) {
148 $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
152 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
153 while ($contact = DBA::fetch($contacts)) {
154 $list[] = $contact['url'];
158 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
161 $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
163 $data['orderedItems'] = $list;
169 public static function getOutbox($owner, $page = null)
171 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
173 $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact,
174 'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
175 'deleted' => false, 'visible' => true];
176 $count = DBA::count('item', $condition);
178 $data = ['@context' => self::CONTEXT];
179 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
180 $data['type'] = 'OrderedCollection';
181 $data['totalItems'] = $count;
184 $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
188 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
190 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
191 while ($item = Item::fetch($items)) {
192 $object = self::createObjectFromItemID($item['id']);
193 unset($object['@context']);
198 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
201 $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
203 $data['orderedItems'] = $list;
210 * Return the ActivityPub profile of the given user
212 * @param integer $uid User ID
215 public static function profile($uid)
217 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
218 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
219 'account_removed' => false, 'verified' => true];
220 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
221 $user = DBA::selectFirst('user', $fields, $condition);
222 if (!DBA::isResult($user)) {
226 $fields = ['locality', 'region', 'country-name'];
227 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
228 if (!DBA::isResult($profile)) {
232 $fields = ['name', 'url', 'location', 'about', 'avatar'];
233 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
234 if (!DBA::isResult($contact)) {
238 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
239 ['vcard' => 'http://www.w3.org/2006/vcard/ns#', 'uuid' => 'http://schema.org/identifier',
240 'sensitive' => 'as:sensitive', 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers']]];
242 $data['id'] = $contact['url'];
243 $data['uuid'] = $user['guid'];
244 $data['type'] = $accounttype[$user['account-type']];
245 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
246 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
247 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
248 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
249 $data['preferredUsername'] = $user['nickname'];
250 $data['name'] = $contact['name'];
251 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
252 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
253 $data['summary'] = $contact['about'];
254 $data['url'] = $contact['url'];
255 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
256 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
257 'owner' => $contact['url'],
258 'publicKeyPem' => $user['pubkey']];
259 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
260 $data['icon'] = ['type' => 'Image',
261 'url' => $contact['avatar']];
263 // tags: https://kitty.town/@inmysocks/100656097926961126.json
267 private static function fetchPermissionBlockFromConversation($item)
269 if (empty($item['thr-parent'])) {
273 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
274 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
275 if (!DBA::isResult($conversation)) {
279 $activity = json_decode($conversation['source'], true);
281 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
282 $profile = APContact::getProfileByURL($actor);
284 $item_profile = APContact::getProfileByURL($item['author-link']);
285 $exclude[] = $item['author-link'];
287 if ($item['gravity'] == GRAVITY_PARENT) {
288 $exclude[] = $item['owner-link'];
291 $permissions['to'][] = $actor;
293 $elements = ['to', 'cc', 'bto', 'bcc'];
294 foreach ($elements as $element) {
295 if (empty($activity[$element])) {
298 if (is_string($activity[$element])) {
299 $activity[$element] = [$activity[$element]];
302 foreach ($activity[$element] as $receiver) {
303 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
304 $receiver = $item_profile['followers'];
306 if (!in_array($receiver, $exclude)) {
307 $permissions[$element][] = $receiver;
314 public static function createPermissionBlockForItem($item)
316 $data = ['to' => [], 'cc' => []];
318 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
320 $actor_profile = APContact::getProfileByURL($item['author-link']);
322 $terms = Term::tagArrayFromItemId($item['id']);
324 $contacts[$item['author-link']] = $item['author-link'];
326 if (!$item['private']) {
327 $data['to'][] = self::PUBLIC;
328 if (!empty($actor_profile['followers'])) {
329 $data['cc'][] = $actor_profile['followers'];
332 foreach ($terms as $term) {
333 if ($term['type'] != TERM_MENTION) {
336 $profile = APContact::getProfileByURL($term['url'], false);
337 if (!empty($profile) && empty($contacts[$profile['url']])) {
338 $data['cc'][] = $profile['url'];
339 $contacts[$profile['url']] = $profile['url'];
343 $receiver_list = Item::enumeratePermissions($item);
347 foreach ($terms as $term) {
348 if ($term['type'] != TERM_MENTION) {
351 $cid = Contact::getIdForURL($term['url'], $item['uid']);
352 if (!empty($cid) && in_array($cid, $receiver_list)) {
353 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
354 $data['to'][] = $contact['url'];
355 $contacts[$contact['url']] = $contact['url'];
359 foreach ($receiver_list as $receiver) {
360 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
361 if (empty($contacts[$contact['url']])) {
362 $data['cc'][] = $contact['url'];
363 $contacts[$contact['url']] = $contact['url'];
368 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
369 while ($parent = Item::fetch($parents)) {
370 // Don't include data from future posts
371 if ($parent['id'] >= $item['id']) {
375 $profile = APContact::getProfileByURL($parent['author-link'], false);
376 if (!empty($profile) && empty($contacts[$profile['url']])) {
377 $data['cc'][] = $profile['url'];
378 $contacts[$profile['url']] = $profile['url'];
381 if ($item['gravity'] != GRAVITY_PARENT) {
385 $profile = APContact::getProfileByURL($parent['owner-link'], false);
386 if (!empty($profile) && empty($contacts[$profile['url']])) {
387 $data['cc'][] = $profile['url'];
388 $contacts[$profile['url']] = $profile['url'];
391 DBA::close($parents);
393 if (empty($data['to'])) {
394 $data['to'] = $data['cc'];
401 public static function fetchTargetInboxes($item, $uid)
403 $permissions = self::createPermissionBlockForItem($item);
404 if (empty($permissions)) {
410 if ($item['gravity'] == GRAVITY_ACTIVITY) {
411 $item_profile = APContact::getProfileByURL($item['author-link']);
413 $item_profile = APContact::getProfileByURL($item['owner-link']);
416 $elements = ['to', 'cc', 'bto', 'bcc'];
417 foreach ($elements as $element) {
418 if (empty($permissions[$element])) {
422 foreach ($permissions[$element] as $receiver) {
423 if ($receiver == $item_profile['followers']) {
424 $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
425 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB]);
426 while ($contact = DBA::fetch($contacts)) {
427 $contact = defaults($contact, 'batch', $contact['notify']);
428 $inboxes[$contact] = $contact;
430 DBA::close($contacts);
432 $profile = APContact::getProfileByURL($receiver);
433 if (!empty($profile)) {
434 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
435 $inboxes[$target] = $target;
444 public static function getTypeOfItem($item)
446 if ($item['verb'] == ACTIVITY_POST) {
447 if ($item['created'] == $item['edited']) {
452 } elseif ($item['verb'] == ACTIVITY_LIKE) {
454 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
456 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
458 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
460 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
461 $type = 'TentativeAccept';
469 public static function createActivityFromItem($item_id, $object_mode = false)
471 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
473 if (!DBA::isResult($item)) {
477 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
478 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
479 if (DBA::isResult($conversation)) {
480 $data = json_decode($conversation['source']);
486 $type = self::getTypeOfItem($item);
489 $data = ['@context' => self::CONTEXT];
491 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
493 } elseif ($item['deleted']) {
500 $data['id'] = $item['uri'] . '#' . $type;
501 $data['type'] = $type;
502 $data['actor'] = $item['author-link'];
504 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
506 if ($item["created"] != $item["edited"]) {
507 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
510 $data['context'] = self::fetchContextURLForItem($item);
512 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
514 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
515 $data['object'] = self::CreateNote($item);
516 } elseif ($data['type'] == 'Undo') {
517 $data['object'] = self::createActivityFromItem($item_id, true);
519 $data['object'] = $item['thr-parent'];
522 $owner = User::getOwnerDataById($item['uid']);
525 return LDSignature::sign($data, $owner);
531 public static function createObjectFromItemID($item_id)
533 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
535 if (!DBA::isResult($item)) {
539 $data = ['@context' => self::CONTEXT];
540 $data = array_merge($data, self::CreateNote($item));
545 private static function createTagList($item)
549 $terms = Term::tagArrayFromItemId($item['id']);
550 foreach ($terms as $term) {
551 if ($term['type'] == TERM_MENTION) {
552 $contact = Contact::getDetailsByURL($term['url']);
553 if (!empty($contact['addr'])) {
554 $mention = '@' . $contact['addr'];
556 $mention = '@' . $term['url'];
559 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
565 private static function fetchConversationURLForItem($item)
567 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
568 if (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
569 $conversation_uri = $conversation['conversation-uri'];
570 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
571 $conversation_uri = $conversation['conversation-href'];
573 $conversation_uri = str_replace('/object/', '/context/', $item['parent-uri']);
575 return $conversation_uri;
578 private static function fetchContextURLForItem($item)
580 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
581 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
582 $context_uri = $conversation['conversation-href'];
583 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
584 $context_uri = $conversation['conversation-uri'];
586 $context_uri = str_replace('/object/', '/context/', $item['parent-uri']);
591 private static function CreateNote($item)
593 if (!empty($item['title'])) {
599 if ($item['deleted']) {
604 $data['id'] = $item['uri'];
605 $data['type'] = $type;
607 if ($item['deleted']) {
611 $data['summary'] = null; // Ignore by now
613 if ($item['uri'] != $item['thr-parent']) {
614 $data['inReplyTo'] = $item['thr-parent'];
616 $data['inReplyTo'] = null;
619 $data['uuid'] = $item['guid'];
620 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
622 if ($item["created"] != $item["edited"]) {
623 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
626 $data['url'] = $item['plink'];
627 $data['attributedTo'] = $item['author-link'];
628 $data['actor'] = $item['author-link'];
629 $data['sensitive'] = false; // - Query NSFW
630 $data['conversation'] = self::fetchConversationURLForItem($item);
631 $data['context'] = self::fetchContextURLForItem($item);
633 if (!empty($item['title'])) {
634 $data['name'] = BBCode::convert($item['title'], false, 7);
637 $data['content'] = BBCode::convert($item['body'], false, 7);
638 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
639 $data['attachment'] = []; // @ToDo
640 $data['tag'] = self::createTagList($item);
641 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
646 public static function transmitActivity($activity, $target, $uid)
648 $profile = APContact::getProfileByURL($target);
650 $owner = User::getOwnerDataById($uid);
652 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
653 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
655 'actor' => $owner['url'],
656 'object' => $profile['url'],
657 'to' => $profile['url']];
659 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
661 $signed = LDSignature::sign($data, $owner);
662 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
665 public static function transmitContactAccept($target, $id, $uid)
667 $profile = APContact::getProfileByURL($target);
669 $owner = User::getOwnerDataById($uid);
670 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
671 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
673 'actor' => $owner['url'],
674 'object' => ['id' => $id, 'type' => 'Follow',
675 'actor' => $profile['url'],
676 'object' => $owner['url']],
677 'to' => $profile['url']];
679 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
681 $signed = LDSignature::sign($data, $owner);
682 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
685 public static function transmitContactReject($target, $id, $uid)
687 $profile = APContact::getProfileByURL($target);
689 $owner = User::getOwnerDataById($uid);
690 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
691 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
693 'actor' => $owner['url'],
694 'object' => ['id' => $id, 'type' => 'Follow',
695 'actor' => $profile['url'],
696 'object' => $owner['url']],
697 'to' => $profile['url']];
699 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
701 $signed = LDSignature::sign($data, $owner);
702 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
705 public static function transmitContactUndo($target, $uid)
707 $profile = APContact::getProfileByURL($target);
709 $id = System::baseUrl() . '/activity/' . System::createGUID();
711 $owner = User::getOwnerDataById($uid);
712 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
715 'actor' => $owner['url'],
716 'object' => ['id' => $id, 'type' => 'Follow',
717 'actor' => $owner['url'],
718 'object' => $profile['url']],
719 'to' => $profile['url']];
721 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
723 $signed = LDSignature::sign($data, $owner);
724 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
728 * Fetches ActivityPub content from the given url
730 * @param string $url content url
733 public static function fetchContent($url)
735 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
736 if (!$ret['success'] || empty($ret['body'])) {
739 return json_decode($ret['body'], true);
743 * Fetches a profile from the given url into an array that is compatible to Probe::uri
745 * @param string $url profile url
748 public static function probeProfile($url)
750 $apcontact = APContact::getProfileByURL($url, true);
751 if (empty($apcontact)) {
755 $profile = ['network' => Protocol::ACTIVITYPUB];
756 $profile['nick'] = $apcontact['nick'];
757 $profile['name'] = $apcontact['name'];
758 $profile['guid'] = $apcontact['uuid'];
759 $profile['url'] = $apcontact['url'];
760 $profile['addr'] = $apcontact['addr'];
761 $profile['alias'] = $apcontact['alias'];
762 $profile['photo'] = $apcontact['photo'];
763 // $profile['community']
764 // $profile['keywords']
765 // $profile['location']
766 $profile['about'] = $apcontact['about'];
767 $profile['batch'] = $apcontact['sharedinbox'];
768 $profile['notify'] = $apcontact['inbox'];
769 $profile['poll'] = $apcontact['outbox'];
770 $profile['pubkey'] = $apcontact['pubkey'];
771 $profile['baseurl'] = $apcontact['baseurl'];
773 // Remove all "null" fields
774 foreach ($profile as $field => $content) {
775 if (is_null($content)) {
776 unset($profile[$field]);
783 public static function processInbox($body, $header, $uid)
785 $http_signer = HTTPSignature::getSigner($body, $header);
786 if (empty($http_signer)) {
787 logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
790 logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
793 $activity = json_decode($body, true);
795 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
796 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
798 if (empty($activity)) {
799 logger('Invalid body.', LOGGER_DEBUG);
803 if (LDSignature::isSigned($activity)) {
804 $ld_signer = LDSignature::getSigner($activity);
805 if (empty($ld_signer)) {
806 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
808 if (!empty($ld_signer && ($actor == $http_signer))) {
809 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
810 $trust_source = true;
811 } elseif (!empty($ld_signer)) {
812 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
813 $trust_source = true;
814 } elseif ($actor == $http_signer) {
815 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
816 $trust_source = true;
818 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
819 $trust_source = false;
821 } elseif ($actor == $http_signer) {
822 logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
823 $trust_source = true;
825 logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
826 $trust_source = false;
829 self::processActivity($activity, $body, $uid, $trust_source);
832 public static function fetchOutbox($url, $uid)
834 $data = self::fetchContent($url);
839 if (!empty($data['orderedItems'])) {
840 $items = $data['orderedItems'];
841 } elseif (!empty($data['first']['orderedItems'])) {
842 $items = $data['first']['orderedItems'];
843 } elseif (!empty($data['first'])) {
844 self::fetchOutbox($data['first'], $uid);
850 foreach ($items as $activity) {
851 self::processActivity($activity, '', $uid, true);
855 private static function prepareObjectData($activity, $uid, &$trust_source)
857 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
859 logger('Empty actor', LOGGER_DEBUG);
863 // Fetch all receivers from to, cc, bto and bcc
864 $receivers = self::getReceivers($activity, $actor);
866 // When it is a delivery to a personal inbox we add that user to the receivers
868 $owner = User::getOwnerDataById($uid);
869 $additional = ['uid:' . $uid => $uid];
870 $receivers = array_merge($receivers, $additional);
873 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
875 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
876 if (empty($object_id)) {
877 logger('No object found', LOGGER_DEBUG);
881 // Fetch the content only on activities where this matters
882 if (in_array($activity['type'], ['Create', 'Announce'])) {
883 $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
884 if (empty($object_data)) {
885 logger("Object data couldn't be processed", LOGGER_DEBUG);
888 // We had been able to retrieve the object data - so we can trust the source
889 $trust_source = true;
890 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
891 // Create a mostly empty array out of the activity data (instead of the object).
892 // This way we later don't have to check for the existence of ech individual array element.
893 $object_data = self::ProcessObject($activity);
894 $object_data['name'] = $activity['type'];
895 $object_data['author'] = $activity['actor'];
896 $object_data['object'] = $object_id;
897 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
900 $object_data['id'] = $activity['id'];
901 $object_data['object'] = $activity['object'];
902 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
905 $object_data = self::addActivityFields($object_data, $activity);
907 $object_data['type'] = $activity['type'];
908 $object_data['owner'] = $actor;
909 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
911 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
916 private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
918 if (empty($activity['type'])) {
919 logger('Empty type', LOGGER_DEBUG);
923 if (empty($activity['object'])) {
924 logger('Empty object', LOGGER_DEBUG);
928 if (empty($activity['actor'])) {
929 logger('Empty actor', LOGGER_DEBUG);
934 // $trust_source is called by reference and is set to true if the content was retrieved successfully
935 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
936 if (empty($object_data)) {
937 logger('No object data found', LOGGER_DEBUG);
941 if (!$trust_source) {
942 logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
945 switch ($activity['type']) {
948 self::createItem($object_data, $body);
952 self::likeItem($object_data, $body);
956 self::dislikeItem($object_data, $body);
960 if (in_array($object_data['object_type'], ['Person', 'Organization', 'Service', 'Group', 'Application'])) {
961 self::updatePerson($object_data, $body);
969 self::followUser($object_data);
973 if ($object_data['object_type'] == 'Follow') {
974 self::acceptFollowUser($object_data);
979 if ($object_data['object_type'] == 'Follow') {
980 self::undoFollowUser($object_data);
981 } elseif (in_array($object_data['object_type'], ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'])) {
982 self::undoActivity($object_data);
987 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
992 private static function getReceivers($activity, $actor)
996 // When it is an answer, we inherite the receivers from the parent
997 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
998 if (!empty($replyto)) {
999 $parents = Item::select(['uid'], ['uri' => $replyto]);
1000 while ($parent = Item::fetch($parents)) {
1001 $receivers['uid:' . $parent['uid']] = $parent['uid'];
1005 if (!empty($actor)) {
1006 $profile = APContact::getProfileByURL($actor);
1007 $followers = defaults($profile, 'followers', '');
1009 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
1011 logger('Empty actor', LOGGER_DEBUG);
1015 $elements = ['to', 'cc', 'bto', 'bcc'];
1016 foreach ($elements as $element) {
1017 if (empty($activity[$element])) {
1021 // The receiver can be an arror or a string
1022 if (is_string($activity[$element])) {
1023 $activity[$element] = [$activity[$element]];
1026 foreach ($activity[$element] as $receiver) {
1027 if ($receiver == self::PUBLIC) {
1028 $receivers['uid:0'] = 0;
1031 if (($receiver == self::PUBLIC) && !empty($actor)) {
1032 // This will most likely catch all OStatus connections to Mastodon
1033 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]];
1034 $contacts = DBA::select('contact', ['uid'], $condition);
1035 while ($contact = DBA::fetch($contacts)) {
1036 if ($contact['uid'] != 0) {
1037 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1040 DBA::close($contacts);
1043 if (in_array($receiver, [$followers, self::PUBLIC]) && !empty($actor)) {
1044 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1045 'network' => Protocol::ACTIVITYPUB];
1046 $contacts = DBA::select('contact', ['uid'], $condition);
1047 while ($contact = DBA::fetch($contacts)) {
1048 if ($contact['uid'] != 0) {
1049 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1052 DBA::close($contacts);
1056 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1057 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1058 if (!DBA::isResult($contact)) {
1061 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1065 self::switchContacts($receivers, $actor);
1070 private static function switchContact($cid, $uid, $url)
1072 $profile = ActivityPub::probeProfile($url);
1073 if (empty($profile)) {
1077 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
1079 $photo = $profile['photo'];
1080 unset($profile['photo']);
1081 unset($profile['baseurl']);
1083 $profile['nurl'] = normalise_link($profile['url']);
1084 DBA::update('contact', $profile, ['id' => $cid]);
1086 Contact::updateAvatar($photo, $uid, $cid);
1089 private static function switchContacts($receivers, $actor)
1091 if (empty($actor)) {
1095 foreach ($receivers as $receiver) {
1096 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
1097 if (DBA::isResult($contact)) {
1098 self::switchContact($contact['id'], $receiver, $actor);
1101 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
1102 if (DBA::isResult($contact)) {
1103 self::switchContact($contact['id'], $receiver, $actor);
1108 private static function addActivityFields($object_data, $activity)
1110 if (!empty($activity['published']) && empty($object_data['published'])) {
1111 $object_data['published'] = $activity['published'];
1114 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1115 $object_data['updated'] = $activity['updated'];
1118 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1119 $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1122 if (!empty($activity['instrument'])) {
1123 $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1125 return $object_data;
1128 private static function fetchObject($object_id, $object = [], $trust_source = false)
1130 if (!$trust_source || is_string($object)) {
1131 $data = self::fetchContent($object_id);
1133 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
1136 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
1139 logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
1143 if (is_string($data)) {
1144 $item = Item::selectFirst([], ['uri' => $data]);
1145 if (!DBA::isResult($item)) {
1146 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1149 logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
1150 $data = self::CreateNote($item);
1153 if (empty($data['type'])) {
1154 logger('Empty type', LOGGER_DEBUG);
1158 switch ($data['type']) {
1162 return self::ProcessObject($data);
1165 if (empty($data['object'])) {
1168 return self::fetchObject($data['object']);
1175 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
1180 private static function ProcessObject(&$object)
1182 if (empty($object['id'])) {
1187 $object_data['object_type'] = $object['type'];
1188 $object_data['id'] = $object['id'];
1190 if (!empty($object['inReplyTo'])) {
1191 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1193 $object_data['reply-to-id'] = $object_data['id'];
1196 $object_data['published'] = defaults($object, 'published', null);
1197 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1199 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1200 $object_data['published'] = $object_data['updated'];
1203 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
1204 if (empty($actor)) {
1205 $actor = defaults($object, 'actor', null);
1208 $object_data['uuid'] = defaults($object, 'uuid', null);
1209 $object_data['owner'] = $object_data['author'] = $actor;
1210 $object_data['context'] = defaults($object, 'context', null);
1211 $object_data['conversation'] = defaults($object, 'conversation', null);
1212 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1213 $object_data['name'] = defaults($object, 'title', null);
1214 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1215 $object_data['summary'] = defaults($object, 'summary', null);
1216 $object_data['content'] = defaults($object, 'content', null);
1217 $object_data['source'] = defaults($object, 'source', null);
1218 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1219 $object_data['attachments'] = defaults($object, 'attachment', null);
1220 $object_data['tags'] = defaults($object, 'tag', null);
1221 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1222 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1223 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1225 // Common object data:
1228 // @context, type, actor, signature, mediaType, duration, replies, icon
1230 // Also missing: (Defined in the standard, but currently unused)
1231 // audience, preview, endTime, startTime, generator, image
1236 // emoji, atomUri, inReplyToAtomUri
1239 // contentMap, announcement_count, announcements, context_id, likes, like_count
1240 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1245 // category, licence, language, commentsEnabled
1248 // views, waitTranscoding, state, support, subtitleLanguage
1249 // likes, dislikes, shares, comments
1252 return $object_data;
1255 private static function convertMentions($body)
1257 $URLSearchString = "^\[\]";
1258 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1263 private static function constructTagList($tags, $sensitive)
1270 foreach ($tags as $tag) {
1271 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1272 if (!empty($tag_text)) {
1276 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1280 /// @todo add nsfw for $sensitive
1285 private static function constructAttachList($attachments, $item)
1287 if (empty($attachments)) {
1291 foreach ($attachments as $attach) {
1292 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1293 if ($filetype == 'image') {
1294 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1296 if (!empty($item["attach"])) {
1297 $item["attach"] .= ',';
1299 $item["attach"] = '';
1301 if (!isset($attach['length'])) {
1302 $attach['length'] = "0";
1304 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1311 private static function createItem($activity, $body)
1314 $item['verb'] = ACTIVITY_POST;
1315 $item['parent-uri'] = $activity['reply-to-id'];
1317 if ($activity['reply-to-id'] == $activity['id']) {
1318 $item['gravity'] = GRAVITY_PARENT;
1319 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1321 $item['gravity'] = GRAVITY_COMMENT;
1322 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1325 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
1326 logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
1327 self::fetchMissingActivity($activity['reply-to-id'], $activity);
1330 self::postItem($activity, $item, $body);
1333 private static function likeItem($activity, $body)
1336 $item['verb'] = ACTIVITY_LIKE;
1337 $item['parent-uri'] = $activity['object'];
1338 $item['gravity'] = GRAVITY_ACTIVITY;
1339 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1341 self::postItem($activity, $item, $body);
1344 private static function dislikeItem($activity, $body)
1347 $item['verb'] = ACTIVITY_DISLIKE;
1348 $item['parent-uri'] = $activity['object'];
1349 $item['gravity'] = GRAVITY_ACTIVITY;
1350 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1352 self::postItem($activity, $item, $body);
1355 private static function postItem($activity, $item, $body)
1357 /// @todo What to do with $activity['context']?
1359 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
1360 logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
1364 $item['network'] = Protocol::ACTIVITYPUB;
1365 $item['private'] = !in_array(0, $activity['receiver']);
1366 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1367 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1368 $item['uri'] = $activity['id'];
1369 $item['created'] = $activity['published'];
1370 $item['edited'] = $activity['updated'];
1371 $item['guid'] = $activity['uuid'];
1372 $item['title'] = HTML::toBBCode($activity['name']);
1373 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1374 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1375 $item['location'] = $activity['location'];
1376 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1377 $item['app'] = $activity['service'];
1378 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1380 $item = self::constructAttachList($activity['attachments'], $item);
1382 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1383 if (!empty($source)) {
1384 $item['body'] = $source;
1387 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1388 $item['source'] = $body;
1389 $item['conversation-href'] = $activity['context'];
1390 $item['conversation-uri'] = $activity['conversation'];
1392 foreach ($activity['receiver'] as $receiver) {
1393 $item['uid'] = $receiver;
1394 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1396 if (($receiver != 0) && empty($item['contact-id'])) {
1397 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1400 $item_id = Item::insert($item);
1401 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1405 private static function fetchMissingActivity($url, $child)
1407 if (Config::get('system', 'ostatus_full_threads')) {
1411 $object = ActivityPub::fetchContent($url);
1412 if (empty($object)) {
1413 logger('Activity ' . $url . ' was not fetchable, aborting.');
1418 $activity['@context'] = $object['@context'];
1419 unset($object['@context']);
1420 $activity['id'] = $object['id'];
1421 $activity['to'] = defaults($object, 'to', []);
1422 $activity['cc'] = defaults($object, 'cc', []);
1423 $activity['actor'] = $child['author'];
1424 $activity['object'] = $object;
1425 $activity['published'] = $object['published'];
1426 $activity['type'] = 'Create';
1428 self::processActivity($activity);
1429 logger('Activity ' . $url . ' had been fetched and processed.');
1432 private static function getUserOfObject($object)
1434 $self = DBA::selectFirst('contact', ['uid'], ['nurl' => normalise_link($object), 'self' => true]);
1435 if (!DBA::isResult($self)) {
1438 return $self['uid'];
1442 private static function followUser($activity)
1444 $actor = JsonLD::fetchElement($activity, 'object', 'id');
1445 $uid = self::getUserOfObject($actor);
1450 $owner = User::getOwnerDataById($uid);
1452 $cid = Contact::getIdForURL($activity['owner'], $uid);
1454 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1459 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1460 'author-link' => $activity['owner']];
1462 Contact::addRelationship($owner, $contact, $item);
1463 $cid = Contact::getIdForURL($activity['owner'], $uid);
1468 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1469 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1470 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1473 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1474 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1477 private static function updatePerson($activity)
1479 if (empty($activity['object']['id'])) {
1483 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
1484 APContact::getProfileByURL($activity['object']['id'], true);
1487 private static function acceptFollowUser($activity)
1489 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1490 $uid = self::getUserOfObject($actor);
1495 $owner = User::getOwnerDataById($uid);
1497 $cid = Contact::getIdForURL($activity['owner'], $uid);
1499 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1503 $fields = ['pending' => false];
1505 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1506 if ($contact['rel'] == Contact::FOLLOWER) {
1507 $fields['rel'] = Contact::FRIEND;
1510 $condition = ['id' => $cid];
1511 DBA::update('contact', $fields, $condition);
1512 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1515 private static function undoActivity($activity)
1517 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
1518 if (empty($activity_url)) {
1522 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1523 if (empty($actor)) {
1527 $author_id = Contact::getIdForURL($actor);
1528 if (empty($author_id)) {
1532 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1535 private static function undoFollowUser($activity)
1537 $object = JsonLD::fetchElement($activity, 'object', 'object');
1538 $uid = self::getUserOfObject($object);
1543 $owner = User::getOwnerDataById($uid);
1545 $cid = Contact::getIdForURL($activity['owner'], $uid);
1547 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1551 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1552 if (!DBA::isResult($contact)) {
1556 Contact::removeFollower($owner, $contact);
1557 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);