3 * Name: Bluesky Connector
4 * Description: Post to Bluesky
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
11 * Possibly not possible:
12 * - only fetch new posts
14 * Currently not possible, due to limitations in Friendica
15 * - mute contacts https://atproto.com/lexicons/app-bsky-graph#appbskygraphmuteactor
16 * - unmute contacts https://atproto.com/lexicons/app-bsky-graph#appbskygraphunmuteactor
18 * Possibly interesting:
19 * - https://atproto.com/lexicons/com-atproto-label#comatprotolabelsubscribelabels
20 * - https://atproto.com/lexicons/com-atproto-moderation#comatprotomoderationcreatereport
21 * - https://atproto.com/lexicons/com-atproto-repo#comatprotorepoputrecord
22 * - https://atproto.com/lexicons/app-bsky-embed#appbskyembedrecordwithmedia
23 * - https://atproto.com/lexicons/app-bsky-feed#appbskyfeedgetpostthread
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Content\Text\HTML;
28 use Friendica\Content\Text\Plaintext;
29 use Friendica\Core\Cache\Enum\Duration;
30 use Friendica\Core\Config\Util\ConfigFileManager;
31 use Friendica\Core\Hook;
32 use Friendica\Core\Logger;
33 use Friendica\Core\Protocol;
34 use Friendica\Core\Renderer;
35 use Friendica\Core\Worker;
36 use Friendica\Database\DBA;
38 use Friendica\Model\Contact;
39 use Friendica\Model\Item;
40 use Friendica\Model\ItemURI;
41 use Friendica\Model\Photo;
42 use Friendica\Model\Post;
43 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
44 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
45 use Friendica\Protocol\Activity;
46 use Friendica\Protocol\Relay;
47 use Friendica\Util\DateTimeFormat;
48 use Friendica\Util\Strings;
50 const BLUESKY_DEFAULT_POLL_INTERVAL = 10; // given in minutes
51 const BLUESKY_HOST = 'https://bsky.app'; // Hard wired until Bluesky will run on multiple systems
53 function bluesky_install()
55 Hook::register('load_config', __FILE__, 'bluesky_load_config');
56 Hook::register('hook_fork', __FILE__, 'bluesky_hook_fork');
57 Hook::register('post_local', __FILE__, 'bluesky_post_local');
58 Hook::register('notifier_normal', __FILE__, 'bluesky_send');
59 Hook::register('jot_networks', __FILE__, 'bluesky_jot_nets');
60 Hook::register('connector_settings', __FILE__, 'bluesky_settings');
61 Hook::register('connector_settings_post', __FILE__, 'bluesky_settings_post');
62 Hook::register('cron', __FILE__, 'bluesky_cron');
63 Hook::register('support_follow', __FILE__, 'bluesky_support_follow');
64 Hook::register('support_probe', __FILE__, 'bluesky_support_probe');
65 Hook::register('follow', __FILE__, 'bluesky_follow');
66 Hook::register('unfollow', __FILE__, 'bluesky_unfollow');
67 Hook::register('block', __FILE__, 'bluesky_block');
68 Hook::register('unblock', __FILE__, 'bluesky_unblock');
69 Hook::register('check_item_notification', __FILE__, 'bluesky_check_item_notification');
70 Hook::register('probe_detect', __FILE__, 'bluesky_probe_detect');
71 Hook::register('item_by_link', __FILE__, 'bluesky_item_by_link');
74 function bluesky_load_config(ConfigFileManager $loader)
76 DI::app()->getConfigCache()->load($loader->loadAddonConfig('bluesky'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
79 function bluesky_check_item_notification(array &$notification_data)
81 $did = DI::pConfig()->get($notification_data['uid'], 'bluesky', 'did');
84 $notification_data['profiles'][] = $did;
88 function bluesky_probe_detect(array &$hookData)
90 // Don't overwrite an existing result
91 if (isset($hookData['result'])) {
95 // Avoid a lookup for the wrong network
96 if (!in_array($hookData['network'], ['', Protocol::BLUESKY])) {
100 $pconfig = DBA::selectFirst('pconfig', ['uid'], ["`cat` = ? AND `k` = ? AND `v` != ?", 'bluesky', 'access_token', '']);
101 if (empty($pconfig['uid'])) {
105 if (parse_url($hookData['uri'], PHP_URL_SCHEME) == 'did') {
106 $did = $hookData['uri'];
107 } elseif (preg_match('#^' . BLUESKY_HOST . '/profile/(.+)#', $hookData['uri'], $matches)) {
108 $did = bluesky_get_did($pconfig['uid'], $matches[1]);
116 $token = bluesky_get_token($pconfig['uid']);
121 $data = bluesky_get($pconfig['uid'], '/xrpc/app.bsky.actor.getProfile?actor=' . $did, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $token]]]);
126 $hookData['result'] = bluesky_get_contact_fields($data, 0, false);
128 // Preparing probe data. This differs slightly from the contact array
129 $hookData['result']['about'] = HTML::toBBCode($data->description ?? '');
130 $hookData['result']['photo'] = $data->avatar ?? '';
131 $hookData['result']['header'] = $data->banner ?? '';
132 $hookData['result']['batch'] = '';
133 $hookData['result']['notify'] = '';
134 $hookData['result']['poll'] = '';
135 $hookData['result']['poco'] = '';
136 $hookData['result']['pubkey'] = '';
137 $hookData['result']['priority'] = 0;
138 $hookData['result']['guid'] = '';
141 function bluesky_item_by_link(array &$hookData)
143 // Don't overwrite an existing result
144 if (isset($hookData['item_id'])) {
148 $token = bluesky_get_token($hookData['uid']);
153 if (!preg_match('#^' . BLUESKY_HOST . '/profile/(.+)/post/(.+)#', $hookData['uri'], $matches)) {
157 $did = bluesky_get_did($hookData['uid'], $matches[1]);
162 Logger::debug('Found bluesky post', ['url' => $hookData['uri'], 'handle' => $matches[1], 'did' => $did, 'cid' => $matches[2]]);
164 $uri = 'at://' . $did . '/app.bsky.feed.post/' . $matches[2];
166 $uri = bluesky_fetch_missing_post($uri, $hookData['uid'], 0);
167 Logger::debug('Got post', ['profile' => $matches[1], 'cid' => $matches[2], 'result' => $uri]);
169 $item = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $hookData['uid']]);
170 if (!empty($item['id'])) {
171 $hookData['item_id'] = $item['id'];
176 function bluesky_support_follow(array &$data)
178 if ($data['protocol'] == Protocol::BLUESKY) {
179 $data['result'] = true;
183 function bluesky_support_probe(array &$data)
185 if ($data['protocol'] == Protocol::BLUESKY) {
186 $data['result'] = true;
190 function bluesky_follow(array &$hook_data)
192 $token = bluesky_get_token($hook_data['uid']);
197 Logger::debug('Check if contact is bluesky', ['data' => $hook_data]);
198 $contact = DBA::selectFirst('contact', [], ['network' => Protocol::BLUESKY, 'url' => $hook_data['url'], 'uid' => [0, $hook_data['uid']]]);
199 if (empty($contact)) {
204 'subject' => $contact['url'],
205 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
206 '$type' => 'app.bsky.graph.follow'
210 'collection' => 'app.bsky.graph.follow',
211 'repo' => DI::pConfig()->get($hook_data['uid'], 'bluesky', 'did'),
215 $activity = bluesky_post($hook_data['uid'], '/xrpc/com.atproto.repo.createRecord', json_encode($post), ['Content-type' => 'application/json', 'Authorization' => ['Bearer ' . $token]]);
216 if (!empty($activity->uri)) {
217 $hook_data['contact'] = $contact;
218 Logger::debug('Successfully start following', ['url' => $contact['url'], 'uri' => $activity->uri]);
222 function bluesky_unfollow(array &$hook_data)
224 $token = bluesky_get_token($hook_data['uid']);
229 if ($hook_data['contact']['network'] != Protocol::BLUESKY) {
233 $data = bluesky_get($hook_data['uid'], '/xrpc/app.bsky.actor.getProfile?actor=' . $hook_data['contact']['url'], HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $token]]]);
234 if (empty($data->viewer) || empty($data->viewer->following)) {
238 bluesky_delete_post($data->viewer->following, $hook_data['uid']);
240 $hook_data['result'] = true;
243 function bluesky_block(array &$hook_data)
245 $token = bluesky_get_token($hook_data['uid']);
250 Logger::debug('Check if contact is bluesky', ['data' => $hook_data]);
251 $contact = DBA::selectFirst('contact', [], ['network' => Protocol::BLUESKY, 'url' => $hook_data['url'], 'uid' => [0, $hook_data['uid']]]);
252 if (empty($contact)) {
257 'subject' => $contact['url'],
258 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
259 '$type' => 'app.bsky.graph.block'
263 'collection' => 'app.bsky.graph.block',
264 'repo' => DI::pConfig()->get($hook_data['uid'], 'bluesky', 'did'),
268 $activity = bluesky_post($hook_data['uid'], '/xrpc/com.atproto.repo.createRecord', json_encode($post), ['Content-type' => 'application/json', 'Authorization' => ['Bearer ' . $token]]);
269 if (!empty($activity->uri)) {
270 $cdata = Contact::getPublicAndUserContactID($hook_data['contact']['id'], $hook_data['uid']);
271 if (!empty($cdata['user'])) {
272 Contact::remove($cdata['user']);
274 Logger::debug('Successfully blocked contact', ['url' => $hook_data['contact']['url'], 'uri' => $activity->uri]);
278 function bluesky_unblock(array &$hook_data)
280 $token = bluesky_get_token($hook_data['uid']);
285 if ($hook_data['contact']['network'] != Protocol::BLUESKY) {
289 $data = bluesky_get($hook_data['uid'], '/xrpc/app.bsky.actor.getProfile?actor=' . $hook_data['contact']['url'], HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $token]]]);
290 if (empty($data->viewer) || empty($data->viewer->blocking)) {
294 bluesky_delete_post($data->viewer->blocking, $hook_data['uid']);
296 $hook_data['result'] = true;
299 function bluesky_settings(array &$data)
301 if (!DI::userSession()->getLocalUserId()) {
305 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post') ?? false;
306 $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default') ?? false;
307 $host = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'host') ?: 'https://bsky.social';
308 $handle = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle');
309 $did = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
310 $token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'access_token');
311 $import = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'import') ?? false;
312 $import_feeds = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'import_feeds') ?? false;
314 $status = $token ? DI::l10n()->t("You are authenticated to Bluesky. For security reasons the password isn't stored.") : DI::l10n()->t('You are not authenticated. Please enter the app password.');
316 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/bluesky/');
317 $html = Renderer::replaceMacros($t, [
318 '$enable' => ['bluesky', DI::l10n()->t('Enable Bluesky Post Addon'), $enabled],
319 '$bydefault' => ['bluesky_bydefault', DI::l10n()->t('Post to Bluesky by default'), $def_enabled],
320 '$import' => ['bluesky_import', DI::l10n()->t('Import the remote timeline'), $import],
321 '$import_feeds' => ['bluesky_import_feeds', DI::l10n()->t('Import the pinned feeds'), $import_feeds, DI::l10n()->t('When activated, Posts will be imported from all the feeds that you pinned in Bluesky.')],
322 '$host' => ['bluesky_host', DI::l10n()->t('Bluesky host'), $host, '', '', 'readonly'],
323 '$handle' => ['bluesky_handle', DI::l10n()->t('Bluesky handle'), $handle],
324 '$did' => ['bluesky_did', DI::l10n()->t('Bluesky DID'), $did, DI::l10n()->t('This is the unique identifier. It will be fetched automatically, when the handle is entered.'), '', 'readonly'],
325 '$password' => ['bluesky_password', DI::l10n()->t('Bluesky app password'), '', DI::l10n()->t("Please don't add your real password here, but instead create a specific app password in the Bluesky settings.")],
330 'connector' => 'bluesky',
331 'title' => DI::l10n()->t('Bluesky Import/Export'),
332 'image' => 'images/bluesky.jpg',
333 'enabled' => $enabled,
338 function bluesky_settings_post(array &$b)
340 if (empty($_POST['bluesky-submit'])) {
344 $old_host = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'host');
345 $old_handle = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle');
346 $old_did = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
348 $host = $_POST['bluesky_host'];
349 $handle = $_POST['bluesky_handle'];
351 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'post', intval($_POST['bluesky']));
352 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default', intval($_POST['bluesky_bydefault']));
353 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'host', $host);
354 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'handle', $handle);
355 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'import', intval($_POST['bluesky_import']));
356 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'import_feeds', intval($_POST['bluesky_import_feeds']));
358 if (!empty($host) && !empty($handle)) {
359 if (empty($old_did) || $old_host != $host || $old_handle != $handle) {
360 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'did', bluesky_get_did(DI::userSession()->getLocalUserId(), DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle')));
363 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
366 if (!empty($_POST['bluesky_password'])) {
367 bluesky_create_token(DI::userSession()->getLocalUserId(), $_POST['bluesky_password']);
371 function bluesky_jot_nets(array &$jotnets_fields)
373 if (!DI::userSession()->getLocalUserId()) {
377 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post')) {
378 $jotnets_fields[] = [
379 'type' => 'checkbox',
382 DI::l10n()->t('Post to Bluesky'),
383 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default')
389 function bluesky_cron()
391 $last = DI::keyValue()->get('bluesky_last_poll');
393 $poll_interval = intval(DI::config()->get('bluesky', 'poll_interval'));
394 if (!$poll_interval) {
395 $poll_interval = BLUESKY_DEFAULT_POLL_INTERVAL;
399 $next = $last + ($poll_interval * 60);
400 if ($next > time()) {
401 Logger::notice('poll interval not reached');
405 Logger::notice('cron_start');
407 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
408 if ($abandon_days < 1) {
412 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
414 $pconfigs = DBA::selectToArray('pconfig', [], ['cat' => 'bluesky', 'k' => 'import', 'v' => true]);
415 foreach ($pconfigs as $pconfig) {
416 if ($abandon_days != 0) {
417 if (!DBA::exists('user', ["`uid` = ? AND `login_date` >= ?", $pconfig['uid'], $abandon_limit])) {
418 Logger::notice('abandoned account: timeline from user will not be imported', ['user' => $pconfig['uid']]);
423 Logger::notice('importing timeline - start', ['user' => $pconfig['uid']]);
424 bluesky_fetch_timeline($pconfig['uid']);
425 Logger::notice('importing timeline - done', ['user' => $pconfig['uid']]);
427 Logger::notice('importing notifications - start', ['user' => $pconfig['uid']]);
428 bluesky_fetch_notifications($pconfig['uid']);
429 Logger::notice('importing notifications - done', ['user' => $pconfig['uid']]);
431 if (DI::pConfig()->get($pconfig['uid'], 'bluesky', 'import_feeds')) {
432 Logger::notice('importing feeds - start', ['user' => $pconfig['uid']]);
433 $feeds = bluesky_get_feeds($pconfig['uid']);
434 foreach ($feeds as $feed) {
435 Logger::debug('Importing feed', ['user' => $pconfig['uid'], 'feed' => $feed]);
436 bluesky_fetch_feed($pconfig['uid'], $feed);
438 Logger::notice('importing feeds - done', ['user' => $pconfig['uid']]);
442 $last_clean = DI::keyValue()->get('bluesky_last_clean');
443 if (empty($last_clean) || ($last_clean + 86400 < time())) {
444 Logger::notice('Start contact cleanup');
445 $contacts = DBA::select('account-user-view', ['id', 'pid'], ["`network` = ? AND `uid` != ? AND `rel` = ?", Protocol::BLUESKY, 0, Contact::NOTHING]);
446 while ($contact = DBA::fetch($contacts)) {
447 Worker::add(Worker::PRIORITY_LOW, 'MergeContact', $contact['pid'], $contact['id'], 0);
449 DBA::close($contacts);
450 DI::keyValue()->set('bluesky_last_clean', time());
451 Logger::notice('Contact cleanup done');
454 Logger::notice('cron_end');
456 DI::keyValue()->set('bluesky_last_poll', time());
459 function bluesky_hook_fork(array &$b)
461 if ($b['name'] != 'notifier_normal') {
467 if (($post['created'] !== $post['edited']) && !$post['deleted']) {
468 DI::logger()->info('Editing is not supported by the addon');
469 $b['execute'] = false;
473 if (DI::pConfig()->get($post['uid'], 'bluesky', 'import')) {
474 // Don't post if it isn't a reply to a bluesky post
475 if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::BLUESKY])) {
476 Logger::notice('No bluesky parent found', ['item' => $post['id']]);
477 $b['execute'] = false;
480 } elseif (!strstr($post['postopts'] ?? '', 'bluesky') || ($post['parent'] != $post['id']) || $post['private']) {
481 DI::logger()->info('Activities are never exported when we don\'t import the bluesky timeline', ['uid' => $post['uid']]);
482 $b['execute'] = false;
487 function bluesky_post_local(array &$b)
493 if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
497 if ($b['private'] || $b['parent']) {
501 $bluesky_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post'));
502 $bluesky_enable = (($bluesky_post && !empty($_REQUEST['bluesky_enable'])) ? intval($_REQUEST['bluesky_enable']) : 0);
504 // if API is used, default to the chosen settings
505 if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default'))) {
509 if (!$bluesky_enable) {
513 if (strlen($b['postopts'])) {
514 $b['postopts'] .= ',';
517 $b['postopts'] .= 'bluesky';
520 function bluesky_send(array &$b)
522 if (($b['created'] !== $b['edited']) && !$b['deleted']) {
526 if ($b['gravity'] != Item::GRAVITY_PARENT) {
527 Logger::debug('Got comment', ['item' => $b]);
530 $uri = bluesky_get_uri_class($b['uri']);
532 Logger::debug('Not a bluesky post', ['uri' => $b['uri']]);
535 bluesky_delete_post($b['uri'], $b['uid']);
539 $root = bluesky_get_uri_class($b['parent-uri']);
540 $parent = bluesky_get_uri_class($b['thr-parent']);
542 if (empty($root) || empty($parent)) {
543 Logger::debug('No bluesky post', ['parent' => $b['parent'], 'thr-parent' => $b['thr-parent']]);
547 if ($b['gravity'] == Item::GRAVITY_COMMENT) {
548 Logger::debug('Posting comment', ['root' => $root, 'parent' => $parent]);
549 bluesky_create_post($b, $root, $parent);
551 } elseif (in_array($b['verb'], [Activity::LIKE, Activity::ANNOUNCE])) {
552 bluesky_create_activity($b, $parent);
555 } elseif ($b['private'] || !strstr($b['postopts'], 'bluesky')) {
559 bluesky_create_post($b);
562 function bluesky_create_activity(array $item, stdClass $parent = null)
565 $token = bluesky_get_token($uid);
570 $did = DI::pConfig()->get($uid, 'bluesky', 'did');
572 if ($item['verb'] == Activity::LIKE) {
574 'subject' => $parent,
575 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
576 '$type' => 'app.bsky.feed.like'
580 'collection' => 'app.bsky.feed.like',
584 } elseif ($item['verb'] == Activity::ANNOUNCE) {
586 'subject' => $parent,
587 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
588 '$type' => 'app.bsky.feed.repost'
592 'collection' => 'app.bsky.feed.repost',
598 $activity = bluesky_post($uid, '/xrpc/com.atproto.repo.createRecord', json_encode($post), ['Content-type' => 'application/json', 'Authorization' => ['Bearer ' . $token]]);
599 if (empty($activity)) {
602 Logger::debug('Activity done', ['return' => $activity]);
603 $uri = bluesky_get_uri($activity);
604 Item::update(['extid' => $uri], ['id' => $item['id']]);
605 Logger::debug('Set extid', ['id' => $item['id'], 'extid' => $activity]);
608 function bluesky_create_post(array $item, stdClass $root = null, stdClass $parent = null)
611 $token = bluesky_get_token($uid);
616 $did = DI::pConfig()->get($uid, 'bluesky', 'did');
617 $urls = bluesky_get_urls($item['body']);
619 $msg = Plaintext::getPost($item, 300, false, BBCode::BLUESKY);
620 foreach ($msg['parts'] as $key => $part) {
622 $facets = bluesky_get_facets($part, $urls);
625 'text' => $facets['body'],
626 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
627 '$type' => 'app.bsky.feed.post'
630 if (!empty($facets['facets'])) {
631 $record['facets'] = $facets['facets'];
635 $record['reply'] = ['root' => $root, 'parent' => $parent];
638 if ($key == count($msg['parts']) - 1) {
639 $record = bluesky_add_embed($uid, $msg, $record);
643 'collection' => 'app.bsky.feed.post',
648 $parent = bluesky_post($uid, '/xrpc/com.atproto.repo.createRecord', json_encode($post), ['Content-type' => 'application/json', 'Authorization' => ['Bearer ' . $token]]);
649 if (empty($parent)) {
652 Logger::debug('Posting done', ['return' => $parent]);
656 if (($key == 0) && ($item['gravity'] != Item::GRAVITY_PARENT)) {
657 $uri = bluesky_get_uri($parent);
658 Item::update(['extid' => $uri], ['id' => $item['id']]);
659 Logger::debug('Set extid', ['id' => $item['id'], 'extid' => $uri]);
664 function bluesky_get_urls(string $body): array
666 // Remove all hashtags and mentions
667 $body = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '', $body);
671 // Search for pure links
672 if (preg_match_all("/\[url\](https?:.*?)\[\/url\]/ism", $body, $matches)) {
673 foreach ($matches[1] as $url) {
678 // Search for links with descriptions
679 if (preg_match_all("/\[url\=(https?:.*?)\].*?\[\/url\]/ism", $body, $matches)) {
680 foreach ($matches[1] as $url) {
687 function bluesky_get_facets(string $body, array $urls): array
691 foreach ($urls as $url) {
692 $pos = strpos($body, $url);
693 if ($pos === false) {
697 $prefix = substr($body, 0, $pos);
701 $linktext = Strings::getStyledURL($url);
702 $body = $prefix . $linktext . substr($body, $pos + strlen($url));
704 $facet = new stdClass;
705 $facet->index = new stdClass;
706 $facet->index->byteEnd = $pos + strlen($linktext);
707 $facet->index->byteStart = $pos;
709 $feature = new stdClass;
710 $feature->uri = $url;
712 $feature->$type = 'app.bsky.richtext.facet#link';
714 $facet->features = [$feature];
718 return ['facets' => $facets, 'body' => $body];
721 function bluesky_add_embed(int $uid, array $msg, array $record): array
723 if (($msg['type'] != 'link') && !empty($msg['images'])) {
725 foreach ($msg['images'] as $image) {
726 $photo = Photo::selectFirst(['resource-id'], ['id' => $image['id']]);
727 $photo = Photo::selectFirst([], ["`resource-id` = ? AND `scale` > ?", $photo['resource-id'], 0], ['order' => ['scale']]);
728 $blob = bluesky_upload_blob($uid, $photo);
729 if (!empty($blob) && count($images) < 4) {
730 $images[] = ['alt' => $image['description'] ?? '', 'image' => $blob];
733 if (!empty($images)) {
734 $record['embed'] = ['$type' => 'app.bsky.embed.images', 'images' => $images];
736 } elseif ($msg['type'] == 'link') {
738 '$type' => 'app.bsky.embed.external',
740 'uri' => $msg['url'],
741 'title' => $msg['title'],
742 'description' => $msg['description'],
745 if (!empty($msg['image'])) {
746 $photo = Photo::createPhotoForExternalResource($msg['image']);
747 $blob = bluesky_upload_blob($uid, $photo);
749 $record['embed']['external']['thumb'] = $blob;
756 function bluesky_upload_blob(int $uid, array $photo): ?stdClass
758 $content = Photo::getImageForPhoto($photo);
759 $data = bluesky_post($uid, '/xrpc/com.atproto.repo.uploadBlob', $content, ['Content-type' => $photo['type'], 'Authorization' => ['Bearer ' . bluesky_get_token($uid)]]);
764 Logger::debug('Uploaded blob', ['return' => $data]);
768 function bluesky_delete_post(string $uri, int $uid)
770 $token = bluesky_get_token($uid);
771 $parts = bluesky_get_uri_parts($uri);
773 Logger::debug('No uri delected', ['uri' => $uri]);
776 bluesky_post($uid, '/xrpc/com.atproto.repo.deleteRecord', json_encode($parts), ['Content-type' => 'application/json', 'Authorization' => ['Bearer ' . $token]]);
777 Logger::debug('Deleted', ['parts' => $parts]);
780 function bluesky_fetch_timeline(int $uid)
782 $data = bluesky_get($uid, '/xrpc/app.bsky.feed.getTimeline', HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]);
787 if (empty($data->feed)) {
791 foreach (array_reverse($data->feed) as $entry) {
792 bluesky_process_post($entry->post, $uid, Item::PR_NONE);
793 if (!empty($entry->reason)) {
794 bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid);
798 // @todo Support paging
799 // [cursor] => 1684670516000::bafyreidq3ilwslmlx72jf5vrk367xcc63s6lrhzlyup2bi3zwcvso6w2vi
802 function bluesky_process_reason(stdClass $reason, string $uri, int $uid)
805 if ($reason->$type != 'app.bsky.feed.defs#reasonRepost') {
809 $contact = bluesky_get_contact($reason->by, $uid, $uid);
812 'network' => Protocol::BLUESKY,
815 'uri' => $reason->by->did . '/app.bsky.feed.repost/' . $reason->indexedAt,
816 'private' => Item::UNLISTED,
817 'verb' => Activity::POST,
818 'contact-id' => $contact['id'],
819 'author-name' => $contact['name'],
820 'author-link' => $contact['url'],
821 'author-avatar' => $contact['avatar'],
822 'verb' => Activity::ANNOUNCE,
823 'body' => Activity::ANNOUNCE,
824 'gravity' => Item::GRAVITY_ACTIVITY,
825 'object-type' => Activity\ObjectType::NOTE,
826 'thr-parent' => $uri,
829 if (Post::exists(['uri' => $item['uri'], 'uid' => $uid])) {
833 $item['owner-name'] = $item['author-name'];
834 $item['owner-link'] = $item['author-link'];
835 $item['owner-avatar'] = $item['author-avatar'];
836 if (Item::insert($item)) {
837 $cdata = Contact::getPublicAndUserContactID($contact['id'], $uid);
838 Item::update(['post-reason' => Item::PR_ANNOUNCEMENT, 'causer-id' => $cdata['public']], ['uri' => $uri, 'uid' => $uid]);
842 function bluesky_fetch_notifications(int $uid)
844 $result = bluesky_get($uid, '/xrpc/app.bsky.notification.listNotifications', HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]);
845 if (empty($result->notifications)) {
848 foreach ($result->notifications as $notification) {
849 $uri = bluesky_get_uri($notification);
850 if (Post::exists(['uri' => $uri, 'uid' => $uid]) || Post::exists(['extid' => $uri, 'uid' => $uid])) {
851 Logger::debug('Notification already processed', ['uid' => $uid, 'reason' => $notification->reason, 'uri' => $uri, 'indexedAt' => $notification->indexedAt]);
854 Logger::debug('Process notification', ['uid' => $uid, 'reason' => $notification->reason, 'uri' => $uri, 'indexedAt' => $notification->indexedAt]);
855 switch ($notification->reason) {
857 $item = bluesky_get_header($notification, $uri, $uid, $uid);
858 $item['gravity'] = Item::GRAVITY_ACTIVITY;
859 $item['body'] = $item['verb'] = Activity::LIKE;
860 $item['thr-parent'] = bluesky_get_uri($notification->record->subject);
861 $result = Item::insert($item);
862 Logger::debug('Got like', ['uid' => $uid, 'result' => $result]);
866 $item = bluesky_get_header($notification, $uri, $uid, $uid);
867 $item['gravity'] = Item::GRAVITY_ACTIVITY;
868 $item['body'] = $item['verb'] = Activity::ANNOUNCE;
869 $item['thr-parent'] = bluesky_get_uri($notification->record->subject);
870 $result = Item::insert($item);
871 Logger::debug('Got repost', ['uid' => $uid, 'result' => $result]);
875 $contact = bluesky_get_contact($notification->author, $uid, $uid);
876 Logger::debug('New follower', ['uid' => $uid, 'nick' => $contact['nick']]);
880 $result = bluesky_process_post($notification, $uid, Item::PR_PUSHED);
881 Logger::debug('Got mention', ['uid' => $uid, 'result' => $result]);
885 $result = bluesky_process_post($notification, $uid, Item::PR_PUSHED);
886 Logger::debug('Got reply', ['uid' => $uid, 'result' => $result]);
890 $result = bluesky_process_post($notification, $uid, Item::PR_PUSHED);
891 Logger::debug('Got quote', ['uid' => $uid, 'result' => $result]);
895 Logger::notice('Unhandled reason', ['reason' => $notification->reason]);
901 function bluesky_fetch_feed(int $uid, string $feed)
903 $data = bluesky_get($uid, '/xrpc/app.bsky.feed.getFeed?feed=' . $feed, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]);
908 if (empty($data->feed)) {
912 foreach (array_reverse($data->feed) as $entry) {
913 if (!Relay::isWantedLanguage($entry->post->record->text)) {
914 Logger::debug('Unwanted language detected', ['text' => $entry->post->record->text]);
917 bluesky_process_post($entry->post, $uid, Item::PR_TAG);
918 if (!empty($entry->reason)) {
919 bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid);
924 function bluesky_process_post(stdClass $post, int $uid, int $post_reason): int
926 $uri = bluesky_get_uri($post);
928 if (Post::exists(['uri' => $uri, 'uid' => $uid]) || Post::exists(['extid' => $uri, 'uid' => $uid])) {
932 Logger::debug('Importing post', ['uid' => $uid, 'indexedAt' => $post->indexedAt, 'uri' => $post->uri, 'cid' => $post->cid]);
934 $item = bluesky_get_header($post, $uri, $uid, $uid);
936 $item = bluesky_get_content($item, $post->record, $uid);
938 if (!empty($post->embed)) {
939 $item = bluesky_add_media($post->embed, $item, $uid);
942 if (empty($item['post-reason'])) {
943 $item['post-reason'] = $post_reason;
946 return item::insert($item);
949 function bluesky_get_header(stdClass $post, string $uri, int $uid, int $fetch_uid): array
951 $parts = bluesky_get_uri_parts($uri);
952 if (empty($post->author)) {
955 $contact = bluesky_get_contact($post->author, $uid, $fetch_uid);
957 'network' => Protocol::BLUESKY,
961 'guid' => $post->cid,
962 'private' => Item::UNLISTED,
963 'verb' => Activity::POST,
964 'contact-id' => $contact['id'],
965 'author-name' => $contact['name'],
966 'author-link' => $contact['url'],
967 'author-avatar' => $contact['avatar'],
968 'plink' => $contact['alias'] . '/post/' . $parts->rkey,
971 $item['uri-id'] = ItemURI::getIdByURI($uri);
972 $item['owner-name'] = $item['author-name'];
973 $item['owner-link'] = $item['author-link'];
974 $item['owner-avatar'] = $item['author-avatar'];
976 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
977 $item['post-reason'] = Item::PR_FOLLOWER;
983 function bluesky_get_content(array $item, stdClass $record, int $uid): array
985 if (!empty($record->reply)) {
986 $item['parent-uri'] = bluesky_get_uri($record->reply->root);
987 $item['parent-uri'] = bluesky_fetch_missing_post($item['parent-uri'], $uid, $item['contact-id']);
988 $item['thr-parent'] = bluesky_get_uri($record->reply->parent);
989 $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $item['contact-id']);
992 $item['body'] = bluesky_get_text($record, $uid);
993 $item['created'] = DateTimeFormat::utc($record->createdAt, DateTimeFormat::MYSQL);
997 function bluesky_get_text(stdClass $record, int $uid): string
999 $text = $record->text;
1001 if (empty($record->facets)) {
1006 foreach ($record->facets as $facet) {
1007 $facets[$facet->index->byteStart] = $facet;
1011 foreach ($facets as $facet) {
1012 $prefix = substr($text, 0, $facet->index->byteStart);
1013 $linktext = substr($text, $facet->index->byteStart, $facet->index->byteEnd - $facet->index->byteStart);
1014 $suffix = substr($text, $facet->index->byteEnd);
1018 foreach ($facet->features as $feature) {
1020 switch ($feature->$type) {
1021 case 'app.bsky.richtext.facet#link':
1022 $url = $feature->uri;
1025 case 'app.bsky.richtext.facet#mention':
1026 $contact = Contact::selectFirst(['id'], ['nurl' => $feature->did, 'uid' => [0, $uid]]);
1027 if (!empty($contact['id'])) {
1028 $url = DI::baseUrl() . '/contact/' . $contact['id'];
1029 if (substr($linktext, 0, 1) == '@') {
1031 $linktext = substr($linktext, 1);
1037 Logger::notice('Unhandled feature type', ['type' => $feature->$type, 'record' => $record]);
1042 $text = $prefix . '[url=' . $url . ']' . $linktext . '[/url]' . $suffix;
1048 function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid): array
1051 switch ($embed->$type) {
1052 case 'app.bsky.embed.images#view':
1053 foreach ($embed->images as $image) {
1055 'uri-id' => $item['uri-id'],
1056 'type' => Post\Media::IMAGE,
1057 'url' => $image->fullsize,
1058 'preview' => $image->thumb,
1059 'description' => $image->alt,
1061 Post\Media::insert($media);
1065 case 'app.bsky.embed.external#view':
1067 'uri-id' => $item['uri-id'],
1068 'type' => Post\Media::HTML,
1069 'url' => $embed->external->uri,
1070 'name' => $embed->external->title,
1071 'description' => $embed->external->description,
1073 Post\Media::insert($media);
1076 case 'app.bsky.embed.record#view':
1077 $uri = bluesky_get_uri($embed->record);
1078 $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]);
1079 if (empty($shared)) {
1080 $shared = bluesky_get_header($embed->record, $uri, 0, $fetch_uid);
1081 if (!empty($shared)) {
1082 $shared = bluesky_get_content($shared, $embed->record->value, $item['uid']);
1084 if (!empty($embed->record->embeds)) {
1085 foreach ($embed->record->embeds as $single) {
1086 $shared = bluesky_add_media($single, $shared, $fetch_uid);
1089 $id = Item::insert($shared);
1090 $shared = Post::selectFirst(['uri-id'], ['id' => $id]);
1093 if (!empty($shared)) {
1094 $item['quote-uri-id'] = $shared['uri-id'];
1098 case 'app.bsky.embed.recordWithMedia#view':
1099 $uri = bluesky_get_uri($embed->record->record);
1100 $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]);
1101 if (empty($shared)) {
1102 $shared = bluesky_get_header($embed->record->record, $uri, 0, $fetch_uid);
1103 if (!empty($shared)) {
1104 $shared = bluesky_get_content($shared, $embed->record->record->value, $item['uid']);
1106 if (!empty($embed->record->embeds)) {
1107 foreach ($embed->record->record->embeds as $single) {
1108 $shared = bluesky_add_media($single, $shared, $fetch_uid);
1112 if (!empty($embed->media)) {
1113 bluesky_add_media($embed->media, $item, $fetch_uid);
1116 $id = Item::insert($shared);
1117 $shared = Post::selectFirst(['uri-id'], ['id' => $id]);
1120 if (!empty($shared)) {
1121 $item['quote-uri-id'] = $shared['uri-id'];
1126 Logger::notice('Unhandled embed type', ['type' => $embed->$type, 'embed' => $embed]);
1132 function bluesky_get_uri(stdClass $post): string
1134 return $post->uri . ':' . $post->cid;
1137 function bluesky_get_uri_class(string $uri): ?stdClass
1143 $elements = explode(':', $uri);
1144 if (empty($elements) || ($elements[0] != 'at')) {
1145 $post = Post::selectFirstPost(['extid'], ['uri' => $uri]);
1146 return bluesky_get_uri_class($post['extid'] ?? '');
1149 $class = new stdClass;
1151 $class->cid = array_pop($elements);
1152 $class->uri = implode(':', $elements);
1154 if ((substr_count($class->uri, '/') == 2) && (substr_count($class->cid, '/') == 2)) {
1155 $class->uri .= ':' . $class->cid;
1162 function bluesky_get_uri_parts(string $uri): ?stdClass
1164 $class = bluesky_get_uri_class($uri);
1165 if (empty($class)) {
1169 $parts = explode('/', substr($class->uri, 5));
1171 $class = new stdClass;
1173 $class->repo = $parts[0];
1174 $class->collection = $parts[1];
1175 $class->rkey = $parts[2];
1180 function bluesky_fetch_missing_post(string $uri, int $uid, int $causer): string
1182 if (Post::exists(['uri' => $uri, 'uid' => [$uid, 0]])) {
1183 Logger::debug('Post exists', ['uri' => $uri]);
1187 $reply = Post::selectFirst(['uri'], ['extid' => $uri, 'uid' => [$uid, 0]]);
1188 if (!empty($reply['uri'])) {
1189 return $reply['uri'];
1192 Logger::debug('Fetch missing post', ['uri' => $uri]);
1193 $class = bluesky_get_uri_class($uri);
1194 $fetch_uri = $class->uri;
1196 $data = bluesky_get($uid, '/xrpc/app.bsky.feed.getPosts?uris=' . urlencode($fetch_uri), HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]);
1202 $cdata = Contact::getPublicAndUserContactID($causer, $uid);
1205 foreach ($data->posts as $post) {
1206 $uri = bluesky_get_uri($post);
1207 $item = bluesky_get_header($post, $uri, $uid, $uid);
1208 $item = bluesky_get_content($item, $post->record, $uid);
1210 $item['post-reason'] = Item::PR_FETCHED;
1212 if (!empty($cdata['public'])) {
1213 $item['causer-id'] = $cdata['public'];
1216 if (!empty($post->embed)) {
1217 $item = bluesky_add_media($post->embed, $item, $uid);
1219 $id = Item::insert($item);
1220 Logger::debug('Stored item', ['id' => $id, 'uri' => $uri]);
1226 function bluesky_get_contact(stdClass $author, int $uid, int $fetch_uid): array
1228 $condition = ['network' => Protocol::BLUESKY, 'uid' => 0, 'url' => $author->did];
1229 $contact = Contact::selectFirst(['id', 'updated'], $condition);
1231 $update = empty($contact) || $contact['updated'] < DateTimeFormat::utc('now -24 hours');
1233 $public_fields = $fields = bluesky_get_contact_fields($author, $fetch_uid, $update);
1235 $public_fields['uid'] = 0;
1236 $public_fields['rel'] = Contact::NOTHING;
1238 if (empty($contact)) {
1239 $cid = Contact::insert($public_fields);
1241 $cid = $contact['id'];
1242 Contact::update($public_fields, ['id' => $cid], true);
1246 $condition = ['network' => Protocol::BLUESKY, 'uid' => $uid, 'url' => $author->did];
1248 $contact = Contact::selectFirst(['id', 'rel', 'uid'], $condition);
1249 if (!isset($fields['rel']) && isset($contact['rel'])) {
1250 $fields['rel'] = $contact['rel'];
1251 } elseif (!isset($fields['rel'])) {
1252 $fields['rel'] = Contact::NOTHING;
1256 if (($uid != 0) && ($fields['rel'] != Contact::NOTHING)) {
1257 if (empty($contact)) {
1258 $cid = Contact::insert($fields);
1260 $cid = $contact['id'];
1261 Contact::update($fields, ['id' => $cid], true);
1263 Logger::debug('Get user contact', ['id' => $cid, 'uid' => $uid, 'update' => $update]);
1265 Logger::debug('Get public contact', ['id' => $cid, 'uid' => $uid, 'update' => $update]);
1267 if (!empty($author->avatar)) {
1268 Contact::updateAvatar($cid, $author->avatar);
1271 return Contact::getById($cid);
1274 function bluesky_get_contact_fields(stdClass $author, int $uid, bool $update): array
1278 'network' => Protocol::BLUESKY,
1282 'readonly' => false,
1284 'baseurl' => BLUESKY_HOST,
1285 'url' => $author->did,
1286 'nurl' => $author->did,
1287 'alias' => BLUESKY_HOST . '/profile/' . $author->handle,
1288 'name' => $author->displayName ?? $author->handle,
1289 'nick' => $author->handle,
1290 'addr' => $author->handle,
1294 Logger::debug('Got contact fields', ['uid' => $uid, 'url' => $fields['url']]);
1298 $data = bluesky_get($uid, '/xrpc/app.bsky.actor.getProfile?actor=' . $author->did, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]);
1300 Logger::debug('Error fetching contact fields', ['uid' => $uid, 'url' => $fields['url']]);
1304 $fields['updated'] = DateTimeFormat::utcNow(DateTimeFormat::MYSQL);
1306 if (!empty($data->description)) {
1307 $fields['about'] = HTML::toBBCode($data->description);
1310 if (!empty($data->banner)) {
1311 $fields['header'] = $data->banner;
1314 if (!empty($data->viewer)) {
1315 if (!empty($data->viewer->following) && !empty($data->viewer->followedBy)) {
1316 $fields['rel'] = Contact::FRIEND;
1317 } elseif (!empty($data->viewer->following) && empty($data->viewer->followedBy)) {
1318 $fields['rel'] = Contact::SHARING;
1319 } elseif (empty($data->viewer->following) && !empty($data->viewer->followedBy)) {
1320 $fields['rel'] = Contact::FOLLOWER;
1322 $fields['rel'] = Contact::NOTHING;
1326 Logger::debug('Got updated contact fields', ['uid' => $uid, 'url' => $fields['url']]);
1330 function bluesky_get_feeds(int $uid): array
1333 $preferences = bluesky_get_preferences($uid);
1334 foreach ($preferences->preferences as $preference) {
1335 if ($preference->$type == 'app.bsky.actor.defs#savedFeedsPref') {
1336 return $preference->pinned ?? [];
1342 function bluesky_get_preferences(int $uid): stdClass
1344 $cachekey = 'bluesky:preferences:' . $uid;
1345 $data = DI::cache()->get($cachekey);
1346 if (!is_null($data)) {
1350 $data = bluesky_get($uid, '/xrpc/app.bsky.actor.getPreferences', HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]);
1352 DI::cache()->set($cachekey, $data, Duration::HOUR);
1356 function bluesky_get_did(int $uid, string $handle): string
1358 $data = bluesky_get($uid, '/xrpc/com.atproto.identity.resolveHandle?handle=' . $handle);
1362 Logger::debug('Got DID', ['return' => $data]);
1366 function bluesky_get_token(int $uid): string
1368 $token = DI::pConfig()->get($uid, 'bluesky', 'access_token');
1369 $created = DI::pConfig()->get($uid, 'bluesky', 'token_created');
1370 if (empty($token)) {
1374 if ($created + 300 < time()) {
1375 return bluesky_refresh_token($uid);
1380 function bluesky_refresh_token(int $uid): string
1382 $token = DI::pConfig()->get($uid, 'bluesky', 'refresh_token');
1384 $data = bluesky_post($uid, '/xrpc/com.atproto.server.refreshSession', '', ['Authorization' => ['Bearer ' . $token]]);
1389 Logger::debug('Refreshed token', ['return' => $data]);
1390 DI::pConfig()->set($uid, 'bluesky', 'access_token', $data->accessJwt);
1391 DI::pConfig()->set($uid, 'bluesky', 'refresh_token', $data->refreshJwt);
1392 DI::pConfig()->set($uid, 'bluesky', 'token_created', time());
1393 return $data->accessJwt;
1396 function bluesky_create_token(int $uid, string $password): string
1398 $did = DI::pConfig()->get($uid, 'bluesky', 'did');
1400 $data = bluesky_post($uid, '/xrpc/com.atproto.server.createSession', json_encode(['identifier' => $did, 'password' => $password]), ['Content-type' => 'application/json']);
1405 Logger::debug('Created token', ['return' => $data]);
1406 DI::pConfig()->set($uid, 'bluesky', 'access_token', $data->accessJwt);
1407 DI::pConfig()->set($uid, 'bluesky', 'refresh_token', $data->refreshJwt);
1408 DI::pConfig()->set($uid, 'bluesky', 'token_created', time());
1409 return $data->accessJwt;
1412 function bluesky_post(int $uid, string $url, string $params, array $headers): ?stdClass
1415 $curlResult = DI::httpClient()->post(DI::pConfig()->get($uid, 'bluesky', 'host') . $url, $params, $headers);
1416 } catch (\Exception $e) {
1417 Logger::notice('Exception on post', ['exception' => $e]);
1421 if (!$curlResult->isSuccess()) {
1422 Logger::notice('API Error', ['error' => json_decode($curlResult->getBody()) ?: $curlResult->getBody()]);
1426 return json_decode($curlResult->getBody());
1429 function bluesky_get(int $uid, string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ?stdClass
1432 $curlResult = DI::httpClient()->get(DI::pConfig()->get($uid, 'bluesky', 'host') . $url, $accept_content, $opts);
1433 } catch (\Exception $e) {
1434 Logger::notice('Exception on get', ['exception' => $e]);
1438 if (!$curlResult->isSuccess()) {
1439 Logger::notice('API Error', ['error' => json_decode($curlResult->getBody()) ?: $curlResult->getBody()]);
1443 return json_decode($curlResult->getBody());