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']);
283 if (!$ret['success'] || empty($ret['body'])) {
287 return json_decode($ret['body'], true);
291 * Resolves the profile url from the address by using webfinger
293 * @param string $addr profile address (user@domain.tld)
296 private static function addrToUrl($addr)
298 $addr_parts = explode('@', $addr);
299 if (count($addr_parts) != 2) {
303 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
305 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
306 if (!$ret['success'] || empty($ret['body'])) {
310 $data = json_decode($ret['body'], true);
312 if (empty($data['links'])) {
316 foreach ($data['links'] as $link) {
317 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
321 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
322 return $link['href'];
329 public static function verifySignature($content, $http_headers)
331 $object = json_decode($content, true);
333 if (empty($object)) {
337 $actor = self::processElement($object, 'actor', 'id');
340 $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . $http_headers['REQUEST_URI'];
342 // First take every header
343 foreach ($http_headers as $k => $v) {
344 $field = str_replace('_', '-', strtolower($k));
345 $headers[$field] = $v;
348 // Now add every http header
349 foreach ($http_headers as $k => $v) {
350 if (strpos($k, 'HTTP_') === 0) {
351 $field = str_replace('_', '-', strtolower(substr($k, 5)));
352 $headers[$field] = $v;
356 $sig_block = ActivityPub::parseSigHeader($http_headers['HTTP_SIGNATURE']);
358 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
363 foreach ($sig_block['headers'] as $h) {
364 if (array_key_exists($h, $headers)) {
365 $signed_data .= $h . ': ' . $headers[$h] . "\n";
368 $signed_data = rtrim($signed_data, "\n");
370 if (empty($signed_data)) {
376 if ($sig_block['algorithm'] === 'rsa-sha256') {
377 $algorithm = 'sha256';
380 if ($sig_block['algorithm'] === 'rsa-sha512') {
381 $algorithm = 'sha512';
384 if (empty($algorithm)) {
388 $key = self::fetchKey($sig_block['keyId'], $actor);
394 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm)) {
398 // Check the digest when it is part of the signed data
399 if (in_array('digest', $sig_block['headers'])) {
400 $digest = explode('=', $headers['digest'], 2);
401 if ($digest[0] === 'SHA-256') {
404 if ($digest[0] === 'SHA-512') {
408 /// @todo add all hashes from the rfc
410 if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
415 // Check the content-length when it is part of the signed data
416 if (in_array('content-length', $sig_block['headers'])) {
417 if (strlen($content) != $headers['content-length']) {
426 private static function fetchKey($id, $actor)
428 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
430 $profile = Probe::uri($url, Protocol::ACTIVITYPUB);
431 if (!empty($profile)) {
432 return $profile['pubkey'];
433 } elseif ($url != $actor) {
434 $profile = Probe::uri($actor, Protocol::ACTIVITYPUB);
435 if (!empty($profile)) {
436 return $profile['pubkey'];
446 * @param string $header
447 * @return array associate array with
448 * - \e string \b keyID
449 * - \e string \b algorithm
450 * - \e array \b headers
451 * - \e string \b signature
453 private static function parseSigHeader($header)
458 if (preg_match('/keyId="(.*?)"/ism',$header,$matches)) {
459 $ret['keyId'] = $matches[1];
462 if (preg_match('/algorithm="(.*?)"/ism',$header,$matches)) {
463 $ret['algorithm'] = $matches[1];
466 if (preg_match('/headers="(.*?)"/ism',$header,$matches)) {
467 $ret['headers'] = explode(' ', $matches[1]);
470 if (preg_match('/signature="(.*?)"/ism',$header,$matches)) {
471 $ret['signature'] = base64_decode(preg_replace('/\s+/','',$matches[1]));
478 * Fetches a profile from the given url
480 * @param string $url profile url
483 public static function fetchProfile($url)
485 if (empty(parse_url($url, PHP_URL_SCHEME))) {
486 $url = self::addrToUrl($url);
492 $data = self::fetchContent($url);
494 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
498 $profile = ['network' => Protocol::ACTIVITYPUB];
499 $profile['nick'] = $data['preferredUsername'];
500 $profile['name'] = defaults($data, 'name', $profile['nick']);
501 $profile['guid'] = defaults($data, 'uuid', null);
502 $profile['url'] = $data['id'];
504 $parts = parse_url($profile['url']);
505 unset($parts['scheme']);
506 unset($parts['path']);
507 $profile['addr'] = $profile['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
508 $profile['alias'] = self::processElement($data, 'url', 'href');
509 $profile['photo'] = self::processElement($data, 'icon', 'url');
510 // $profile['community']
511 // $profile['keywords']
512 // $profile['location']
513 $profile['about'] = defaults($data, 'summary', '');
514 $profile['batch'] = self::processElement($data, 'endpoints', 'sharedInbox');
515 $profile['notify'] = $data['inbox'];
516 $profile['poll'] = $data['outbox'];
517 $profile['pubkey'] = self::processElement($data, 'publicKey', 'publicKeyPem');
519 // Check if the address is resolvable
520 if (self::addrToUrl($profile['addr']) == $profile['url']) {
521 $parts = parse_url($profile['url']);
522 unset($parts['path']);
523 $profile['baseurl'] = Network::unparseURL($parts);
525 unset($profile['addr']);
528 if ($profile['url'] == $profile['alias']) {
529 unset($profile['alias']);
532 // Remove all "null" fields
533 foreach ($profile as $field => $content) {
534 if (is_null($content)) {
535 unset($profile[$field]);
540 // type, manuallyApprovesFollowers
543 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
545 // Unhandled from Misskey
546 // sharedInbox, isCat
548 // Unhandled from Kroeg
549 // kroeg:blocks, updated
554 public static function processInbox($body, $header, $uid)
556 logger('Incoming message for user ' . $uid, LOGGER_DEBUG);
558 if (!self::verifySignature($body, $header)) {
559 logger('Invalid signature, message will be discarded.', LOGGER_DEBUG);
563 $activity = json_decode($body, true);
565 if (!is_array($activity)) {
566 logger('Invalid body.', LOGGER_DEBUG);
570 self::processActivity($activity, $body, $uid);
573 public static function fetchOutbox($url)
575 $data = self::fetchContent($url);
580 if (!empty($data['orderedItems'])) {
581 $items = $data['orderedItems'];
582 } elseif (!empty($data['first']['orderedItems'])) {
583 $items = $data['first']['orderedItems'];
584 } elseif (!empty($data['first'])) {
585 self::fetchOutbox($data['first']);
591 foreach ($items as $activity) {
592 self::processActivity($activity);
596 private static function prepareObjectData($activity, $uid)
598 $actor = self::processElement($activity, 'actor', 'id');
600 logger('Empty actor', LOGGER_DEBUG);
604 // Fetch all receivers from to, cc, bto and bcc
605 $receivers = self::getReceivers($activity, $actor);
607 // When it is a delivery to a personal inbox we add that user to the receivers
609 $owner = User::getOwnerDataById($uid);
610 $additional = [$owner['url'] => $uid];
611 $receivers = array_merge($receivers, $additional);
614 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
616 if (is_string($activity['object'])) {
617 $object_url = $activity['object'];
618 } elseif (!empty($activity['object']['id'])) {
619 $object_url = $activity['object']['id'];
621 logger('No object found', LOGGER_DEBUG);
625 // Fetch the content only on activities where this matters
626 if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) {
627 $object_data = self::fetchObject($object_url, $activity['object']);
628 if (empty($object_data)) {
629 logger("Object data couldn't be processed", LOGGER_DEBUG);
632 } elseif ($activity['type'] == 'Accept') {
634 $object_data['object_type'] = self::processElement($activity, 'object', 'type');
635 $object_data['object'] = self::processElement($activity, 'object', 'actor');
636 } elseif ($activity['type'] == 'Undo') {
638 $object_data['object_type'] = self::processElement($activity, 'object', 'type');
639 $object_data['object'] = self::processElement($activity, 'object', 'object');
640 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
641 // Create a mostly empty array out of the activity data (instead of the object).
642 // This way we later don't have to check for the existence of ech individual array element.
643 $object_data = self::processCommonData($activity);
644 $object_data['name'] = $activity['type'];
645 $object_data['author'] = $activity['actor'];
646 $object_data['object'] = $object_url;
647 } elseif ($activity['type'] == 'Follow') {
648 $object_data['id'] = $activity['id'];
649 $object_data['object'] = $object_url;
652 $object_data = self::addActivityFields($object_data, $activity);
654 $object_data['type'] = $activity['type'];
655 $object_data['owner'] = $actor;
656 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
661 private static function processActivity($activity, $body = '', $uid = null)
663 if (empty($activity['type'])) {
664 logger('Empty type', LOGGER_DEBUG);
668 if (empty($activity['object'])) {
669 logger('Empty object', LOGGER_DEBUG);
673 if (empty($activity['actor'])) {
674 logger('Empty actor', LOGGER_DEBUG);
680 // title, atomUri, context_id, statusnetConversationId
683 // context, location, signature;
685 logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
687 $object_data = self::prepareObjectData($activity, $uid);
688 if (empty($object_data)) {
689 logger('No object data found', LOGGER_DEBUG);
693 switch ($activity['type']) {
696 self::createItem($object_data, $body);
700 self::likeItem($object_data, $body);
713 self::followUser($object_data);
717 if ($object_data['object_type'] == 'Follow') {
718 self::acceptFollowUser($object_data);
723 if ($object_data['object_type'] == 'Follow') {
724 self::undoFollowUser($object_data);
729 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
734 private static function getReceivers($activity, $actor)
738 $data = self::fetchContent($actor);
739 $followers = defaults($data, 'followers', '');
741 $elements = ['to', 'cc', 'bto', 'bcc'];
742 foreach ($elements as $element) {
743 if (empty($activity[$element])) {
747 // The receiver can be an arror or a string
748 if (is_string($activity[$element])) {
749 $activity[$element] = [$activity[$element]];
752 foreach ($activity[$element] as $receiver) {
753 // Mastodon puts public only in "cc" not in "to" when the post should not be listed
754 if (($receiver == self::PUBLIC) && ($element == 'to')) {
755 $receivers['uid:0'] = 0;
758 if (($receiver == self::PUBLIC)) {
759 $receivers['uid:-1'] = -1;
762 if (in_array($receiver, [$followers, self::PUBLIC])) {
763 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND]];
764 $contacts = DBA::select('contact', ['uid'], $condition);
765 while ($contact = DBA::fetch($contacts)) {
766 if ($contact['uid'] != 0) {
767 $receivers['uid:' . $contact['uid']] = $contact['uid'];
770 DBA::close($contacts);
774 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
775 $contact = DBA::selectFirst('contact', ['uid'], $condition);
776 if (!DBA::isResult($contact)) {
779 $receivers['cid:' . $contact['uid']] = $contact['uid'];
785 private static function addActivityFields($object_data, $activity)
787 if (!empty($activity['published']) && empty($object_data['published'])) {
788 $object_data['published'] = $activity['published'];
791 if (!empty($activity['updated']) && empty($object_data['updated'])) {
792 $object_data['updated'] = $activity['updated'];
795 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
796 $object_data['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
799 if (!empty($activity['instrument'])) {
800 $object_data['service'] = self::processElement($activity, 'instrument', 'name', 'type', 'Service');
805 private static function fetchObject($object_url, $object = [])
807 $data = self::fetchContent($object_url);
811 logger('Empty content', LOGGER_DEBUG);
813 } elseif (is_string($data)) {
814 logger('No object array provided.', LOGGER_DEBUG);
815 $item = Item::selectFirst([], ['uri' => $data]);
816 if (!DBA::isResult($item)) {
817 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
820 logger('Using already stored item', LOGGER_DEBUG);
821 $data = self::createNote($item);
823 logger('Using provided object', LOGGER_DEBUG);
827 if (empty($data['type'])) {
828 logger('Empty type', LOGGER_DEBUG);
831 $type = $data['type'];
832 logger('Type ' . $type, LOGGER_DEBUG);
835 if (in_array($type, ['Note', 'Article', 'Video'])) {
836 $common = self::processCommonData($data);
841 return array_merge($common, self::processNote($data));
843 return array_merge($common, self::processArticle($data));
845 return array_merge($common, self::processVideo($data));
848 if (empty($data['object'])) {
851 return self::fetchObject($data['object']);
858 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
863 private static function processCommonData(&$object)
865 if (empty($object['id'])) {
870 $object_data['type'] = $object['type'];
871 $object_data['uri'] = $object['id'];
873 if (!empty($object['inReplyTo'])) {
874 $object_data['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
876 $object_data['reply-to-uri'] = $object_data['uri'];
879 $object_data['published'] = defaults($object, 'published', null);
880 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
882 if (empty($object_data['published']) && !empty($object_data['updated'])) {
883 $object_data['published'] = $object_data['updated'];
886 $object_data['uuid'] = defaults($object, 'uuid', null);
887 $object_data['owner'] = $object_data['author'] = self::processElement($object, 'attributedTo', 'id');
888 $object_data['context'] = defaults($object, 'context', null);
889 $object_data['conversation'] = defaults($object, 'conversation', null);
890 $object_data['sensitive'] = defaults($object, 'sensitive', null);
891 $object_data['name'] = defaults($object, 'title', null);
892 $object_data['name'] = defaults($object, 'name', $object_data['name']);
893 $object_data['summary'] = defaults($object, 'summary', null);
894 $object_data['content'] = defaults($object, 'content', null);
895 $object_data['source'] = defaults($object, 'source', null);
896 $object_data['location'] = self::processElement($object, 'location', 'name', 'type', 'Place');
897 $object_data['attachments'] = defaults($object, 'attachment', null);
898 $object_data['tags'] = defaults($object, 'tag', null);
899 $object_data['service'] = self::processElement($object, 'instrument', 'name', 'type', 'Service');
900 $object_data['alternate-url'] = self::processElement($object, 'url', 'href');
901 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
904 // @context, type, actor, signature, mediaType, duration, replies, icon
906 // Also missing: (Defined in the standard, but currently unused)
907 // audience, preview, endTime, startTime, generator, image
912 private static function processNote($object)
917 // emoji, atomUri, inReplyToAtomUri
920 // contentMap, announcement_count, announcements, context_id, likes, like_count
921 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
926 private static function processArticle($object)
933 private static function processVideo($object)
938 // category, licence, language, commentsEnabled
941 // views, waitTranscoding, state, support, subtitleLanguage
942 // likes, dislikes, shares, comments
947 private static function processElement($array, $element, $key, $type = null, $type_value = null)
953 if (empty($array[$element])) {
957 if (is_string($array[$element])) {
958 return $array[$element];
961 if (is_null($type_value)) {
962 if (!empty($array[$element][$key])) {
963 return $array[$element][$key];
966 if (!empty($array[$element][0][$key])) {
967 return $array[$element][0][$key];
973 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
974 return $array[$element][$key];
977 /// @todo Add array search
982 private static function convertMentions($body)
984 $URLSearchString = "^\[\]";
985 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
990 private static function constructTagList($tags, $sensitive)
997 foreach ($tags as $tag) {
998 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
999 if (!empty($tag_text)) {
1003 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1007 /// @todo add nsfw for $sensitive
1012 private static function constructAttachList($attachments, $item)
1014 if (empty($attachments)) {
1018 foreach ($attachments as $attach) {
1019 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1020 if ($filetype == 'image') {
1021 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1023 if (!empty($item["attach"])) {
1024 $item["attach"] .= ',';
1026 $item["attach"] = '';
1028 if (!isset($attach['length'])) {
1029 $attach['length'] = "0";
1031 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1038 private static function createItem($activity, $body)
1041 $item['verb'] = ACTIVITY_POST;
1042 $item['parent-uri'] = $activity['reply-to-uri'];
1044 if ($activity['reply-to-uri'] == $activity['uri']) {
1045 $item['gravity'] = GRAVITY_PARENT;
1046 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1048 $item['gravity'] = GRAVITY_COMMENT;
1049 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1052 self::postItem($activity, $item, $body);
1055 private static function likeItem($activity, $body)
1058 $item['verb'] = ACTIVITY_LIKE;
1059 $item['parent-uri'] = $activity['object'];
1060 $item['gravity'] = GRAVITY_ACTIVITY;
1061 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1063 self::postItem($activity, $item, $body);
1066 private static function postItem($activity, $item, $body)
1068 /// @todo What to do with $activity['context']?
1070 $item['network'] = Protocol::ACTIVITYPUB;
1071 $item['private'] = !in_array(-1, $activity['receiver']);
1072 if (in_array(-1, $activity['receiver'])) {
1073 $item['private'] = 2;
1076 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1077 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1078 $item['uri'] = $activity['uri'];
1079 $item['created'] = $activity['published'];
1080 $item['edited'] = $activity['updated'];
1081 $item['guid'] = $activity['uuid'];
1082 $item['title'] = HTML::toBBCode($activity['name']);
1083 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1084 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1085 $item['location'] = $activity['location'];
1086 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1087 $item['app'] = $activity['service'];
1088 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1090 $item = self::constructAttachList($activity['attachments'], $item);
1092 $source = self::processElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1093 if (!empty($source)) {
1094 $item['body'] = $source;
1097 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1098 $item['source'] = $body;
1099 $item['conversation-uri'] = $activity['conversation'];
1101 foreach ($activity['receiver'] as $receiver) {
1102 if ($receiver < 0) {
1106 $item['uid'] = $receiver;
1107 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1109 if (($receiver != 0) && empty($item['contact-id'])) {
1110 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1113 $item_id = Item::insert($item);
1114 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1118 private static function getUserOfObject($object)
1120 $self = DBA::selectFirst('contact', ['uid'], ['nurl' => normalise_link($object), 'self' => true]);
1121 if (!DBA::isResult(§self)) {
1124 return $self['uid'];
1128 private static function followUser($activity)
1130 $uid = self::getUserOfObject[$activity['object']];
1135 $owner = User::getOwnerDataById($uid);
1137 $cid = Contact::getIdForURL($activity['owner'], $uid);
1139 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1144 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1145 'author-link' => $activity['owner']];
1147 Contact::addRelationship($owner, $contact, $item);
1148 $cid = Contact::getIdForURL($activity['owner'], $uid);
1153 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1154 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1155 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1158 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1159 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1162 private static function acceptFollowUser($activity)
1164 $uid = self::getUserOfObject[$activity['object']];
1169 $owner = User::getOwnerDataById($uid);
1171 $cid = Contact::getIdForURL($activity['owner'], $uid);
1173 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1177 $fields = ['pending' => false];
1179 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1180 if ($contact['rel'] == Contact::FOLLOWER) {
1181 $fields['rel'] = Contact::FRIEND;
1184 $condition = ['id' => $cid];
1185 DBA::update('contact', $fields, $condition);
1186 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1189 private static function undoFollowUser($activity)
1191 $uid = self::getUserOfObject[$activity['object']];
1196 $owner = User::getOwnerDataById($uid);
1198 $cid = Contact::getIdForURL($activity['owner'], $uid);
1200 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1204 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1205 if (!DBA::isResult($contact)) {
1209 Contact::removeFollower($owner, $contact);
1210 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);