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\Item;
16 use Friendica\Model\Term;
17 use Friendica\Model\User;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Crypto;
20 use Friendica\Content\Text\BBCode;
21 use Friendica\Content\Text\HTML;
22 use Friendica\Util\JsonLD;
23 use Friendica\Util\LDSignature;
26 * @brief ActivityPub Protocol class
27 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
28 * https://www.w3.org/TR/activitypub/
29 * https://www.w3.org/TR/activitystreams-core/
30 * https://www.w3.org/TR/activitystreams-vocabulary/
32 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
33 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
35 * Digest: https://tools.ietf.org/html/rfc5843
36 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
37 * https://github.com/digitalbazaar/php-json-ld
39 * Part of the code for HTTP signing is taken from the Osada project.
40 * https://framagit.org/macgirvin/osada
45 * - Activities: Dislike, Update, Delete
46 * - Object Types: Person, Tombstome
49 * - Activities: Like, Dislike, Update, Delete, Announce
50 * - Object Tyoes: Article, Person, Tombstone
53 * - Endpoints: Outbox, Follower, Following
55 * - Queueing unsucessful deliveries
59 const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
61 public static function isRequest()
63 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
64 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
68 * Return the ActivityPub profile of the given user
70 * @param integer $uid User ID
73 public static function profile($uid)
75 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
76 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
77 'account_removed' => false, 'verified' => true];
78 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
79 $user = DBA::selectFirst('user', $fields, $condition);
80 if (!DBA::isResult($user)) {
84 $fields = ['locality', 'region', 'country-name'];
85 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
86 if (!DBA::isResult($profile)) {
90 $fields = ['name', 'url', 'location', 'about', 'avatar'];
91 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
92 if (!DBA::isResult($contact)) {
96 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
97 ['vcard' => 'http://www.w3.org/2006/vcard/ns#', 'uuid' => 'http://schema.org/identifier',
98 'sensitive' => 'as:sensitive', 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers']]];
100 $data['id'] = $contact['url'];
101 $data['uuid'] = $user['guid'];
102 $data['type'] = $accounttype[$user['account-type']];
103 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
104 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
105 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
106 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
107 $data['preferredUsername'] = $user['nickname'];
108 $data['name'] = $contact['name'];
109 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
110 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
111 $data['summary'] = $contact['about'];
112 $data['url'] = $contact['url'];
113 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
114 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
115 'owner' => $contact['url'],
116 'publicKeyPem' => $user['pubkey']];
117 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
118 $data['icon'] = ['type' => 'Image',
119 'url' => $contact['avatar']];
121 // tags: https://kitty.town/@inmysocks/100656097926961126.json
125 private static function fetchPermissionBlockFromConversation($item)
127 if (empty($item['thr-parent'])) {
131 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
132 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
133 if (!DBA::isResult($conversation)) {
137 $activity = json_decode($conversation['source'], true);
139 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
140 $profile = ActivityPub::fetchprofile($actor);
144 $elements = ['to', 'cc', 'bto', 'bcc'];
145 foreach ($elements as $element) {
146 if (empty($activity[$element])) {
149 if (is_string($activity[$element])) {
150 $activity[$element] = [$activity[$element]];
152 foreach ($activity[$element] as $receiver) {
153 if ($receiver == $profile['followers']) {
154 $receiver = System::baseUrl() . '/followers/' . $item['author-nick'];
156 $permissions[$element][] = $receiver;
162 public static function createPermissionBlockForItem($item)
164 $data = ['to' => [], 'cc' => []];
166 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
168 $terms = Term::tagArrayFromItemId($item['id']);
170 if (!$item['private']) {
171 $data['to'][] = self::PUBLIC;
172 $data['cc'][] = System::baseUrl() . '/followers/' . $item['author-nick'];
174 foreach ($terms as $term) {
175 if ($term['type'] != TERM_MENTION) {
178 $profile = self::fetchprofile($term['url']);
179 if (!empty($profile)) {
180 $data['cc'][] = $profile['url'];
184 //$data['cc'][] = System::baseUrl() . '/followers/' . $item['author-nick'];
185 $receiver_list = Item::enumeratePermissions($item);
190 foreach ($terms as $term) {
191 if ($term['type'] != TERM_MENTION) {
194 $cid = Contact::getIdForURL($term['url'], $item['uid']);
195 if (!empty($cid) && in_array($cid, $receiver_list)) {
196 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
197 $data['to'][] = $contact['url'];
198 $contacts[$contact['url']] = $contact['url'];
202 foreach ($receiver_list as $receiver) {
203 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
204 if (empty($contacts[$contact['url']])) {
205 $data['cc'][] = $contact['url'];
206 $contacts[$contact['url']] = $contact['url'];
210 if (empty($data['to'])) {
211 $data['to'] = $data['cc'];
219 private static function fetchTargetInboxesFromConversation($item)
221 if (empty($item['thr-parent'])) {
225 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
226 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
227 if (!DBA::isResult($conversation)) {
231 $activity = json_decode($conversation['source'], true);
233 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
234 $profile = ActivityPub::fetchprofile($actor);
238 $elements = ['to', 'cc', 'bto', 'bcc'];
239 foreach ($elements as $element) {
240 if (empty($activity[$element])) {
243 if (is_string($activity[$element])) {
244 $activity[$element] = [$activity[$element]];
246 foreach ($activity[$element] as $receiver) {
247 $profile = self::fetchprofile($receiver);
248 if (!empty($profile)) {
249 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
250 $inboxes[$target] = $target;
257 public static function fetchTargetInboxes($item, $uid)
259 $inboxes = self::fetchTargetInboxesFromConversation($item);
261 $parents = Item::select(['author-link', 'owner-link'], ['parent' => $item['parent']]);
262 while ($parent = Item::fetch($parents)) {
263 $profile = self::fetchprofile($parent['author-link']);
264 if (!empty($profile)) {
265 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
266 $inboxes[$target] = $target;
268 $profile = self::fetchprofile($parent['owner-link']);
269 if (!empty($profile)) {
270 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
271 $inboxes[$target] = $target;
274 DBA::close($parents);
276 $terms = Term::tagArrayFromItemId($item['id']);
277 if (!$item['private']) {
278 $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
279 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB]);
280 while ($contact = DBA::fetch($contacts)) {
281 $contact = defaults($contact, 'batch', $contact['notify']);
282 $inboxes[$contact] = $contact;
284 DBA::close($contacts);
286 foreach ($terms as $term) {
287 if ($term['type'] != TERM_MENTION) {
290 $profile = self::fetchprofile($term['url']);
291 if (!empty($profile)) {
292 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
293 $inboxes[$target] = $target;
297 $receiver_list = Item::enumeratePermissions($item);
300 foreach ($terms as $term) {
301 if ($term['type'] != TERM_MENTION) {
304 $cid = Contact::getIdForURL($term['url'], $uid);
305 if (!empty($cid) && in_array($cid, $receiver_list)) {
306 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
307 $profile = self::fetchprofile($contact['url']);
308 if (!empty($profile)) {
309 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
310 $inboxes[$target] = $target;
315 foreach ($receiver_list as $receiver) {
316 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
317 $profile = self::fetchprofile($contact['url']);
318 if (!empty($profile)) {
319 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
320 $inboxes[$target] = $target;
325 $profile = self::fetchprofile($item['author-link']);
326 if (!empty($profile['sharedinbox'])) {
327 unset($inboxes[$profile['sharedinbox']]);
330 if (!empty($profile['inbox'])) {
331 unset($inboxes[$profile['inbox']]);
337 public static function createActivityFromItem($item_id)
339 $item = Item::selectFirst([], ['id' => $item_id]);
341 if (!DBA::isResult($item)) {
345 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
346 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
347 if (DBA::isResult($conversation)) {
348 $data = json_decode($conversation['source']);
354 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
355 ['ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
356 'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
357 'conversation' => 'ostatus:conversation',
358 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri']]];
360 $data['id'] = $item['uri'] . '#activity';
361 $data['type'] = 'Create';
362 $data['actor'] = $item['author-link'];
364 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
366 if ($item["created"] != $item["edited"]) {
367 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
370 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
372 $data['object'] = self::createNote($item);
374 $owner = User::getOwnerDataById($item['uid']);
376 return LDSignature::sign($data, $owner);
379 public static function createObjectFromItemID($item_id)
381 $item = Item::selectFirst([], ['id' => $item_id]);
383 if (!DBA::isResult($item)) {
387 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
388 ['ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
389 'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
390 'conversation' => 'ostatus:conversation',
391 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri']]];
393 $data = array_merge($data, self::createNote($item));
399 private static function createTagList($item)
403 $terms = Term::tagArrayFromItemId($item['id']);
404 foreach ($terms as $term) {
405 if ($term['type'] == TERM_MENTION) {
406 $contact = Contact::getDetailsByURL($term['url']);
407 if (!empty($contact['addr'])) {
408 $mention = '@' . $contact['addr'];
410 $mention = '@' . $term['url'];
413 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
420 public static function createNote($item)
423 $data['id'] = $item['uri'];
424 $data['type'] = 'Note';
425 $data['summary'] = null; // Ignore by now
427 if ($item['uri'] != $item['thr-parent']) {
428 $data['inReplyTo'] = $item['thr-parent'];
430 $data['inReplyTo'] = null;
433 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
435 if ($item["created"] != $item["edited"]) {
436 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
439 $data['url'] = $item['uri'];
440 $data['attributedTo'] = $item['author-link'];
441 $data['actor'] = $item['author-link'];
442 $data['sensitive'] = false; // - Query NSFW
444 $conversation = DBA::selectFirst('conversation', ['conversation-uri'], ['item-uri' => $item['parent-uri']]);
445 if (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
446 $conversation_uri = $conversation['conversation-uri'];
448 $conversation_uri = $item['parent-uri'];
451 $data['conversation'] = $conversation_uri;
453 if (!empty($item['title'])) {
454 $data['name'] = BBCode::convert($item['title'], false, 7);
457 $data['content'] = BBCode::convert($item['body'], false, 7);
458 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
459 $data['attachment'] = []; // @ToDo
460 $data['tag'] = self::createTagList($item);
461 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
463 //$data['emoji'] = []; // Ignore by now
467 public static function transmitActivity($activity, $target, $uid)
469 $profile = self::fetchprofile($target);
471 $owner = User::getOwnerDataById($uid);
473 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
474 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
476 'actor' => $owner['url'],
477 'object' => $profile['url'],
478 'to' => $profile['url']];
480 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
482 $signed = LDSignature::sign($data, $owner);
483 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
486 public static function transmitContactAccept($target, $id, $uid)
488 $profile = self::fetchprofile($target);
490 $owner = User::getOwnerDataById($uid);
491 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
492 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
494 'actor' => $owner['url'],
495 'object' => ['id' => $id, 'type' => 'Follow',
496 'actor' => $profile['url'],
497 'object' => $owner['url']],
498 'to' => $profile['url']];
500 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
502 $signed = LDSignature::sign($data, $owner);
503 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
506 public static function transmitContactReject($target, $id, $uid)
508 $profile = self::fetchprofile($target);
510 $owner = User::getOwnerDataById($uid);
511 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
512 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
514 'actor' => $owner['url'],
515 'object' => ['id' => $id, 'type' => 'Follow',
516 'actor' => $profile['url'],
517 'object' => $owner['url']],
518 'to' => $profile['url']];
520 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
522 $signed = LDSignature::sign($data, $owner);
523 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
526 public static function transmitContactUndo($target, $uid)
528 $profile = self::fetchprofile($target);
530 $id = System::baseUrl() . '/activity/' . System::createGUID();
532 $owner = User::getOwnerDataById($uid);
533 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
536 'actor' => $owner['url'],
537 'object' => ['id' => $id, 'type' => 'Follow',
538 'actor' => $owner['url'],
539 'object' => $profile['url']],
540 'to' => $profile['url']];
542 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
544 $signed = LDSignature::sign($data, $owner);
545 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
549 * Fetches ActivityPub content from the given url
551 * @param string $url content url
554 public static function fetchContent($url)
556 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
557 if (!$ret['success'] || empty($ret['body'])) {
561 return json_decode($ret['body'], true);
565 * Resolves the profile url from the address by using webfinger
567 * @param string $addr profile address (user@domain.tld)
570 private static function addrToUrl($addr)
572 $addr_parts = explode('@', $addr);
573 if (count($addr_parts) != 2) {
577 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
579 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
580 if (!$ret['success'] || empty($ret['body'])) {
584 $data = json_decode($ret['body'], true);
586 if (empty($data['links'])) {
590 foreach ($data['links'] as $link) {
591 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
595 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
596 return $link['href'];
603 public static function fetchprofile($url, $update = false)
610 $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
611 if (DBA::isResult($apcontact)) {
615 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
616 if (DBA::isResult($apcontact)) {
620 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
621 if (DBA::isResult($apcontact)) {
626 if (empty(parse_url($url, PHP_URL_SCHEME))) {
627 $url = self::addrToUrl($url);
633 $data = self::fetchContent($url);
635 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
640 $apcontact['url'] = $data['id'];
641 $apcontact['uuid'] = defaults($data, 'uuid', null);
642 $apcontact['type'] = defaults($data, 'type', null);
643 $apcontact['following'] = defaults($data, 'following', null);
644 $apcontact['followers'] = defaults($data, 'followers', null);
645 $apcontact['inbox'] = defaults($data, 'inbox', null);
646 $apcontact['outbox'] = defaults($data, 'outbox', null);
647 $apcontact['sharedinbox'] = JsonLD::fetchElement($data, 'endpoints', 'sharedInbox');
648 $apcontact['nick'] = defaults($data, 'preferredUsername', null);
649 $apcontact['name'] = defaults($data, 'name', $apcontact['nick']);
650 $apcontact['about'] = defaults($data, 'summary', '');
651 $apcontact['photo'] = JsonLD::fetchElement($data, 'icon', 'url');
652 $apcontact['alias'] = JsonLD::fetchElement($data, 'url', 'href');
654 $parts = parse_url($apcontact['url']);
655 unset($parts['scheme']);
656 unset($parts['path']);
657 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
659 $apcontact['pubkey'] = trim(JsonLD::fetchElement($data, 'publicKey', 'publicKeyPem'));
662 // manuallyApprovesFollowers
665 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
667 // Unhandled from Misskey
668 // sharedInbox, isCat
670 // Unhandled from Kroeg
671 // kroeg:blocks, updated
673 // Check if the address is resolvable
674 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
675 $parts = parse_url($apcontact['url']);
676 unset($parts['path']);
677 $apcontact['baseurl'] = Network::unparseURL($parts);
679 $apcontact['addr'] = null;
682 if ($apcontact['url'] == $apcontact['alias']) {
683 $apcontact['alias'] = null;
686 $apcontact['updated'] = DateTimeFormat::utcNow();
688 DBA::update('apcontact', $apcontact, ['url' => $url], true);
694 * Fetches a profile from the given url into an array that is compatible to Probe::uri
696 * @param string $url profile url
699 public static function probeProfile($url)
701 $apcontact = self::fetchprofile($url, true);
702 if (empty($apcontact)) {
706 $profile = ['network' => Protocol::ACTIVITYPUB];
707 $profile['nick'] = $apcontact['nick'];
708 $profile['name'] = $apcontact['name'];
709 $profile['guid'] = $apcontact['uuid'];
710 $profile['url'] = $apcontact['url'];
711 $profile['addr'] = $apcontact['addr'];
712 $profile['alias'] = $apcontact['alias'];
713 $profile['photo'] = $apcontact['photo'];
714 // $profile['community']
715 // $profile['keywords']
716 // $profile['location']
717 $profile['about'] = $apcontact['about'];
718 $profile['batch'] = $apcontact['sharedinbox'];
719 $profile['notify'] = $apcontact['inbox'];
720 $profile['poll'] = $apcontact['outbox'];
721 $profile['pubkey'] = $apcontact['pubkey'];
722 $profile['baseurl'] = $apcontact['baseurl'];
724 // Remove all "null" fields
725 foreach ($profile as $field => $content) {
726 if (is_null($content)) {
727 unset($profile[$field]);
734 public static function processInbox($body, $header, $uid)
736 $http_signer = HTTPSignature::getSigner($body, $header);
737 if (empty($http_signer)) {
738 logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
741 logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
744 $activity = json_decode($body, true);
746 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
747 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
749 if (empty($activity)) {
750 logger('Invalid body.', LOGGER_DEBUG);
754 if (LDSignature::isSigned($activity)) {
755 $ld_signer = LDSignature::getSigner($activity);
756 if (!empty($ld_signer)) {
757 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
758 $trust_source = true;
759 } elseif ($actor == $http_signer) {
760 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
761 $trust_source = true;
763 logger('Invalid JSON-LD signature.', LOGGER_DEBUG);
764 $trust_source = false;
766 } elseif ($actor == $http_signer) {
767 logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
768 $trust_source = true;
770 logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
771 $trust_source = false;
774 self::processActivity($activity, $body, $uid, $trust_source);
777 public static function fetchOutbox($url, $uid)
779 $data = self::fetchContent($url);
784 if (!empty($data['orderedItems'])) {
785 $items = $data['orderedItems'];
786 } elseif (!empty($data['first']['orderedItems'])) {
787 $items = $data['first']['orderedItems'];
788 } elseif (!empty($data['first'])) {
789 self::fetchOutbox($data['first'], $uid);
795 foreach ($items as $activity) {
796 self::processActivity($activity, '', $uid, true);
800 private static function prepareObjectData($activity, $uid, $trust_source)
802 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
804 logger('Empty actor', LOGGER_DEBUG);
808 // Fetch all receivers from to, cc, bto and bcc
809 $receivers = self::getReceivers($activity, $actor);
811 // When it is a delivery to a personal inbox we add that user to the receivers
813 $owner = User::getOwnerDataById($uid);
814 $additional = ['uid:' . $uid => $uid];
815 $receivers = array_merge($receivers, $additional);
818 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
820 if (is_string($activity['object'])) {
821 $object_url = $activity['object'];
822 } elseif (!empty($activity['object']['id'])) {
823 $object_url = $activity['object']['id'];
825 logger('No object found', LOGGER_DEBUG);
829 // Fetch the content only on activities where this matters
830 if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) {
831 $object_data = self::fetchObject($object_url, $activity['object'], $trust_source);
832 if (empty($object_data)) {
833 logger("Object data couldn't be processed", LOGGER_DEBUG);
836 } elseif ($activity['type'] == 'Accept') {
838 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
839 $object_data['object'] = JsonLD::fetchElement($activity, 'object', 'actor');
840 } elseif ($activity['type'] == 'Undo') {
842 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
843 $object_data['object'] = JsonLD::fetchElement($activity, 'object', 'object');
844 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
845 // Create a mostly empty array out of the activity data (instead of the object).
846 // This way we later don't have to check for the existence of ech individual array element.
847 $object_data = self::processCommonData($activity);
848 $object_data['name'] = $activity['type'];
849 $object_data['author'] = $activity['actor'];
850 $object_data['object'] = $object_url;
851 } elseif ($activity['type'] == 'Follow') {
852 $object_data['id'] = $activity['id'];
853 $object_data['object'] = $object_url;
858 $object_data = self::addActivityFields($object_data, $activity);
860 $object_data['type'] = $activity['type'];
861 $object_data['owner'] = $actor;
862 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
867 private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
869 if (empty($activity['type'])) {
870 logger('Empty type', LOGGER_DEBUG);
874 if (empty($activity['object'])) {
875 logger('Empty object', LOGGER_DEBUG);
879 if (empty($activity['actor'])) {
880 logger('Empty actor', LOGGER_DEBUG);
886 // title, atomUri, context_id, statusnetConversationId
889 // context, location, signature;
891 logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
893 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
894 if (empty($object_data)) {
895 logger('No object data found', LOGGER_DEBUG);
899 switch ($activity['type']) {
902 self::createItem($object_data, $body);
906 self::likeItem($object_data, $body);
919 self::followUser($object_data);
923 if ($object_data['object_type'] == 'Follow') {
924 self::acceptFollowUser($object_data);
929 if ($object_data['object_type'] == 'Follow') {
930 self::undoFollowUser($object_data);
935 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
940 private static function getReceivers($activity, $actor)
944 // When it is an answer, we inherite the receivers from the parent
945 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
946 if (!empty($replyto)) {
947 $parents = Item::select(['uid'], ['uri' => $replyto]);
948 while ($parent = Item::fetch($parents)) {
949 $receivers['uid:' . $parent['uid']] = $parent['uid'];
953 if (!empty($actor)) {
954 $profile = self::fetchprofile($actor);
955 $followers = defaults($profile, 'followers', '');
957 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
959 logger('Empty actor', LOGGER_DEBUG);
963 $elements = ['to', 'cc', 'bto', 'bcc'];
964 foreach ($elements as $element) {
965 if (empty($activity[$element])) {
969 // The receiver can be an arror or a string
970 if (is_string($activity[$element])) {
971 $activity[$element] = [$activity[$element]];
974 foreach ($activity[$element] as $receiver) {
975 if ($receiver == self::PUBLIC) {
976 $receivers['uid:0'] = 0;
979 if (($receiver == self::PUBLIC) && !empty($actor)) {
980 // This will most likely catch all OStatus connections to Mastodon
981 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]];
982 $contacts = DBA::select('contact', ['uid'], $condition);
983 while ($contact = DBA::fetch($contacts)) {
984 if ($contact['uid'] != 0) {
985 $receivers['uid:' . $contact['uid']] = $contact['uid'];
988 DBA::close($contacts);
991 if (in_array($receiver, [$followers, self::PUBLIC]) && !empty($actor)) {
992 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
993 'network' => Protocol::ACTIVITYPUB];
994 $contacts = DBA::select('contact', ['uid'], $condition);
995 while ($contact = DBA::fetch($contacts)) {
996 if ($contact['uid'] != 0) {
997 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1000 DBA::close($contacts);
1004 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1005 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1006 if (!DBA::isResult($contact)) {
1009 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1015 private static function addActivityFields($object_data, $activity)
1017 if (!empty($activity['published']) && empty($object_data['published'])) {
1018 $object_data['published'] = $activity['published'];
1021 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1022 $object_data['updated'] = $activity['updated'];
1025 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1026 $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1029 if (!empty($activity['instrument'])) {
1030 $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1032 return $object_data;
1035 private static function fetchObject($object_url, $object = [], $trust_source = false)
1037 if (!$trust_source || is_string($object)) {
1038 $data = self::fetchContent($object_url);
1040 logger('Empty content for ' . $object_url . ', check if content is available locally.', LOGGER_DEBUG);
1041 $data = $object_url;
1043 logger('Fetched content for ' . $object_url, LOGGER_DEBUG);
1046 logger('Using original object for url ' . $object_url, LOGGER_DEBUG);
1050 if (is_string($data)) {
1051 $item = Item::selectFirst([], ['uri' => $data]);
1052 if (!DBA::isResult($item)) {
1053 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1056 logger('Using already stored item for url ' . $object_url, LOGGER_DEBUG);
1057 $data = self::createNote($item);
1060 if (empty($data['type'])) {
1061 logger('Empty type', LOGGER_DEBUG);
1064 $type = $data['type'];
1065 logger('Type ' . $type, LOGGER_DEBUG);
1068 if (in_array($type, ['Note', 'Article', 'Video'])) {
1069 $common = self::processCommonData($data);
1074 return array_merge($common, self::processNote($data));
1076 return array_merge($common, self::processArticle($data));
1078 return array_merge($common, self::processVideo($data));
1081 if (empty($data['object'])) {
1084 return self::fetchObject($data['object']);
1091 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
1096 private static function processCommonData(&$object)
1098 if (empty($object['id'])) {
1103 $object_data['type'] = $object['type'];
1104 $object_data['uri'] = $object['id'];
1106 if (!empty($object['inReplyTo'])) {
1107 $object_data['reply-to-uri'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1109 $object_data['reply-to-uri'] = $object_data['uri'];
1112 $object_data['published'] = defaults($object, 'published', null);
1113 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1115 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1116 $object_data['published'] = $object_data['updated'];
1119 $object_data['uuid'] = defaults($object, 'uuid', null);
1120 $object_data['owner'] = $object_data['author'] = JsonLD::fetchElement($object, 'attributedTo', 'id');
1121 $object_data['context'] = defaults($object, 'context', null);
1122 $object_data['conversation'] = defaults($object, 'conversation', null);
1123 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1124 $object_data['name'] = defaults($object, 'title', null);
1125 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1126 $object_data['summary'] = defaults($object, 'summary', null);
1127 $object_data['content'] = defaults($object, 'content', null);
1128 $object_data['source'] = defaults($object, 'source', null);
1129 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1130 $object_data['attachments'] = defaults($object, 'attachment', null);
1131 $object_data['tags'] = defaults($object, 'tag', null);
1132 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1133 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1134 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1137 // @context, type, actor, signature, mediaType, duration, replies, icon
1139 // Also missing: (Defined in the standard, but currently unused)
1140 // audience, preview, endTime, startTime, generator, image
1142 return $object_data;
1145 private static function processNote($object)
1150 // emoji, atomUri, inReplyToAtomUri
1153 // contentMap, announcement_count, announcements, context_id, likes, like_count
1154 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1156 return $object_data;
1159 private static function processArticle($object)
1163 return $object_data;
1166 private static function processVideo($object)
1171 // category, licence, language, commentsEnabled
1174 // views, waitTranscoding, state, support, subtitleLanguage
1175 // likes, dislikes, shares, comments
1177 return $object_data;
1180 private static function convertMentions($body)
1182 $URLSearchString = "^\[\]";
1183 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1188 private static function constructTagList($tags, $sensitive)
1195 foreach ($tags as $tag) {
1196 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1197 if (!empty($tag_text)) {
1201 if (empty($tag['href'])) {
1206 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1210 /// @todo add nsfw for $sensitive
1215 private static function constructAttachList($attachments, $item)
1217 if (empty($attachments)) {
1221 foreach ($attachments as $attach) {
1222 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1223 if ($filetype == 'image') {
1224 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1226 if (!empty($item["attach"])) {
1227 $item["attach"] .= ',';
1229 $item["attach"] = '';
1231 if (!isset($attach['length'])) {
1232 $attach['length'] = "0";
1234 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1241 private static function createItem($activity, $body)
1244 $item['verb'] = ACTIVITY_POST;
1245 $item['parent-uri'] = $activity['reply-to-uri'];
1247 if ($activity['reply-to-uri'] == $activity['uri']) {
1248 $item['gravity'] = GRAVITY_PARENT;
1249 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1251 $item['gravity'] = GRAVITY_COMMENT;
1252 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1255 if (($activity['uri'] != $activity['reply-to-uri']) && !Item::exists(['uri' => $activity['reply-to-uri']])) {
1256 logger('Parent ' . $activity['reply-to-uri'] . ' not found. Try to refetch it.');
1257 self::fetchMissingActivity($activity['reply-to-uri'], $activity);
1260 self::postItem($activity, $item, $body);
1263 private static function likeItem($activity, $body)
1266 $item['verb'] = ACTIVITY_LIKE;
1267 $item['parent-uri'] = $activity['object'];
1268 $item['gravity'] = GRAVITY_ACTIVITY;
1269 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1271 self::postItem($activity, $item, $body);
1274 private static function postItem($activity, $item, $body)
1276 /// @todo What to do with $activity['context']?
1278 $item['network'] = Protocol::ACTIVITYPUB;
1279 $item['private'] = !in_array(0, $activity['receiver']);
1280 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1281 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1282 $item['uri'] = $activity['uri'];
1283 $item['created'] = $activity['published'];
1284 $item['edited'] = $activity['updated'];
1285 $item['guid'] = $activity['uuid'];
1286 $item['title'] = HTML::toBBCode($activity['name']);
1287 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1288 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1289 $item['location'] = $activity['location'];
1290 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1291 $item['app'] = $activity['service'];
1292 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1294 $item = self::constructAttachList($activity['attachments'], $item);
1296 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1297 if (!empty($source)) {
1298 $item['body'] = $source;
1301 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1302 $item['source'] = $body;
1303 $item['conversation-uri'] = $activity['conversation'];
1305 foreach ($activity['receiver'] as $receiver) {
1306 $item['uid'] = $receiver;
1307 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1309 if (($receiver != 0) && empty($item['contact-id'])) {
1310 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1313 $item_id = Item::insert($item);
1314 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1318 private static function fetchMissingActivity($url, $child)
1320 $object = ActivityPub::fetchContent($url);
1321 if (empty($object)) {
1322 logger('Activity ' . $url . ' was not fetchable, aborting.');
1327 $activity['@context'] = $object['@context'];
1328 unset($object['@context']);
1329 $activity['id'] = $object['id'];
1330 $activity['to'] = defaults($object, 'to', []);
1331 $activity['cc'] = defaults($object, 'cc', []);
1332 $activity['actor'] = $child['author'];
1333 $activity['object'] = $object;
1334 $activity['published'] = $object['published'];
1335 $activity['type'] = 'Create';
1337 self::processActivity($activity);
1338 logger('Activity ' . $url . ' had been fetched and processed.');
1341 private static function getUserOfObject($object)
1343 $self = DBA::selectFirst('contact', ['uid'], ['nurl' => normalise_link($object), 'self' => true]);
1344 if (!DBA::isResult($self)) {
1347 return $self['uid'];
1351 private static function followUser($activity)
1353 $uid = self::getUserOfObject($activity['object']);
1358 $owner = User::getOwnerDataById($uid);
1360 $cid = Contact::getIdForURL($activity['owner'], $uid);
1362 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1367 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1368 'author-link' => $activity['owner']];
1370 Contact::addRelationship($owner, $contact, $item);
1371 $cid = Contact::getIdForURL($activity['owner'], $uid);
1376 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1377 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1378 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1381 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1382 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1385 private static function acceptFollowUser($activity)
1387 $uid = self::getUserOfObject($activity['object']);
1392 $owner = User::getOwnerDataById($uid);
1394 $cid = Contact::getIdForURL($activity['owner'], $uid);
1396 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1400 $fields = ['pending' => false];
1402 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1403 if ($contact['rel'] == Contact::FOLLOWER) {
1404 $fields['rel'] = Contact::FRIEND;
1407 $condition = ['id' => $cid];
1408 DBA::update('contact', $fields, $condition);
1409 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1412 private static function undoFollowUser($activity)
1414 $uid = self::getUserOfObject($activity['object']);
1419 $owner = User::getOwnerDataById($uid);
1421 $cid = Contact::getIdForURL($activity['owner'], $uid);
1423 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1427 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1428 if (!DBA::isResult($contact)) {
1432 Contact::removeFollower($owner, $contact);
1433 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);