3 * Name: Bluesky Connector
4 * Description: Post to Bluesky
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
9 * Currently technical issues in the core:
12 * At some point in time:
13 * - Sending Quote shares https://atproto.com/lexicons/app-bsky-embed#appbskyembedrecord and https://atproto.com/lexicons/app-bsky-embed#appbskyembedrecordwithmedia
15 * Possibly not possible:
16 * - only fetch new posts
18 * Currently not possible, due to limitations in Friendica
19 * - mute contacts https://atproto.com/lexicons/app-bsky-graph#appbskygraphmuteactor
20 * - unmute contacts https://atproto.com/lexicons/app-bsky-graph#appbskygraphunmuteactor
22 * Possibly interesting:
23 * - https://atproto.com/lexicons/com-atproto-label#comatprotolabelsubscribelabels
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\GServer;
40 use Friendica\Model\Item;
41 use Friendica\Model\ItemURI;
42 use Friendica\Model\Photo;
43 use Friendica\Model\Post;
44 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
45 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
46 use Friendica\Object\Image;
47 use Friendica\Protocol\Activity;
48 use Friendica\Protocol\Relay;
49 use Friendica\Util\DateTimeFormat;
50 use Friendica\Util\Strings;
52 const BLUESKY_DEFAULT_POLL_INTERVAL = 10; // given in minutes
53 const BLUESKY_IMAGE_SIZE = [1000000, 500000, 100000, 50000];
56 * (Currently) hard wired paths for Bluesky services
58 const BLUESKY_DIRECTORY = 'https://plc.directory'; // Path to the directory server service to fetch the PDS of a given DID
59 const BLUESKY_PDS = 'https://bsky.social'; // Path to the personal data server service (PDS) to fetch the DID for a given handle
60 const BLUESKY_WEB = 'https://bsky.app'; // Path to the web interface with the user profile and posts
62 function bluesky_install()
64 Hook::register('load_config', __FILE__, 'bluesky_load_config');
65 Hook::register('hook_fork', __FILE__, 'bluesky_hook_fork');
66 Hook::register('post_local', __FILE__, 'bluesky_post_local');
67 Hook::register('notifier_normal', __FILE__, 'bluesky_send');
68 Hook::register('jot_networks', __FILE__, 'bluesky_jot_nets');
69 Hook::register('connector_settings', __FILE__, 'bluesky_settings');
70 Hook::register('connector_settings_post', __FILE__, 'bluesky_settings_post');
71 Hook::register('cron', __FILE__, 'bluesky_cron');
72 Hook::register('support_follow', __FILE__, 'bluesky_support_follow');
73 Hook::register('support_probe', __FILE__, 'bluesky_support_probe');
74 Hook::register('follow', __FILE__, 'bluesky_follow');
75 Hook::register('unfollow', __FILE__, 'bluesky_unfollow');
76 Hook::register('block', __FILE__, 'bluesky_block');
77 Hook::register('unblock', __FILE__, 'bluesky_unblock');
78 Hook::register('check_item_notification', __FILE__, 'bluesky_check_item_notification');
79 Hook::register('probe_detect', __FILE__, 'bluesky_probe_detect');
80 Hook::register('item_by_link', __FILE__, 'bluesky_item_by_link');
83 function bluesky_load_config(ConfigFileManager $loader)
85 DI::app()->getConfigCache()->load($loader->loadAddonConfig('bluesky'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
88 function bluesky_check_item_notification(array &$notification_data)
90 $did = DI::pConfig()->get($notification_data['uid'], 'bluesky', 'did');
93 $notification_data['profiles'][] = $did;
97 function bluesky_probe_detect(array &$hookData)
99 // Don't overwrite an existing result
100 if (isset($hookData['result'])) {
104 // Avoid a lookup for the wrong network
105 if (!in_array($hookData['network'], ['', Protocol::BLUESKY])) {
109 $pconfig = DBA::selectFirst('pconfig', ['uid'], ["`cat` = ? AND `k` = ? AND `v` != ?", 'bluesky', 'access_token', '']);
110 if (empty($pconfig['uid'])) {
114 if (parse_url($hookData['uri'], PHP_URL_SCHEME) == 'did') {
115 $did = $hookData['uri'];
116 } elseif (preg_match('#^' . BLUESKY_WEB . '/profile/(.+)#', $hookData['uri'], $matches)) {
117 $did = bluesky_get_did($matches[1]);
125 $token = bluesky_get_token($pconfig['uid']);
130 $data = bluesky_xrpc_get($pconfig['uid'], 'app.bsky.actor.getProfile', ['actor' => $did]);
135 $hookData['result'] = bluesky_get_contact_fields($data, 0, false);
137 $hookData['result']['baseurl'] = bluesky_get_pds($did);
139 // Preparing probe data. This differs slightly from the contact array
140 $hookData['result']['about'] = HTML::toBBCode($data->description ?? '');
141 $hookData['result']['photo'] = $data->avatar ?? '';
142 $hookData['result']['header'] = $data->banner ?? '';
143 $hookData['result']['batch'] = '';
144 $hookData['result']['notify'] = '';
145 $hookData['result']['poll'] = '';
146 $hookData['result']['poco'] = '';
147 $hookData['result']['pubkey'] = '';
148 $hookData['result']['priority'] = 0;
149 $hookData['result']['guid'] = '';
152 function bluesky_item_by_link(array &$hookData)
154 // Don't overwrite an existing result
155 if (isset($hookData['item_id'])) {
159 $token = bluesky_get_token($hookData['uid']);
164 if (!preg_match('#^' . BLUESKY_WEB . '/profile/(.+)/post/(.+)#', $hookData['uri'], $matches)) {
168 $did = bluesky_get_did($matches[1]);
173 Logger::debug('Found bluesky post', ['url' => $hookData['uri'], 'handle' => $matches[1], 'did' => $did, 'cid' => $matches[2]]);
175 $uri = 'at://' . $did . '/app.bsky.feed.post/' . $matches[2];
177 $uri = bluesky_fetch_missing_post($uri, $hookData['uid'], $hookData['uid'], 0, 0);
178 Logger::debug('Got post', ['profile' => $matches[1], 'cid' => $matches[2], 'result' => $uri]);
180 $item = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $hookData['uid']]);
181 if (!empty($item['id'])) {
182 $hookData['item_id'] = $item['id'];
187 function bluesky_support_follow(array &$data)
189 if ($data['protocol'] == Protocol::BLUESKY) {
190 $data['result'] = true;
194 function bluesky_support_probe(array &$data)
196 if ($data['protocol'] == Protocol::BLUESKY) {
197 $data['result'] = true;
201 function bluesky_follow(array &$hook_data)
203 $token = bluesky_get_token($hook_data['uid']);
208 Logger::debug('Check if contact is bluesky', ['data' => $hook_data]);
209 $contact = DBA::selectFirst('contact', [], ['network' => Protocol::BLUESKY, 'url' => $hook_data['url'], 'uid' => [0, $hook_data['uid']]]);
210 if (empty($contact)) {
215 'subject' => $contact['url'],
216 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
217 '$type' => 'app.bsky.graph.follow'
221 'collection' => 'app.bsky.graph.follow',
222 'repo' => DI::pConfig()->get($hook_data['uid'], 'bluesky', 'did'),
226 $activity = bluesky_xrpc_post($hook_data['uid'], 'com.atproto.repo.createRecord', $post);
227 if (!empty($activity->uri)) {
228 $hook_data['contact'] = $contact;
229 Logger::debug('Successfully start following', ['url' => $contact['url'], 'uri' => $activity->uri]);
233 function bluesky_unfollow(array &$hook_data)
235 $token = bluesky_get_token($hook_data['uid']);
240 if ($hook_data['contact']['network'] != Protocol::BLUESKY) {
244 $data = bluesky_xrpc_get($hook_data['uid'], 'app.bsky.actor.getProfile', ['actor' => $hook_data['contact']['url']]);
245 if (empty($data->viewer) || empty($data->viewer->following)) {
249 bluesky_delete_post($data->viewer->following, $hook_data['uid']);
251 $hook_data['result'] = true;
254 function bluesky_block(array &$hook_data)
256 $token = bluesky_get_token($hook_data['uid']);
261 Logger::debug('Check if contact is bluesky', ['data' => $hook_data]);
262 $contact = DBA::selectFirst('contact', [], ['network' => Protocol::BLUESKY, 'url' => $hook_data['url'], 'uid' => [0, $hook_data['uid']]]);
263 if (empty($contact)) {
268 'subject' => $contact['url'],
269 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
270 '$type' => 'app.bsky.graph.block'
274 'collection' => 'app.bsky.graph.block',
275 'repo' => DI::pConfig()->get($hook_data['uid'], 'bluesky', 'did'),
279 $activity = bluesky_xrpc_post($hook_data['uid'], 'com.atproto.repo.createRecord', $post);
280 if (!empty($activity->uri)) {
281 $cdata = Contact::getPublicAndUserContactID($hook_data['contact']['id'], $hook_data['uid']);
282 if (!empty($cdata['user'])) {
283 Contact::remove($cdata['user']);
285 Logger::debug('Successfully blocked contact', ['url' => $hook_data['contact']['url'], 'uri' => $activity->uri]);
289 function bluesky_unblock(array &$hook_data)
291 $token = bluesky_get_token($hook_data['uid']);
296 if ($hook_data['contact']['network'] != Protocol::BLUESKY) {
300 $data = bluesky_xrpc_get($hook_data['uid'], 'app.bsky.actor.getProfile', ['actor' => $hook_data['contact']['url']]);
301 if (empty($data->viewer) || empty($data->viewer->blocking)) {
305 bluesky_delete_post($data->viewer->blocking, $hook_data['uid']);
307 $hook_data['result'] = true;
310 function bluesky_settings(array &$data)
312 if (!DI::userSession()->getLocalUserId()) {
316 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post') ?? false;
317 $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default') ?? false;
318 $pds = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'pds');
319 $handle = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle');
320 $did = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
321 $token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'access_token');
322 $import = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'import') ?? false;
323 $import_feeds = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'import_feeds') ?? false;
325 $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.');
327 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/bluesky/');
328 $html = Renderer::replaceMacros($t, [
329 '$enable' => ['bluesky', DI::l10n()->t('Enable Bluesky Post Addon'), $enabled],
330 '$bydefault' => ['bluesky_bydefault', DI::l10n()->t('Post to Bluesky by default'), $def_enabled],
331 '$import' => ['bluesky_import', DI::l10n()->t('Import the remote timeline'), $import],
332 '$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.')],
333 '$pds' => ['bluesky_pds', DI::l10n()->t('Personal Data Server'), $pds, DI::l10n()->t('The personal data server (PDS) is the system that hosts your profile.'), '', 'readonly'],
334 '$handle' => ['bluesky_handle', DI::l10n()->t('Bluesky handle'), $handle],
335 '$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'],
336 '$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.")],
341 'connector' => 'bluesky',
342 'title' => DI::l10n()->t('Bluesky Import/Export'),
343 'image' => 'images/bluesky.jpg',
344 'enabled' => $enabled,
349 function bluesky_settings_post(array &$b)
351 if (empty($_POST['bluesky-submit'])) {
355 $old_pds = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'pds');
356 $old_handle = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle');
357 $old_did = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
359 $handle = $_POST['bluesky_handle'];
361 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'post', intval($_POST['bluesky']));
362 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default', intval($_POST['bluesky_bydefault']));
363 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'handle', $handle);
364 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'import', intval($_POST['bluesky_import']));
365 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'import_feeds', intval($_POST['bluesky_import_feeds']));
367 if (!empty($host) && !empty($handle)) {
368 if (empty($old_did) || $old_handle != $handle) {
369 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'did', bluesky_get_did(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle')));
371 if (empty($old_pds) || $old_handle != $handle) {
372 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'pds', bluesky_get_pds(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'did')));
375 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
376 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'bluesky', 'pds');
379 if (!empty($_POST['bluesky_password'])) {
380 bluesky_create_token(DI::userSession()->getLocalUserId(), $_POST['bluesky_password']);
384 function bluesky_jot_nets(array &$jotnets_fields)
386 if (!DI::userSession()->getLocalUserId()) {
390 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post')) {
391 $jotnets_fields[] = [
392 'type' => 'checkbox',
395 DI::l10n()->t('Post to Bluesky'),
396 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default')
402 function bluesky_cron()
404 $last = DI::keyValue()->get('bluesky_last_poll');
406 $poll_interval = intval(DI::config()->get('bluesky', 'poll_interval'));
407 if (!$poll_interval) {
408 $poll_interval = BLUESKY_DEFAULT_POLL_INTERVAL;
412 $next = $last + ($poll_interval * 60);
413 if ($next > time()) {
414 Logger::notice('poll interval not reached');
418 Logger::notice('cron_start');
420 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
421 if ($abandon_days < 1) {
425 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
427 $pconfigs = DBA::selectToArray('pconfig', [], ['cat' => 'bluesky', 'k' => 'import', 'v' => true]);
428 foreach ($pconfigs as $pconfig) {
429 if ($abandon_days != 0) {
430 if (!DBA::exists('user', ["`uid` = ? AND `login_date` >= ?", $pconfig['uid'], $abandon_limit])) {
431 Logger::notice('abandoned account: timeline from user will not be imported', ['user' => $pconfig['uid']]);
436 // Refresh the token now, so that it doesn't need to be refreshed in parallel by the following workers
437 bluesky_get_token($pconfig['uid']);
439 Worker::add(['priority' => Worker::PRIORITY_MEDIUM, 'force_priority' => true], 'addon/bluesky/bluesky_timeline.php', $pconfig['uid']);
440 Worker::add(['priority' => Worker::PRIORITY_MEDIUM, 'force_priority' => true], 'addon/bluesky/bluesky_notifications.php', $pconfig['uid']);
442 if (DI::pConfig()->get($pconfig['uid'], 'bluesky', 'import_feeds')) {
443 $feeds = bluesky_get_feeds($pconfig['uid']);
444 foreach ($feeds as $feed) {
445 Worker::add(['priority' => Worker::PRIORITY_MEDIUM, 'force_priority' => true], 'addon/bluesky/bluesky_feed.php', $pconfig['uid'], $feed);
450 $last_clean = DI::keyValue()->get('bluesky_last_clean');
451 if (empty($last_clean) || ($last_clean + 86400 < time())) {
452 Logger::notice('Start contact cleanup');
453 $contacts = DBA::select('account-user-view', ['id', 'pid'], ["`network` = ? AND `uid` != ? AND `rel` = ?", Protocol::BLUESKY, 0, Contact::NOTHING]);
454 while ($contact = DBA::fetch($contacts)) {
455 Worker::add(Worker::PRIORITY_LOW, 'MergeContact', $contact['pid'], $contact['id'], 0);
457 DBA::close($contacts);
458 DI::keyValue()->set('bluesky_last_clean', time());
459 Logger::notice('Contact cleanup done');
462 Logger::notice('cron_end');
464 DI::keyValue()->set('bluesky_last_poll', time());
467 function bluesky_hook_fork(array &$b)
469 if ($b['name'] != 'notifier_normal') {
475 if (($post['created'] !== $post['edited']) && !$post['deleted']) {
476 DI::logger()->info('Editing is not supported by the addon');
477 $b['execute'] = false;
481 if (DI::pConfig()->get($post['uid'], 'bluesky', 'import')) {
482 // Don't post if it isn't a reply to a bluesky post
483 if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::BLUESKY])) {
484 Logger::notice('No bluesky parent found', ['item' => $post['id']]);
485 $b['execute'] = false;
488 } elseif (!strstr($post['postopts'] ?? '', 'bluesky') || ($post['parent'] != $post['id']) || $post['private']) {
489 DI::logger()->info('Activities are never exported when we don\'t import the bluesky timeline', ['uid' => $post['uid']]);
490 $b['execute'] = false;
495 function bluesky_post_local(array &$b)
501 if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
505 if ($b['private'] || $b['parent']) {
509 $bluesky_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post'));
510 $bluesky_enable = (($bluesky_post && !empty($_REQUEST['bluesky_enable'])) ? intval($_REQUEST['bluesky_enable']) : 0);
512 // if API is used, default to the chosen settings
513 if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default'))) {
517 if (!$bluesky_enable) {
521 if (strlen($b['postopts'])) {
522 $b['postopts'] .= ',';
525 $b['postopts'] .= 'bluesky';
528 function bluesky_send(array &$b)
530 if (($b['created'] !== $b['edited']) && !$b['deleted']) {
534 if ($b['gravity'] != Item::GRAVITY_PARENT) {
535 Logger::debug('Got comment', ['item' => $b]);
538 $uri = bluesky_get_uri_class($b['uri']);
540 Logger::debug('Not a bluesky post', ['uri' => $b['uri']]);
543 bluesky_delete_post($b['uri'], $b['uid']);
547 $root = bluesky_get_uri_class($b['parent-uri']);
548 $parent = bluesky_get_uri_class($b['thr-parent']);
550 if (empty($root) || empty($parent)) {
551 Logger::debug('No bluesky post', ['parent' => $b['parent'], 'thr-parent' => $b['thr-parent']]);
555 if ($b['gravity'] == Item::GRAVITY_COMMENT) {
556 Logger::debug('Posting comment', ['root' => $root, 'parent' => $parent]);
557 bluesky_create_post($b, $root, $parent);
559 } elseif (in_array($b['verb'], [Activity::LIKE, Activity::ANNOUNCE])) {
560 bluesky_create_activity($b, $parent);
563 } elseif ($b['private'] || !strstr($b['postopts'], 'bluesky')) {
567 bluesky_create_post($b);
570 function bluesky_create_activity(array $item, stdClass $parent = null)
573 $token = bluesky_get_token($uid);
578 $did = DI::pConfig()->get($uid, 'bluesky', 'did');
580 if ($item['verb'] == Activity::LIKE) {
582 'subject' => $parent,
583 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
584 '$type' => 'app.bsky.feed.like'
588 'collection' => 'app.bsky.feed.like',
592 } elseif ($item['verb'] == Activity::ANNOUNCE) {
594 'subject' => $parent,
595 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
596 '$type' => 'app.bsky.feed.repost'
600 'collection' => 'app.bsky.feed.repost',
606 $activity = bluesky_xrpc_post($uid, 'com.atproto.repo.createRecord', $post);
607 if (empty($activity)) {
610 Logger::debug('Activity done', ['return' => $activity]);
611 $uri = bluesky_get_uri($activity);
612 Item::update(['extid' => $uri], ['id' => $item['id']]);
613 Logger::debug('Set extid', ['id' => $item['id'], 'extid' => $activity]);
616 function bluesky_create_post(array $item, stdClass $root = null, stdClass $parent = null)
619 $token = bluesky_get_token($uid);
624 // Try to fetch the language from the post itself
625 if (!empty($item['language'])) {
626 $language = array_key_first(json_decode($item['language'], true));
631 $did = DI::pConfig()->get($uid, 'bluesky', 'did');
632 $urls = bluesky_get_urls(Post\Media::removeFromBody($item['body']));
633 $item['body'] = $urls['body'];
635 $msg = Plaintext::getPost($item, 300, false, BBCode::BLUESKY);
636 foreach ($msg['parts'] as $key => $part) {
638 $facets = bluesky_get_facets($part, $urls['urls']);
641 'text' => $facets['body'],
642 '$type' => 'app.bsky.feed.post',
643 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
646 if (!empty($language)) {
647 $record['langs'] = [$language];
650 if (!empty($facets['facets'])) {
651 $record['facets'] = $facets['facets'];
655 $record['reply'] = ['root' => $root, 'parent' => $parent];
658 if ($key == count($msg['parts']) - 1) {
659 $record = bluesky_add_embed($uid, $msg, $record);
660 if (empty($record)) {
661 if (Worker::getRetrial() < 3) {
669 'collection' => 'app.bsky.feed.post',
674 $parent = bluesky_xrpc_post($uid, 'com.atproto.repo.createRecord', $post);
675 if (empty($parent)) {
681 Logger::debug('Posting done', ['return' => $parent]);
685 if (($key == 0) && ($item['gravity'] != Item::GRAVITY_PARENT)) {
686 $uri = bluesky_get_uri($parent);
687 Item::update(['extid' => $uri], ['id' => $item['id']]);
688 Logger::debug('Set extid', ['id' => $item['id'], 'extid' => $uri]);
693 function bluesky_get_urls(string $body): array
695 // Remove all hashtag and mention links
696 $body = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $body);
698 $body = BBCode::expandVideoLinks($body);
701 // Search for hash tags
702 if (preg_match_all("/#\[url\=(https?:.*?)\](.*?)\[\/url\]/ism", $body, $matches, PREG_SET_ORDER)) {
703 foreach ($matches as $match) {
704 $text = '#' . $match[2];
705 $urls[] = ['tag' => $match[2], 'text' => $text, 'hash' => $text];
706 $body = str_replace($match[0], $text, $body);
710 // Search for pure links
711 if (preg_match_all("/\[url\](https?:.*?)\[\/url\]/ism", $body, $matches, PREG_SET_ORDER)) {
712 foreach ($matches as $match) {
713 $text = Strings::getStyledURL($match[1]);
714 $hash = bluesky_get_hash_for_url($match[0], mb_strlen($text));
715 $urls[] = ['url' => $match[1], 'text' => $text, 'hash' => $hash];
716 $body = str_replace($match[0], $hash, $body);
720 // Search for links with descriptions
721 if (preg_match_all("/\[url\=(https?:.*?)\](.*?)\[\/url\]/ism", $body, $matches, PREG_SET_ORDER)) {
722 foreach ($matches as $match) {
723 if ($match[1] == $match[2]) {
724 $text = Strings::getStyledURL($match[1]);
728 if (mb_strlen($text) < 100) {
729 $hash = bluesky_get_hash_for_url($match[0], mb_strlen($text));
730 $urls[] = ['url' => $match[1], 'text' => $text, 'hash' => $hash];
731 $body = str_replace($match[0], $hash, $body);
733 $text = Strings::getStyledURL($match[1]);
734 $hash = bluesky_get_hash_for_url($match[0], mb_strlen($text));
735 $urls[] = ['url' => $match[1], 'text' => $text, 'hash' => $hash];
736 $body = str_replace($match[0], $text . ' ' . $hash, $body);
741 return ['body' => $body, 'urls' => $urls];
744 function bluesky_get_hash_for_url(string $text, int $linklength): string
746 if ($linklength <= 10) {
747 return '|' . hash('crc32', $text) . '|';
749 return substr('|' . hash('crc32', $text) . base64_encode($text), 0, $linklength - 2) . '|';
752 function bluesky_get_facets(string $body, array $urls): array
756 foreach ($urls as $url) {
757 $pos = strpos($body, $url['hash']);
758 if ($pos === false) {
762 $prefix = substr($body, 0, $pos);
767 $body = $prefix . $url['text'] . substr($body, $pos + strlen($url['hash']));
769 $facet = new stdClass;
770 $facet->index = new stdClass;
771 $facet->index->byteEnd = $pos + strlen($url['text']);
772 $facet->index->byteStart = $pos;
774 $feature = new stdClass;
777 if (!empty($url['tag'])) {
778 $feature->tag = $url['tag'];
779 $feature->$type = 'app.bsky.richtext.facet#tag';
780 } elseif (!empty($url['url'])) {
781 $feature->uri = $url['url'];
782 $feature->$type = 'app.bsky.richtext.facet#link';
787 $facet->features = [$feature];
791 return ['facets' => $facets, 'body' => $body];
794 function bluesky_add_embed(int $uid, array $msg, array $record): array
796 if (($msg['type'] != 'link') && !empty($msg['images'])) {
798 foreach ($msg['images'] as $image) {
799 if (count($images) == 4) {
802 $photo = Photo::selectFirst([], ['id' => $image['id']]);
803 $blob = bluesky_upload_blob($uid, $photo);
807 $images[] = ['alt' => $image['description'] ?? '', 'image' => $blob];
809 if (!empty($images)) {
810 $record['embed'] = ['$type' => 'app.bsky.embed.images', 'images' => $images];
812 } elseif ($msg['type'] == 'link') {
814 '$type' => 'app.bsky.embed.external',
816 'uri' => $msg['url'],
817 'title' => $msg['title'] ?? '',
818 'description' => $msg['description'] ?? '',
821 if (!empty($msg['image'])) {
822 $photo = Photo::createPhotoForExternalResource($msg['image']);
823 $blob = bluesky_upload_blob($uid, $photo);
825 $record['embed']['external']['thumb'] = $blob;
832 function bluesky_upload_blob(int $uid, array $photo): ?stdClass
834 $retrial = Worker::getRetrial();
835 $content = Photo::getImageForPhoto($photo);
837 $picture = new Image($content, $photo['type']);
838 $height = $picture->getHeight();
839 $width = $picture->getWidth();
840 $size = strlen($content);
842 $picture = Photo::resizeToFileSize($picture, BLUESKY_IMAGE_SIZE[$retrial]);
843 $new_height = $picture->getHeight();
844 $new_width = $picture->getWidth();
845 $content = $picture->asString();
846 $new_size = strlen($content);
848 Logger::info('Uploading', ['uid' => $uid, 'retrial' => $retrial, 'height' => $new_height, 'width' => $new_width, 'size' => $new_size, 'orig-height' => $height, 'orig-width' => $width, 'orig-size' => $size]);
850 $data = bluesky_post($uid, '/xrpc/com.atproto.repo.uploadBlob', $content, ['Content-type' => $photo['type'], 'Authorization' => ['Bearer ' . bluesky_get_token($uid)]]);
852 Logger::info('Uploading failed', ['uid' => $uid, 'retrial' => $retrial, 'height' => $new_height, 'width' => $new_width, 'size' => $new_size, 'orig-height' => $height, 'orig-width' => $width, 'orig-size' => $size]);
856 Logger::debug('Uploaded blob', ['return' => $data, 'uid' => $uid, 'retrial' => $retrial, 'height' => $new_height, 'width' => $new_width, 'size' => $new_size, 'orig-height' => $height, 'orig-width' => $width, 'orig-size' => $size]);
860 function bluesky_delete_post(string $uri, int $uid)
862 $parts = bluesky_get_uri_parts($uri);
864 Logger::debug('No uri delected', ['uri' => $uri]);
867 bluesky_xrpc_post($uid, 'com.atproto.repo.deleteRecord', $parts);
868 Logger::debug('Deleted', ['parts' => $parts]);
871 function bluesky_fetch_timeline(int $uid)
873 $data = bluesky_xrpc_get($uid, 'app.bsky.feed.getTimeline');
878 if (empty($data->feed)) {
882 foreach (array_reverse($data->feed) as $entry) {
883 bluesky_process_post($entry->post, $uid, Item::PR_NONE, 0);
884 if (!empty($entry->reason)) {
885 bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid);
889 // @todo Support paging
890 // [cursor] => 1684670516000::bafyreidq3ilwslmlx72jf5vrk367xcc63s6lrhzlyup2bi3zwcvso6w2vi
893 function bluesky_process_reason(stdClass $reason, string $uri, int $uid)
896 if ($reason->$type != 'app.bsky.feed.defs#reasonRepost') {
900 $contact = bluesky_get_contact($reason->by, $uid, $uid);
903 'network' => Protocol::BLUESKY,
906 'uri' => $reason->by->did . '/app.bsky.feed.repost/' . $reason->indexedAt,
907 'private' => Item::UNLISTED,
908 'verb' => Activity::POST,
909 'contact-id' => $contact['id'],
910 'author-name' => $contact['name'],
911 'author-link' => $contact['url'],
912 'author-avatar' => $contact['avatar'],
913 'verb' => Activity::ANNOUNCE,
914 'body' => Activity::ANNOUNCE,
915 'gravity' => Item::GRAVITY_ACTIVITY,
916 'object-type' => Activity\ObjectType::NOTE,
917 'thr-parent' => $uri,
920 if (Post::exists(['uri' => $item['uri'], 'uid' => $uid])) {
924 $item['guid'] = Item::guidFromUri($item['uri'], $contact['alias']);
925 $item['owner-name'] = $item['author-name'];
926 $item['owner-link'] = $item['author-link'];
927 $item['owner-avatar'] = $item['author-avatar'];
928 if (Item::insert($item)) {
929 $cdata = Contact::getPublicAndUserContactID($contact['id'], $uid);
930 Item::update(['post-reason' => Item::PR_ANNOUNCEMENT, 'causer-id' => $cdata['public']], ['uri' => $uri, 'uid' => $uid]);
934 function bluesky_fetch_notifications(int $uid)
936 $data = bluesky_xrpc_get($uid, 'app.bsky.notification.listNotifications');
937 if (empty($data->notifications)) {
940 foreach ($data->notifications as $notification) {
941 $uri = bluesky_get_uri($notification);
942 if (Post::exists(['uri' => $uri, 'uid' => $uid]) || Post::exists(['extid' => $uri, 'uid' => $uid])) {
943 Logger::debug('Notification already processed', ['uid' => $uid, 'reason' => $notification->reason, 'uri' => $uri, 'indexedAt' => $notification->indexedAt]);
946 Logger::debug('Process notification', ['uid' => $uid, 'reason' => $notification->reason, 'uri' => $uri, 'indexedAt' => $notification->indexedAt]);
947 switch ($notification->reason) {
949 $item = bluesky_get_header($notification, $uri, $uid, $uid);
950 $item['gravity'] = Item::GRAVITY_ACTIVITY;
951 $item['body'] = $item['verb'] = Activity::LIKE;
952 $item['thr-parent'] = bluesky_get_uri($notification->record->subject);
953 $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $uid, $item['contact-id'], 0);
954 if (!empty($item['thr-parent'])) {
955 $data = Item::insert($item);
956 Logger::debug('Got like', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
958 Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $item['thr-parent'], 'uri' => $uri]);
963 $item = bluesky_get_header($notification, $uri, $uid, $uid);
964 $item['gravity'] = Item::GRAVITY_ACTIVITY;
965 $item['body'] = $item['verb'] = Activity::ANNOUNCE;
966 $item['thr-parent'] = bluesky_get_uri($notification->record->subject);
967 $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $uid, $item['contact-id'], 0);
968 if (!empty($item['thr-parent'])) {
969 $data = Item::insert($item);
970 Logger::debug('Got repost', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
972 Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $item['thr-parent'], 'uri' => $uri]);
977 $contact = bluesky_get_contact($notification->author, $uid, $uid);
978 Logger::debug('New follower', ['uid' => $uid, 'nick' => $contact['nick'], 'uri' => $uri]);
982 $data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0);
983 Logger::debug('Got mention', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
987 $data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0);
988 Logger::debug('Got reply', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
992 $data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0);
993 Logger::debug('Got quote', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
997 Logger::notice('Unhandled reason', ['reason' => $notification->reason, 'uri' => $uri]);
1003 function bluesky_fetch_feed(int $uid, string $feed)
1005 $data = bluesky_xrpc_get($uid, 'app.bsky.feed.getFeed', ['feed' => $feed]);
1010 if (empty($data->feed)) {
1014 $feeddata = bluesky_xrpc_get($uid, 'app.bsky.feed.getFeedGenerator', ['feed' => $feed]);
1015 if (!empty($feeddata)) {
1016 $feedurl = $feeddata->view->uri;
1017 $feedname = $feeddata->view->displayName;
1023 foreach (array_reverse($data->feed) as $entry) {
1024 $contact = bluesky_get_contact($entry->post->author, 0, $uid);
1025 $languages = $entry->post->record->langs ?? [];
1027 if (!Relay::isWantedLanguage($entry->post->record->text, 0, $contact['id'] ?? 0, $languages)) {
1028 Logger::debug('Unwanted language detected', ['text' => $entry->post->record->text]);
1031 $id = bluesky_process_post($entry->post, $uid, Item::PR_TAG, 0);
1033 $post = Post::selectFirst(['uri-id'], ['id' => $id]);
1034 if (!empty($post['uri-id'])) {
1035 $stored = Post\Category::storeFileByURIId($post['uri-id'], $uid, Post\Category::SUBCRIPTION, $feedname, $feedurl);
1036 Logger::debug('Stored tag subscription for user', ['uri-id' => $post['uri-id'], 'uid' => $uid, 'name' => $feedname, 'url' => $feedurl, 'stored' => $stored]);
1038 Logger::notice('Post not found', ['id' => $id, 'entry' => $entry]);
1041 if (!empty($entry->reason)) {
1042 bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid);
1047 function bluesky_process_post(stdClass $post, int $uid, int $post_reason, $level): int
1049 $uri = bluesky_get_uri($post);
1051 if ($id = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $uid])) {
1055 if ($id = Post::selectFirst(['id'], ['extid' => $uri, 'uid' => $uid])) {
1059 Logger::debug('Importing post', ['uid' => $uid, 'indexedAt' => $post->indexedAt, 'uri' => $post->uri, 'cid' => $post->cid, 'root' => $post->record->reply->root ?? '']);
1061 $item = bluesky_get_header($post, $uri, $uid, $uid);
1062 $item = bluesky_get_content($item, $post->record, $uri, $uid, $uid, $level);
1067 if (!empty($post->embed)) {
1068 $item = bluesky_add_media($post->embed, $item, $uid, $level);
1071 if (empty($item['post-reason'])) {
1072 $item['post-reason'] = $post_reason;
1075 return item::insert($item);
1078 function bluesky_get_header(stdClass $post, string $uri, int $uid, int $fetch_uid): array
1080 $parts = bluesky_get_uri_parts($uri);
1081 if (empty($post->author)) {
1084 $contact = bluesky_get_contact($post->author, $uid, $fetch_uid);
1086 'network' => Protocol::BLUESKY,
1090 'guid' => $post->cid,
1091 'private' => Item::UNLISTED,
1092 'verb' => Activity::POST,
1093 'contact-id' => $contact['id'],
1094 'author-name' => $contact['name'],
1095 'author-link' => $contact['url'],
1096 'author-avatar' => $contact['avatar'],
1097 'plink' => $contact['alias'] . '/post/' . $parts->rkey,
1098 'source' => json_encode($post),
1101 $item['uri-id'] = ItemURI::getIdByURI($uri);
1102 $item['owner-name'] = $item['author-name'];
1103 $item['owner-link'] = $item['author-link'];
1104 $item['owner-avatar'] = $item['author-avatar'];
1106 if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
1107 $item['post-reason'] = Item::PR_FOLLOWER;
1113 function bluesky_get_content(array $item, stdClass $record, string $uri, int $uid, int $fetch_uid, int $level): array
1119 if (!empty($record->reply)) {
1120 $item['parent-uri'] = bluesky_get_uri($record->reply->root);
1121 if ($item['parent-uri'] != $uri) {
1122 $item['parent-uri'] = bluesky_fetch_missing_post($item['parent-uri'], $uid, $fetch_uid, $item['contact-id'], $level);
1123 if (empty($item['parent-uri'])) {
1128 $item['thr-parent'] = bluesky_get_uri($record->reply->parent);
1129 if (!in_array($item['thr-parent'], [$uri, $item['parent-uri']])) {
1130 $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $fetch_uid, $item['contact-id'], $level, $item['parent-uri']);
1131 if (empty($item['thr-parent'])) {
1137 $item['body'] = bluesky_get_text($record);
1138 $item['created'] = DateTimeFormat::utc($record->createdAt, DateTimeFormat::MYSQL);
1139 $item['transmitted-languages'] = $record->langs ?? [];
1143 function bluesky_get_text(stdClass $record): string
1145 $text = $record->text ?? '';
1147 if (empty($record->facets)) {
1152 foreach ($record->facets as $facet) {
1153 $facets[$facet->index->byteStart] = $facet;
1157 foreach ($facets as $facet) {
1158 $prefix = substr($text, 0, $facet->index->byteStart);
1159 $linktext = substr($text, $facet->index->byteStart, $facet->index->byteEnd - $facet->index->byteStart);
1160 $suffix = substr($text, $facet->index->byteEnd);
1164 foreach ($facet->features as $feature) {
1166 switch ($feature->$type) {
1167 case 'app.bsky.richtext.facet#link':
1168 $url = $feature->uri;
1171 case 'app.bsky.richtext.facet#mention':
1172 $contact = Contact::getByURL($feature->did, null, ['id']);
1173 if (!empty($contact['id'])) {
1174 $url = DI::baseUrl() . '/contact/' . $contact['id'];
1175 if (substr($linktext, 0, 1) == '@') {
1177 $linktext = substr($linktext, 1);
1182 case 'app.bsky.richtext.facet#tag';
1183 $url = DI::baseUrl() . '/search?tag=' . urlencode($feature->tag);
1184 $linktext = '#' . $feature->tag;
1188 Logger::notice('Unhandled feature type', ['type' => $feature->$type, 'feature' => $feature, 'record' => $record]);
1193 $text = $prefix . '[url=' . $url . ']' . $linktext . '[/url]' . $suffix;
1199 function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $level): array
1202 switch ($embed->$type) {
1203 case 'app.bsky.embed.images#view':
1204 foreach ($embed->images as $image) {
1206 'uri-id' => $item['uri-id'],
1207 'type' => Post\Media::IMAGE,
1208 'url' => $image->fullsize,
1209 'preview' => $image->thumb,
1210 'description' => $image->alt,
1212 Post\Media::insert($media);
1216 case 'app.bsky.embed.external#view':
1218 'uri-id' => $item['uri-id'],
1219 'type' => Post\Media::HTML,
1220 'url' => $embed->external->uri,
1221 'name' => $embed->external->title,
1222 'description' => $embed->external->description,
1224 Post\Media::insert($media);
1227 case 'app.bsky.embed.record#view':
1228 $uri = bluesky_get_uri($embed->record);
1229 $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]);
1230 if (empty($shared)) {
1231 if (empty($embed->record->value)) {
1232 Logger::info('Record has got no value', ['record' => $embed->record]);
1235 $shared = bluesky_get_header($embed->record, $uri, 0, $fetch_uid);
1236 $shared = bluesky_get_content($shared, $embed->record->value, $uri, $item['uid'], $fetch_uid, $level);
1237 if (!empty($shared)) {
1238 if (!empty($embed->record->embeds)) {
1239 foreach ($embed->record->embeds as $single) {
1240 $shared = bluesky_add_media($single, $shared, $fetch_uid, $level);
1243 Item::insert($shared);
1246 if (!empty($shared['uri-id'])) {
1247 $item['quote-uri-id'] = $shared['uri-id'];
1251 case 'app.bsky.embed.recordWithMedia#view':
1252 $uri = bluesky_get_uri($embed->record->record);
1253 $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]);
1254 if (empty($shared)) {
1255 $shared = bluesky_get_header($embed->record->record, $uri, 0, $fetch_uid);
1256 $shared = bluesky_get_content($shared, $embed->record->record->value, $uri, $item['uid'], $fetch_uid, $level);
1257 if (!empty($shared)) {
1258 if (!empty($embed->record->record->embeds)) {
1259 foreach ($embed->record->record->embeds as $single) {
1260 $shared = bluesky_add_media($single, $shared, $fetch_uid, $level);
1263 Item::insert($shared);
1266 if (!empty($shared['uri-id'])) {
1267 $item['quote-uri-id'] = $shared['uri-id'];
1270 if (!empty($embed->media)) {
1271 $item = bluesky_add_media($embed->media, $item, $fetch_uid, $level);
1276 Logger::notice('Unhandled embed type', ['type' => $embed->$type, 'embed' => $embed]);
1282 function bluesky_get_uri(stdClass $post): string
1284 if (empty($post->cid)) {
1285 Logger::info('Invalid URI', ['post' => $post]);
1288 return $post->uri . ':' . $post->cid;
1291 function bluesky_get_uri_class(string $uri): ?stdClass
1297 $elements = explode(':', $uri);
1298 if (empty($elements) || ($elements[0] != 'at')) {
1299 $post = Post::selectFirstPost(['extid'], ['uri' => $uri]);
1300 return bluesky_get_uri_class($post['extid'] ?? '');
1303 $class = new stdClass;
1305 $class->cid = array_pop($elements);
1306 $class->uri = implode(':', $elements);
1308 if ((substr_count($class->uri, '/') == 2) && (substr_count($class->cid, '/') == 2)) {
1309 $class->uri .= ':' . $class->cid;
1316 function bluesky_get_uri_parts(string $uri): ?stdClass
1318 $class = bluesky_get_uri_class($uri);
1319 if (empty($class)) {
1323 $parts = explode('/', substr($class->uri, 5));
1325 $class = new stdClass;
1327 $class->repo = $parts[0];
1328 $class->collection = $parts[1];
1329 $class->rkey = $parts[2];
1334 function bluesky_fetch_missing_post(string $uri, int $uid, int $fetch_uid, int $causer, int $level, string $fallback = ''): string
1336 $fetched_uri = bluesky_fetch_post($uri, $uid);
1337 if (!empty($fetched_uri)) {
1338 return $fetched_uri;
1341 if (++$level > 100) {
1342 Logger::info('Recursion level too deep', ['level' => $level, 'uid' => $uid, 'uri' => $uri, 'fallback' => $fallback]);
1343 // When the level is too deep we will fallback to the parent uri.
1344 // Allthough the threading won't be correct, we at least had stored all posts and won't try again
1348 $class = bluesky_get_uri_class($uri);
1349 $fetch_uri = $class->uri;
1351 Logger::debug('Fetch missing post', ['level' => $level, 'uid' => $uid, 'uri' => $uri]);
1352 $data = bluesky_xrpc_get($fetch_uid, 'app.bsky.feed.getPostThread', ['uri' => $fetch_uri]);
1354 Logger::info('Thread was not fetched', ['level' => $level, 'uid' => $uid, 'uri' => $uri, 'fallback' => $fallback]);
1358 Logger::debug('Reply count', ['level' => $level, 'uid' => $uid, 'uri' => $uri]);
1361 $cdata = Contact::getPublicAndUserContactID($causer, $uid);
1366 return bluesky_process_thread($data->thread, $uid, $fetch_uid, $cdata, $level);
1369 function bluesky_fetch_post(string $uri, int $uid): string
1371 if (Post::exists(['uri' => $uri, 'uid' => [$uid, 0]])) {
1372 Logger::debug('Post exists', ['uri' => $uri]);
1376 $reply = Post::selectFirst(['uri'], ['extid' => $uri, 'uid' => [$uid, 0]]);
1377 if (!empty($reply['uri'])) {
1378 Logger::debug('Post with extid exists', ['uri' => $uri]);
1379 return $reply['uri'];
1384 function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, array $cdata, int $level): string
1386 if (empty($thread->post)) {
1387 Logger::info('Invalid post', ['post' => $thread]);
1390 $uri = bluesky_get_uri($thread->post);
1392 $fetched_uri = bluesky_fetch_post($uri, $uid);
1393 if (empty($fetched_uri)) {
1394 Logger::debug('Process missing post', ['uri' => $uri]);
1395 $item = bluesky_get_header($thread->post, $uri, $uid, $uid);
1396 $item = bluesky_get_content($item, $thread->post->record, $uri, $uid, $fetch_uid, $level);
1397 if (!empty($item)) {
1398 $item['post-reason'] = Item::PR_FETCHED;
1400 if (!empty($cdata['public'])) {
1401 $item['causer-id'] = $cdata['public'];
1404 if (!empty($thread->post->embed)) {
1405 $item = bluesky_add_media($thread->post->embed, $item, $uid, $level);
1407 $id = Item::insert($item);
1409 Logger::info('Item has not not been stored', ['uri' => $uri]);
1412 Logger::debug('Stored item', ['id' => $id, 'uri' => $uri]);
1414 Logger::info('Post has not not been fetched', ['uri' => $uri]);
1418 Logger::debug('Post exists', ['uri' => $uri]);
1419 $uri = $fetched_uri;
1422 foreach ($thread->replies ?? [] as $reply) {
1423 $reply_uri = bluesky_process_thread($reply, $uid, $fetch_uid, $cdata, $level);
1424 Logger::debug('Reply has been processed', ['uri' => $uri, 'reply' => $reply_uri]);
1430 function bluesky_get_contact(stdClass $author, int $uid, int $fetch_uid): array
1432 $condition = ['network' => Protocol::BLUESKY, 'uid' => 0, 'url' => $author->did];
1433 $contact = Contact::selectFirst(['id', 'updated'], $condition);
1435 $update = empty($contact) || $contact['updated'] < DateTimeFormat::utc('now -24 hours');
1437 $public_fields = $fields = bluesky_get_contact_fields($author, $fetch_uid, $update);
1439 $public_fields['uid'] = 0;
1440 $public_fields['rel'] = Contact::NOTHING;
1442 if (empty($contact)) {
1443 $cid = Contact::insert($public_fields);
1445 $cid = $contact['id'];
1446 Contact::update($public_fields, ['id' => $cid], true);
1450 $condition = ['network' => Protocol::BLUESKY, 'uid' => $uid, 'url' => $author->did];
1452 $contact = Contact::selectFirst(['id', 'rel', 'uid'], $condition);
1453 if (!isset($fields['rel']) && isset($contact['rel'])) {
1454 $fields['rel'] = $contact['rel'];
1455 } elseif (!isset($fields['rel'])) {
1456 $fields['rel'] = Contact::NOTHING;
1460 if (($uid != 0) && ($fields['rel'] != Contact::NOTHING)) {
1461 if (empty($contact)) {
1462 $cid = Contact::insert($fields);
1464 $cid = $contact['id'];
1465 Contact::update($fields, ['id' => $cid], true);
1467 Logger::debug('Get user contact', ['id' => $cid, 'uid' => $uid, 'update' => $update]);
1469 Logger::debug('Get public contact', ['id' => $cid, 'uid' => $uid, 'update' => $update]);
1471 if (!empty($author->avatar)) {
1472 Contact::updateAvatar($cid, $author->avatar);
1475 return Contact::getById($cid);
1478 function bluesky_get_contact_fields(stdClass $author, int $uid, bool $update): array
1482 'network' => Protocol::BLUESKY,
1486 'readonly' => false,
1488 'url' => $author->did,
1489 'nurl' => $author->did,
1490 'alias' => BLUESKY_WEB . '/profile/' . $author->handle,
1491 'name' => $author->displayName ?? $author->handle,
1492 'nick' => $author->handle,
1493 'addr' => $author->handle,
1497 Logger::debug('Got contact fields', ['uid' => $uid, 'url' => $fields['url']]);
1501 $fields['baseurl'] = bluesky_get_pds($author->did);
1502 if (!empty($fields['baseurl'])) {
1503 GServer::check($fields['baseurl'], Protocol::BLUESKY);
1504 $fields['gsid'] = GServer::getID($fields['baseurl'], true);
1507 $data = bluesky_xrpc_get($uid, 'app.bsky.actor.getProfile', ['actor' => $author->did]);
1509 Logger::debug('Error fetching contact fields', ['uid' => $uid, 'url' => $fields['url']]);
1513 $fields['updated'] = DateTimeFormat::utcNow(DateTimeFormat::MYSQL);
1515 if (!empty($data->description)) {
1516 $fields['about'] = HTML::toBBCode($data->description);
1519 if (!empty($data->banner)) {
1520 $fields['header'] = $data->banner;
1523 if (!empty($data->viewer)) {
1524 if (!empty($data->viewer->following) && !empty($data->viewer->followedBy)) {
1525 $fields['rel'] = Contact::FRIEND;
1526 } elseif (!empty($data->viewer->following) && empty($data->viewer->followedBy)) {
1527 $fields['rel'] = Contact::SHARING;
1528 } elseif (empty($data->viewer->following) && !empty($data->viewer->followedBy)) {
1529 $fields['rel'] = Contact::FOLLOWER;
1531 $fields['rel'] = Contact::NOTHING;
1535 Logger::debug('Got updated contact fields', ['uid' => $uid, 'url' => $fields['url']]);
1539 function bluesky_get_feeds(int $uid): array
1542 $preferences = bluesky_get_preferences($uid);
1543 foreach ($preferences->preferences as $preference) {
1544 if ($preference->$type == 'app.bsky.actor.defs#savedFeedsPref') {
1545 return $preference->pinned ?? [];
1551 function bluesky_get_preferences(int $uid): stdClass
1553 $cachekey = 'bluesky:preferences:' . $uid;
1554 $data = DI::cache()->get($cachekey);
1555 if (!is_null($data)) {
1559 $data = bluesky_xrpc_get($uid, 'app.bsky.actor.getPreferences');
1561 DI::cache()->set($cachekey, $data, Duration::HOUR);
1565 function bluesky_get_did(string $handle): string
1567 $data = bluesky_get(BLUESKY_PDS . '/xrpc/com.atproto.identity.resolveHandle?handle=' . urlencode($handle));
1571 Logger::debug('Got DID', ['return' => $data]);
1575 function bluesky_get_user_pds(int $uid): string
1577 $pds = DI::pConfig()->get($uid, 'bluesky', 'pds');
1581 $did = DI::pConfig()->get($uid, 'bluesky', 'did');
1583 Logger::notice('Empty did for user', ['uid' => $uid]);
1586 $pds = bluesky_get_pds($did);
1587 DI::pConfig()->set($uid, 'bluesky', 'pds', $pds);
1591 function bluesky_get_pds(string $did): ?string
1593 $data = bluesky_get(BLUESKY_DIRECTORY . '/' . $did);
1594 if (empty($data) || empty($data->service)) {
1598 foreach ($data->service as $service) {
1599 if (($service->id == '#atproto_pds') && ($service->type == 'AtprotoPersonalDataServer') && !empty($service->serviceEndpoint)) {
1600 return $service->serviceEndpoint;
1607 function bluesky_get_token(int $uid): string
1609 $token = DI::pConfig()->get($uid, 'bluesky', 'access_token');
1610 $created = DI::pConfig()->get($uid, 'bluesky', 'token_created');
1611 if (empty($token)) {
1615 if ($created + 300 < time()) {
1616 return bluesky_refresh_token($uid);
1621 function bluesky_refresh_token(int $uid): string
1623 $token = DI::pConfig()->get($uid, 'bluesky', 'refresh_token');
1625 $data = bluesky_post($uid, '/xrpc/com.atproto.server.refreshSession', '', ['Authorization' => ['Bearer ' . $token]]);
1630 Logger::debug('Refreshed token', ['return' => $data]);
1631 DI::pConfig()->set($uid, 'bluesky', 'access_token', $data->accessJwt);
1632 DI::pConfig()->set($uid, 'bluesky', 'refresh_token', $data->refreshJwt);
1633 DI::pConfig()->set($uid, 'bluesky', 'token_created', time());
1634 return $data->accessJwt;
1637 function bluesky_create_token(int $uid, string $password): string
1639 $did = DI::pConfig()->get($uid, 'bluesky', 'did');
1641 $data = bluesky_post($uid, '/xrpc/com.atproto.server.createSession', json_encode(['identifier' => $did, 'password' => $password]), ['Content-type' => 'application/json']);
1646 Logger::debug('Created token', ['return' => $data]);
1647 DI::pConfig()->set($uid, 'bluesky', 'access_token', $data->accessJwt);
1648 DI::pConfig()->set($uid, 'bluesky', 'refresh_token', $data->refreshJwt);
1649 DI::pConfig()->set($uid, 'bluesky', 'token_created', time());
1650 return $data->accessJwt;
1653 function bluesky_xrpc_post(int $uid, string $url, $parameters): ?stdClass
1655 return bluesky_post($uid, '/xrpc/' . $url, json_encode($parameters), ['Content-type' => 'application/json', 'Authorization' => ['Bearer ' . bluesky_get_token($uid)]]);
1658 function bluesky_post(int $uid, string $url, string $params, array $headers): ?stdClass
1661 $curlResult = DI::httpClient()->post(bluesky_get_user_pds($uid) . $url, $params, $headers);
1662 } catch (\Exception $e) {
1663 Logger::notice('Exception on post', ['exception' => $e]);
1667 if (!$curlResult->isSuccess()) {
1668 Logger::notice('API Error', ['error' => json_decode($curlResult->getBody()) ?: $curlResult->getBody()]);
1672 return json_decode($curlResult->getBody());
1675 function bluesky_xrpc_get(int $uid, string $url, array $parameters = []): ?stdClass
1677 if (!empty($parameters)) {
1678 $url .= '?' . http_build_query($parameters);
1681 return bluesky_get(bluesky_get_user_pds($uid) . '/xrpc/' . $url, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]);
1684 function bluesky_get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ?stdClass
1687 $curlResult = DI::httpClient()->get($url, $accept_content, $opts);
1688 } catch (\Exception $e) {
1689 Logger::notice('Exception on get', ['exception' => $e]);
1693 if (!$curlResult->isSuccess()) {
1694 Logger::notice('API Error', ['error' => json_decode($curlResult->getBody()) ?: $curlResult->getBody()]);
1698 return json_decode($curlResult->getBody());