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\User;
17 use Friendica\Util\DateTimeFormat;
18 use Friendica\Util\Crypto;
19 use Friendica\Content\Text\BBCode;
20 use Friendica\Content\Text\HTML;
21 use Friendica\Network\Probe;
24 * @brief ActivityPub Protocol class
25 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
26 * https://www.w3.org/TR/activitypub/
27 * https://www.w3.org/TR/activitystreams-core/
28 * https://www.w3.org/TR/activitystreams-vocabulary/
30 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
31 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
33 * Digest: https://tools.ietf.org/html/rfc5843
34 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
36 * Part of the code for HTTP signing is taken from the Osada project.
37 * https://framagit.org/macgirvin/osada
42 * - Activities: Dislike, Update, Delete
43 * - Object Types: Person, Tombstome
46 * - Activities: Like, Dislike, Update, Delete
47 * - Object Tyoes: Article, Announce, Person, Tombstone
50 * - Message distribution
51 * - Endpoints: Outbox, Object, Follower, Following
56 const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
58 public static function transmit($data, $target, $uid)
60 $owner = User::getOwnerDataById($uid);
66 $content = json_encode($data);
68 // Header data that is about to be signed.
69 /// @todo Add "digest"
70 $host = parse_url($target, PHP_URL_HOST);
71 $path = parse_url($target, PHP_URL_PATH);
73 $content_length = strlen($content);
75 $headers = ['Host: ' . $host, 'Date: ' . $date, 'Content-Length: ' . $content_length];
77 $signed_data = "(request-target): post " . $path . "\nhost: " . $host . "\ndate: " . $date . "\ncontent-length: " . $content_length;
79 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
81 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",headers="(request-target) host date content-length",signature="' . $signature . '"';
82 $headers[] = 'Content-Type: application/activity+json';
84 Network::post($target, $content, $headers);
85 $return_code = BaseObject::getApp()->get_curl_code();
87 logger('Transmit to ' . $target . ' returned ' . $return_code);
91 * Return the ActivityPub profile of the given user
93 * @param integer $uid User ID
96 public static function profile($uid)
98 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
100 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
101 'account_removed' => false, 'verified' => true];
102 $fields = ['guid', 'nickname', 'pubkey', 'account-type'];
103 $user = DBA::selectFirst('user', $fields, $condition);
104 if (!DBA::isResult($user)) {
108 $fields = ['locality', 'region', 'country-name', 'page-flags'];
109 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
110 if (!DBA::isResult($profile)) {
114 $fields = ['name', 'url', 'location', 'about', 'avatar'];
115 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
116 if (!DBA::isResult($contact)) {
120 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
121 ['uuid' => 'http://schema.org/identifier', 'sensitive' => 'as:sensitive',
122 'vcard' => 'http://www.w3.org/2006/vcard/ns#']]];
124 $data['id'] = $contact['url'];
125 $data['uuid'] = $user['guid'];
126 $data['type'] = $accounttype[$user['account-type']];
127 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
128 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
129 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
130 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
131 $data['preferredUsername'] = $user['nickname'];
132 $data['name'] = $contact['name'];
133 $data['vcard:hasAddress'] = ['@type' => 'Home', 'vcard:country-name' => $profile['country-name'],
134 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
135 $data['summary'] = $contact['about'];
136 $data['url'] = $contact['url'];
137 $data['manuallyApprovesFollowers'] = in_array($profile['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
138 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
139 'owner' => $contact['url'],
140 'publicKeyPem' => $user['pubkey']];
141 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
142 $data['icon'] = ['type' => 'Image',
143 'url' => $contact['avatar']];
145 // tags: https://kitty.town/@inmysocks/100656097926961126.json
149 public static function createActivityFromItem($item_id)
151 $item = Item::selectFirst([], ['id' => $item_id]);
153 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
154 ['Emoji' => 'toot:Emoji', 'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
155 'conversation' => 'ostatus:conversation', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
156 'ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
157 'toot' => 'http://joinmastodon.org/ns#']]];
159 $data['type'] = 'Create';
160 $data['id'] = $item['uri'];
161 $data['actor'] = $item['author-link'];
162 $data['to'] = 'https://www.w3.org/ns/activitystreams#Public';
163 $data['object'] = self::createNote($item);
167 public static function createNote($item)
170 $data['type'] = 'Note';
171 $data['id'] = $item['uri'];
173 if ($item['uri'] != $item['thr-parent']) {
174 $data['inReplyTo'] = $item['thr-parent'];
177 $conversation = DBA::selectFirst('conversation', ['conversation-uri'], ['item-uri' => $item['parent-uri']]);
178 if (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
179 $conversation_uri = $conversation['conversation-uri'];
181 $conversation_uri = $item['parent-uri'];
184 $data['context'] = $data['conversation'] = $conversation_uri;
185 $data['actor'] = $item['author-link'];
187 if (!$item['private']) {
188 $data['to'][] = 'https://www.w3.org/ns/activitystreams#Public';
190 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
191 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
192 $data['attributedTo'] = $item['author-link'];
193 $data['name'] = BBCode::convert($item['title'], false, 7);
194 $data['content'] = BBCode::convert($item['body'], false, 7);
195 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
196 //$data['summary'] = ''; // Ignore by now
197 //$data['sensitive'] = false; // - Query NSFW
198 //$data['emoji'] = []; // Ignore by now
199 //$data['tag'] = []; /// @ToDo
200 //$data['attachment'] = []; // @ToDo
204 public static function transmitActivity($activity, $target, $uid)
206 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
208 $owner = User::getOwnerDataById($uid);
210 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
211 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
213 'actor' => $owner['url'],
214 'object' => $profile['url']];
216 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
217 return self::transmit($data, $profile['notify'], $uid);
220 public static function transmitContactAccept($target, $id, $uid)
222 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
224 $owner = User::getOwnerDataById($uid);
225 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
226 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
228 'actor' => $owner['url'],
229 'object' => ['id' => $id, 'type' => 'Follow',
230 'actor' => $profile['url'],
231 'object' => $owner['url']]];
233 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
234 return self::transmit($data, $profile['notify'], $uid);
237 public static function transmitContactReject($target, $id, $uid)
239 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
241 $owner = User::getOwnerDataById($uid);
242 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
243 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
245 'actor' => $owner['url'],
246 'object' => ['id' => $id, 'type' => 'Follow',
247 'actor' => $profile['url'],
248 'object' => $owner['url']]];
250 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
251 return self::transmit($data, $profile['notify'], $uid);
254 public static function transmitContactUndo($target, $uid)
256 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
258 $id = System::baseUrl() . '/activity/' . System::createGUID();
260 $owner = User::getOwnerDataById($uid);
261 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
264 'actor' => $owner['url'],
265 'object' => ['id' => $id, 'type' => 'Follow',
266 'actor' => $owner['url'],
267 'object' => $profile['url']]];
269 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
270 return self::transmit($data, $profile['notify'], $uid);
274 * Fetches ActivityPub content from the given url
276 * @param string $url content url
279 public static function fetchContent($url)
281 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json']);
282 if (!$ret['success'] || empty($ret['body'])) {
286 return json_decode($ret['body'], true);
290 * Resolves the profile url from the address by using webfinger
292 * @param string $addr profile address (user@domain.tld)
295 private static function addrToUrl($addr)
297 $addr_parts = explode('@', $addr);
298 if (count($addr_parts) != 2) {
302 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
304 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
305 if (!$ret['success'] || empty($ret['body'])) {
309 $data = json_decode($ret['body'], true);
311 if (empty($data['links'])) {
315 foreach ($data['links'] as $link) {
316 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
320 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
321 return $link['href'];
328 public static function verifySignature($content, $http_headers)
330 $object = json_decode($content, true);
332 if (empty($object)) {
336 $actor = self::processElement($object, 'actor', 'id');
339 $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . $http_headers['REQUEST_URI'];
341 // First take every header
342 foreach ($http_headers as $k => $v) {
343 $field = str_replace('_', '-', strtolower($k));
344 $headers[$field] = $v;
347 // Now add every http header
348 foreach ($http_headers as $k => $v) {
349 if (strpos($k, 'HTTP_') === 0) {
350 $field = str_replace('_', '-', strtolower(substr($k, 5)));
351 $headers[$field] = $v;
355 $sig_block = ActivityPub::parseSigHeader($http_headers['HTTP_SIGNATURE']);
357 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
362 foreach ($sig_block['headers'] as $h) {
363 if (array_key_exists($h, $headers)) {
364 $signed_data .= $h . ': ' . $headers[$h] . "\n";
367 $signed_data = rtrim($signed_data, "\n");
369 if (empty($signed_data)) {
375 if ($sig_block['algorithm'] === 'rsa-sha256') {
376 $algorithm = 'sha256';
379 if ($sig_block['algorithm'] === 'rsa-sha512') {
380 $algorithm = 'sha512';
383 if (empty($algorithm)) {
387 $key = self::fetchKey($sig_block['keyId'], $actor);
393 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm)) {
397 // Check the digest when it is part of the signed data
398 if (in_array('digest', $sig_block['headers'])) {
399 $digest = explode('=', $headers['digest'], 2);
400 if ($digest[0] === 'SHA-256') {
403 if ($digest[0] === 'SHA-512') {
407 /// @todo add all hashes from the rfc
409 if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
414 // Check the content-length when it is part of the signed data
415 if (in_array('content-length', $sig_block['headers'])) {
416 if (strlen($content) != $headers['content-length']) {
425 private static function fetchKey($id, $actor)
427 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
429 $profile = Probe::uri($url, Protocol::ACTIVITYPUB);
430 if (!empty($profile)) {
431 return $profile['pubkey'];
432 } elseif ($url != $actor) {
433 $profile = Probe::uri($actor, Protocol::ACTIVITYPUB);
434 if (!empty($profile)) {
435 return $profile['pubkey'];
445 * @param string $header
446 * @return array associate array with
447 * - \e string \b keyID
448 * - \e string \b algorithm
449 * - \e array \b headers
450 * - \e string \b signature
452 private static function parseSigHeader($header)
457 if (preg_match('/keyId="(.*?)"/ism',$header,$matches)) {
458 $ret['keyId'] = $matches[1];
461 if (preg_match('/algorithm="(.*?)"/ism',$header,$matches)) {
462 $ret['algorithm'] = $matches[1];
465 if (preg_match('/headers="(.*?)"/ism',$header,$matches)) {
466 $ret['headers'] = explode(' ', $matches[1]);
469 if (preg_match('/signature="(.*?)"/ism',$header,$matches)) {
470 $ret['signature'] = base64_decode(preg_replace('/\s+/','',$matches[1]));
477 * Fetches a profile from the given url
479 * @param string $url profile url
482 public static function fetchProfile($url)
484 if (empty(parse_url($url, PHP_URL_SCHEME))) {
485 $url = self::addrToUrl($url);
491 $data = self::fetchContent($url);
493 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
497 $profile = ['network' => Protocol::ACTIVITYPUB];
498 $profile['nick'] = $data['preferredUsername'];
499 $profile['name'] = defaults($data, 'name', $profile['nick']);
500 $profile['guid'] = defaults($data, 'uuid', null);
501 $profile['url'] = $data['id'];
503 $parts = parse_url($profile['url']);
504 unset($parts['scheme']);
505 unset($parts['path']);
506 $profile['addr'] = $profile['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
507 $profile['alias'] = self::processElement($data, 'url', 'href');
508 $profile['photo'] = self::processElement($data, 'icon', 'url');
509 // $profile['community']
510 // $profile['keywords']
511 // $profile['location']
512 $profile['about'] = defaults($data, 'summary', '');
513 $profile['batch'] = self::processElement($data, 'endpoints', 'sharedInbox');
514 $profile['notify'] = $data['inbox'];
515 $profile['poll'] = $data['outbox'];
516 $profile['pubkey'] = self::processElement($data, 'publicKey', 'publicKeyPem');
518 // Check if the address is resolvable
519 if (self::addrToUrl($profile['addr']) == $profile['url']) {
520 $parts = parse_url($profile['url']);
521 unset($parts['path']);
522 $profile['baseurl'] = Network::unparseURL($parts);
524 unset($profile['addr']);
527 if ($profile['url'] == $profile['alias']) {
528 unset($profile['alias']);
531 // Remove all "null" fields
532 foreach ($profile as $field => $content) {
533 if (is_null($content)) {
534 unset($profile[$field]);
539 // type, manuallyApprovesFollowers
542 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
544 // Unhandled from Misskey
545 // sharedInbox, isCat
547 // Unhandled from Kroeg
548 // kroeg:blocks, updated
553 public static function processInbox($body, $header, $uid)
555 logger('Incoming message for user ' . $uid, LOGGER_DEBUG);
557 if (!self::verifySignature($body, $header)) {
558 logger('Invalid signature, message will be discarded.', LOGGER_DEBUG);
562 $activity = json_decode($body, true);
564 if (!is_array($activity)) {
565 logger('Invalid body.', LOGGER_DEBUG);
569 self::processActivity($activity, $body, $uid);
572 public static function fetchOutbox($url)
574 $data = self::fetchContent($url);
579 if (!empty($data['orderedItems'])) {
580 $items = $data['orderedItems'];
581 } elseif (!empty($data['first']['orderedItems'])) {
582 $items = $data['first']['orderedItems'];
583 } elseif (!empty($data['first'])) {
584 self::fetchOutbox($data['first']);
590 foreach ($items as $activity) {
591 self::processActivity($activity);
595 private static function prepareObjectData($activity, $uid)
597 $actor = self::processElement($activity, 'actor', 'id');
599 logger('Empty actor', LOGGER_DEBUG);
603 // Fetch all receivers from to, cc, bto and bcc
604 $receivers = self::getReceivers($activity, $actor);
606 // When it is a delivery to a personal inbox we add that user to the receivers
608 $owner = User::getOwnerDataById($uid);
609 $additional = [$owner['url'] => $uid];
610 $receivers = array_merge($receivers, $additional);
613 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
615 $public = in_array(0, $receivers);
617 if (is_string($activity['object'])) {
618 $object_url = $activity['object'];
619 } elseif (!empty($activity['object']['id'])) {
620 $object_url = $activity['object']['id'];
622 logger('No object found', LOGGER_DEBUG);
626 // Fetch the content only on activities where this matters
627 if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) {
628 $object_data = self::fetchObject($object_url, $activity['object']);
629 if (empty($object_data)) {
630 logger("Object data couldn't be processed", LOGGER_DEBUG);
633 } elseif ($activity['type'] == 'Accept') {
635 $object_data['object_type'] = self::processElement($activity, 'object', 'type');
636 $object_data['object'] = self::processElement($activity, 'object', 'actor');
637 } elseif ($activity['type'] == 'Undo') {
639 $object_data['object_type'] = self::processElement($activity, 'object', 'type');
640 $object_data['object'] = self::processElement($activity, 'object', 'object');
641 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
642 // Create a mostly empty array out of the activity data (instead of the object).
643 // This way we later don't have to check for the existence of ech individual array element.
644 $object_data = self::processCommonData($activity);
645 $object_data['name'] = $activity['type'];
646 $object_data['author'] = $activity['actor'];
647 $object_data['object'] = $object_url;
648 } elseif ($activity['type'] == 'Follow') {
649 $object_data['id'] = $activity['id'];
650 $object_data['object'] = $object_url;
655 $object_data = self::addActivityFields($object_data, $activity);
657 $object_data['type'] = $activity['type'];
658 $object_data['owner'] = $actor;
659 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
664 private static function processActivity($activity, $body = '', $uid = null)
666 if (empty($activity['type'])) {
667 logger('Empty type', LOGGER_DEBUG);
671 if (empty($activity['object'])) {
672 logger('Empty object', LOGGER_DEBUG);
676 if (empty($activity['actor'])) {
677 logger('Empty actor', LOGGER_DEBUG);
683 // title, atomUri, context_id, statusnetConversationId
686 // context, location, signature;
688 logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
690 $object_data = self::prepareObjectData($activity, $uid);
691 if (empty($object_data)) {
692 logger('No object data found', LOGGER_DEBUG);
696 switch ($activity['type']) {
699 self::createItem($object_data, $body);
703 self::likeItem($object_data, $body);
716 self::followUser($object_data);
720 if ($object_data['object_type'] == 'Follow') {
721 self::acceptFollowUser($object_data);
726 if ($object_data['object_type'] == 'Follow') {
727 self::undoFollowUser($object_data);
732 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
737 private static function getReceivers($activity, $actor)
741 $data = self::fetchContent($actor);
742 $followers = defaults($data, 'followers', '');
744 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
746 $elements = ['to', 'cc', 'bto', 'bcc'];
747 foreach ($elements as $element) {
748 if (empty($activity[$element])) {
752 // The receiver can be an arror or a string
753 if (is_string($activity[$element])) {
754 $activity[$element] = [$activity[$element]];
757 foreach ($activity[$element] as $receiver) {
758 if ($receiver == self::PUBLIC) {
759 $receivers['uid:0'] = 0;
761 // This will most likely catch all OStatus connections to Mastodon
762 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]];
763 $contacts = DBA::select('contact', ['uid'], $condition);
764 while ($contact = DBA::fetch($contacts)) {
765 if ($contact['uid'] != 0) {
766 $receivers['uid:' . $contact['uid']] = $contact['uid'];
769 DBA::close($contacts);
772 if (in_array($receiver, [$followers, self::PUBLIC])) {
773 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
774 'network' => Protocol::ACTIVITYPUB];
775 $contacts = DBA::select('contact', ['uid'], $condition);
776 while ($contact = DBA::fetch($contacts)) {
777 if ($contact['uid'] != 0) {
778 $receivers['uid:' . $contact['uid']] = $contact['uid'];
781 DBA::close($contacts);
785 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
786 $contact = DBA::selectFirst('contact', ['uid'], $condition);
787 if (!DBA::isResult($contact)) {
790 $receivers['cid:' . $contact['uid']] = $contact['uid'];
796 private static function addActivityFields($object_data, $activity)
798 if (!empty($activity['published']) && empty($object_data['published'])) {
799 $object_data['published'] = $activity['published'];
802 if (!empty($activity['updated']) && empty($object_data['updated'])) {
803 $object_data['updated'] = $activity['updated'];
806 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
807 $object_data['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
810 if (!empty($activity['instrument'])) {
811 $object_data['service'] = self::processElement($activity, 'instrument', 'name', 'type', 'Service');
816 private static function fetchObject($object_url, $object = [], $public = true)
819 $data = self::fetchContent($object_url);
821 logger('Empty content for ' . $object_url . ', check if content is available locally.', LOGGER_DEBUG);
825 logger('Using original object for url ' . $object_url, LOGGER_DEBUG);
829 if (is_string($data)) {
830 $item = Item::selectFirst([], ['uri' => $data]);
831 if (!DBA::isResult($item)) {
832 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
835 logger('Using already stored item for url ' . $object_url, LOGGER_DEBUG);
836 $data = self::createNote($item);
839 if (empty($data['type'])) {
840 logger('Empty type', LOGGER_DEBUG);
843 $type = $data['type'];
844 logger('Type ' . $type, LOGGER_DEBUG);
847 if (in_array($type, ['Note', 'Article', 'Video'])) {
848 $common = self::processCommonData($data);
853 return array_merge($common, self::processNote($data));
855 return array_merge($common, self::processArticle($data));
857 return array_merge($common, self::processVideo($data));
860 if (empty($data['object'])) {
863 return self::fetchObject($data['object']);
870 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
875 private static function processCommonData(&$object)
877 if (empty($object['id'])) {
882 $object_data['type'] = $object['type'];
883 $object_data['uri'] = $object['id'];
885 if (!empty($object['inReplyTo'])) {
886 $object_data['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
888 $object_data['reply-to-uri'] = $object_data['uri'];
891 $object_data['published'] = defaults($object, 'published', null);
892 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
894 if (empty($object_data['published']) && !empty($object_data['updated'])) {
895 $object_data['published'] = $object_data['updated'];
898 $object_data['uuid'] = defaults($object, 'uuid', null);
899 $object_data['owner'] = $object_data['author'] = self::processElement($object, 'attributedTo', 'id');
900 $object_data['context'] = defaults($object, 'context', null);
901 $object_data['conversation'] = defaults($object, 'conversation', null);
902 $object_data['sensitive'] = defaults($object, 'sensitive', null);
903 $object_data['name'] = defaults($object, 'title', null);
904 $object_data['name'] = defaults($object, 'name', $object_data['name']);
905 $object_data['summary'] = defaults($object, 'summary', null);
906 $object_data['content'] = defaults($object, 'content', null);
907 $object_data['source'] = defaults($object, 'source', null);
908 $object_data['location'] = self::processElement($object, 'location', 'name', 'type', 'Place');
909 $object_data['attachments'] = defaults($object, 'attachment', null);
910 $object_data['tags'] = defaults($object, 'tag', null);
911 $object_data['service'] = self::processElement($object, 'instrument', 'name', 'type', 'Service');
912 $object_data['alternate-url'] = self::processElement($object, 'url', 'href');
913 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
916 // @context, type, actor, signature, mediaType, duration, replies, icon
918 // Also missing: (Defined in the standard, but currently unused)
919 // audience, preview, endTime, startTime, generator, image
924 private static function processNote($object)
929 // emoji, atomUri, inReplyToAtomUri
932 // contentMap, announcement_count, announcements, context_id, likes, like_count
933 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
938 private static function processArticle($object)
945 private static function processVideo($object)
950 // category, licence, language, commentsEnabled
953 // views, waitTranscoding, state, support, subtitleLanguage
954 // likes, dislikes, shares, comments
959 private static function processElement($array, $element, $key, $type = null, $type_value = null)
965 if (empty($array[$element])) {
969 if (is_string($array[$element])) {
970 return $array[$element];
973 if (is_null($type_value)) {
974 if (!empty($array[$element][$key])) {
975 return $array[$element][$key];
978 if (!empty($array[$element][0][$key])) {
979 return $array[$element][0][$key];
985 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
986 return $array[$element][$key];
989 /// @todo Add array search
994 private static function convertMentions($body)
996 $URLSearchString = "^\[\]";
997 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1002 private static function constructTagList($tags, $sensitive)
1009 foreach ($tags as $tag) {
1010 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1011 if (!empty($tag_text)) {
1015 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1019 /// @todo add nsfw for $sensitive
1024 private static function constructAttachList($attachments, $item)
1026 if (empty($attachments)) {
1030 foreach ($attachments as $attach) {
1031 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1032 if ($filetype == 'image') {
1033 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1035 if (!empty($item["attach"])) {
1036 $item["attach"] .= ',';
1038 $item["attach"] = '';
1040 if (!isset($attach['length'])) {
1041 $attach['length'] = "0";
1043 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1050 private static function createItem($activity, $body)
1053 $item['verb'] = ACTIVITY_POST;
1054 $item['parent-uri'] = $activity['reply-to-uri'];
1056 if ($activity['reply-to-uri'] == $activity['uri']) {
1057 $item['gravity'] = GRAVITY_PARENT;
1058 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1060 $item['gravity'] = GRAVITY_COMMENT;
1061 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1064 if (($activity['uri'] != $activity['reply-to-uri']) && !Item::exists(['uri' => $activity['reply-to-uri']])) {
1065 logger('Parent ' . $activity['reply-to-uri'] . ' not found. Try to refetch it.');
1066 self::fetchMissingActivity($activity['reply-to-uri']);
1069 self::postItem($activity, $item, $body);
1072 private static function likeItem($activity, $body)
1075 $item['verb'] = ACTIVITY_LIKE;
1076 $item['parent-uri'] = $activity['object'];
1077 $item['gravity'] = GRAVITY_ACTIVITY;
1078 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1080 self::postItem($activity, $item, $body);
1083 private static function postItem($activity, $item, $body)
1085 /// @todo What to do with $activity['context']?
1087 $item['network'] = Protocol::ACTIVITYPUB;
1088 $item['private'] = !in_array(0, $activity['receiver']);
1089 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1090 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1091 $item['uri'] = $activity['uri'];
1092 $item['created'] = $activity['published'];
1093 $item['edited'] = $activity['updated'];
1094 $item['guid'] = $activity['uuid'];
1095 $item['title'] = HTML::toBBCode($activity['name']);
1096 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1097 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1098 $item['location'] = $activity['location'];
1099 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1100 $item['app'] = $activity['service'];
1101 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1103 $item = self::constructAttachList($activity['attachments'], $item);
1105 $source = self::processElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1106 if (!empty($source)) {
1107 $item['body'] = $source;
1110 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1111 $item['source'] = $body;
1112 $item['conversation-uri'] = $activity['conversation'];
1114 foreach ($activity['receiver'] as $receiver) {
1115 $item['uid'] = $receiver;
1116 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1118 if (($receiver != 0) && empty($item['contact-id'])) {
1119 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1122 $item_id = Item::insert($item);
1123 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1127 private static function fetchMissingActivity($url)
1129 $object = ActivityPub::fetchContent($url);
1130 if (empty($object)) {
1131 logger('Activity ' . $url . ' was not fetchable, aborting.');
1136 $activity['@context'] = $object['@context'];
1137 unset($object['@context']);
1138 $activity['id'] = $object['id'];
1139 $activity['to'] = defaults($object, 'to', []);
1140 $activity['cc'] = defaults($object, 'cc', []);
1141 $activity['actor'] = $object['attributedTo'];
1142 $activity['object'] = $object;
1143 $activity['published'] = $object['published'];
1144 $activity['type'] = 'Create';
1145 self::processActivity($activity);
1146 logger('Activity ' . $url . ' had been fetched and processed.');
1149 private static function getUserOfObject($object)
1151 $self = DBA::selectFirst('contact', ['uid'], ['nurl' => normalise_link($object), 'self' => true]);
1152 if (!DBA::isResult($self)) {
1155 return $self['uid'];
1159 private static function followUser($activity)
1161 $uid = self::getUserOfObject($activity['object']);
1166 $owner = User::getOwnerDataById($uid);
1168 $cid = Contact::getIdForURL($activity['owner'], $uid);
1170 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1175 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1176 'author-link' => $activity['owner']];
1178 Contact::addRelationship($owner, $contact, $item);
1179 $cid = Contact::getIdForURL($activity['owner'], $uid);
1184 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1185 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1186 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1189 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1190 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1193 private static function acceptFollowUser($activity)
1195 $uid = self::getUserOfObject($activity['object']);
1200 $owner = User::getOwnerDataById($uid);
1202 $cid = Contact::getIdForURL($activity['owner'], $uid);
1204 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1208 $fields = ['pending' => false];
1210 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1211 if ($contact['rel'] == Contact::FOLLOWER) {
1212 $fields['rel'] = Contact::FRIEND;
1215 $condition = ['id' => $cid];
1216 DBA::update('contact', $fields, $condition);
1217 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1220 private static function undoFollowUser($activity)
1222 $uid = self::getUserOfObject($activity['object']);
1227 $owner = User::getOwnerDataById($uid);
1229 $cid = Contact::getIdForURL($activity['owner'], $uid);
1231 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1235 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1236 if (!DBA::isResult($contact)) {
1240 Contact::removeFollower($owner, $contact);
1241 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);