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 'vcard' => 'http://www.w3.org/2006/vcard/ns#',
82 'diaspora' => 'https://diasporafoundation.org#',
83 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
84 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
86 public static function isRequest()
88 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
89 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
92 public static function getFollowers($owner, $page = null)
94 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
95 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
96 $count = DBA::count('contact', $condition);
98 $data = ['@context' => self::CONTEXT];
99 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
100 $data['type'] = 'OrderedCollection';
101 $data['totalItems'] = $count;
103 // When we hide our friends we will only show the pure number but don't allow more.
104 $profile = Profile::getProfileForUser($owner['uid']);
105 if (!empty($profile['hide-friends'])) {
110 $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
114 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
115 while ($contact = DBA::fetch($contacts)) {
116 $list[] = $contact['url'];
120 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
123 $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
125 $data['orderedItems'] = $list;
131 public static function getFollowing($owner, $page = null)
133 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
134 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
135 $count = DBA::count('contact', $condition);
137 $data = ['@context' => self::CONTEXT];
138 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
139 $data['type'] = 'OrderedCollection';
140 $data['totalItems'] = $count;
142 // When we hide our friends we will only show the pure number but don't allow more.
143 $profile = Profile::getProfileForUser($owner['uid']);
144 if (!empty($profile['hide-friends'])) {
149 $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
153 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
154 while ($contact = DBA::fetch($contacts)) {
155 $list[] = $contact['url'];
159 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
162 $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
164 $data['orderedItems'] = $list;
170 public static function getOutbox($owner, $page = null)
172 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
174 $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact,
175 'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
176 'deleted' => false, 'visible' => true];
177 $count = DBA::count('item', $condition);
179 $data = ['@context' => self::CONTEXT];
180 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
181 $data['type'] = 'OrderedCollection';
182 $data['totalItems'] = $count;
185 $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
189 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
191 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
192 while ($item = Item::fetch($items)) {
193 $object = self::createObjectFromItemID($item['id']);
194 unset($object['@context']);
199 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
202 $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
204 $data['orderedItems'] = $list;
211 * Return the ActivityPub profile of the given user
213 * @param integer $uid User ID
216 public static function profile($uid)
218 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
219 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
220 'account_removed' => false, 'verified' => true];
221 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
222 $user = DBA::selectFirst('user', $fields, $condition);
223 if (!DBA::isResult($user)) {
227 $fields = ['locality', 'region', 'country-name'];
228 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
229 if (!DBA::isResult($profile)) {
233 $fields = ['name', 'url', 'location', 'about', 'avatar'];
234 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
235 if (!DBA::isResult($contact)) {
239 $data = ['@context' => self::CONTEXT];
240 $data['id'] = $contact['url'];
241 $data['diaspora:guid'] = $user['guid'];
242 $data['type'] = $accounttype[$user['account-type']];
243 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
244 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
245 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
246 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
247 $data['preferredUsername'] = $user['nickname'];
248 $data['name'] = $contact['name'];
249 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
250 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
251 $data['summary'] = $contact['about'];
252 $data['url'] = $contact['url'];
253 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
254 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
255 'owner' => $contact['url'],
256 'publicKeyPem' => $user['pubkey']];
257 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
258 $data['icon'] = ['type' => 'Image',
259 'url' => $contact['avatar']];
261 // tags: https://kitty.town/@inmysocks/100656097926961126.json
265 private static function fetchPermissionBlockFromConversation($item)
267 if (empty($item['thr-parent'])) {
271 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
272 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
273 if (!DBA::isResult($conversation)) {
277 $activity = json_decode($conversation['source'], true);
279 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
280 $profile = APContact::getProfileByURL($actor);
282 $item_profile = APContact::getProfileByURL($item['author-link']);
283 $exclude[] = $item['author-link'];
285 if ($item['gravity'] == GRAVITY_PARENT) {
286 $exclude[] = $item['owner-link'];
289 $permissions['to'][] = $actor;
291 $elements = ['to', 'cc', 'bto', 'bcc'];
292 foreach ($elements as $element) {
293 if (empty($activity[$element])) {
296 if (is_string($activity[$element])) {
297 $activity[$element] = [$activity[$element]];
300 foreach ($activity[$element] as $receiver) {
301 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
302 $receiver = $item_profile['followers'];
304 if (!in_array($receiver, $exclude)) {
305 $permissions[$element][] = $receiver;
312 public static function createPermissionBlockForItem($item)
314 $data = ['to' => [], 'cc' => []];
316 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
318 $actor_profile = APContact::getProfileByURL($item['author-link']);
320 $terms = Term::tagArrayFromItemId($item['id']);
322 $contacts[$item['author-link']] = $item['author-link'];
324 if (!$item['private']) {
325 $data['to'][] = self::PUBLIC;
326 if (!empty($actor_profile['followers'])) {
327 $data['cc'][] = $actor_profile['followers'];
330 foreach ($terms as $term) {
331 if ($term['type'] != TERM_MENTION) {
334 $profile = APContact::getProfileByURL($term['url'], false);
335 if (!empty($profile) && empty($contacts[$profile['url']])) {
336 $data['cc'][] = $profile['url'];
337 $contacts[$profile['url']] = $profile['url'];
341 $receiver_list = Item::enumeratePermissions($item);
345 foreach ($terms as $term) {
346 if ($term['type'] != TERM_MENTION) {
349 $cid = Contact::getIdForURL($term['url'], $item['uid']);
350 if (!empty($cid) && in_array($cid, $receiver_list)) {
351 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
352 $data['to'][] = $contact['url'];
353 $contacts[$contact['url']] = $contact['url'];
357 foreach ($receiver_list as $receiver) {
358 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
359 if (empty($contacts[$contact['url']])) {
360 $data['cc'][] = $contact['url'];
361 $contacts[$contact['url']] = $contact['url'];
366 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
367 while ($parent = Item::fetch($parents)) {
368 // Don't include data from future posts
369 if ($parent['id'] >= $item['id']) {
373 $profile = APContact::getProfileByURL($parent['author-link'], false);
374 if (!empty($profile) && empty($contacts[$profile['url']])) {
375 $data['cc'][] = $profile['url'];
376 $contacts[$profile['url']] = $profile['url'];
379 if ($item['gravity'] != GRAVITY_PARENT) {
383 $profile = APContact::getProfileByURL($parent['owner-link'], false);
384 if (!empty($profile) && empty($contacts[$profile['url']])) {
385 $data['cc'][] = $profile['url'];
386 $contacts[$profile['url']] = $profile['url'];
389 DBA::close($parents);
391 if (empty($data['to'])) {
392 $data['to'] = $data['cc'];
399 public static function fetchTargetInboxes($item, $uid)
401 $permissions = self::createPermissionBlockForItem($item);
402 if (empty($permissions)) {
408 if ($item['gravity'] == GRAVITY_ACTIVITY) {
409 $item_profile = APContact::getProfileByURL($item['author-link']);
411 $item_profile = APContact::getProfileByURL($item['owner-link']);
414 $elements = ['to', 'cc', 'bto', 'bcc'];
415 foreach ($elements as $element) {
416 if (empty($permissions[$element])) {
420 foreach ($permissions[$element] as $receiver) {
421 if ($receiver == $item_profile['followers']) {
422 $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
423 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB]);
424 while ($contact = DBA::fetch($contacts)) {
425 $contact = defaults($contact, 'batch', $contact['notify']);
426 $inboxes[$contact] = $contact;
428 DBA::close($contacts);
430 $profile = APContact::getProfileByURL($receiver);
431 if (!empty($profile)) {
432 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
433 $inboxes[$target] = $target;
442 public static function getTypeOfItem($item)
444 if ($item['verb'] == ACTIVITY_POST) {
445 if ($item['created'] == $item['edited']) {
450 } elseif ($item['verb'] == ACTIVITY_LIKE) {
452 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
454 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
456 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
458 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
459 $type = 'TentativeAccept';
467 public static function createActivityFromItem($item_id, $object_mode = false)
469 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
471 if (!DBA::isResult($item)) {
475 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
476 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
477 if (DBA::isResult($conversation)) {
478 $data = json_decode($conversation['source']);
484 $type = self::getTypeOfItem($item);
487 $data = ['@context' => self::CONTEXT];
489 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
491 } elseif ($item['deleted']) {
498 $data['id'] = $item['uri'] . '#' . $type;
499 $data['type'] = $type;
500 $data['actor'] = $item['author-link'];
502 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
504 if ($item["created"] != $item["edited"]) {
505 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
508 $data['context'] = self::fetchContextURLForItem($item);
510 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
512 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
513 $data['object'] = self::CreateNote($item);
514 } elseif ($data['type'] == 'Undo') {
515 $data['object'] = self::createActivityFromItem($item_id, true);
517 $data['object'] = $item['thr-parent'];
520 $owner = User::getOwnerDataById($item['uid']);
523 return LDSignature::sign($data, $owner);
529 public static function createObjectFromItemID($item_id)
531 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
533 if (!DBA::isResult($item)) {
537 $data = ['@context' => self::CONTEXT];
538 $data = array_merge($data, self::CreateNote($item));
543 private static function createTagList($item)
547 $terms = Term::tagArrayFromItemId($item['id']);
548 foreach ($terms as $term) {
549 if ($term['type'] == TERM_MENTION) {
550 $contact = Contact::getDetailsByURL($term['url']);
551 if (!empty($contact['addr'])) {
552 $mention = '@' . $contact['addr'];
554 $mention = '@' . $term['url'];
557 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
563 private static function fetchContextURLForItem($item)
565 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
566 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
567 $context_uri = $conversation['conversation-href'];
568 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
569 $context_uri = $conversation['conversation-uri'];
571 $context_uri = str_replace('/object/', '/context/', $item['parent-uri']);
576 private static function CreateNote($item)
578 if (!empty($item['title'])) {
584 if ($item['deleted']) {
589 $data['id'] = $item['uri'];
590 $data['type'] = $type;
592 if ($item['deleted']) {
596 $data['summary'] = null; // Ignore by now
598 if ($item['uri'] != $item['thr-parent']) {
599 $data['inReplyTo'] = $item['thr-parent'];
601 $data['inReplyTo'] = null;
604 $data['diaspora:guid'] = $item['guid'];
605 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
607 if ($item["created"] != $item["edited"]) {
608 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
611 $data['url'] = $item['plink'];
612 $data['attributedTo'] = $item['author-link'];
613 $data['actor'] = $item['author-link'];
614 $data['sensitive'] = false; // - Query NSFW
615 $data['context'] = self::fetchContextURLForItem($item);
617 if (!empty($item['title'])) {
618 $data['name'] = BBCode::convert($item['title'], false, 7);
621 $data['content'] = BBCode::convert($item['body'], false, 7);
622 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
623 $data['attachment'] = []; // @ToDo
624 $data['tag'] = self::createTagList($item);
625 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
630 public static function transmitActivity($activity, $target, $uid)
632 $profile = APContact::getProfileByURL($target);
634 $owner = User::getOwnerDataById($uid);
636 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
637 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
639 'actor' => $owner['url'],
640 'object' => $profile['url'],
641 'to' => $profile['url']];
643 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
645 $signed = LDSignature::sign($data, $owner);
646 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
649 public static function transmitContactAccept($target, $id, $uid)
651 $profile = APContact::getProfileByURL($target);
653 $owner = User::getOwnerDataById($uid);
654 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
655 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
657 'actor' => $owner['url'],
658 'object' => ['id' => $id, 'type' => 'Follow',
659 'actor' => $profile['url'],
660 'object' => $owner['url']],
661 'to' => $profile['url']];
663 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
665 $signed = LDSignature::sign($data, $owner);
666 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
669 public static function transmitContactReject($target, $id, $uid)
671 $profile = APContact::getProfileByURL($target);
673 $owner = User::getOwnerDataById($uid);
674 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
675 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
677 'actor' => $owner['url'],
678 'object' => ['id' => $id, 'type' => 'Follow',
679 'actor' => $profile['url'],
680 'object' => $owner['url']],
681 'to' => $profile['url']];
683 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
685 $signed = LDSignature::sign($data, $owner);
686 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
689 public static function transmitContactUndo($target, $uid)
691 $profile = APContact::getProfileByURL($target);
693 $id = System::baseUrl() . '/activity/' . System::createGUID();
695 $owner = User::getOwnerDataById($uid);
696 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
699 'actor' => $owner['url'],
700 'object' => ['id' => $id, 'type' => 'Follow',
701 'actor' => $owner['url'],
702 'object' => $profile['url']],
703 'to' => $profile['url']];
705 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
707 $signed = LDSignature::sign($data, $owner);
708 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
712 * Fetches ActivityPub content from the given url
714 * @param string $url content url
717 public static function fetchContent($url)
719 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
720 if (!$ret['success'] || empty($ret['body'])) {
723 return json_decode($ret['body'], true);
727 * Fetches a profile from the given url into an array that is compatible to Probe::uri
729 * @param string $url profile url
732 public static function probeProfile($url)
734 $apcontact = APContact::getProfileByURL($url, true);
735 if (empty($apcontact)) {
739 $profile = ['network' => Protocol::ACTIVITYPUB];
740 $profile['nick'] = $apcontact['nick'];
741 $profile['name'] = $apcontact['name'];
742 $profile['guid'] = $apcontact['diaspora:guid'];
743 $profile['url'] = $apcontact['url'];
744 $profile['addr'] = $apcontact['addr'];
745 $profile['alias'] = $apcontact['alias'];
746 $profile['photo'] = $apcontact['photo'];
747 // $profile['community']
748 // $profile['keywords']
749 // $profile['location']
750 $profile['about'] = $apcontact['about'];
751 $profile['batch'] = $apcontact['sharedinbox'];
752 $profile['notify'] = $apcontact['inbox'];
753 $profile['poll'] = $apcontact['outbox'];
754 $profile['pubkey'] = $apcontact['pubkey'];
755 $profile['baseurl'] = $apcontact['baseurl'];
757 // Remove all "null" fields
758 foreach ($profile as $field => $content) {
759 if (is_null($content)) {
760 unset($profile[$field]);
767 public static function processInbox($body, $header, $uid)
769 $http_signer = HTTPSignature::getSigner($body, $header);
770 if (empty($http_signer)) {
771 logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
774 logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
777 $activity = json_decode($body, true);
779 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
780 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
782 if (empty($activity)) {
783 logger('Invalid body.', LOGGER_DEBUG);
787 if (LDSignature::isSigned($activity)) {
788 $ld_signer = LDSignature::getSigner($activity);
789 if (empty($ld_signer)) {
790 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
792 if (!empty($ld_signer && ($actor == $http_signer))) {
793 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
794 $trust_source = true;
795 } elseif (!empty($ld_signer)) {
796 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
797 $trust_source = true;
798 } elseif ($actor == $http_signer) {
799 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
800 $trust_source = true;
802 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
803 $trust_source = false;
805 } elseif ($actor == $http_signer) {
806 logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
807 $trust_source = true;
809 logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
810 $trust_source = false;
813 self::processActivity($activity, $body, $uid, $trust_source);
816 public static function fetchOutbox($url, $uid)
818 $data = self::fetchContent($url);
823 if (!empty($data['orderedItems'])) {
824 $items = $data['orderedItems'];
825 } elseif (!empty($data['first']['orderedItems'])) {
826 $items = $data['first']['orderedItems'];
827 } elseif (!empty($data['first'])) {
828 self::fetchOutbox($data['first'], $uid);
834 foreach ($items as $activity) {
835 self::processActivity($activity, '', $uid, true);
839 private static function prepareObjectData($activity, $uid, &$trust_source)
841 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
843 logger('Empty actor', LOGGER_DEBUG);
847 // Fetch all receivers from to, cc, bto and bcc
848 $receivers = self::getReceivers($activity, $actor);
850 // When it is a delivery to a personal inbox we add that user to the receivers
852 $owner = User::getOwnerDataById($uid);
853 $additional = ['uid:' . $uid => $uid];
854 $receivers = array_merge($receivers, $additional);
857 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
859 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
860 if (empty($object_id)) {
861 logger('No object found', LOGGER_DEBUG);
865 // Fetch the content only on activities where this matters
866 if (in_array($activity['type'], ['Create', 'Announce'])) {
867 $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
868 if (empty($object_data)) {
869 logger("Object data couldn't be processed", LOGGER_DEBUG);
872 // We had been able to retrieve the object data - so we can trust the source
873 $trust_source = true;
874 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
875 // Create a mostly empty array out of the activity data (instead of the object).
876 // This way we later don't have to check for the existence of ech individual array element.
877 $object_data = self::ProcessObject($activity);
878 $object_data['name'] = $activity['type'];
879 $object_data['author'] = $activity['actor'];
880 $object_data['object'] = $object_id;
881 $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
884 $object_data['id'] = $activity['id'];
885 $object_data['object'] = $activity['object'];
886 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
889 $object_data = self::addActivityFields($object_data, $activity);
891 $object_data['type'] = $activity['type'];
892 $object_data['owner'] = $actor;
893 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
895 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
900 private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
902 if (empty($activity['type'])) {
903 logger('Empty type', LOGGER_DEBUG);
907 if (empty($activity['object'])) {
908 logger('Empty object', LOGGER_DEBUG);
912 if (empty($activity['actor'])) {
913 logger('Empty actor', LOGGER_DEBUG);
918 // $trust_source is called by reference and is set to true if the content was retrieved successfully
919 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
920 if (empty($object_data)) {
921 logger('No object data found', LOGGER_DEBUG);
925 if (!$trust_source) {
926 logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
929 switch ($activity['type']) {
932 self::createItem($object_data, $body);
936 self::likeItem($object_data, $body);
940 self::dislikeItem($object_data, $body);
944 if (in_array($object_data['object_type'], ['Person', 'Organization', 'Service', 'Group', 'Application'])) {
945 self::updatePerson($object_data, $body);
953 self::followUser($object_data);
957 if ($object_data['object_type'] == 'Follow') {
958 self::acceptFollowUser($object_data);
963 if ($object_data['object_type'] == 'Follow') {
964 self::undoFollowUser($object_data);
965 } elseif (in_array($object_data['object_type'], ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'])) {
966 self::undoActivity($object_data);
971 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
976 private static function getReceivers($activity, $actor)
980 // When it is an answer, we inherite the receivers from the parent
981 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
982 if (!empty($replyto)) {
983 $parents = Item::select(['uid'], ['uri' => $replyto]);
984 while ($parent = Item::fetch($parents)) {
985 $receivers['uid:' . $parent['uid']] = $parent['uid'];
989 if (!empty($actor)) {
990 $profile = APContact::getProfileByURL($actor);
991 $followers = defaults($profile, 'followers', '');
993 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
995 logger('Empty actor', LOGGER_DEBUG);
999 $elements = ['to', 'cc', 'bto', 'bcc'];
1000 foreach ($elements as $element) {
1001 if (empty($activity[$element])) {
1005 // The receiver can be an arror or a string
1006 if (is_string($activity[$element])) {
1007 $activity[$element] = [$activity[$element]];
1010 foreach ($activity[$element] as $receiver) {
1011 if ($receiver == self::PUBLIC) {
1012 $receivers['uid:0'] = 0;
1015 if (($receiver == self::PUBLIC) && !empty($actor)) {
1016 // This will most likely catch all OStatus connections to Mastodon
1017 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]];
1018 $contacts = DBA::select('contact', ['uid'], $condition);
1019 while ($contact = DBA::fetch($contacts)) {
1020 if ($contact['uid'] != 0) {
1021 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1024 DBA::close($contacts);
1027 if (in_array($receiver, [$followers, self::PUBLIC]) && !empty($actor)) {
1028 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1029 'network' => Protocol::ACTIVITYPUB];
1030 $contacts = DBA::select('contact', ['uid'], $condition);
1031 while ($contact = DBA::fetch($contacts)) {
1032 if ($contact['uid'] != 0) {
1033 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1036 DBA::close($contacts);
1040 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1041 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1042 if (!DBA::isResult($contact)) {
1045 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1049 self::switchContacts($receivers, $actor);
1054 private static function switchContact($cid, $uid, $url)
1056 $profile = ActivityPub::probeProfile($url);
1057 if (empty($profile)) {
1061 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
1063 $photo = $profile['photo'];
1064 unset($profile['photo']);
1065 unset($profile['baseurl']);
1067 $profile['nurl'] = normalise_link($profile['url']);
1068 DBA::update('contact', $profile, ['id' => $cid]);
1070 Contact::updateAvatar($photo, $uid, $cid);
1073 private static function switchContacts($receivers, $actor)
1075 if (empty($actor)) {
1079 foreach ($receivers as $receiver) {
1080 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
1081 if (DBA::isResult($contact)) {
1082 self::switchContact($contact['id'], $receiver, $actor);
1085 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
1086 if (DBA::isResult($contact)) {
1087 self::switchContact($contact['id'], $receiver, $actor);
1092 private static function addActivityFields($object_data, $activity)
1094 if (!empty($activity['published']) && empty($object_data['published'])) {
1095 $object_data['published'] = $activity['published'];
1098 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1099 $object_data['updated'] = $activity['updated'];
1102 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1103 $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1106 if (!empty($activity['instrument'])) {
1107 $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1109 return $object_data;
1112 private static function fetchObject($object_id, $object = [], $trust_source = false)
1114 if (!$trust_source || is_string($object)) {
1115 $data = self::fetchContent($object_id);
1117 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
1120 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
1123 logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
1127 if (is_string($data)) {
1128 $item = Item::selectFirst([], ['uri' => $data]);
1129 if (!DBA::isResult($item)) {
1130 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1133 logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
1134 $data = self::CreateNote($item);
1137 if (empty($data['type'])) {
1138 logger('Empty type', LOGGER_DEBUG);
1142 switch ($data['type']) {
1146 return self::ProcessObject($data);
1149 if (empty($data['object'])) {
1152 return self::fetchObject($data['object']);
1159 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
1164 private static function ProcessObject(&$object)
1166 if (empty($object['id'])) {
1171 $object_data['object_type'] = $object['type'];
1172 $object_data['id'] = $object['id'];
1174 if (!empty($object['inReplyTo'])) {
1175 $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1177 $object_data['reply-to-id'] = $object_data['id'];
1180 $object_data['published'] = defaults($object, 'published', null);
1181 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1183 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1184 $object_data['published'] = $object_data['updated'];
1187 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
1188 if (empty($actor)) {
1189 $actor = defaults($object, 'actor', null);
1192 $object_data['uuid'] = defaults($object, 'uuid', null);
1193 $object_data['owner'] = $object_data['author'] = $actor;
1194 $object_data['context'] = defaults($object, 'context', null);
1195 $object_data['conversation'] = defaults($object, 'conversation', null);
1196 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1197 $object_data['name'] = defaults($object, 'title', null);
1198 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1199 $object_data['summary'] = defaults($object, 'summary', null);
1200 $object_data['content'] = defaults($object, 'content', null);
1201 $object_data['source'] = defaults($object, 'source', null);
1202 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1203 $object_data['attachments'] = defaults($object, 'attachment', null);
1204 $object_data['tags'] = defaults($object, 'tag', null);
1205 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1206 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1207 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1209 // Common object data:
1212 // @context, type, actor, signature, mediaType, duration, replies, icon
1214 // Also missing: (Defined in the standard, but currently unused)
1215 // audience, preview, endTime, startTime, generator, image
1220 // contentMap, announcement_count, announcements, context_id, likes, like_count
1221 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1226 // category, licence, language, commentsEnabled
1229 // views, waitTranscoding, state, support, subtitleLanguage
1230 // likes, dislikes, shares, comments
1233 return $object_data;
1236 private static function convertMentions($body)
1238 $URLSearchString = "^\[\]";
1239 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1244 private static function constructTagList($tags, $sensitive)
1251 foreach ($tags as $tag) {
1252 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1253 if (!empty($tag_text)) {
1257 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1261 /// @todo add nsfw for $sensitive
1266 private static function constructAttachList($attachments, $item)
1268 if (empty($attachments)) {
1272 foreach ($attachments as $attach) {
1273 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1274 if ($filetype == 'image') {
1275 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1277 if (!empty($item["attach"])) {
1278 $item["attach"] .= ',';
1280 $item["attach"] = '';
1282 if (!isset($attach['length'])) {
1283 $attach['length'] = "0";
1285 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1292 private static function createItem($activity, $body)
1295 $item['verb'] = ACTIVITY_POST;
1296 $item['parent-uri'] = $activity['reply-to-id'];
1298 if ($activity['reply-to-id'] == $activity['id']) {
1299 $item['gravity'] = GRAVITY_PARENT;
1300 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1302 $item['gravity'] = GRAVITY_COMMENT;
1303 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1306 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
1307 logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
1308 self::fetchMissingActivity($activity['reply-to-id'], $activity);
1311 self::postItem($activity, $item, $body);
1314 private static function likeItem($activity, $body)
1317 $item['verb'] = ACTIVITY_LIKE;
1318 $item['parent-uri'] = $activity['object'];
1319 $item['gravity'] = GRAVITY_ACTIVITY;
1320 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1322 self::postItem($activity, $item, $body);
1325 private static function dislikeItem($activity, $body)
1328 $item['verb'] = ACTIVITY_DISLIKE;
1329 $item['parent-uri'] = $activity['object'];
1330 $item['gravity'] = GRAVITY_ACTIVITY;
1331 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1333 self::postItem($activity, $item, $body);
1336 private static function postItem($activity, $item, $body)
1338 /// @todo What to do with $activity['context']?
1340 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
1341 logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
1345 $item['network'] = Protocol::ACTIVITYPUB;
1346 $item['private'] = !in_array(0, $activity['receiver']);
1347 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1348 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1349 $item['uri'] = $activity['id'];
1350 $item['created'] = $activity['published'];
1351 $item['edited'] = $activity['updated'];
1352 $item['guid'] = $activity['diaspora:guid'];
1353 $item['title'] = HTML::toBBCode($activity['name']);
1354 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1355 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1356 $item['location'] = $activity['location'];
1357 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1358 $item['app'] = $activity['service'];
1359 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1361 $item = self::constructAttachList($activity['attachments'], $item);
1363 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1364 if (!empty($source)) {
1365 $item['body'] = $source;
1368 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1369 $item['source'] = $body;
1370 $item['conversation-href'] = $activity['context'];
1371 $item['conversation-uri'] = $activity['conversation'];
1373 foreach ($activity['receiver'] as $receiver) {
1374 $item['uid'] = $receiver;
1375 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1377 if (($receiver != 0) && empty($item['contact-id'])) {
1378 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1381 $item_id = Item::insert($item);
1382 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1386 private static function fetchMissingActivity($url, $child)
1388 if (Config::get('system', 'ostatus_full_threads')) {
1392 $object = ActivityPub::fetchContent($url);
1393 if (empty($object)) {
1394 logger('Activity ' . $url . ' was not fetchable, aborting.');
1399 $activity['@context'] = $object['@context'];
1400 unset($object['@context']);
1401 $activity['id'] = $object['id'];
1402 $activity['to'] = defaults($object, 'to', []);
1403 $activity['cc'] = defaults($object, 'cc', []);
1404 $activity['actor'] = $child['author'];
1405 $activity['object'] = $object;
1406 $activity['published'] = $object['published'];
1407 $activity['type'] = 'Create';
1409 self::processActivity($activity);
1410 logger('Activity ' . $url . ' had been fetched and processed.');
1413 private static function getUserOfObject($object)
1415 $self = DBA::selectFirst('contact', ['uid'], ['nurl' => normalise_link($object), 'self' => true]);
1416 if (!DBA::isResult($self)) {
1419 return $self['uid'];
1423 private static function followUser($activity)
1425 $actor = JsonLD::fetchElement($activity, 'object', 'id');
1426 $uid = self::getUserOfObject($actor);
1431 $owner = User::getOwnerDataById($uid);
1433 $cid = Contact::getIdForURL($activity['owner'], $uid);
1435 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1440 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1441 'author-link' => $activity['owner']];
1443 Contact::addRelationship($owner, $contact, $item);
1444 $cid = Contact::getIdForURL($activity['owner'], $uid);
1449 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1450 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1451 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1454 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1455 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1458 private static function updatePerson($activity)
1460 if (empty($activity['object']['id'])) {
1464 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
1465 APContact::getProfileByURL($activity['object']['id'], true);
1468 private static function acceptFollowUser($activity)
1470 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1471 $uid = self::getUserOfObject($actor);
1476 $owner = User::getOwnerDataById($uid);
1478 $cid = Contact::getIdForURL($activity['owner'], $uid);
1480 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1484 $fields = ['pending' => false];
1486 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1487 if ($contact['rel'] == Contact::FOLLOWER) {
1488 $fields['rel'] = Contact::FRIEND;
1491 $condition = ['id' => $cid];
1492 DBA::update('contact', $fields, $condition);
1493 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1496 private static function undoActivity($activity)
1498 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
1499 if (empty($activity_url)) {
1503 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1504 if (empty($actor)) {
1508 $author_id = Contact::getIdForURL($actor);
1509 if (empty($author_id)) {
1513 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1516 private static function undoFollowUser($activity)
1518 $object = JsonLD::fetchElement($activity, 'object', 'object');
1519 $uid = self::getUserOfObject($object);
1524 $owner = User::getOwnerDataById($uid);
1526 $cid = Contact::getIdForURL($activity['owner'], $uid);
1528 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1532 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1533 if (!DBA::isResult($contact)) {
1537 Contact::removeFollower($owner, $contact);
1538 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);