]> git.mxchange.org Git - friendica-addons.git/blob - bluesky/bluesky.php
Improved logging message
[friendica-addons.git] / bluesky / bluesky.php
1 <?php
2 /**
3  * Name: Bluesky Connector
4  * Description: Post to Bluesky
5  * Version: 1.1
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  *
8  * @todo
9  * Currently technical issues in the core:
10  * - Outgoing mentions
11  *
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
14  *
15  * Possibly not possible:
16  * - only fetch new posts
17  *
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
21  *
22  * Possibly interesting:
23  * - https://atproto.com/lexicons/com-atproto-label#comatprotolabelsubscribelabels
24  */
25
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;
37 use Friendica\DI;
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;
51
52 const BLUESKY_DEFAULT_POLL_INTERVAL = 10; // given in minutes
53 const BLUESKY_IMAGE_SIZE = [1000000, 500000, 100000, 50000];
54
55 /*
56  * (Currently) hard wired paths for Bluesky services
57  */
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
61
62 function bluesky_install()
63 {
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');
81 }
82
83 function bluesky_load_config(ConfigFileManager $loader)
84 {
85         DI::app()->getConfigCache()->load($loader->loadAddonConfig('bluesky'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
86 }
87
88 function bluesky_check_item_notification(array &$notification_data)
89 {
90         $did = DI::pConfig()->get($notification_data['uid'], 'bluesky', 'did');
91
92         if (!empty($did)) {
93                 $notification_data['profiles'][] = $did;
94         }
95 }
96
97 function bluesky_probe_detect(array &$hookData)
98 {
99         // Don't overwrite an existing result
100         if (isset($hookData['result'])) {
101                 return;
102         }
103
104         // Avoid a lookup for the wrong network
105         if (!in_array($hookData['network'], ['', Protocol::BLUESKY])) {
106                 return;
107         }
108
109         $pconfig = DBA::selectFirst('pconfig', ['uid'], ["`cat` = ? AND `k` = ? AND `v` != ?", 'bluesky', 'access_token', '']);
110         if (empty($pconfig['uid'])) {
111                 return;
112         }
113
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]);
118                 if (empty($did)) {
119                         return;
120                 }
121         } else {
122                 return;
123         }
124
125         $token = bluesky_get_token($pconfig['uid']);
126         if (empty($token)) {
127                 return;
128         }
129
130         $data = bluesky_xrpc_get($pconfig['uid'], 'app.bsky.actor.getProfile', ['actor' => $did]);
131         if (empty($data)) {
132                 return;
133         }
134
135         $hookData['result'] = bluesky_get_contact_fields($data, 0, false);
136
137         $hookData['result']['baseurl'] = bluesky_get_pds($did);
138
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']     = '';
150 }
151
152 function bluesky_item_by_link(array &$hookData)
153 {
154         // Don't overwrite an existing result
155         if (isset($hookData['item_id'])) {
156                 return;
157         }
158
159         $token = bluesky_get_token($hookData['uid']);
160         if (empty($token)) {
161                 return;
162         }
163
164         if (!preg_match('#^' . BLUESKY_WEB . '/profile/(.+)/post/(.+)#', $hookData['uri'], $matches)) {
165                 return;
166         }
167
168         $did = bluesky_get_did($matches[1]);
169         if (empty($did)) {
170                 return;
171         }
172
173         Logger::debug('Found bluesky post', ['url' => $hookData['uri'], 'handle' => $matches[1], 'did' => $did, 'cid' => $matches[2]]);
174
175         $uri = 'at://' . $did . '/app.bsky.feed.post/' . $matches[2];
176
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]);
179         if (!empty($uri)) {
180                 $item = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $hookData['uid']]);
181                 if (!empty($item['id'])) {
182                         $hookData['item_id'] = $item['id'];
183                 }
184         }
185 }
186
187 function bluesky_support_follow(array &$data)
188 {
189         if ($data['protocol'] == Protocol::BLUESKY) {
190                 $data['result'] = true;
191         }
192 }
193
194 function bluesky_support_probe(array &$data)
195 {
196         if ($data['protocol'] == Protocol::BLUESKY) {
197                 $data['result'] = true;
198         }
199 }
200
201 function bluesky_follow(array &$hook_data)
202 {
203         $token = bluesky_get_token($hook_data['uid']);
204         if (empty($token)) {
205                 return;
206         }
207
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)) {
211                 return;
212         }
213
214         $record = [
215                 'subject'   => $contact['url'],
216                 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
217                 '$type'     => 'app.bsky.graph.follow'
218         ];
219
220         $post = [
221                 'collection' => 'app.bsky.graph.follow',
222                 'repo'       => DI::pConfig()->get($hook_data['uid'], 'bluesky', 'did'),
223                 'record'     => $record
224         ];
225
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]);
230         }
231 }
232
233 function bluesky_unfollow(array &$hook_data)
234 {
235         $token = bluesky_get_token($hook_data['uid']);
236         if (empty($token)) {
237                 return;
238         }
239
240         if ($hook_data['contact']['network'] != Protocol::BLUESKY) {
241                 return;
242         }
243
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)) {
246                 return;
247         }
248
249         bluesky_delete_post($data->viewer->following, $hook_data['uid']);
250
251         $hook_data['result'] = true;
252 }
253
254 function bluesky_block(array &$hook_data)
255 {
256         $token = bluesky_get_token($hook_data['uid']);
257         if (empty($token)) {
258                 return;
259         }
260
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)) {
264                 return;
265         }
266
267         $record = [
268                 'subject'   => $contact['url'],
269                 'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
270                 '$type'     => 'app.bsky.graph.block'
271         ];
272
273         $post = [
274                 'collection' => 'app.bsky.graph.block',
275                 'repo'       => DI::pConfig()->get($hook_data['uid'], 'bluesky', 'did'),
276                 'record'     => $record
277         ];
278
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']);
284                 }
285                 Logger::debug('Successfully blocked contact', ['url' => $hook_data['contact']['url'], 'uri' => $activity->uri]);
286         }
287 }
288
289 function bluesky_unblock(array &$hook_data)
290 {
291         $token = bluesky_get_token($hook_data['uid']);
292         if (empty($token)) {
293                 return;
294         }
295
296         if ($hook_data['contact']['network'] != Protocol::BLUESKY) {
297                 return;
298         }
299
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)) {
302                 return;
303         }
304
305         bluesky_delete_post($data->viewer->blocking, $hook_data['uid']);
306
307         $hook_data['result'] = true;
308 }
309
310 function bluesky_settings(array &$data)
311 {
312         if (!DI::userSession()->getLocalUserId()) {
313                 return;
314         }
315
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;
324
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.');
326
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.")],
337                 '$status'       => $status
338         ]);
339
340         $data = [
341                 'connector' => 'bluesky',
342                 'title'     => DI::l10n()->t('Bluesky Import/Export'),
343                 'image'     => 'images/bluesky.jpg',
344                 'enabled'   => $enabled,
345                 'html'      => $html,
346         ];
347 }
348
349 function bluesky_settings_post(array &$b)
350 {
351         if (empty($_POST['bluesky-submit'])) {
352                 return;
353         }
354         
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');
358
359         $handle = $_POST['bluesky_handle'];
360
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']));
366
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')));
370                 }
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')));
373                 }
374         } else {
375                 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
376                 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'bluesky', 'pds');
377         }
378
379         if (!empty($_POST['bluesky_password'])) {
380                 bluesky_create_token(DI::userSession()->getLocalUserId(), $_POST['bluesky_password']);
381         }
382 }
383
384 function bluesky_jot_nets(array &$jotnets_fields)
385 {
386         if (!DI::userSession()->getLocalUserId()) {
387                 return;
388         }
389
390         if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post')) {
391                 $jotnets_fields[] = [
392                         'type'  => 'checkbox',
393                         'field' => [
394                                 'bluesky_enable',
395                                 DI::l10n()->t('Post to Bluesky'),
396                                 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default')
397                         ]
398                 ];
399         }
400 }
401
402 function bluesky_cron()
403 {
404         $last = DI::keyValue()->get('bluesky_last_poll');
405
406         $poll_interval = intval(DI::config()->get('bluesky', 'poll_interval'));
407         if (!$poll_interval) {
408                 $poll_interval = BLUESKY_DEFAULT_POLL_INTERVAL;
409         }
410
411         if ($last) {
412                 $next = $last + ($poll_interval * 60);
413                 if ($next > time()) {
414                         Logger::notice('poll interval not reached');
415                         return;
416                 }
417         }
418         Logger::notice('cron_start');
419
420         $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
421         if ($abandon_days < 1) {
422                 $abandon_days = 0;
423         }
424
425         $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
426
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']]);
432                                 continue;
433                         }
434                 }
435
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']);
438
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']);
441
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);
446                         }
447                 }
448         }
449
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);
456                 }
457                 DBA::close($contacts);
458                 DI::keyValue()->set('bluesky_last_clean', time());
459                 Logger::notice('Contact cleanup done');
460         }
461
462         Logger::notice('cron_end');
463
464         DI::keyValue()->set('bluesky_last_poll', time());
465 }
466
467 function bluesky_hook_fork(array &$b)
468 {
469         if ($b['name'] != 'notifier_normal') {
470                 return;
471         }
472
473         $post = $b['data'];
474
475         if (($post['created'] !== $post['edited']) && !$post['deleted']) {
476                 DI::logger()->info('Editing is not supported by the addon');
477                 $b['execute'] = false;
478                 return;
479         }
480
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;
486                         return;
487                 }
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;
491                 return;
492         }
493 }
494
495 function bluesky_post_local(array &$b)
496 {
497         if ($b['edit']) {
498                 return;
499         }
500
501         if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
502                 return;
503         }
504
505         if ($b['private'] || $b['parent']) {
506                 return;
507         }
508
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);
511
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'))) {
514                 $bluesky_enable = 1;
515         }
516
517         if (!$bluesky_enable) {
518                 return;
519         }
520
521         if (strlen($b['postopts'])) {
522                 $b['postopts'] .= ',';
523         }
524
525         $b['postopts'] .= 'bluesky';
526 }
527
528 function bluesky_send(array &$b)
529 {
530         if (($b['created'] !== $b['edited']) && !$b['deleted']) {
531                 return;
532         }
533
534         if ($b['gravity'] != Item::GRAVITY_PARENT) {
535                 Logger::debug('Got comment', ['item' => $b]);
536
537                 if ($b['deleted']) {
538                         $uri = bluesky_get_uri_class($b['uri']);
539                         if (empty($uri)) {
540                                 Logger::debug('Not a bluesky post', ['uri' => $b['uri']]);
541                                 return;
542                         }
543                         bluesky_delete_post($b['uri'], $b['uid']);
544                         return;
545                 }
546
547                 $root   = bluesky_get_uri_class($b['parent-uri']);
548                 $parent = bluesky_get_uri_class($b['thr-parent']);
549
550                 if (empty($root) || empty($parent)) {
551                         Logger::debug('No bluesky post', ['parent' => $b['parent'], 'thr-parent' => $b['thr-parent']]);
552                         return;
553                 }
554
555                 if ($b['gravity'] == Item::GRAVITY_COMMENT) {
556                         Logger::debug('Posting comment', ['root' => $root, 'parent' => $parent]);
557                         bluesky_create_post($b, $root, $parent);
558                         return;
559                 } elseif (in_array($b['verb'], [Activity::LIKE, Activity::ANNOUNCE])) {
560                         bluesky_create_activity($b, $parent);
561                 }
562                 return;
563         } elseif ($b['private'] || !strstr($b['postopts'], 'bluesky')) {
564                 return;
565         }
566
567         bluesky_create_post($b);
568 }
569
570 function bluesky_create_activity(array $item, stdClass $parent = null)
571 {
572         $uid = $item['uid'];
573         $token = bluesky_get_token($uid);
574         if (empty($token)) {
575                 return;
576         }
577
578         $did  = DI::pConfig()->get($uid, 'bluesky', 'did');
579
580         if ($item['verb'] == Activity::LIKE) {
581                 $record = [
582                         'subject'   => $parent,
583                         'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
584                         '$type'     => 'app.bsky.feed.like'
585                 ];
586
587                 $post = [
588                         'collection' => 'app.bsky.feed.like',
589                         'repo'       => $did,
590                         'record'     => $record
591                 ];
592         } elseif ($item['verb'] == Activity::ANNOUNCE) {
593                 $record = [
594                         'subject'   => $parent,
595                         'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
596                         '$type'     => 'app.bsky.feed.repost'
597                 ];
598
599                 $post = [
600                         'collection' => 'app.bsky.feed.repost',
601                         'repo'       => $did,
602                         'record'     => $record
603                 ];
604         }
605
606         $activity = bluesky_xrpc_post($uid, 'com.atproto.repo.createRecord', $post);
607         if (empty($activity)) {
608                 return;
609         }
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]);
614 }
615
616 function bluesky_create_post(array $item, stdClass $root = null, stdClass $parent = null)
617 {
618         $uid = $item['uid'];
619         $token = bluesky_get_token($uid);
620         if (empty($token)) {
621                 return;
622         }
623
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));
627         } else {
628                 $language = '';
629         }
630
631         $did  = DI::pConfig()->get($uid, 'bluesky', 'did');
632         $urls = bluesky_get_urls(Post\Media::removeFromBody($item['body']));
633         $item['body'] = $urls['body'];
634
635         $msg = Plaintext::getPost($item, 300, false, BBCode::BLUESKY);
636         foreach ($msg['parts'] as $key => $part) {
637
638                 $facets = bluesky_get_facets($part, $urls['urls']);
639
640                 $record = [
641                         'text'      => $facets['body'],
642                         '$type'     => 'app.bsky.feed.post',
643                         'createdAt' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
644                 ];
645
646                 if (!empty($language)) {
647                         $record['langs'] = [$language];
648                 }
649
650                 if (!empty($facets['facets'])) {
651                         $record['facets'] = $facets['facets'];
652                 }
653
654                 if (!empty($root)) {
655                         $record['reply'] = ['root' => $root, 'parent' => $parent];
656                 }
657
658                 if ($key == count($msg['parts']) - 1) {
659                         $record = bluesky_add_embed($uid, $msg, $record);
660                         if (empty($record)) {
661                                 if (Worker::getRetrial() < 3) {
662                                         Worker::defer();
663                                 }
664                                 return;
665                         }
666                 }
667
668                 $post = [
669                         'collection' => 'app.bsky.feed.post',
670                         'repo'       => $did,
671                         'record'     => $record
672                 ];
673
674                 $parent = bluesky_xrpc_post($uid, 'com.atproto.repo.createRecord', $post);
675                 if (empty($parent)) {
676                         if ($part == 0) {
677                                 Worker::defer();
678                         }
679                         return;
680                 }
681                 Logger::debug('Posting done', ['return' => $parent]);
682                 if (empty($root)) {
683                         $root = $parent;
684                 }
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]);
689                 }
690         }
691 }
692
693 function bluesky_get_urls(string $body): array
694 {
695         // Remove all hashtag and mention links
696         $body = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $body);
697
698         $body = BBCode::expandVideoLinks($body);
699         $urls = [];
700
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);
707                 }
708         }
709
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);
717                 }
718         }
719
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]);
725                         } else {
726                                 $text = $match[2];
727                         }
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);
732                         } else {
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);
737                         }
738                 }
739         }
740
741         return ['body' => $body, 'urls' => $urls];
742 }
743
744 function bluesky_get_hash_for_url(string $text, int $linklength): string
745 {
746         if ($linklength <= 10) {
747                 return '|' . hash('crc32', $text) . '|';
748         }
749         return substr('|' . hash('crc32', $text) . base64_encode($text), 0, $linklength - 2) . '|';
750 }
751
752 function bluesky_get_facets(string $body, array $urls): array
753 {
754         $facets = [];
755
756         foreach ($urls as $url) {
757                 $pos = strpos($body, $url['hash']);
758                 if ($pos === false) {
759                         continue;
760                 }
761                 if ($pos > 0) {
762                         $prefix = substr($body, 0, $pos);
763                 } else {
764                         $prefix = '';
765                 }
766
767                 $body = $prefix . $url['text'] . substr($body, $pos + strlen($url['hash']));
768
769                 $facet = new stdClass;
770                 $facet->index = new stdClass;
771                 $facet->index->byteEnd   = $pos + strlen($url['text']);
772                 $facet->index->byteStart = $pos;
773
774                 $feature = new stdClass;
775
776                 $type = '$type';
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';
783                 } else {
784                         continue;
785                 }
786
787                 $facet->features = [$feature];
788                 $facets[] = $facet;
789         }
790
791         return ['facets' => $facets, 'body' => $body];
792 }
793
794 function bluesky_add_embed(int $uid, array $msg, array $record): array
795 {
796         if (($msg['type'] != 'link') && !empty($msg['images'])) {
797                 $images = [];
798                 foreach ($msg['images'] as $image) {
799                         if (count($images) == 4) {
800                                 continue;
801                         }
802                         $photo = Photo::selectFirst([], ['id' => $image['id']]);
803                         $blob = bluesky_upload_blob($uid, $photo);
804                         if (empty($blob)) {
805                                 return [];
806                         }
807                         $images[] = ['alt' => $image['description'] ?? '', 'image' => $blob];
808                 }
809                 if (!empty($images)) {
810                         $record['embed'] = ['$type' => 'app.bsky.embed.images', 'images' => $images];
811                 }
812         } elseif ($msg['type'] == 'link') {
813                 $record['embed'] = [
814                         '$type'    => 'app.bsky.embed.external',
815                         'external' => [
816                                 'uri'         => $msg['url'],
817                                 'title'       => $msg['title'] ?? '',
818                                 'description' => $msg['description'] ?? '',
819                         ]
820                 ];
821                 if (!empty($msg['image'])) {
822                         $photo = Photo::createPhotoForExternalResource($msg['image']);
823                         $blob = bluesky_upload_blob($uid, $photo);
824                         if (!empty($blob)) {
825                                 $record['embed']['external']['thumb'] = $blob;
826                         }
827                 }
828         }
829         return $record;
830 }
831
832 function bluesky_upload_blob(int $uid, array $photo): ?stdClass
833 {
834         $retrial = Worker::getRetrial();
835         $content = Photo::getImageForPhoto($photo);
836
837         $picture = new Image($content, $photo['type']);
838         $height  = $picture->getHeight();
839         $width   = $picture->getWidth();
840         $size    = strlen($content);
841
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);
847
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]);
849
850         $data = bluesky_post($uid, '/xrpc/com.atproto.repo.uploadBlob', $content, ['Content-type' => $photo['type'], 'Authorization' => ['Bearer ' . bluesky_get_token($uid)]]);
851         if (empty($data)) {
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]);
853                 return null;
854         }
855
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]);
857         return $data->blob;
858 }
859
860 function bluesky_delete_post(string $uri, int $uid)
861 {
862         $parts = bluesky_get_uri_parts($uri);
863         if (empty($parts)) {
864                 Logger::debug('No uri delected', ['uri' => $uri]);
865                 return;
866         }
867         bluesky_xrpc_post($uid, 'com.atproto.repo.deleteRecord', $parts);
868         Logger::debug('Deleted', ['parts' => $parts]);
869 }
870
871 function bluesky_fetch_timeline(int $uid)
872 {
873         $data = bluesky_xrpc_get($uid, 'app.bsky.feed.getTimeline');
874         if (empty($data)) {
875                 return;
876         }
877
878         if (empty($data->feed)) {
879                 return;
880         }
881
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);
886                 }
887         }
888
889         // @todo Support paging
890         // [cursor] => 1684670516000::bafyreidq3ilwslmlx72jf5vrk367xcc63s6lrhzlyup2bi3zwcvso6w2vi
891 }
892
893 function bluesky_process_reason(stdClass $reason, string $uri, int $uid)
894 {
895         $type = '$type';
896         if ($reason->$type != 'app.bsky.feed.defs#reasonRepost') {
897                 return;
898         }
899
900         $contact = bluesky_get_contact($reason->by, $uid, $uid);
901
902         $item = [
903                 'network'       => Protocol::BLUESKY,
904                 'uid'           => $uid,
905                 'wall'          => false,
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,
918         ];
919
920         if (Post::exists(['uri' => $item['uri'], 'uid' => $uid])) {
921                 return;
922         }
923
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]);
931         }
932 }
933
934 function bluesky_fetch_notifications(int $uid)
935 {
936         $data = bluesky_xrpc_get($uid, 'app.bsky.notification.listNotifications');
937         if (empty($data->notifications)) {
938                 return;
939         }
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]);
944                         continue;
945                 }
946                 Logger::debug('Process notification', ['uid' => $uid, 'reason' => $notification->reason, 'uri' => $uri, 'indexedAt' => $notification->indexedAt]);
947                 switch ($notification->reason) {
948                         case 'like':
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]);
957                                 } else {
958                                         Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $item['thr-parent'], 'uri' => $uri]);
959                                 }
960                         break;
961
962                         case 'repost':
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]);
971                                 } else {
972                                         Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $item['thr-parent'], 'uri' => $uri]);
973                                 }
974                         break;
975
976                         case 'follow':
977                                 $contact = bluesky_get_contact($notification->author, $uid, $uid);
978                                 Logger::debug('New follower', ['uid' => $uid, 'nick' => $contact['nick'], 'uri' => $uri]);
979                                 break;
980
981                         case 'mention':
982                                 $data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0);
983                                 Logger::debug('Got mention', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
984                                 break;
985
986                         case 'reply':
987                                 $data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0);
988                                 Logger::debug('Got reply', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
989                                 break;
990
991                         case 'quote':
992                                 $data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0);
993                                 Logger::debug('Got quote', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
994                                 break;
995
996                         default:
997                                 Logger::notice('Unhandled reason', ['reason' => $notification->reason, 'uri' => $uri]);
998                                 break;
999                 }
1000         }
1001 }
1002
1003 function bluesky_fetch_feed(int $uid, string $feed)
1004 {
1005         $data = bluesky_xrpc_get($uid, 'app.bsky.feed.getFeed', ['feed' => $feed]);
1006         if (empty($data)) {
1007                 return;
1008         }
1009
1010         if (empty($data->feed)) {
1011                 return;
1012         }
1013
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;
1018         } else {
1019                 $feedurl  = $feed;
1020                 $feedname = $feed;
1021         }
1022
1023         foreach (array_reverse($data->feed) as $entry) {
1024                 $contact   = bluesky_get_contact($entry->post->author, 0, $uid);
1025                 $languages = $entry->post->record->langs ?? [];
1026
1027                 if (!Relay::isWantedLanguage($entry->post->record->text, 0, $contact['id'] ?? 0, $languages)) {
1028                         Logger::debug('Unwanted language detected', ['text' => $entry->post->record->text]);
1029                         continue;
1030                 }
1031                 $id = bluesky_process_post($entry->post, $uid, Item::PR_TAG, 0);
1032                 if (!empty($id)) {
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]);
1037                         } else {
1038                                 Logger::notice('Post not found', ['id' => $id, 'entry' => $entry]);
1039                         }
1040                 }
1041                 if (!empty($entry->reason)) {
1042                         bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid);
1043                 }
1044         }
1045 }
1046
1047 function bluesky_process_post(stdClass $post, int $uid, int $post_reason, $level): int
1048 {
1049         $uri = bluesky_get_uri($post);
1050
1051         if ($id = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $uid])) {
1052                 return $id['id'];       
1053         }
1054
1055         if ($id = Post::selectFirst(['id'], ['extid' => $uri, 'uid' => $uid])) {
1056                 return $id['id'];       
1057         }
1058
1059         Logger::debug('Importing post', ['uid' => $uid, 'indexedAt' => $post->indexedAt, 'uri' => $post->uri, 'cid' => $post->cid, 'root' => $post->record->reply->root ?? '']);
1060
1061         $item = bluesky_get_header($post, $uri, $uid, $uid);
1062         $item = bluesky_get_content($item, $post->record, $uri, $uid, $uid, $level);
1063         if (empty($item)) {
1064                 return 0;
1065         }
1066
1067         if (!empty($post->embed)) {
1068                 $item = bluesky_add_media($post->embed, $item, $uid, $level);
1069         }
1070
1071         if (empty($item['post-reason'])) {
1072                 $item['post-reason'] = $post_reason;
1073         }
1074
1075         return item::insert($item);
1076 }
1077
1078 function bluesky_get_header(stdClass $post, string $uri, int $uid, int $fetch_uid): array
1079 {
1080         $parts = bluesky_get_uri_parts($uri);
1081         if (empty($post->author)) {
1082                 return [];
1083         }
1084         $contact = bluesky_get_contact($post->author, $uid, $fetch_uid);
1085         $item = [
1086                 'network'       => Protocol::BLUESKY,
1087                 'uid'           => $uid,
1088                 'wall'          => false,
1089                 'uri'           => $uri,
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),
1099         ];
1100
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'];
1105
1106         if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
1107                 $item['post-reason'] = Item::PR_FOLLOWER;
1108         }
1109
1110         return $item;
1111 }
1112
1113 function bluesky_get_content(array $item, stdClass $record, string $uri, int $uid, int $fetch_uid, int $level): array
1114 {
1115         if (empty($item)) {
1116                 return [];
1117         }
1118
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'])) {
1124                                 return [];
1125                         }
1126                 }
1127
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'])) {
1132                                 return [];
1133                         }
1134                 }
1135         }
1136
1137         $item['body']    = bluesky_get_text($record);
1138         $item['created'] = DateTimeFormat::utc($record->createdAt, DateTimeFormat::MYSQL);
1139         $item['transmitted-languages'] = $record->langs ?? [];
1140         return $item;
1141 }
1142
1143 function bluesky_get_text(stdClass $record): string
1144 {
1145         $text = $record->text ?? '';
1146
1147         if (empty($record->facets)) {
1148                 return $text;
1149         }
1150
1151         $facets = [];
1152         foreach ($record->facets as $facet) {
1153                 $facets[$facet->index->byteStart] = $facet;
1154         }
1155         krsort($facets);
1156
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);
1161
1162                 $url  = '';
1163                 $type = '$type';
1164                 foreach ($facet->features as $feature) {
1165
1166                         switch ($feature->$type) {
1167                                 case 'app.bsky.richtext.facet#link':
1168                                         $url = $feature->uri;
1169                                         break;
1170
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) == '@') {
1176                                                         $prefix .= '@';
1177                                                         $linktext = substr($linktext, 1);
1178                                                 }
1179                                         }
1180                                         break;
1181
1182                                 case 'app.bsky.richtext.facet#tag';
1183                                         $url      = DI::baseUrl() . '/search?tag=' . urlencode($feature->tag);
1184                                         $linktext = '#' . $feature->tag;
1185                                         break;
1186         
1187                                 default:
1188                                         Logger::notice('Unhandled feature type', ['type' => $feature->$type, 'feature' => $feature, 'record' => $record]);
1189                                         break;
1190                         }
1191                 }
1192                 if (!empty($url)) {
1193                         $text = $prefix . '[url=' . $url . ']' . $linktext . '[/url]' . $suffix;
1194                 }
1195         }
1196         return $text;
1197 }
1198
1199 function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $level): array
1200 {
1201         $type = '$type';
1202         switch ($embed->$type) {
1203                 case 'app.bsky.embed.images#view':
1204                         foreach ($embed->images as $image) {
1205                                 $media = [
1206                                         'uri-id'      => $item['uri-id'],
1207                                         'type'        => Post\Media::IMAGE,
1208                                         'url'         => $image->fullsize,
1209                                         'preview'     => $image->thumb,
1210                                         'description' => $image->alt,
1211                                 ];
1212                                 Post\Media::insert($media);
1213                         }
1214                         break;
1215
1216                 case 'app.bsky.embed.external#view':
1217                         $media = [
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,
1223                         ];
1224                         Post\Media::insert($media);
1225                         break;
1226
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]);
1233                                         break;
1234                                 }
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);
1241                                                 }
1242                                         }
1243                                         Item::insert($shared);
1244                                 }
1245                         }
1246                         if (!empty($shared['uri-id'])) {
1247                                 $item['quote-uri-id'] = $shared['uri-id'];
1248                         }
1249                         break;
1250
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);
1261                                                 }
1262                                         }
1263                                         Item::insert($shared);
1264                                 }
1265                         }
1266                         if (!empty($shared['uri-id'])) {
1267                                 $item['quote-uri-id'] = $shared['uri-id'];
1268                         }
1269
1270                         if (!empty($embed->media)) {
1271                                 $item = bluesky_add_media($embed->media, $item, $fetch_uid, $level);
1272                         }
1273                         break;
1274
1275                 default:
1276                         Logger::notice('Unhandled embed type', ['type' => $embed->$type, 'embed' => $embed]);
1277                         break;
1278         }
1279         return $item;
1280 }
1281
1282 function bluesky_get_uri(stdClass $post): string
1283 {
1284         if (empty($post->cid)) {
1285                 Logger::info('Invalid URI', ['post' => $post]);
1286                 return '';
1287         }
1288         return $post->uri . ':' . $post->cid;
1289 }
1290
1291 function bluesky_get_uri_class(string $uri): ?stdClass
1292 {
1293         if (empty($uri)) {
1294                 return null;
1295         }
1296
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'] ?? '');
1301         }
1302
1303         $class = new stdClass;
1304
1305         $class->cid = array_pop($elements);
1306         $class->uri = implode(':', $elements);
1307
1308         if ((substr_count($class->uri, '/') == 2) && (substr_count($class->cid, '/') == 2)) {
1309                 $class->uri .= ':' . $class->cid;
1310                 $class->cid = '';
1311         }
1312
1313         return $class;
1314 }
1315
1316 function bluesky_get_uri_parts(string $uri): ?stdClass
1317 {
1318         $class = bluesky_get_uri_class($uri);
1319         if (empty($class)) {
1320                 return null;
1321         }
1322
1323         $parts = explode('/', substr($class->uri, 5));
1324
1325         $class = new stdClass;
1326
1327         $class->repo       = $parts[0];
1328         $class->collection = $parts[1];
1329         $class->rkey       = $parts[2];
1330
1331         return $class;
1332 }
1333
1334 function bluesky_fetch_missing_post(string $uri, int $uid, int $fetch_uid, int $causer, int $level, string $fallback = ''): string
1335 {
1336         $fetched_uri = bluesky_fetch_post($uri, $uid);
1337         if (!empty($fetched_uri)) {
1338                 return $fetched_uri;
1339         }
1340
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
1345                 return $fallback;
1346         }
1347
1348         $class = bluesky_get_uri_class($uri);
1349         $fetch_uri = $class->uri;
1350
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]);
1353         if (empty($data)) {
1354                 Logger::info('Thread was not fetched', ['level' => $level, 'uid' => $uid, 'uri' => $uri, 'fallback' => $fallback]);
1355                 return $fallback;
1356         }
1357
1358         Logger::debug('Reply count', ['level' => $level, 'uid' => $uid, 'uri' => $uri]);
1359
1360         if ($causer != 0) {
1361                 $cdata = Contact::getPublicAndUserContactID($causer, $uid);
1362         } else {
1363                 $cdata = [];
1364         }
1365
1366         return bluesky_process_thread($data->thread, $uid, $fetch_uid, $cdata, $level);
1367 }
1368
1369 function bluesky_fetch_post(string $uri, int $uid): string
1370 {
1371         if (Post::exists(['uri' => $uri, 'uid' => [$uid, 0]])) {
1372                 Logger::debug('Post exists', ['uri' => $uri]);
1373                 return $uri;
1374         }
1375
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'];
1380         }
1381         return '';
1382 }
1383
1384 function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, array $cdata, int $level): string
1385 {
1386         if (empty($thread->post)) {
1387                 Logger::info('Invalid post', ['post' => $thread]);
1388                 return '';
1389         }
1390         $uri = bluesky_get_uri($thread->post);
1391
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;
1399
1400                         if (!empty($cdata['public'])) {
1401                                 $item['causer-id']   = $cdata['public'];
1402                         }
1403
1404                         if (!empty($thread->post->embed)) {
1405                                 $item = bluesky_add_media($thread->post->embed, $item, $uid, $level);
1406                         }
1407                         $id = Item::insert($item);
1408                         if (!$id) {
1409                                 Logger::info('Item has not not been stored', ['uri' => $uri]);
1410                                 return '';
1411                         }
1412                         Logger::debug('Stored item', ['id' => $id, 'uri' => $uri]);
1413                 } else {
1414                         Logger::info('Post has not not been fetched', ['uri' => $uri]);
1415                         return '';
1416                 }
1417         } else {
1418                 Logger::debug('Post exists', ['uri' => $uri]);
1419                 $uri = $fetched_uri;
1420         }
1421
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]);
1425         }
1426
1427         return $uri;
1428 }
1429
1430 function bluesky_get_contact(stdClass $author, int $uid, int $fetch_uid): array
1431 {
1432         $condition = ['network' => Protocol::BLUESKY, 'uid' => 0, 'url' => $author->did];
1433         $contact = Contact::selectFirst(['id', 'updated'], $condition);
1434
1435         $update = empty($contact) || $contact['updated'] < DateTimeFormat::utc('now -24 hours');
1436
1437         $public_fields = $fields = bluesky_get_contact_fields($author, $fetch_uid, $update);
1438
1439         $public_fields['uid'] = 0;
1440         $public_fields['rel'] = Contact::NOTHING;
1441
1442         if (empty($contact)) {
1443                 $cid = Contact::insert($public_fields);
1444         } else {
1445                 $cid = $contact['id'];
1446                 Contact::update($public_fields, ['id' => $cid], true);
1447         }
1448
1449         if ($uid != 0) {
1450                 $condition = ['network' => Protocol::BLUESKY, 'uid' => $uid, 'url' => $author->did];
1451
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;
1457                 }
1458         }
1459
1460         if (($uid != 0) && ($fields['rel'] != Contact::NOTHING)) {
1461                 if (empty($contact)) {
1462                         $cid = Contact::insert($fields);
1463                 } else {
1464                         $cid = $contact['id'];
1465                         Contact::update($fields, ['id' => $cid], true);
1466                 }
1467                 Logger::debug('Get user contact', ['id' => $cid, 'uid' => $uid, 'update' => $update]);
1468         } else {
1469                 Logger::debug('Get public contact', ['id' => $cid, 'uid' => $uid, 'update' => $update]);
1470         }
1471         if (!empty($author->avatar)) {
1472                 Contact::updateAvatar($cid, $author->avatar);
1473         }
1474
1475         return Contact::getById($cid);
1476 }
1477
1478 function bluesky_get_contact_fields(stdClass $author, int $uid, bool $update): array
1479 {
1480         $fields = [
1481                 'uid'      => $uid,
1482                 'network'  => Protocol::BLUESKY,
1483                 'priority' => 1,
1484                 'writable' => true,
1485                 'blocked'  => false,
1486                 'readonly' => false,
1487                 'pending'  => 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,
1494         ];
1495
1496         if (!$update) {
1497                 Logger::debug('Got contact fields', ['uid' => $uid, 'url' => $fields['url']]);
1498                 return $fields;
1499         }
1500
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);
1505         }
1506
1507         $data = bluesky_xrpc_get($uid, 'app.bsky.actor.getProfile', ['actor' => $author->did]);
1508         if (empty($data)) {
1509                 Logger::debug('Error fetching contact fields', ['uid' => $uid, 'url' => $fields['url']]);
1510                 return $fields;
1511         }
1512
1513         $fields['updated'] = DateTimeFormat::utcNow(DateTimeFormat::MYSQL);
1514
1515         if (!empty($data->description)) {
1516                 $fields['about'] = HTML::toBBCode($data->description);
1517         }
1518
1519         if (!empty($data->banner)) {
1520                 $fields['header'] = $data->banner;
1521         }
1522
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;
1530                 } else {
1531                         $fields['rel'] = Contact::NOTHING;
1532                 }
1533         }
1534
1535         Logger::debug('Got updated contact fields', ['uid' => $uid, 'url' => $fields['url']]);
1536         return $fields;
1537 }
1538
1539 function bluesky_get_feeds(int $uid): array
1540 {
1541         $type = '$type';
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 ?? [];
1546                 }
1547         }
1548         return [];
1549 }
1550
1551 function bluesky_get_preferences(int $uid): stdClass
1552 {
1553         $cachekey = 'bluesky:preferences:' . $uid;
1554         $data = DI::cache()->get($cachekey);
1555         if (!is_null($data)) {
1556                 return $data;
1557         }
1558
1559         $data = bluesky_xrpc_get($uid, 'app.bsky.actor.getPreferences');
1560
1561         DI::cache()->set($cachekey, $data, Duration::HOUR);
1562         return $data;
1563 }
1564
1565 function bluesky_get_did(string $handle): string
1566 {
1567         $data = bluesky_get(BLUESKY_PDS . '/xrpc/com.atproto.identity.resolveHandle?handle=' . urlencode($handle));
1568         if (empty($data)) {
1569                 return '';
1570         }
1571         Logger::debug('Got DID', ['return' => $data]);
1572         return $data->did;
1573 }
1574
1575 function bluesky_get_user_pds(int $uid): string
1576 {
1577         $pds = DI::pConfig()->get($uid, 'bluesky', 'pds');
1578         if (!empty($pds)) {
1579                 return $pds;
1580         }
1581         $did = DI::pConfig()->get($uid, 'bluesky', 'did');
1582         if (empty($did)) {
1583                 Logger::notice('Empty did for user', ['uid' => $uid]);
1584                 return '';
1585         }
1586         $pds = bluesky_get_pds($did);
1587         DI::pConfig()->set($uid, 'bluesky', 'pds', $pds);
1588         return $pds;
1589 }
1590
1591 function bluesky_get_pds(string $did): ?string
1592 {
1593         $data = bluesky_get(BLUESKY_DIRECTORY . '/' . $did);
1594         if (empty($data) || empty($data->service)) {
1595                 return null;
1596         }
1597
1598         foreach ($data->service as $service) {
1599                 if (($service->id == '#atproto_pds') && ($service->type == 'AtprotoPersonalDataServer') && !empty($service->serviceEndpoint)) {
1600                         return $service->serviceEndpoint;
1601                 }
1602         }
1603
1604         return null;
1605 }
1606
1607 function bluesky_get_token(int $uid): string
1608 {
1609         $token   = DI::pConfig()->get($uid, 'bluesky', 'access_token');
1610         $created = DI::pConfig()->get($uid, 'bluesky', 'token_created');
1611         if (empty($token)) {
1612                 return '';
1613         }
1614
1615         if ($created + 300 < time()) {
1616                 return bluesky_refresh_token($uid);
1617         }
1618         return $token;
1619 }
1620
1621 function bluesky_refresh_token(int $uid): string
1622 {
1623         $token = DI::pConfig()->get($uid, 'bluesky', 'refresh_token');
1624
1625         $data = bluesky_post($uid, '/xrpc/com.atproto.server.refreshSession', '', ['Authorization' => ['Bearer ' . $token]]);
1626         if (empty($data)) {
1627                 return '';
1628         }
1629
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;
1635 }
1636
1637 function bluesky_create_token(int $uid, string $password): string
1638 {
1639         $did = DI::pConfig()->get($uid, 'bluesky', 'did');
1640
1641         $data = bluesky_post($uid, '/xrpc/com.atproto.server.createSession', json_encode(['identifier' => $did, 'password' => $password]), ['Content-type' => 'application/json']);
1642         if (empty($data)) {
1643                 return '';
1644         }
1645
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;
1651 }
1652
1653 function bluesky_xrpc_post(int $uid, string $url, $parameters): ?stdClass
1654 {
1655         return bluesky_post($uid, '/xrpc/' . $url, json_encode($parameters),  ['Content-type' => 'application/json', 'Authorization' => ['Bearer ' . bluesky_get_token($uid)]]);
1656 }
1657
1658 function bluesky_post(int $uid, string $url, string $params, array $headers): ?stdClass
1659 {
1660         try {
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]);
1664                 return null;
1665         }
1666
1667         if (!$curlResult->isSuccess()) {
1668                 Logger::notice('API Error', ['error' => json_decode($curlResult->getBody()) ?: $curlResult->getBody()]);
1669                 return null;
1670         }
1671
1672         return json_decode($curlResult->getBody());
1673 }
1674
1675 function bluesky_xrpc_get(int $uid, string $url, array $parameters = []): ?stdClass
1676 {
1677         if (!empty($parameters)) {
1678                 $url .= '?' . http_build_query($parameters);
1679         }
1680
1681         return bluesky_get(bluesky_get_user_pds($uid) . '/xrpc/' . $url, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]);
1682 }
1683
1684 function bluesky_get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ?stdClass
1685 {
1686         try {
1687                 $curlResult = DI::httpClient()->get($url, $accept_content, $opts);
1688         } catch (\Exception $e) {
1689                 Logger::notice('Exception on get', ['exception' => $e]);
1690                 return null;
1691         }
1692
1693         if (!$curlResult->isSuccess()) {
1694                 Logger::notice('API Error', ['error' => json_decode($curlResult->getBody()) ?: $curlResult->getBody()]);
1695                 return null;
1696         }
1697
1698         return json_decode($curlResult->getBody());
1699 }