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
39 * - Activities: Follow, Accept, Update
40 * - Object Types: Person, Tombstome
43 * - Activities: Like, Dislike, Follow, Accept, Update
44 * - Object Tyoes: Article, Announce, Person, Tombstone
47 * - Message distribution
48 * - Endpoints: Outbox, Object, Follower, Following
52 const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
54 public static function transmit($data, $target, $uid)
56 $owner = User::getOwnerDataById($uid);
62 $content = json_encode($data);
64 $host = parse_url($target, PHP_URL_HOST);
65 $path = parse_url($target, PHP_URL_PATH);
68 $headers = ['Host: ' . $host, 'Date: ' . $date];
70 $signed_data = "(request-target): post " . $path . "\nhost: " . $host . "\ndate: " . $date;
72 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
74 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",headers="(request-target) host date",signature="' . $signature . '"';
75 $headers[] = 'Content-Type: application/activity+json';
77 Network::post($target, $content, $headers);
78 $return_code = BaseObject::getApp()->get_curl_code();
80 echo $return_code."\n";
84 * Return the ActivityPub profile of the given user
86 * @param integer $uid User ID
89 public static function profile($uid)
91 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
93 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
94 'account_removed' => false, 'verified' => true];
95 $fields = ['guid', 'nickname', 'pubkey', 'account-type'];
96 $user = DBA::selectFirst('user', $fields, $condition);
97 if (!DBA::isResult($user)) {
101 $fields = ['locality', 'region', 'country-name'];
102 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
103 if (!DBA::isResult($profile)) {
107 $fields = ['name', 'url', 'location', 'about', 'avatar'];
108 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
109 if (!DBA::isResult($contact)) {
113 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
114 ['uuid' => 'http://schema.org/identifier', 'sensitive' => 'as:sensitive',
115 'vcard' => 'http://www.w3.org/2006/vcard/ns#']]];
117 $data['id'] = $contact['url'];
118 $data['uuid'] = $user['guid'];
119 $data['type'] = $accounttype[$user['account-type']];
120 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
121 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
122 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
123 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
124 $data['preferredUsername'] = $user['nickname'];
125 $data['name'] = $contact['name'];
126 $data['vcard:hasAddress'] = ['@type' => 'Home', 'vcard:country-name' => $profile['country-name'],
127 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
128 $data['summary'] = $contact['about'];
129 $data['url'] = $contact['url'];
130 $data['manuallyApprovesFollowers'] = false;
131 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
132 'owner' => $contact['url'],
133 'publicKeyPem' => $user['pubkey']];
134 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
135 $data['icon'] = ['type' => 'Image',
136 'url' => $contact['avatar']];
138 // tags: https://kitty.town/@inmysocks/100656097926961126.json
142 public static function createActivityFromItem($item_id)
144 $item = Item::selectFirst([], ['id' => $item_id]);
146 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
147 ['Emoji' => 'toot:Emoji', 'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
148 'conversation' => 'ostatus:conversation', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
149 'ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
150 'toot' => 'http://joinmastodon.org/ns#']]];
152 $data['type'] = 'Create';
153 $data['id'] = $item['uri'];
154 $data['actor'] = $item['author-link'];
155 $data['to'] = 'https://www.w3.org/ns/activitystreams#Public';
156 $data['object'] = self::createNote($item);
160 public static function createNote($item)
163 $data['type'] = 'Note';
164 $data['id'] = $item['uri'];
166 if ($item['uri'] != $item['thr-parent']) {
167 $data['inReplyTo'] = $item['thr-parent'];
170 $conversation = DBA::selectFirst('conversation', ['conversation-uri'], ['item-uri' => $item['parent-uri']]);
171 if (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
172 $conversation_uri = $conversation['conversation-uri'];
174 $conversation_uri = $item['parent-uri'];
177 $data['context'] = $data['conversation'] = $conversation_uri;
178 $data['actor'] = $item['author-link'];
180 if (!$item['private']) {
181 $data['to'][] = 'https://www.w3.org/ns/activitystreams#Public';
183 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
184 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
185 $data['attributedTo'] = $item['author-link'];
186 $data['name'] = BBCode::convert($item['title'], false, 7);
187 $data['content'] = BBCode::convert($item['body'], false, 7);
188 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
189 //$data['summary'] = ''; // Ignore by now
190 //$data['sensitive'] = false; // - Query NSFW
191 //$data['emoji'] = []; // Ignore by now
192 //$data['tag'] = []; /// @ToDo
193 //$data['attachment'] = []; // @ToDo
197 public static function transmitActivity($activity, $target, $uid)
199 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
201 $owner = User::getOwnerDataById($uid);
203 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
204 'id' => 'https://pirati.ca/activity/' . System::createGUID(),
206 'actor' => $owner['url'],
207 'object' => $profile['url']];
209 return self::transmit($data, $profile['notify'], $uid);
213 * Fetches ActivityPub content from the given url
215 * @param string $url content url
218 public static function fetchContent($url)
220 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json']);
222 if (!$ret['success'] || empty($ret['body'])) {
226 return json_decode($ret['body'], true);
230 * Resolves the profile url from the address by using webfinger
232 * @param string $addr profile address (user@domain.tld)
235 private static function addrToUrl($addr)
237 $addr_parts = explode('@', $addr);
238 if (count($addr_parts) != 2) {
242 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
244 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
245 if (!$ret['success'] || empty($ret['body'])) {
249 $data = json_decode($ret['body'], true);
251 if (empty($data['links'])) {
255 foreach ($data['links'] as $link) {
256 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
260 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
261 return $link['href'];
268 public static function verifySignature($content, $http_headers)
270 $object = json_decode($content, true);
272 if (empty($object)) {
276 $actor = self::processElement($object, 'actor', 'id');
279 $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . $http_headers['REQUEST_URI'];
281 // First take every header
282 foreach ($http_headers as $k => $v) {
283 $field = str_replace('_', '-', strtolower($k));
284 $headers[$field] = $v;
287 // Now add every http header
288 foreach ($http_headers as $k => $v) {
289 if (strpos($k, 'HTTP_') === 0) {
290 $field = str_replace('_', '-', strtolower(substr($k, 5)));
291 $headers[$field] = $v;
295 $sig_block = ActivityPub::parseSigHeader($http_headers['HTTP_SIGNATURE']);
297 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
302 foreach ($sig_block['headers'] as $h) {
303 if (array_key_exists($h, $headers)) {
304 $signed_data .= $h . ': ' . $headers[$h] . "\n";
307 $signed_data = rtrim($signed_data, "\n");
309 if (empty($signed_data)) {
315 if ($sig_block['algorithm'] === 'rsa-sha256') {
316 $algorithm = 'sha256';
319 if ($sig_block['algorithm'] === 'rsa-sha512') {
320 $algorithm = 'sha512';
323 if (empty($algorithm)) {
327 $key = self::fetchKey($sig_block['keyId'], $actor);
333 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm)) {
337 // Check the digest if it was part of the signed data
338 if (in_array('digest', $sig_block['headers'])) {
339 $digest = explode('=', $headers['digest'], 2);
340 if ($digest[0] === 'SHA-256') {
343 if ($digest[0] === 'SHA-512') {
347 /// @todo add all hashes from the rfc
349 if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
354 // Check the content-length if it was part of the signed data
355 if (in_array('content-length', $sig_block['headers'])) {
356 if (strlen($content) != $headers['content-length']) {
365 private static function fetchKey($id, $actor)
367 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
369 $profile = Probe::uri($url, Protocol::ACTIVITYPUB);
370 if (!empty($profile)) {
371 return $profile['pubkey'];
372 } elseif ($url != $actor) {
373 $profile = Probe::uri($actor, Protocol::ACTIVITYPUB);
374 if (!empty($profile)) {
375 return $profile['pubkey'];
385 * @param string $header
386 * @return array associate array with
387 * - \e string \b keyID
388 * - \e string \b algorithm
389 * - \e array \b headers
390 * - \e string \b signature
392 private static function parseSigHeader($header)
397 if (preg_match('/keyId="(.*?)"/ism',$header,$matches)) {
398 $ret['keyId'] = $matches[1];
401 if (preg_match('/algorithm="(.*?)"/ism',$header,$matches)) {
402 $ret['algorithm'] = $matches[1];
405 if (preg_match('/headers="(.*?)"/ism',$header,$matches)) {
406 $ret['headers'] = explode(' ', $matches[1]);
409 if (preg_match('/signature="(.*?)"/ism',$header,$matches)) {
410 $ret['signature'] = base64_decode(preg_replace('/\s+/','',$matches[1]));
417 * Fetches a profile from the given url
419 * @param string $url profile url
422 public static function fetchProfile($url)
424 if (empty(parse_url($url, PHP_URL_SCHEME))) {
425 $url = self::addrToUrl($url);
431 $data = self::fetchContent($url);
433 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
437 $profile = ['network' => Protocol::ACTIVITYPUB];
438 $profile['nick'] = $data['preferredUsername'];
439 $profile['name'] = defaults($data, 'name', $profile['nick']);
440 $profile['guid'] = defaults($data, 'uuid', null);
441 $profile['url'] = $data['id'];
443 $parts = parse_url($profile['url']);
444 unset($parts['scheme']);
445 unset($parts['path']);
446 $profile['addr'] = $profile['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
447 $profile['alias'] = self::processElement($data, 'url', 'href');
448 $profile['photo'] = self::processElement($data, 'icon', 'url');
449 // $profile['community']
450 // $profile['keywords']
451 // $profile['location']
452 $profile['about'] = defaults($data, 'summary', '');
453 $profile['batch'] = self::processElement($data, 'endpoints', 'sharedInbox');
454 $profile['notify'] = $data['inbox'];
455 $profile['poll'] = $data['outbox'];
456 $profile['pubkey'] = self::processElement($data, 'publicKey', 'publicKeyPem');
458 // Check if the address is resolvable
459 if (self::addrToUrl($profile['addr']) == $profile['url']) {
460 $parts = parse_url($profile['url']);
461 unset($parts['path']);
462 $profile['baseurl'] = Network::unparseURL($parts);
464 unset($profile['addr']);
467 if ($profile['url'] == $profile['alias']) {
468 unset($profile['alias']);
471 // Remove all "null" fields
472 foreach ($profile as $field => $content) {
473 if (is_null($content)) {
474 unset($profile[$field]);
480 unset($data['type']);
481 unset($data['manuallyApprovesFollowers']);
484 unset($data['@context']);
486 unset($data['attachment']);
487 unset($data['image']);
488 unset($data['nomadicLocations']);
489 unset($data['signature']);
490 unset($data['following']);
491 unset($data['followers']);
492 unset($data['featured']);
493 unset($data['movedTo']);
494 unset($data['liked']);
495 unset($data['sharedInbox']); // Misskey
496 unset($data['isCat']); // Misskey
497 unset($data['kroeg:blocks']); // Kroeg
498 unset($data['updated']); // Kroeg
503 public static function processInbox($body, $header, $uid)
505 logger('Incoming message for user ' . $uid, LOGGER_DEBUG);
507 if (!self::verifySignature($body, $header)) {
508 logger('Invalid signature, message will be discarded.', LOGGER_DEBUG);
512 $activity = json_decode($body, true);
514 if (!is_array($activity)) {
515 logger('Invalid body.', LOGGER_DEBUG);
519 self::processActivity($activity, $body, $uid);
522 public static function fetchOutbox($url)
524 $data = self::fetchContent($url);
529 if (!empty($data['orderedItems'])) {
530 $items = $data['orderedItems'];
531 } elseif (!empty($data['first']['orderedItems'])) {
532 $items = $data['first']['orderedItems'];
533 } elseif (!empty($data['first'])) {
534 self::fetchOutbox($data['first']);
540 foreach ($items as $activity) {
541 self::processActivity($activity);
545 function processActivity($activity, $body = '', $uid = null)
547 if (empty($activity['type'])) {
548 logger('Empty type', LOGGER_DEBUG);
552 if (empty($activity['object'])) {
553 logger('Empty object', LOGGER_DEBUG);
557 if (empty($activity['actor'])) {
558 logger('Empty actor', LOGGER_DEBUG);
563 $actor = self::processElement($activity, 'actor', 'id');
565 logger('Empty actor - 2', LOGGER_DEBUG);
569 if (is_string($activity['object'])) {
570 $object_url = $activity['object'];
571 } elseif (!empty($activity['object']['id'])) {
572 $object_url = $activity['object']['id'];
574 logger('No object found', LOGGER_DEBUG);
578 // ----------------------------------
581 unset($activity['@context']);
582 unset($activity['id']);
585 unset($activity['title']);
586 unset($activity['atomUri']);
587 unset($activity['context_id']);
588 unset($activity['statusnetConversationId']);
591 unset($activity['context']);
592 unset($activity['location']);
593 unset($activity['signature']);
595 // Fetch all receivers from to, cc, bto and bcc
596 $receivers = self::getReceivers($activity);
598 // When it is a delivery to a personal inbox we add that user to the receivers
600 $owner = User::getOwnerDataById($uid);
601 $additional = [$owner['url'] => $uid];
602 $receivers = array_merge($receivers, $additional);
605 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
607 if (!in_array($activity['type'], ['Like', 'Dislike'])) {
608 $item = self::fetchObject($object_url, $activity['object']);
610 logger("Object data couldn't be processed", LOGGER_DEBUG);
614 $item['object'] = $object_url;
615 $item['receiver'] = [];
616 $item['type'] = $activity['type'];
619 $item = self::addActivityFields($item, $activity);
621 $item['owner'] = $actor;
623 $item['receiver'] = array_merge($item['receiver'], $receivers);
625 logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
627 switch ($activity['type']) {
631 self::createItem($item, $body);
636 self::activityItem($item, $body);
643 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
648 private static function getReceivers($activity)
652 $elements = ['to', 'cc', 'bto', 'bcc'];
653 foreach ($elements as $element) {
654 if (empty($activity[$element])) {
658 // The receiver can be an arror or a string
659 if (is_string($activity[$element])) {
660 $activity[$element] = [$activity[$element]];
663 foreach ($activity[$element] as $receiver) {
664 if ($receiver == self::PUBLIC) {
665 $receivers[$receiver] = 0;
668 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
669 $contact = DBA::selectFirst('contact', ['uid'], $condition);
670 if (!DBA::isResult($contact)) {
673 $receivers[$receiver] = $contact['uid'];
679 private static function addActivityFields($item, $activity)
681 if (!empty($activity['published']) && empty($item['published'])) {
682 $item['published'] = $activity['published'];
685 if (!empty($activity['updated']) && empty($item['updated'])) {
686 $item['updated'] = $activity['updated'];
689 if (!empty($activity['inReplyTo']) && empty($item['parent-uri'])) {
690 $item['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
693 if (!empty($activity['instrument'])) {
694 $item['service'] = self::processElement($activity, 'instrument', 'name', 'type', 'Service');
699 private static function fetchObject($object_url, $object = [])
701 $data = self::fetchContent($object_url);
705 logger('Empty content', LOGGER_DEBUG);
707 } elseif (is_string($data)) {
708 logger('No object array provided.', LOGGER_DEBUG);
709 $item = Item::selectFirst([], ['uri' => $data]);
710 if (!DBA::isResult($item)) {
711 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
714 logger('Using already stored item', LOGGER_DEBUG);
715 $data = self::createNote($item);
717 logger('Using provided object', LOGGER_DEBUG);
721 if (empty($data['type'])) {
722 logger('Empty type', LOGGER_DEBUG);
725 $type = $data['type'];
726 logger('Type ' . $type, LOGGER_DEBUG);
729 if (in_array($type, ['Note', 'Article', 'Video'])) {
730 $common = self::processCommonData($data);
735 return array_merge($common, self::processNote($data));
737 return array_merge($common, self::processArticle($data));
739 return array_merge($common, self::processVideo($data));
742 if (empty($data['object'])) {
745 return self::fetchObject($data['object']);
752 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
757 private static function processCommonData(&$object)
759 if (empty($object['id']) || empty($object['attributedTo'])) {
764 $item['type'] = $object['type'];
765 $item['uri'] = $object['id'];
767 if (!empty($object['inReplyTo'])) {
768 $item['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
770 $item['reply-to-uri'] = $item['uri'];
773 $item['published'] = defaults($object, 'published', null);
774 $item['updated'] = defaults($object, 'updated', $item['published']);
776 if (empty($item['published']) && !empty($item['updated'])) {
777 $item['published'] = $item['updated'];
780 $item['uuid'] = defaults($object, 'uuid', null);
781 $item['owner'] = $item['author'] = self::processElement($object, 'attributedTo', 'id');
782 $item['context'] = defaults($object, 'context', null);
783 $item['conversation'] = defaults($object, 'conversation', null);
784 $item['sensitive'] = defaults($object, 'sensitive', null);
785 $item['name'] = defaults($object, 'title', null);
786 $item['name'] = defaults($object, 'name', $item['name']);
787 $item['summary'] = defaults($object, 'summary', null);
788 $item['content'] = defaults($object, 'content', null);
789 $item['source'] = defaults($object, 'source', null);
790 $item['location'] = self::processElement($object, 'location', 'name', 'type', 'Place');
791 $item['attachments'] = defaults($object, 'attachment', null);
792 $item['tags'] = defaults($object, 'tag', null);
793 $item['service'] = self::processElement($object, 'instrument', 'name', 'type', 'Service');
794 $item['alternate-url'] = self::processElement($object, 'url', 'href');
795 $item['receiver'] = self::getReceivers($object);
799 unset($object['source']);
802 unset($object['@context']);
803 unset($object['type']);
804 unset($object['actor']);
805 unset($object['signature']);
806 unset($object['mediaType']);
807 unset($object['duration']);
808 unset($object['replies']);
809 unset($object['icon']);
812 audience, preview, endTime, startTime, generator, image
817 private static function processNote($object)
823 unset($object['emoji']);
824 unset($object['atomUri']);
825 unset($object['inReplyToAtomUri']);
828 unset($object['contentMap']);
829 unset($object['announcement_count']);
830 unset($object['announcements']);
831 unset($object['context_id']);
832 unset($object['likes']);
833 unset($object['like_count']);
834 unset($object['inReplyToStatusId']);
835 unset($object['shares']);
836 unset($object['quoteUrl']);
837 unset($object['statusnetConversationId']);
842 private static function processArticle($object)
849 private static function processVideo($object)
854 unset($object['category']);
855 unset($object['licence']);
856 unset($object['language']);
857 unset($object['commentsEnabled']);
860 unset($object['views']);
861 unset($object['waitTranscoding']);
862 unset($object['state']);
863 unset($object['support']);
864 unset($object['subtitleLanguage']);
865 unset($object['likes']);
866 unset($object['dislikes']);
867 unset($object['shares']);
868 unset($object['comments']);
873 private static function processElement($array, $element, $key, $type = null, $type_value = null)
879 if (empty($array[$element])) {
883 if (is_string($array[$element])) {
884 return $array[$element];
887 if (is_null($type_value)) {
888 if (!empty($array[$element][$key])) {
889 return $array[$element][$key];
892 if (!empty($array[$element][0][$key])) {
893 return $array[$element][0][$key];
899 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
900 return $array[$element][$key];
903 /// @todo Add array search
908 private static function convertMentions($body)
910 $URLSearchString = "^\[\]";
911 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
916 private static function constructTagList($tags, $sensitive)
923 foreach ($tags as $tag) {
924 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
925 if (!empty($tag_text)) {
929 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
933 /// @todo add nsfw for $sensitive
938 private static function constructAttachList($attachments, $item)
940 if (empty($attachments)) {
944 foreach ($attachments as $attach) {
945 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
946 if ($filetype == 'image') {
947 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
949 if (!empty($item["attach"])) {
950 $item["attach"] .= ',';
952 $item["attach"] = '';
954 if (!isset($attach['length'])) {
955 $attach['length'] = "0";
957 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
964 private static function createItem($activity, $body)
966 /// @todo What to do with $activity['context']?
969 $item['network'] = Protocol::ACTIVITYPUB;
970 $item['private'] = !in_array(0, $activity['receiver']);
971 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
972 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
973 $item['uri'] = $activity['uri'];
974 $item['parent-uri'] = $activity['reply-to-uri'];
975 $item['verb'] = ACTIVITY_POST;
976 $item['object-type'] = ACTIVITY_OBJ_NOTE; /// Todo?
977 $item['created'] = $activity['published'];
978 $item['edited'] = $activity['updated'];
979 $item['guid'] = $activity['uuid'];
980 $item['title'] = HTML::toBBCode($activity['name']);
981 $item['content-warning'] = HTML::toBBCode($activity['summary']);
982 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
983 $item['location'] = $activity['location'];
984 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
985 $item['app'] = $activity['service'];
986 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
988 $item = self::constructAttachList($activity['attachments'], $item);
990 $source = self::processElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
991 if (!empty($source)) {
992 $item['body'] = $source;
995 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
996 $item['source'] = $body;
997 $item['conversation-uri'] = $activity['conversation'];
999 foreach ($activity['receiver'] as $receiver) {
1000 $item['uid'] = $receiver;
1001 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1003 if (($receiver != 0) && empty($item['contact-id'])) {
1004 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1007 $item_id = Item::insert($item);
1008 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1009 if (!empty($item_id) && ($item['uid'] == 0)) {
1010 Item::distribute($item_id);
1015 private static function activityItem($data)
1017 logger('Activity "' . $data['type'] . '" for ' . $data['object']);
1018 $items = Item::select(['id'], ['uri' => $data['object']]);
1019 while ($item = Item::fetch($items)) {
1020 logger('Activity ' . $data['type'] . ' for item ' . $item['id'], LOGGER_DEBUG);
1021 Item::performLike($item['id'], strtolower($data['type']));
1024 logger('Activity done', LOGGER_DEBUG);