+
+function bluesky_get_uri_parts(string $uri): ?stdClass
+{
+ $class = bluesky_get_uri_class($uri);
+ if (empty($class)) {
+ return null;
+ }
+
+ $parts = explode('/', substr($class->uri, 5));
+
+ $class = new stdClass;
+
+ $class->repo = $parts[0];
+ $class->collection = $parts[1];
+ $class->rkey = $parts[2];
+
+ return $class;
+}
+
+function bluesky_fetch_missing_post(string $uri, int $uid)
+{
+ if (Post::exists(['uri' => $uri, 'uid' => [$uid, 0]])) {
+ Logger::debug('Post exists', ['uri' => $uri]);
+ return;
+ }
+
+ Logger::debug('Fetch missing post', ['uri' => $uri]);
+ $class = bluesky_get_uri_class($uri);
+
+ $data = bluesky_get($uid, '/xrpc/app.bsky.feed.getPosts?uris=' . $class->uri, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]);
+ if (empty($data)) {
+ return;
+ }
+
+ foreach ($data->posts as $post) {
+ $item = bluesky_get_header($post, $uri, $uid);
+ $item = bluesky_get_content($item, $post->record, $uid);
+ if (!empty($post->embed)) {
+ $item = bluesky_add_media($post->embed, $item);
+ }
+ $id = Item::insert($item);
+ Logger::debug('Stored item', ['id' => $id, 'uri' => $uri]);
+ }
+}
+