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.
42 * - Activities: Undo, Update
43 * - Object Types: Person, Tombstome
46 * - Activities: Like, Dislike, Update, Undo
47 * - Object Tyoes: Article, Announce, Person, Tombstone
50 * - Message distribution
51 * - Endpoints: Outbox, Object, Follower, Following
55 const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
57 public static function transmit($data, $target, $uid)
59 $owner = User::getOwnerDataById($uid);
65 $content = json_encode($data);
67 $host = parse_url($target, PHP_URL_HOST);
68 $path = parse_url($target, PHP_URL_PATH);
71 $headers = ['Host: ' . $host, 'Date: ' . $date];
73 $signed_data = "(request-target): post " . $path . "\nhost: " . $host . "\ndate: " . $date;
75 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
77 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",headers="(request-target) host date",signature="' . $signature . '"';
78 $headers[] = 'Content-Type: application/activity+json';
80 Network::post($target, $content, $headers);
81 $return_code = BaseObject::getApp()->get_curl_code();
83 logger('Transmit to ' . $target . ' returned ' . $return_code);
87 * Return the ActivityPub profile of the given user
89 * @param integer $uid User ID
92 public static function profile($uid)
94 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
96 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
97 'account_removed' => false, 'verified' => true];
98 $fields = ['guid', 'nickname', 'pubkey', 'account-type'];
99 $user = DBA::selectFirst('user', $fields, $condition);
100 if (!DBA::isResult($user)) {
104 $fields = ['locality', 'region', 'country-name'];
105 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
106 if (!DBA::isResult($profile)) {
110 $fields = ['name', 'url', 'location', 'about', 'avatar'];
111 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
112 if (!DBA::isResult($contact)) {
116 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
117 ['uuid' => 'http://schema.org/identifier', 'sensitive' => 'as:sensitive',
118 'vcard' => 'http://www.w3.org/2006/vcard/ns#']]];
120 $data['id'] = $contact['url'];
121 $data['uuid'] = $user['guid'];
122 $data['type'] = $accounttype[$user['account-type']];
123 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
124 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
125 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
126 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
127 $data['preferredUsername'] = $user['nickname'];
128 $data['name'] = $contact['name'];
129 $data['vcard:hasAddress'] = ['@type' => 'Home', 'vcard:country-name' => $profile['country-name'],
130 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
131 $data['summary'] = $contact['about'];
132 $data['url'] = $contact['url'];
133 $data['manuallyApprovesFollowers'] = false;
134 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
135 'owner' => $contact['url'],
136 'publicKeyPem' => $user['pubkey']];
137 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
138 $data['icon'] = ['type' => 'Image',
139 'url' => $contact['avatar']];
141 // tags: https://kitty.town/@inmysocks/100656097926961126.json
145 public static function createActivityFromItem($item_id)
147 $item = Item::selectFirst([], ['id' => $item_id]);
149 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
150 ['Emoji' => 'toot:Emoji', 'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
151 'conversation' => 'ostatus:conversation', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
152 'ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
153 'toot' => 'http://joinmastodon.org/ns#']]];
155 $data['type'] = 'Create';
156 $data['id'] = $item['uri'];
157 $data['actor'] = $item['author-link'];
158 $data['to'] = 'https://www.w3.org/ns/activitystreams#Public';
159 $data['object'] = self::createNote($item);
163 public static function createNote($item)
166 $data['type'] = 'Note';
167 $data['id'] = $item['uri'];
169 if ($item['uri'] != $item['thr-parent']) {
170 $data['inReplyTo'] = $item['thr-parent'];
173 $conversation = DBA::selectFirst('conversation', ['conversation-uri'], ['item-uri' => $item['parent-uri']]);
174 if (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
175 $conversation_uri = $conversation['conversation-uri'];
177 $conversation_uri = $item['parent-uri'];
180 $data['context'] = $data['conversation'] = $conversation_uri;
181 $data['actor'] = $item['author-link'];
183 if (!$item['private']) {
184 $data['to'][] = 'https://www.w3.org/ns/activitystreams#Public';
186 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
187 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
188 $data['attributedTo'] = $item['author-link'];
189 $data['name'] = BBCode::convert($item['title'], false, 7);
190 $data['content'] = BBCode::convert($item['body'], false, 7);
191 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
192 //$data['summary'] = ''; // Ignore by now
193 //$data['sensitive'] = false; // - Query NSFW
194 //$data['emoji'] = []; // Ignore by now
195 //$data['tag'] = []; /// @ToDo
196 //$data['attachment'] = []; // @ToDo
200 public static function transmitActivity($activity, $target, $uid)
202 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
204 $owner = User::getOwnerDataById($uid);
206 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
207 'id' => 'https://pirati.ca/activity/' . System::createGUID(),
209 'actor' => $owner['url'],
210 'object' => $profile['url']];
212 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
213 return self::transmit($data, $profile['notify'], $uid);
216 public static function transmitContactActivity($activity, $target, $id, $uid)
218 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
221 $id = 'https://pirati.ca/activity/' . System::createGUID();
224 $owner = User::getOwnerDataById($uid);
225 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
226 'id' => 'https://pirati.ca/activity/' . System::createGUID(),
228 'actor' => $owner['url'],
229 'object' => ['id' => $id, 'type' => 'Follow',
230 'actor' => $owner['url'],
231 'object' => $profile['url']]];
233 logger('Sending ' . $activity . ' to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
234 return self::transmit($data, $profile['notify'], $uid);
238 * Fetches ActivityPub content from the given url
240 * @param string $url content url
243 public static function fetchContent($url)
245 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json']);
247 if (!$ret['success'] || empty($ret['body'])) {
251 return json_decode($ret['body'], true);
255 * Resolves the profile url from the address by using webfinger
257 * @param string $addr profile address (user@domain.tld)
260 private static function addrToUrl($addr)
262 $addr_parts = explode('@', $addr);
263 if (count($addr_parts) != 2) {
267 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
269 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
270 if (!$ret['success'] || empty($ret['body'])) {
274 $data = json_decode($ret['body'], true);
276 if (empty($data['links'])) {
280 foreach ($data['links'] as $link) {
281 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
285 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
286 return $link['href'];
293 public static function verifySignature($content, $http_headers)
295 $object = json_decode($content, true);
297 if (empty($object)) {
301 $actor = self::processElement($object, 'actor', 'id');
304 $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . $http_headers['REQUEST_URI'];
306 // First take every header
307 foreach ($http_headers as $k => $v) {
308 $field = str_replace('_', '-', strtolower($k));
309 $headers[$field] = $v;
312 // Now add every http header
313 foreach ($http_headers as $k => $v) {
314 if (strpos($k, 'HTTP_') === 0) {
315 $field = str_replace('_', '-', strtolower(substr($k, 5)));
316 $headers[$field] = $v;
320 $sig_block = ActivityPub::parseSigHeader($http_headers['HTTP_SIGNATURE']);
322 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
327 foreach ($sig_block['headers'] as $h) {
328 if (array_key_exists($h, $headers)) {
329 $signed_data .= $h . ': ' . $headers[$h] . "\n";
332 $signed_data = rtrim($signed_data, "\n");
334 if (empty($signed_data)) {
340 if ($sig_block['algorithm'] === 'rsa-sha256') {
341 $algorithm = 'sha256';
344 if ($sig_block['algorithm'] === 'rsa-sha512') {
345 $algorithm = 'sha512';
348 if (empty($algorithm)) {
352 $key = self::fetchKey($sig_block['keyId'], $actor);
358 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm)) {
362 // Check the digest if it was part of the signed data
363 if (in_array('digest', $sig_block['headers'])) {
364 $digest = explode('=', $headers['digest'], 2);
365 if ($digest[0] === 'SHA-256') {
368 if ($digest[0] === 'SHA-512') {
372 /// @todo add all hashes from the rfc
374 if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
379 // Check the content-length if it was part of the signed data
380 if (in_array('content-length', $sig_block['headers'])) {
381 if (strlen($content) != $headers['content-length']) {
390 private static function fetchKey($id, $actor)
392 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
394 $profile = Probe::uri($url, Protocol::ACTIVITYPUB);
395 if (!empty($profile)) {
396 return $profile['pubkey'];
397 } elseif ($url != $actor) {
398 $profile = Probe::uri($actor, Protocol::ACTIVITYPUB);
399 if (!empty($profile)) {
400 return $profile['pubkey'];
410 * @param string $header
411 * @return array associate array with
412 * - \e string \b keyID
413 * - \e string \b algorithm
414 * - \e array \b headers
415 * - \e string \b signature
417 private static function parseSigHeader($header)
422 if (preg_match('/keyId="(.*?)"/ism',$header,$matches)) {
423 $ret['keyId'] = $matches[1];
426 if (preg_match('/algorithm="(.*?)"/ism',$header,$matches)) {
427 $ret['algorithm'] = $matches[1];
430 if (preg_match('/headers="(.*?)"/ism',$header,$matches)) {
431 $ret['headers'] = explode(' ', $matches[1]);
434 if (preg_match('/signature="(.*?)"/ism',$header,$matches)) {
435 $ret['signature'] = base64_decode(preg_replace('/\s+/','',$matches[1]));
442 * Fetches a profile from the given url
444 * @param string $url profile url
447 public static function fetchProfile($url)
449 if (empty(parse_url($url, PHP_URL_SCHEME))) {
450 $url = self::addrToUrl($url);
456 $data = self::fetchContent($url);
458 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
462 $profile = ['network' => Protocol::ACTIVITYPUB];
463 $profile['nick'] = $data['preferredUsername'];
464 $profile['name'] = defaults($data, 'name', $profile['nick']);
465 $profile['guid'] = defaults($data, 'uuid', null);
466 $profile['url'] = $data['id'];
468 $parts = parse_url($profile['url']);
469 unset($parts['scheme']);
470 unset($parts['path']);
471 $profile['addr'] = $profile['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
472 $profile['alias'] = self::processElement($data, 'url', 'href');
473 $profile['photo'] = self::processElement($data, 'icon', 'url');
474 // $profile['community']
475 // $profile['keywords']
476 // $profile['location']
477 $profile['about'] = defaults($data, 'summary', '');
478 $profile['batch'] = self::processElement($data, 'endpoints', 'sharedInbox');
479 $profile['notify'] = $data['inbox'];
480 $profile['poll'] = $data['outbox'];
481 $profile['pubkey'] = self::processElement($data, 'publicKey', 'publicKeyPem');
483 // Check if the address is resolvable
484 if (self::addrToUrl($profile['addr']) == $profile['url']) {
485 $parts = parse_url($profile['url']);
486 unset($parts['path']);
487 $profile['baseurl'] = Network::unparseURL($parts);
489 unset($profile['addr']);
492 if ($profile['url'] == $profile['alias']) {
493 unset($profile['alias']);
496 // Remove all "null" fields
497 foreach ($profile as $field => $content) {
498 if (is_null($content)) {
499 unset($profile[$field]);
505 unset($data['type']);
506 unset($data['manuallyApprovesFollowers']);
509 unset($data['@context']);
511 unset($data['attachment']);
512 unset($data['image']);
513 unset($data['nomadicLocations']);
514 unset($data['signature']);
515 unset($data['following']);
516 unset($data['followers']);
517 unset($data['featured']);
518 unset($data['movedTo']);
519 unset($data['liked']);
520 unset($data['sharedInbox']); // Misskey
521 unset($data['isCat']); // Misskey
522 unset($data['kroeg:blocks']); // Kroeg
523 unset($data['updated']); // Kroeg
528 public static function processInbox($body, $header, $uid)
530 logger('Incoming message for user ' . $uid, LOGGER_DEBUG);
532 if (!self::verifySignature($body, $header)) {
533 logger('Invalid signature, message will be discarded.', LOGGER_DEBUG);
537 $activity = json_decode($body, true);
539 if (!is_array($activity)) {
540 logger('Invalid body.', LOGGER_DEBUG);
544 self::processActivity($activity, $body, $uid);
547 public static function fetchOutbox($url)
549 $data = self::fetchContent($url);
554 if (!empty($data['orderedItems'])) {
555 $items = $data['orderedItems'];
556 } elseif (!empty($data['first']['orderedItems'])) {
557 $items = $data['first']['orderedItems'];
558 } elseif (!empty($data['first'])) {
559 self::fetchOutbox($data['first']);
565 foreach ($items as $activity) {
566 self::processActivity($activity);
570 function processActivity($activity, $body = '', $uid = null)
572 if (empty($activity['type'])) {
573 logger('Empty type', LOGGER_DEBUG);
577 if (empty($activity['object'])) {
578 logger('Empty object', LOGGER_DEBUG);
582 if (empty($activity['actor'])) {
583 logger('Empty actor', LOGGER_DEBUG);
588 $actor = self::processElement($activity, 'actor', 'id');
590 logger('Empty actor - 2', LOGGER_DEBUG);
594 if (is_string($activity['object'])) {
595 $object_url = $activity['object'];
596 } elseif (!empty($activity['object']['id'])) {
597 $object_url = $activity['object']['id'];
599 logger('No object found', LOGGER_DEBUG);
603 // ----------------------------------
606 unset($activity['@context']);
607 unset($activity['id']);
610 unset($activity['title']);
611 unset($activity['atomUri']);
612 unset($activity['context_id']);
613 unset($activity['statusnetConversationId']);
616 unset($activity['context']);
617 unset($activity['location']);
618 unset($activity['signature']);
620 // Fetch all receivers from to, cc, bto and bcc
621 $receivers = self::getReceivers($activity);
623 // When it is a delivery to a personal inbox we add that user to the receivers
625 $owner = User::getOwnerDataById($uid);
626 $additional = [$owner['url'] => $uid];
627 $receivers = array_merge($receivers, $additional);
630 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
632 logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
634 // Fetch the content only on activities where this matters
635 if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) {
636 $item = self::fetchObject($object_url, $activity['object']);
638 logger("Object data couldn't be processed", LOGGER_DEBUG);
642 if (in_array($activity['type'], ['Accept'])) {
643 $item['object'] = self::processElement($activity, 'object', 'actor', 'type', 'Follow');
644 } elseif (in_array($activity['type'], ['Undo'])) {
645 $item['object'] = self::processElement($activity, 'object', 'object', 'type', 'Follow');
647 $item['object'] = $object_url;
649 $item['id'] = $activity['id'];
650 $item['receiver'] = [];
651 $item['type'] = $activity['type'];
654 $item = self::addActivityFields($item, $activity);
656 $item['owner'] = $actor;
658 $item['receiver'] = array_merge($item['receiver'], $receivers);
660 switch ($activity['type']) {
664 self::createItem($item, $body);
668 self::likeItem($item, $body);
678 self::followUser($item);
682 self::acceptFollowUser($item);
686 self::undoFollowUser($item);
690 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
695 private static function getReceivers($activity)
699 $elements = ['to', 'cc', 'bto', 'bcc'];
700 foreach ($elements as $element) {
701 if (empty($activity[$element])) {
705 // The receiver can be an arror or a string
706 if (is_string($activity[$element])) {
707 $activity[$element] = [$activity[$element]];
710 foreach ($activity[$element] as $receiver) {
711 if ($receiver == self::PUBLIC) {
712 $receivers[$receiver] = 0;
715 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
716 $contact = DBA::selectFirst('contact', ['uid'], $condition);
717 if (!DBA::isResult($contact)) {
720 $receivers[$receiver] = $contact['uid'];
726 private static function addActivityFields($item, $activity)
728 if (!empty($activity['published']) && empty($item['published'])) {
729 $item['published'] = $activity['published'];
732 if (!empty($activity['updated']) && empty($item['updated'])) {
733 $item['updated'] = $activity['updated'];
736 if (!empty($activity['inReplyTo']) && empty($item['parent-uri'])) {
737 $item['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
740 if (!empty($activity['instrument'])) {
741 $item['service'] = self::processElement($activity, 'instrument', 'name', 'type', 'Service');
746 private static function fetchObject($object_url, $object = [])
748 $data = self::fetchContent($object_url);
752 logger('Empty content', LOGGER_DEBUG);
754 } elseif (is_string($data)) {
755 logger('No object array provided.', LOGGER_DEBUG);
756 $item = Item::selectFirst([], ['uri' => $data]);
757 if (!DBA::isResult($item)) {
758 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
761 logger('Using already stored item', LOGGER_DEBUG);
762 $data = self::createNote($item);
764 logger('Using provided object', LOGGER_DEBUG);
768 if (empty($data['type'])) {
769 logger('Empty type', LOGGER_DEBUG);
772 $type = $data['type'];
773 logger('Type ' . $type, LOGGER_DEBUG);
776 if (in_array($type, ['Note', 'Article', 'Video'])) {
777 $common = self::processCommonData($data);
782 return array_merge($common, self::processNote($data));
784 return array_merge($common, self::processArticle($data));
786 return array_merge($common, self::processVideo($data));
789 if (empty($data['object'])) {
792 return self::fetchObject($data['object']);
799 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
804 private static function processCommonData(&$object)
806 if (empty($object['id']) || empty($object['attributedTo'])) {
811 $item['type'] = $object['type'];
812 $item['uri'] = $object['id'];
814 if (!empty($object['inReplyTo'])) {
815 $item['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
817 $item['reply-to-uri'] = $item['uri'];
820 $item['published'] = defaults($object, 'published', null);
821 $item['updated'] = defaults($object, 'updated', $item['published']);
823 if (empty($item['published']) && !empty($item['updated'])) {
824 $item['published'] = $item['updated'];
827 $item['uuid'] = defaults($object, 'uuid', null);
828 $item['owner'] = $item['author'] = self::processElement($object, 'attributedTo', 'id');
829 $item['context'] = defaults($object, 'context', null);
830 $item['conversation'] = defaults($object, 'conversation', null);
831 $item['sensitive'] = defaults($object, 'sensitive', null);
832 $item['name'] = defaults($object, 'title', null);
833 $item['name'] = defaults($object, 'name', $item['name']);
834 $item['summary'] = defaults($object, 'summary', null);
835 $item['content'] = defaults($object, 'content', null);
836 $item['source'] = defaults($object, 'source', null);
837 $item['location'] = self::processElement($object, 'location', 'name', 'type', 'Place');
838 $item['attachments'] = defaults($object, 'attachment', null);
839 $item['tags'] = defaults($object, 'tag', null);
840 $item['service'] = self::processElement($object, 'instrument', 'name', 'type', 'Service');
841 $item['alternate-url'] = self::processElement($object, 'url', 'href');
842 $item['receiver'] = self::getReceivers($object);
846 unset($object['source']);
849 unset($object['@context']);
850 unset($object['type']);
851 unset($object['actor']);
852 unset($object['signature']);
853 unset($object['mediaType']);
854 unset($object['duration']);
855 unset($object['replies']);
856 unset($object['icon']);
859 audience, preview, endTime, startTime, generator, image
864 private static function processNote($object)
870 unset($object['emoji']);
871 unset($object['atomUri']);
872 unset($object['inReplyToAtomUri']);
875 unset($object['contentMap']);
876 unset($object['announcement_count']);
877 unset($object['announcements']);
878 unset($object['context_id']);
879 unset($object['likes']);
880 unset($object['like_count']);
881 unset($object['inReplyToStatusId']);
882 unset($object['shares']);
883 unset($object['quoteUrl']);
884 unset($object['statusnetConversationId']);
889 private static function processArticle($object)
896 private static function processVideo($object)
901 unset($object['category']);
902 unset($object['licence']);
903 unset($object['language']);
904 unset($object['commentsEnabled']);
907 unset($object['views']);
908 unset($object['waitTranscoding']);
909 unset($object['state']);
910 unset($object['support']);
911 unset($object['subtitleLanguage']);
912 unset($object['likes']);
913 unset($object['dislikes']);
914 unset($object['shares']);
915 unset($object['comments']);
920 private static function processElement($array, $element, $key, $type = null, $type_value = null)
926 if (empty($array[$element])) {
930 if (is_string($array[$element])) {
931 return $array[$element];
934 if (is_null($type_value)) {
935 if (!empty($array[$element][$key])) {
936 return $array[$element][$key];
939 if (!empty($array[$element][0][$key])) {
940 return $array[$element][0][$key];
946 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
947 return $array[$element][$key];
950 /// @todo Add array search
955 private static function convertMentions($body)
957 $URLSearchString = "^\[\]";
958 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
963 private static function constructTagList($tags, $sensitive)
970 foreach ($tags as $tag) {
971 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
972 if (!empty($tag_text)) {
976 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
980 /// @todo add nsfw for $sensitive
985 private static function constructAttachList($attachments, $item)
987 if (empty($attachments)) {
991 foreach ($attachments as $attach) {
992 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
993 if ($filetype == 'image') {
994 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
996 if (!empty($item["attach"])) {
997 $item["attach"] .= ',';
999 $item["attach"] = '';
1001 if (!isset($attach['length'])) {
1002 $attach['length'] = "0";
1004 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1011 private static function createItem($activity, $body)
1014 $item['verb'] = ACTIVITY_POST;
1015 $item['parent-uri'] = $activity['reply-to-uri'];
1017 if ($activity['reply-to-uri'] == $activity['uri']) {
1018 $item['gravity'] = GRAVITY_PARENT;
1019 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1021 $item['gravity'] = GRAVITY_COMMENT;
1022 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1025 self::postItem($activity, $item, $body);
1028 private static function likeItem($activity, $body)
1031 $item['verb'] = ACTIVITY_LIKE;
1032 $item['parent-uri'] = $activity['object'];
1033 $item['gravity'] = GRAVITY_ACTIVITY;
1034 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1036 self::postItem($activity, $item, $body);
1039 private static function postItem($activity, $item, $body)
1041 /// @todo What to do with $activity['context']?
1043 $item['network'] = Protocol::ACTIVITYPUB;
1044 $item['private'] = !in_array(0, $activity['receiver']);
1045 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1046 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1047 $item['uri'] = $activity['uri'];
1048 $item['created'] = $activity['published'];
1049 $item['edited'] = $activity['updated'];
1050 $item['guid'] = $activity['uuid'];
1051 $item['title'] = HTML::toBBCode($activity['name']);
1052 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1053 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1054 $item['location'] = $activity['location'];
1055 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1056 $item['app'] = $activity['service'];
1057 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1059 $item = self::constructAttachList($activity['attachments'], $item);
1061 $source = self::processElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1062 if (!empty($source)) {
1063 $item['body'] = $source;
1066 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1067 $item['source'] = $body;
1068 $item['conversation-uri'] = $activity['conversation'];
1070 foreach ($activity['receiver'] as $receiver) {
1071 $item['uid'] = $receiver;
1072 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1074 if (($receiver != 0) && empty($item['contact-id'])) {
1075 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1078 $item_id = Item::insert($item);
1079 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1080 if (!empty($item_id) && ($item['uid'] == 0)) {
1081 Item::distribute($item_id);
1086 private static function followUser($activity)
1088 if (empty($activity['receiver'][$activity['object']])) {
1092 $uid = $activity['receiver'][$activity['object']];
1093 $owner = User::getOwnerDataById($uid);
1095 $cid = Contact::getIdForURL($activity['owner'], $uid);
1097 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1102 $item = ['author-id' => Contact::getIdForURL($activity['owner'])];
1104 Contact::addRelationship($owner, $contact, $item);
1106 $cid = Contact::getIdForURL($activity['owner'], $uid);
1111 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1112 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1113 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1116 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1117 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1120 private static function acceptFollowUser($activity)
1122 if (empty($activity['receiver'][$activity['object']])) {
1126 $uid = $activity['receiver'][$activity['object']];
1127 $owner = User::getOwnerDataById($uid);
1129 $cid = Contact::getIdForURL($activity['owner'], $uid);
1131 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1135 $fields = ['pending' => false];
1136 $condition = ['id' => $cid, 'pending' => true];
1137 DBA::update('comtact', $fields, $condition);
1138 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1141 private static function undoFollowUser($activity)
1143 if (empty($activity['receiver'][$activity['object']])) {
1147 $uid = $activity['receiver'][$activity['object']];
1148 $owner = User::getOwnerDataById($uid);
1150 $cid = Contact::getIdForURL($activity['owner'], $uid);
1152 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1156 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1157 if (!DBA::isResult($contact)) {
1161 Contact::removeFollower($owner, $contact);
1162 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);