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 $host = parse_url($target, PHP_URL_HOST);
69 $path = parse_url($target, PHP_URL_PATH);
72 $headers = ['Host: ' . $host, 'Date: ' . $date];
74 $signed_data = "(request-target): post " . $path . "\nhost: " . $host . "\ndate: " . $date;
76 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
78 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",headers="(request-target) host date",signature="' . $signature . '"';
79 $headers[] = 'Content-Type: application/activity+json';
81 Network::post($target, $content, $headers);
82 $return_code = BaseObject::getApp()->get_curl_code();
84 logger('Transmit to ' . $target . ' returned ' . $return_code);
88 * Return the ActivityPub profile of the given user
90 * @param integer $uid User ID
93 public static function profile($uid)
95 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
97 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
98 'account_removed' => false, 'verified' => true];
99 $fields = ['guid', 'nickname', 'pubkey', 'account-type'];
100 $user = DBA::selectFirst('user', $fields, $condition);
101 if (!DBA::isResult($user)) {
105 $fields = ['locality', 'region', 'country-name'];
106 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
107 if (!DBA::isResult($profile)) {
111 $fields = ['name', 'url', 'location', 'about', 'avatar'];
112 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
113 if (!DBA::isResult($contact)) {
117 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
118 ['uuid' => 'http://schema.org/identifier', 'sensitive' => 'as:sensitive',
119 'vcard' => 'http://www.w3.org/2006/vcard/ns#']]];
121 $data['id'] = $contact['url'];
122 $data['uuid'] = $user['guid'];
123 $data['type'] = $accounttype[$user['account-type']];
124 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
125 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
126 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
127 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
128 $data['preferredUsername'] = $user['nickname'];
129 $data['name'] = $contact['name'];
130 $data['vcard:hasAddress'] = ['@type' => 'Home', 'vcard:country-name' => $profile['country-name'],
131 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
132 $data['summary'] = $contact['about'];
133 $data['url'] = $contact['url'];
134 $data['manuallyApprovesFollowers'] = false; /// @todo
135 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
136 'owner' => $contact['url'],
137 'publicKeyPem' => $user['pubkey']];
138 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
139 $data['icon'] = ['type' => 'Image',
140 'url' => $contact['avatar']];
142 // tags: https://kitty.town/@inmysocks/100656097926961126.json
146 public static function createActivityFromItem($item_id)
148 $item = Item::selectFirst([], ['id' => $item_id]);
150 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
151 ['Emoji' => 'toot:Emoji', 'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
152 'conversation' => 'ostatus:conversation', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
153 'ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
154 'toot' => 'http://joinmastodon.org/ns#']]];
156 $data['type'] = 'Create';
157 $data['id'] = $item['uri'];
158 $data['actor'] = $item['author-link'];
159 $data['to'] = 'https://www.w3.org/ns/activitystreams#Public';
160 $data['object'] = self::createNote($item);
164 public static function createNote($item)
167 $data['type'] = 'Note';
168 $data['id'] = $item['uri'];
170 if ($item['uri'] != $item['thr-parent']) {
171 $data['inReplyTo'] = $item['thr-parent'];
174 $conversation = DBA::selectFirst('conversation', ['conversation-uri'], ['item-uri' => $item['parent-uri']]);
175 if (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
176 $conversation_uri = $conversation['conversation-uri'];
178 $conversation_uri = $item['parent-uri'];
181 $data['context'] = $data['conversation'] = $conversation_uri;
182 $data['actor'] = $item['author-link'];
184 if (!$item['private']) {
185 $data['to'][] = 'https://www.w3.org/ns/activitystreams#Public';
187 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
188 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
189 $data['attributedTo'] = $item['author-link'];
190 $data['name'] = BBCode::convert($item['title'], false, 7);
191 $data['content'] = BBCode::convert($item['body'], false, 7);
192 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
193 //$data['summary'] = ''; // Ignore by now
194 //$data['sensitive'] = false; // - Query NSFW
195 //$data['emoji'] = []; // Ignore by now
196 //$data['tag'] = []; /// @ToDo
197 //$data['attachment'] = []; // @ToDo
201 public static function transmitActivity($activity, $target, $uid)
203 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
205 $owner = User::getOwnerDataById($uid);
207 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
208 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
210 'actor' => $owner['url'],
211 'object' => $profile['url']];
213 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
214 return self::transmit($data, $profile['notify'], $uid);
217 public static function transmitContactAccept($target, $id, $uid)
219 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
221 $owner = User::getOwnerDataById($uid);
222 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
223 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
225 'actor' => $owner['url'],
226 'object' => ['id' => $id, 'type' => 'Follow',
227 'actor' => $profile['url'],
228 'object' => $owner['url']]];
230 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
231 return self::transmit($data, $profile['notify'], $uid);
234 public static function transmitContactReject($target, $id, $uid)
236 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
238 $owner = User::getOwnerDataById($uid);
239 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
240 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
242 'actor' => $owner['url'],
243 'object' => ['id' => $id, 'type' => 'Follow',
244 'actor' => $profile['url'],
245 'object' => $owner['url']]];
247 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
248 return self::transmit($data, $profile['notify'], $uid);
251 public static function transmitContactUndo($target, $uid)
253 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
255 $id = System::baseUrl() . '/activity/' . System::createGUID();
257 $owner = User::getOwnerDataById($uid);
258 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
261 'actor' => $owner['url'],
262 'object' => ['id' => $id, 'type' => 'Follow',
263 'actor' => $owner['url'],
264 'object' => $profile['url']]];
266 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
267 return self::transmit($data, $profile['notify'], $uid);
271 * Fetches ActivityPub content from the given url
273 * @param string $url content url
276 public static function fetchContent($url)
278 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json']);
280 if (!$ret['success'] || empty($ret['body'])) {
284 return json_decode($ret['body'], true);
288 * Resolves the profile url from the address by using webfinger
290 * @param string $addr profile address (user@domain.tld)
293 private static function addrToUrl($addr)
295 $addr_parts = explode('@', $addr);
296 if (count($addr_parts) != 2) {
300 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
302 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
303 if (!$ret['success'] || empty($ret['body'])) {
307 $data = json_decode($ret['body'], true);
309 if (empty($data['links'])) {
313 foreach ($data['links'] as $link) {
314 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
318 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
319 return $link['href'];
326 public static function verifySignature($content, $http_headers)
328 $object = json_decode($content, true);
330 if (empty($object)) {
334 $actor = self::processElement($object, 'actor', 'id');
337 $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . $http_headers['REQUEST_URI'];
339 // First take every header
340 foreach ($http_headers as $k => $v) {
341 $field = str_replace('_', '-', strtolower($k));
342 $headers[$field] = $v;
345 // Now add every http header
346 foreach ($http_headers as $k => $v) {
347 if (strpos($k, 'HTTP_') === 0) {
348 $field = str_replace('_', '-', strtolower(substr($k, 5)));
349 $headers[$field] = $v;
353 $sig_block = ActivityPub::parseSigHeader($http_headers['HTTP_SIGNATURE']);
355 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
360 foreach ($sig_block['headers'] as $h) {
361 if (array_key_exists($h, $headers)) {
362 $signed_data .= $h . ': ' . $headers[$h] . "\n";
365 $signed_data = rtrim($signed_data, "\n");
367 if (empty($signed_data)) {
373 if ($sig_block['algorithm'] === 'rsa-sha256') {
374 $algorithm = 'sha256';
377 if ($sig_block['algorithm'] === 'rsa-sha512') {
378 $algorithm = 'sha512';
381 if (empty($algorithm)) {
385 $key = self::fetchKey($sig_block['keyId'], $actor);
391 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm)) {
395 // Check the digest if it was part of the signed data
396 if (in_array('digest', $sig_block['headers'])) {
397 $digest = explode('=', $headers['digest'], 2);
398 if ($digest[0] === 'SHA-256') {
401 if ($digest[0] === 'SHA-512') {
405 /// @todo add all hashes from the rfc
407 if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
412 // Check the content-length if it was part of the signed data
413 if (in_array('content-length', $sig_block['headers'])) {
414 if (strlen($content) != $headers['content-length']) {
423 private static function fetchKey($id, $actor)
425 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
427 $profile = Probe::uri($url, Protocol::ACTIVITYPUB);
428 if (!empty($profile)) {
429 return $profile['pubkey'];
430 } elseif ($url != $actor) {
431 $profile = Probe::uri($actor, Protocol::ACTIVITYPUB);
432 if (!empty($profile)) {
433 return $profile['pubkey'];
443 * @param string $header
444 * @return array associate array with
445 * - \e string \b keyID
446 * - \e string \b algorithm
447 * - \e array \b headers
448 * - \e string \b signature
450 private static function parseSigHeader($header)
455 if (preg_match('/keyId="(.*?)"/ism',$header,$matches)) {
456 $ret['keyId'] = $matches[1];
459 if (preg_match('/algorithm="(.*?)"/ism',$header,$matches)) {
460 $ret['algorithm'] = $matches[1];
463 if (preg_match('/headers="(.*?)"/ism',$header,$matches)) {
464 $ret['headers'] = explode(' ', $matches[1]);
467 if (preg_match('/signature="(.*?)"/ism',$header,$matches)) {
468 $ret['signature'] = base64_decode(preg_replace('/\s+/','',$matches[1]));
475 * Fetches a profile from the given url
477 * @param string $url profile url
480 public static function fetchProfile($url)
482 if (empty(parse_url($url, PHP_URL_SCHEME))) {
483 $url = self::addrToUrl($url);
489 $data = self::fetchContent($url);
491 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
495 $profile = ['network' => Protocol::ACTIVITYPUB];
496 $profile['nick'] = $data['preferredUsername'];
497 $profile['name'] = defaults($data, 'name', $profile['nick']);
498 $profile['guid'] = defaults($data, 'uuid', null);
499 $profile['url'] = $data['id'];
501 $parts = parse_url($profile['url']);
502 unset($parts['scheme']);
503 unset($parts['path']);
504 $profile['addr'] = $profile['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
505 $profile['alias'] = self::processElement($data, 'url', 'href');
506 $profile['photo'] = self::processElement($data, 'icon', 'url');
507 // $profile['community']
508 // $profile['keywords']
509 // $profile['location']
510 $profile['about'] = defaults($data, 'summary', '');
511 $profile['batch'] = self::processElement($data, 'endpoints', 'sharedInbox');
512 $profile['notify'] = $data['inbox'];
513 $profile['poll'] = $data['outbox'];
514 $profile['pubkey'] = self::processElement($data, 'publicKey', 'publicKeyPem');
516 // Check if the address is resolvable
517 if (self::addrToUrl($profile['addr']) == $profile['url']) {
518 $parts = parse_url($profile['url']);
519 unset($parts['path']);
520 $profile['baseurl'] = Network::unparseURL($parts);
522 unset($profile['addr']);
525 if ($profile['url'] == $profile['alias']) {
526 unset($profile['alias']);
529 // Remove all "null" fields
530 foreach ($profile as $field => $content) {
531 if (is_null($content)) {
532 unset($profile[$field]);
537 // type, manuallyApprovesFollowers
540 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
542 // Unhandled from Misskey
543 // sharedInbox, isCat
545 // Unhandled from Kroeg
546 // kroeg:blocks, updated
551 public static function processInbox($body, $header, $uid)
553 logger('Incoming message for user ' . $uid, LOGGER_DEBUG);
555 if (!self::verifySignature($body, $header)) {
556 logger('Invalid signature, message will be discarded.', LOGGER_DEBUG);
560 $activity = json_decode($body, true);
562 if (!is_array($activity)) {
563 logger('Invalid body.', LOGGER_DEBUG);
567 self::processActivity($activity, $body, $uid);
570 public static function fetchOutbox($url)
572 $data = self::fetchContent($url);
577 if (!empty($data['orderedItems'])) {
578 $items = $data['orderedItems'];
579 } elseif (!empty($data['first']['orderedItems'])) {
580 $items = $data['first']['orderedItems'];
581 } elseif (!empty($data['first'])) {
582 self::fetchOutbox($data['first']);
588 foreach ($items as $activity) {
589 self::processActivity($activity);
593 private static function prepareObjectData($activity, $uid)
595 $actor = self::processElement($activity, 'actor', 'id');
597 logger('Empty actor', LOGGER_DEBUG);
601 // Fetch all receivers from to, cc, bto and bcc
602 $receivers = self::getReceivers($activity);
604 // When it is a delivery to a personal inbox we add that user to the receivers
606 $owner = User::getOwnerDataById($uid);
607 $additional = [$owner['url'] => $uid];
608 $receivers = array_merge($receivers, $additional);
611 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
613 if (is_string($activity['object'])) {
614 $object_url = $activity['object'];
615 } elseif (!empty($activity['object']['id'])) {
616 $object_url = $activity['object']['id'];
618 logger('No object found', LOGGER_DEBUG);
622 // Fetch the content only on activities where this matters
623 if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) {
624 $object_data = self::fetchObject($object_url, $activity['object']);
625 if (empty($object_data)) {
626 logger("Object data couldn't be processed", LOGGER_DEBUG);
629 } elseif ($activity['type'] == 'Accept') {
631 $object_data['object_type'] = self::processElement($activity, 'object', 'type');
632 $object_data['object'] = self::processElement($activity, 'object', 'actor');
633 } elseif ($activity['type'] == 'Undo') {
635 $object_data['object_type'] = self::processElement($activity, 'object', 'type');
636 $object_data['object'] = self::processElement($activity, 'object', 'object');
637 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
638 // Create a mostly empty array out of the activity data (instead of the object).
639 // This way we later don't have to check for the existence of ech individual array element.
640 $object_data = self::processCommonData($activity);
641 $object_data['name'] = $activity['type'];
642 $object_data['author'] = $activity['actor'];
643 $object_data['object'] = $object_url;
644 } elseif ($activity['type'] == 'Follow') {
645 $object_data['id'] = $activity['id'];
646 $object_data['object'] = $object_url;
649 $object_data = self::addActivityFields($object_data, $activity);
651 $object_data['type'] = $activity['type'];
652 $object_data['owner'] = $actor;
653 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
658 private static function processActivity($activity, $body = '', $uid = null)
660 if (empty($activity['type'])) {
661 logger('Empty type', LOGGER_DEBUG);
665 if (empty($activity['object'])) {
666 logger('Empty object', LOGGER_DEBUG);
670 if (empty($activity['actor'])) {
671 logger('Empty actor', LOGGER_DEBUG);
677 // title, atomUri, context_id, statusnetConversationId
680 // context, location, signature;
682 logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
684 $object_data = self::prepareObjectData($activity, $uid);
685 if (empty($object_data)) {
686 logger('No object data found', LOGGER_DEBUG);
690 switch ($activity['type']) {
693 self::createItem($object_data, $body);
697 self::likeItem($object_data, $body);
710 self::followUser($object_data);
714 if ($object_data['object_type'] == 'Follow') {
715 self::acceptFollowUser($object_data);
720 if ($object_data['object_type'] == 'Follow') {
721 self::undoFollowUser($object_data);
726 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
731 private static function getReceivers($activity)
735 $elements = ['to', 'cc', 'bto', 'bcc'];
736 foreach ($elements as $element) {
737 if (empty($activity[$element])) {
741 // The receiver can be an arror or a string
742 if (is_string($activity[$element])) {
743 $activity[$element] = [$activity[$element]];
746 foreach ($activity[$element] as $receiver) {
747 if ($receiver == self::PUBLIC) {
748 $receivers[$receiver] = 0;
751 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
752 $contact = DBA::selectFirst('contact', ['uid'], $condition);
753 if (!DBA::isResult($contact)) {
756 $receivers[$receiver] = $contact['uid'];
762 private static function addActivityFields($object_data, $activity)
764 if (!empty($activity['published']) && empty($object_data['published'])) {
765 $object_data['published'] = $activity['published'];
768 if (!empty($activity['updated']) && empty($object_data['updated'])) {
769 $object_data['updated'] = $activity['updated'];
772 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
773 $object_data['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
776 if (!empty($activity['instrument'])) {
777 $object_data['service'] = self::processElement($activity, 'instrument', 'name', 'type', 'Service');
782 private static function fetchObject($object_url, $object = [])
784 $data = self::fetchContent($object_url);
788 logger('Empty content', LOGGER_DEBUG);
790 } elseif (is_string($data)) {
791 logger('No object array provided.', LOGGER_DEBUG);
792 $item = Item::selectFirst([], ['uri' => $data]);
793 if (!DBA::isResult($item)) {
794 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
797 logger('Using already stored item', LOGGER_DEBUG);
798 $data = self::createNote($item);
800 logger('Using provided object', LOGGER_DEBUG);
804 if (empty($data['type'])) {
805 logger('Empty type', LOGGER_DEBUG);
808 $type = $data['type'];
809 logger('Type ' . $type, LOGGER_DEBUG);
812 if (in_array($type, ['Note', 'Article', 'Video'])) {
813 $common = self::processCommonData($data);
818 return array_merge($common, self::processNote($data));
820 return array_merge($common, self::processArticle($data));
822 return array_merge($common, self::processVideo($data));
825 if (empty($data['object'])) {
828 return self::fetchObject($data['object']);
835 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
840 private static function processCommonData(&$object)
842 if (empty($object['id'])) {
847 $object_data['type'] = $object['type'];
848 $object_data['uri'] = $object['id'];
850 if (!empty($object['inReplyTo'])) {
851 $object_data['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
853 $object_data['reply-to-uri'] = $object_data['uri'];
856 $object_data['published'] = defaults($object, 'published', null);
857 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
859 if (empty($object_data['published']) && !empty($object_data['updated'])) {
860 $object_data['published'] = $object_data['updated'];
863 $object_data['uuid'] = defaults($object, 'uuid', null);
864 $object_data['owner'] = $object_data['author'] = self::processElement($object, 'attributedTo', 'id');
865 $object_data['context'] = defaults($object, 'context', null);
866 $object_data['conversation'] = defaults($object, 'conversation', null);
867 $object_data['sensitive'] = defaults($object, 'sensitive', null);
868 $object_data['name'] = defaults($object, 'title', null);
869 $object_data['name'] = defaults($object, 'name', $object_data['name']);
870 $object_data['summary'] = defaults($object, 'summary', null);
871 $object_data['content'] = defaults($object, 'content', null);
872 $object_data['source'] = defaults($object, 'source', null);
873 $object_data['location'] = self::processElement($object, 'location', 'name', 'type', 'Place');
874 $object_data['attachments'] = defaults($object, 'attachment', null);
875 $object_data['tags'] = defaults($object, 'tag', null);
876 $object_data['service'] = self::processElement($object, 'instrument', 'name', 'type', 'Service');
877 $object_data['alternate-url'] = self::processElement($object, 'url', 'href');
878 $object_data['receiver'] = self::getReceivers($object);
881 // @context, type, actor, signature, mediaType, duration, replies, icon
883 // Also missing: (Defined in the standard, but currently unused)
884 // audience, preview, endTime, startTime, generator, image
889 private static function processNote($object)
894 // emoji, atomUri, inReplyToAtomUri
897 // contentMap, announcement_count, announcements, context_id, likes, like_count
898 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
903 private static function processArticle($object)
910 private static function processVideo($object)
915 // category, licence, language, commentsEnabled
918 // views, waitTranscoding, state, support, subtitleLanguage
919 // likes, dislikes, shares, comments
924 private static function processElement($array, $element, $key, $type = null, $type_value = null)
930 if (empty($array[$element])) {
934 if (is_string($array[$element])) {
935 return $array[$element];
938 if (is_null($type_value)) {
939 if (!empty($array[$element][$key])) {
940 return $array[$element][$key];
943 if (!empty($array[$element][0][$key])) {
944 return $array[$element][0][$key];
950 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
951 return $array[$element][$key];
954 /// @todo Add array search
959 private static function convertMentions($body)
961 $URLSearchString = "^\[\]";
962 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
967 private static function constructTagList($tags, $sensitive)
974 foreach ($tags as $tag) {
975 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
976 if (!empty($tag_text)) {
980 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
984 /// @todo add nsfw for $sensitive
989 private static function constructAttachList($attachments, $item)
991 if (empty($attachments)) {
995 foreach ($attachments as $attach) {
996 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
997 if ($filetype == 'image') {
998 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1000 if (!empty($item["attach"])) {
1001 $item["attach"] .= ',';
1003 $item["attach"] = '';
1005 if (!isset($attach['length'])) {
1006 $attach['length'] = "0";
1008 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1015 private static function createItem($activity, $body)
1018 $item['verb'] = ACTIVITY_POST;
1019 $item['parent-uri'] = $activity['reply-to-uri'];
1021 if ($activity['reply-to-uri'] == $activity['uri']) {
1022 $item['gravity'] = GRAVITY_PARENT;
1023 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1025 $item['gravity'] = GRAVITY_COMMENT;
1026 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1029 self::postItem($activity, $item, $body);
1032 private static function likeItem($activity, $body)
1035 $item['verb'] = ACTIVITY_LIKE;
1036 $item['parent-uri'] = $activity['object'];
1037 $item['gravity'] = GRAVITY_ACTIVITY;
1038 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1040 self::postItem($activity, $item, $body);
1043 private static function postItem($activity, $item, $body)
1045 /// @todo What to do with $activity['context']?
1047 $item['network'] = Protocol::ACTIVITYPUB;
1048 $item['private'] = !in_array(0, $activity['receiver']);
1049 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1050 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1051 $item['uri'] = $activity['uri'];
1052 $item['created'] = $activity['published'];
1053 $item['edited'] = $activity['updated'];
1054 $item['guid'] = $activity['uuid'];
1055 $item['title'] = HTML::toBBCode($activity['name']);
1056 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1057 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1058 $item['location'] = $activity['location'];
1059 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1060 $item['app'] = $activity['service'];
1061 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1063 $item = self::constructAttachList($activity['attachments'], $item);
1065 $source = self::processElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1066 if (!empty($source)) {
1067 $item['body'] = $source;
1070 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1071 $item['source'] = $body;
1072 $item['conversation-uri'] = $activity['conversation'];
1074 foreach ($activity['receiver'] as $receiver) {
1075 $item['uid'] = $receiver;
1076 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1078 if (($receiver != 0) && empty($item['contact-id'])) {
1079 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1082 $item_id = Item::insert($item);
1083 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1084 if (!empty($item_id) && ($item['uid'] == 0)) {
1085 Item::distribute($item_id);
1090 private static function followUser($activity)
1092 if (empty($activity['receiver'][$activity['object']])) {
1096 $uid = $activity['receiver'][$activity['object']];
1097 $owner = User::getOwnerDataById($uid);
1099 $cid = Contact::getIdForURL($activity['owner'], $uid);
1101 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1106 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1107 'author-link' => $activity['owner']];
1109 Contact::addRelationship($owner, $contact, $item);
1110 $cid = Contact::getIdForURL($activity['owner'], $uid);
1115 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1116 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1117 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1120 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1121 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1124 private static function acceptFollowUser($activity)
1126 if (empty($activity['receiver'][$activity['object']])) {
1130 $uid = $activity['receiver'][$activity['object']];
1131 $owner = User::getOwnerDataById($uid);
1133 $cid = Contact::getIdForURL($activity['owner'], $uid);
1135 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1139 $fields = ['pending' => false];
1141 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1142 if ($contact['rel'] == Contact::FOLLOWER) {
1143 $fields['rel'] = Contact::FRIEND;
1146 $condition = ['id' => $cid];
1147 DBA::update('contact', $fields, $condition);
1148 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1151 private static function undoFollowUser($activity)
1153 if (empty($activity['receiver'][$activity['object']])) {
1157 $uid = $activity['receiver'][$activity['object']];
1158 $owner = User::getOwnerDataById($uid);
1160 $cid = Contact::getIdForURL($activity['owner'], $uid);
1162 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1166 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1167 if (!DBA::isResult($contact)) {
1171 Contact::removeFollower($owner, $contact);
1172 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);