From e4bb463b5b36b90018672b8c8fc9c23192ddde4c Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 23 Feb 2024 13:32:24 +0000 Subject: [PATCH 1/4] Bluesky: Several improvements and fixes --- bluesky/bluesky.php | 171 ++++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 92 deletions(-) diff --git a/bluesky/bluesky.php b/bluesky/bluesky.php index fe1faab1..b651238b 100644 --- a/bluesky/bluesky.php +++ b/bluesky/bluesky.php @@ -141,7 +141,7 @@ function bluesky_probe_detect(array &$hookData) return; } - $hookData['result'] = bluesky_get_contact_fields($data, 0, false); + $hookData['result'] = bluesky_get_contact_fields($data, 0, $pconfig['uid'], false); $hookData['result']['baseurl'] = bluesky_get_pds($did); @@ -942,7 +942,7 @@ function bluesky_fetch_timeline(int $uid, int $last_poll) } foreach (array_reverse($data->feed) as $entry) { - bluesky_process_post($entry->post, $uid, Item::PR_NONE, 0, $last_poll); + bluesky_process_post($entry->post, $uid, $uid, Item::PR_NONE, 0, 0, $last_poll); if (!empty($entry->reason)) { bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid); } @@ -1041,18 +1041,21 @@ function bluesky_fetch_notifications(int $uid, int $last_poll) break; case 'mention': - $data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0, $last_poll); - Logger::debug('Got mention', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); + $contact = bluesky_get_contact($notification->author, 0, $uid); + $result = bluesky_fetch_missing_post($uri, $uid, $uid, $contact['id'], 0, $last_poll); + Logger::debug('Got mention', ['uid' => $uid, 'nick' => $contact['nick'], 'result' => $result, 'uri' => $uri]); break; case 'reply': - $data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0, $last_poll); - Logger::debug('Got reply', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); + $contact = bluesky_get_contact($notification->author, 0, $uid); + $result = bluesky_fetch_missing_post($uri, $uid, $uid, $contact['id'], 0, $last_poll); + Logger::debug('Got reply', ['uid' => $uid, 'nick' => $contact['nick'], 'result' => $result, 'uri' => $uri]); break; case 'quote': - $data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0, $last_poll); - Logger::debug('Got quote', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); + $contact = bluesky_get_contact($notification->author, 0, $uid); + $result = bluesky_fetch_missing_post($uri, $uid, $uid, $contact['id'], 0, $last_poll); + Logger::debug('Got quote', ['uid' => $uid, 'nick' => $contact['nick'], 'result' => $result, 'uri' => $uri]); break; default: @@ -1090,15 +1093,12 @@ function bluesky_fetch_feed(int $uid, string $feed, int $last_poll) Logger::debug('Unwanted language detected', ['text' => $entry->post->record->text]); continue; } - $id = bluesky_process_post($entry->post, $uid, Item::PR_TAG, 0, $last_poll); - if (!empty($id)) { - $post = Post::selectFirst(['uri-id'], ['id' => $id]); - if (!empty($post['uri-id'])) { - $stored = Post\Category::storeFileByURIId($post['uri-id'], $uid, Post\Category::SUBCRIPTION, $feedname, $feedurl); - Logger::debug('Stored tag subscription for user', ['uri-id' => $post['uri-id'], 'uid' => $uid, 'name' => $feedname, 'url' => $feedurl, 'stored' => $stored]); - } else { - Logger::notice('Post not found', ['id' => $id, 'entry' => $entry]); - } + $uri_id = bluesky_process_post($entry->post, $uid, $uid, Item::PR_TAG, 0, 0, $last_poll); + if (!empty($uri_id)) { + $stored = Post\Category::storeFileByURIId($uri_id, $uid, Post\Category::SUBCRIPTION, $feedname, $feedurl); + Logger::debug('Stored tag subscription for user', ['uri-id' => $uri_id, 'uid' => $uid, 'name' => $feedname, 'url' => $feedurl, 'stored' => $stored]); + } else { + Logger::notice('Post not found', ['entry' => $entry]); } if (!empty($entry->reason)) { bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid); @@ -1106,22 +1106,23 @@ function bluesky_fetch_feed(int $uid, string $feed, int $last_poll) } } -function bluesky_process_post(stdClass $post, int $uid, int $post_reason, int $level, int $last_poll): int +function bluesky_process_post(stdClass $post, int $uid, int $fetch_uid, int $post_reason, int $causer, int $level, int $last_poll): int { $uri = bluesky_get_uri($post); - if ($id = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $uid])) { - return $id['id']; + if ($uri_id = bluesky_fetch_uri_id($uri, $uid)) { + return $uri_id; } - if ($id = Post::selectFirst(['id'], ['extid' => $uri, 'uid' => $uid])) { - return $id['id']; + if (empty($post->record)) { + Logger::debug('Invalid post', ['uri' => $uri]); + return 0; } Logger::debug('Importing post', ['uid' => $uid, 'indexedAt' => $post->indexedAt, 'uri' => $post->uri, 'cid' => $post->cid, 'root' => $post->record->reply->root ?? '']); - $item = bluesky_get_header($post, $uri, $uid, $uid); - $item = bluesky_get_content($item, $post->record, $uri, $uid, $uid, $level, $last_poll); + $item = bluesky_get_header($post, $uri, $uid, $fetch_uid); + $item = bluesky_get_content($item, $post->record, $uri, $uid, $fetch_uid, $level, $last_poll); if (empty($item)) { return 0; } @@ -1134,13 +1135,18 @@ function bluesky_process_post(stdClass $post, int $uid, int $post_reason, int $l $item['post-reason'] = $post_reason; } - return Item::insert($item); + if ($causer != 0) { + $item['causer-id'] = $causer; + } + + Item::insert($item); + return bluesky_fetch_uri_id($uri, $uid); } function bluesky_get_header(stdClass $post, string $uri, int $uid, int $fetch_uid): array { $parts = bluesky_get_uri_parts($uri); - if (empty($post->author)) { + if (empty($post->author) || empty($post->cid) || empty($parts->rkey)) { return []; } $contact = bluesky_get_contact($post->author, $uid, $fetch_uid); @@ -1293,55 +1299,35 @@ function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $le break; case 'app.bsky.embed.record#view': - $uri = bluesky_get_uri($embed->record); - $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]); - if (empty($shared)) { - if (empty($embed->record->value)) { - Logger::info('Record has got no value', ['record' => $embed->record]); - break; - } - $shared = bluesky_get_header($embed->record, $uri, 0, $fetch_uid); - $shared = bluesky_get_content($shared, $embed->record->value, $uri, $item['uid'], $fetch_uid, $level, $last_poll); - if (!empty($shared)) { - if (!empty($embed->record->embeds)) { - foreach ($embed->record->embeds as $single) { - $shared = bluesky_add_media($single, $shared, $fetch_uid, $level, $last_poll); - } - } - Item::insert($shared); - } + $original_uri = $uri = bluesky_get_uri($embed->record); + $uri = bluesky_fetch_missing_post($uri, $item['uid'], $fetch_uid, $item['contact-id'], $level, $last_poll); + if ($uri) { + $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => [$item['uid'], 0]]); + $uri_id = $shared['uri-id'] ?? 0; } - if (!empty($shared['uri-id'])) { - $item['quote-uri-id'] = $shared['uri-id']; + if (!empty($uri_id)) { + $item['quote-uri-id'] = $uri_id; + } else { + Logger::debug('Quoted post could not be fetched', ['original-uri' => $original_uri, 'uri' => $uri]); } break; case 'app.bsky.embed.recordWithMedia#view': - $uri = bluesky_get_uri($embed->record->record); - $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]); - if (empty($shared)) { - $shared = bluesky_get_header($embed->record->record, $uri, 0, $fetch_uid); - $shared = bluesky_get_content($shared, $embed->record->record->value, $uri, $item['uid'], $fetch_uid, $level, $last_poll); - if (!empty($shared)) { - if (!empty($embed->record->record->embeds)) { - foreach ($embed->record->record->embeds as $single) { - $shared = bluesky_add_media($single, $shared, $fetch_uid, $level, $last_poll); - } - } - Item::insert($shared); - } + $original_uri = $uri = bluesky_get_uri($embed->record->record); + $uri = bluesky_fetch_missing_post($uri, $item['uid'], $fetch_uid, $item['contact-id'], $level, $last_poll); + if ($uri) { + $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => [$item['uid'], 0]]); + $uri_id = $shared['uri-id'] ?? 0; } - if (!empty($shared['uri-id'])) { - $item['quote-uri-id'] = $shared['uri-id']; - } - - if (!empty($embed->media)) { - $item = bluesky_add_media($embed->media, $item, $fetch_uid, $level, $last_poll); + if (!empty($uri_id)) { + $item['quote-uri-id'] = $uri_id; + } else { + Logger::debug('Quoted post could not be fetched', ['original-uri' => $original_uri, 'uri' => $uri]); } break; default: - Logger::notice('Unhandled embed type', ['type' => $embed->$type, 'embed' => $embed]); + Logger::notice('Unhandled embed type', ['uri-id' => $item['uri-id'], 'type' => $embed->$type, 'embed' => $embed]); break; } return $item; @@ -1449,6 +1435,21 @@ function bluesky_fetch_post(string $uri, int $uid): string return ''; } +function bluesky_fetch_uri_id(string $uri, int $uid): string +{ + $reply = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => [$uid, 0]]); + if (!empty($reply['uri-id'])) { + Logger::debug('Post with extid exists', ['uri' => $uri]); + return $reply['uri-id']; + } + $reply = Post::selectFirst(['uri-id'], ['extid' => $uri, 'uid' => [$uid, 0]]); + if (!empty($reply['uri-id'])) { + Logger::debug('Post with extid exists', ['uri' => $uri]); + return $reply['uri-id']; + } + return 0; +} + function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, array $cdata, int $level, int $last_poll): string { if (empty($thread->post)) { @@ -1459,27 +1460,12 @@ function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, arra $fetched_uri = bluesky_fetch_post($uri, $uid); if (empty($fetched_uri)) { - Logger::debug('Process missing post', ['uri' => $uri]); - $item = bluesky_get_header($thread->post, $uri, $uid, $uid); - $item = bluesky_get_content($item, $thread->post->record, $uri, $uid, $fetch_uid, $level, $last_poll); - if (!empty($item)) { - $item['post-reason'] = Item::PR_FETCHED; - - if (!empty($cdata['public'])) { - $item['causer-id'] = $cdata['public']; - } - - if (!empty($thread->post->embed)) { - $item = bluesky_add_media($thread->post->embed, $item, $uid, $level, $last_poll); - } - $id = Item::insert($item); - if (!$id) { - Logger::info('Item has not not been stored', ['uri' => $uri]); - return ''; - } - Logger::debug('Stored item', ['id' => $id, 'uri' => $uri]); + $uri_id = bluesky_process_post($thread->post, $uid, $fetch_uid, Item::PR_FETCHED, $cdata['public'], $level, $last_poll); + if ($uri_id) { + Logger::debug('Post has been processed and stored', ['uri-id' => $uri_id, 'uri' => $uri]); + return $uri; } else { - Logger::info('Post has not not been fetched', ['uri' => $uri]); + Logger::info('Post has not not been stored', ['uri' => $uri]); return ''; } } else { @@ -1502,7 +1488,7 @@ function bluesky_get_contact(stdClass $author, int $uid, int $fetch_uid): array $update = empty($contact) || $contact['updated'] < DateTimeFormat::utc('now -24 hours'); - $public_fields = $fields = bluesky_get_contact_fields($author, $fetch_uid, $update); + $public_fields = $fields = bluesky_get_contact_fields($author, $uid, $fetch_uid, $update); $public_fields['uid'] = 0; $public_fields['rel'] = Contact::NOTHING; @@ -1543,8 +1529,9 @@ function bluesky_get_contact(stdClass $author, int $uid, int $fetch_uid): array return Contact::getById($cid); } -function bluesky_get_contact_fields(stdClass $author, int $uid, bool $update): array +function bluesky_get_contact_fields(stdClass $author, int $uid, int $fetch_uid, bool $update): array { + $nick = $author->handle ?? $author->did; $fields = [ 'uid' => $uid, 'network' => Protocol::BLUESKY, @@ -1555,10 +1542,10 @@ function bluesky_get_contact_fields(stdClass $author, int $uid, bool $update): a 'pending' => false, 'url' => $author->did, 'nurl' => $author->did, - 'alias' => BLUESKY_WEB . '/profile/' . $author->handle, - 'name' => $author->displayName ?? $author->handle, - 'nick' => $author->handle, - 'addr' => $author->handle, + 'alias' => BLUESKY_WEB . '/profile/' . $nick, + 'name' => $author->displayName ?? $nick, + 'nick' => $nick, + 'addr' => $nick, ]; if (!$update) { @@ -1572,7 +1559,7 @@ function bluesky_get_contact_fields(stdClass $author, int $uid, bool $update): a $fields['gsid'] = GServer::getID($fields['baseurl'], true); } - $data = bluesky_xrpc_get($uid, 'app.bsky.actor.getProfile', ['actor' => $author->did]); + $data = bluesky_xrpc_get($fetch_uid, 'app.bsky.actor.getProfile', ['actor' => $author->did]); if (empty($data)) { Logger::debug('Error fetching contact fields', ['uid' => $uid, 'url' => $fields['url']]); return $fields; From 2b5f8e9c82c2bae768b5b38a18a4587ce34020e8 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 23 Feb 2024 14:04:06 +0000 Subject: [PATCH 2/4] Fix coding standards --- bluesky/bluesky.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bluesky/bluesky.php b/bluesky/bluesky.php index b651238b..5fa2f32f 100644 --- a/bluesky/bluesky.php +++ b/bluesky/bluesky.php @@ -1019,7 +1019,7 @@ function bluesky_fetch_notifications(int $uid, int $last_poll) } else { Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $item['thr-parent'], 'uri' => $uri]); } - break; + break; case 'repost': $item = bluesky_get_header($notification, $uri, $uid, $uid); @@ -1033,7 +1033,7 @@ function bluesky_fetch_notifications(int $uid, int $last_poll) } else { Logger::info('Thread parent not found', ['uid' => $uid, 'parent' => $item['thr-parent'], 'uri' => $uri]); } - break; + break; case 'follow': $contact = bluesky_get_contact($notification->author, $uid, $uid); @@ -1531,7 +1531,7 @@ function bluesky_get_contact(stdClass $author, int $uid, int $fetch_uid): array function bluesky_get_contact_fields(stdClass $author, int $uid, int $fetch_uid, bool $update): array { - $nick = $author->handle ?? $author->did; + $nick = $author->handle ?: $author->did; $fields = [ 'uid' => $uid, 'network' => Protocol::BLUESKY, @@ -1543,7 +1543,7 @@ function bluesky_get_contact_fields(stdClass $author, int $uid, int $fetch_uid, 'url' => $author->did, 'nurl' => $author->did, 'alias' => BLUESKY_WEB . '/profile/' . $nick, - 'name' => $author->displayName ?? $nick, + 'name' => $author->displayName ?: $nick, 'nick' => $nick, 'addr' => $nick, ]; @@ -1741,7 +1741,6 @@ function bluesky_xrpc_get(int $uid, string $url, array $parameters = []): ?stdCl $data = bluesky_get(bluesky_get_user_pds($uid) . '/xrpc/' . $url, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . bluesky_get_token($uid)]]]); DI::pConfig()->set($uid, 'bluesky', 'status', is_null($data) ? BLUEKSY_STATUS_API_FAIL : BLUEKSY_STATUS_SUCCESS); return $data; - } function bluesky_get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ?stdClass From bcef83e14852d4bcab31fcc533a05ec96ebfcfb8 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 24 Feb 2024 06:48:19 +0000 Subject: [PATCH 3/4] Set post reasons --- bluesky/bluesky.php | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/bluesky/bluesky.php b/bluesky/bluesky.php index 5fa2f32f..7da7d6b5 100644 --- a/bluesky/bluesky.php +++ b/bluesky/bluesky.php @@ -183,7 +183,7 @@ function bluesky_item_by_link(array &$hookData) $uri = 'at://' . $did . '/app.bsky.feed.post/' . $matches[2]; - $uri = bluesky_fetch_missing_post($uri, $hookData['uid'], $hookData['uid'], 0, 0); + $uri = bluesky_fetch_missing_post($uri, $hookData['uid'], $hookData['uid'], Item::PR_FETCHED, 0, 0, 0); Logger::debug('Got post', ['profile' => $matches[1], 'cid' => $matches[2], 'result' => $uri]); if (!empty($uri)) { $item = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $hookData['uid']]); @@ -1012,7 +1012,7 @@ function bluesky_fetch_notifications(int $uid, int $last_poll) $item['gravity'] = Item::GRAVITY_ACTIVITY; $item['body'] = $item['verb'] = Activity::LIKE; $item['thr-parent'] = bluesky_get_uri($notification->record->subject); - $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $uid, $item['contact-id'], 0, $last_poll); + $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $uid, Item::PR_FETCHED, $item['contact-id'], 0, $last_poll); if (!empty($item['thr-parent'])) { $data = Item::insert($item); Logger::debug('Got like', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); @@ -1026,7 +1026,7 @@ function bluesky_fetch_notifications(int $uid, int $last_poll) $item['gravity'] = Item::GRAVITY_ACTIVITY; $item['body'] = $item['verb'] = Activity::ANNOUNCE; $item['thr-parent'] = bluesky_get_uri($notification->record->subject); - $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $uid, $item['contact-id'], 0, $last_poll); + $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $uid, Item::PR_FETCHED, $item['contact-id'], 0, $last_poll); if (!empty($item['thr-parent'])) { $data = Item::insert($item); Logger::debug('Got repost', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); @@ -1042,19 +1042,19 @@ function bluesky_fetch_notifications(int $uid, int $last_poll) case 'mention': $contact = bluesky_get_contact($notification->author, 0, $uid); - $result = bluesky_fetch_missing_post($uri, $uid, $uid, $contact['id'], 0, $last_poll); + $result = bluesky_fetch_missing_post($uri, $uid, $uid, Item::PR_TO, $contact['id'], 0, $last_poll); Logger::debug('Got mention', ['uid' => $uid, 'nick' => $contact['nick'], 'result' => $result, 'uri' => $uri]); break; case 'reply': $contact = bluesky_get_contact($notification->author, 0, $uid); - $result = bluesky_fetch_missing_post($uri, $uid, $uid, $contact['id'], 0, $last_poll); + $result = bluesky_fetch_missing_post($uri, $uid, $uid, Item::PR_COMMENT, $contact['id'], 0, $last_poll); Logger::debug('Got reply', ['uid' => $uid, 'nick' => $contact['nick'], 'result' => $result, 'uri' => $uri]); break; case 'quote': $contact = bluesky_get_contact($notification->author, 0, $uid); - $result = bluesky_fetch_missing_post($uri, $uid, $uid, $contact['id'], 0, $last_poll); + $result = bluesky_fetch_missing_post($uri, $uid, $uid, Item::PR_PUSHED, $contact['id'], 0, $last_poll); Logger::debug('Got quote', ['uid' => $uid, 'nick' => $contact['nick'], 'result' => $result, 'uri' => $uri]); break; @@ -1187,7 +1187,7 @@ function bluesky_get_content(array $item, stdClass $record, string $uri, int $ui if (!empty($record->reply)) { $item['parent-uri'] = bluesky_get_uri($record->reply->root); if ($item['parent-uri'] != $uri) { - $item['parent-uri'] = bluesky_fetch_missing_post($item['parent-uri'], $uid, $fetch_uid, $item['contact-id'], $level, $last_poll); + $item['parent-uri'] = bluesky_fetch_missing_post($item['parent-uri'], $uid, $fetch_uid, Item::PR_COMPLETION, $item['contact-id'], $level, $last_poll); if (empty($item['parent-uri'])) { return []; } @@ -1195,7 +1195,7 @@ function bluesky_get_content(array $item, stdClass $record, string $uri, int $ui $item['thr-parent'] = bluesky_get_uri($record->reply->parent); if (!in_array($item['thr-parent'], [$uri, $item['parent-uri']])) { - $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $fetch_uid, $item['contact-id'], $level, $last_poll, $item['parent-uri']); + $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $fetch_uid, Item::PR_COMPLETION, $item['contact-id'], $level, $last_poll, $item['parent-uri']); if (empty($item['thr-parent'])) { return []; } @@ -1300,7 +1300,7 @@ function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $le case 'app.bsky.embed.record#view': $original_uri = $uri = bluesky_get_uri($embed->record); - $uri = bluesky_fetch_missing_post($uri, $item['uid'], $fetch_uid, $item['contact-id'], $level, $last_poll); + $uri = bluesky_fetch_missing_post($uri, $item['uid'], $fetch_uid, Item::PR_FETCHED, $item['contact-id'], $level, $last_poll); if ($uri) { $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => [$item['uid'], 0]]); $uri_id = $shared['uri-id'] ?? 0; @@ -1314,7 +1314,7 @@ function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $le case 'app.bsky.embed.recordWithMedia#view': $original_uri = $uri = bluesky_get_uri($embed->record->record); - $uri = bluesky_fetch_missing_post($uri, $item['uid'], $fetch_uid, $item['contact-id'], $level, $last_poll); + $uri = bluesky_fetch_missing_post($uri, $item['uid'], $fetch_uid, Item::PR_FETCHED, $item['contact-id'], $level, $last_poll); if ($uri) { $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => [$item['uid'], 0]]); $uri_id = $shared['uri-id'] ?? 0; @@ -1385,7 +1385,7 @@ function bluesky_get_uri_parts(string $uri): ?stdClass return $class; } -function bluesky_fetch_missing_post(string $uri, int $uid, int $fetch_uid, int $causer, int $level, int $last_poll = 0, string $fallback = ''): string +function bluesky_fetch_missing_post(string $uri, int $uid, int $fetch_uid, int $post_reason, int $causer, int $level, int $last_poll, string $fallback = ''): string { $fetched_uri = bluesky_fetch_post($uri, $uid); if (!empty($fetched_uri)) { @@ -1413,11 +1413,10 @@ function bluesky_fetch_missing_post(string $uri, int $uid, int $fetch_uid, int $ if ($causer != 0) { $cdata = Contact::getPublicAndUserContactID($causer, $uid); - } else { - $cdata = []; + $causer = $cdata['public'] ?? 0; } - return bluesky_process_thread($data->thread, $uid, $fetch_uid, $cdata, $level, $last_poll); + return bluesky_process_thread($data->thread, $uid, $fetch_uid, $post_reason, $causer, $level, $last_poll); } function bluesky_fetch_post(string $uri, int $uid): string @@ -1450,7 +1449,7 @@ function bluesky_fetch_uri_id(string $uri, int $uid): string return 0; } -function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, array $cdata, int $level, int $last_poll): string +function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, int $post_reason, int $causer, int $level, int $last_poll): string { if (empty($thread->post)) { Logger::info('Invalid post', ['post' => $thread]); @@ -1460,7 +1459,7 @@ function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, arra $fetched_uri = bluesky_fetch_post($uri, $uid); if (empty($fetched_uri)) { - $uri_id = bluesky_process_post($thread->post, $uid, $fetch_uid, Item::PR_FETCHED, $cdata['public'], $level, $last_poll); + $uri_id = bluesky_process_post($thread->post, $uid, $fetch_uid, $post_reason, $causer, $level, $last_poll); if ($uri_id) { Logger::debug('Post has been processed and stored', ['uri-id' => $uri_id, 'uri' => $uri]); return $uri; @@ -1474,7 +1473,7 @@ function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, arra } foreach ($thread->replies ?? [] as $reply) { - $reply_uri = bluesky_process_thread($reply, $uid, $fetch_uid, $cdata, $level, $last_poll); + $reply_uri = bluesky_process_thread($reply, $uid, $fetch_uid, Item::PR_COMPLETION, $causer, $level, $last_poll); Logger::debug('Reply has been processed', ['uri' => $uri, 'reply' => $reply_uri]); } @@ -1531,7 +1530,7 @@ function bluesky_get_contact(stdClass $author, int $uid, int $fetch_uid): array function bluesky_get_contact_fields(stdClass $author, int $uid, int $fetch_uid, bool $update): array { - $nick = $author->handle ?: $author->did; + $nick = $author->handle ?? $author->did; $fields = [ 'uid' => $uid, 'network' => Protocol::BLUESKY, @@ -1543,7 +1542,7 @@ function bluesky_get_contact_fields(stdClass $author, int $uid, int $fetch_uid, 'url' => $author->did, 'nurl' => $author->did, 'alias' => BLUESKY_WEB . '/profile/' . $nick, - 'name' => $author->displayName ?: $nick, + 'name' => $author->displayName ?? $nick, 'nick' => $nick, 'addr' => $nick, ]; From 6d578d14952251e4826103d8ab035c8dbd9dee69 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 24 Feb 2024 08:26:06 +0000 Subject: [PATCH 4/4] Changed post reason --- bluesky/bluesky.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bluesky/bluesky.php b/bluesky/bluesky.php index 7da7d6b5..5e3de1a0 100644 --- a/bluesky/bluesky.php +++ b/bluesky/bluesky.php @@ -1187,7 +1187,7 @@ function bluesky_get_content(array $item, stdClass $record, string $uri, int $ui if (!empty($record->reply)) { $item['parent-uri'] = bluesky_get_uri($record->reply->root); if ($item['parent-uri'] != $uri) { - $item['parent-uri'] = bluesky_fetch_missing_post($item['parent-uri'], $uid, $fetch_uid, Item::PR_COMPLETION, $item['contact-id'], $level, $last_poll); + $item['parent-uri'] = bluesky_fetch_missing_post($item['parent-uri'], $uid, $fetch_uid, Item::PR_FETCHED, $item['contact-id'], $level, $last_poll); if (empty($item['parent-uri'])) { return []; } @@ -1195,7 +1195,7 @@ function bluesky_get_content(array $item, stdClass $record, string $uri, int $ui $item['thr-parent'] = bluesky_get_uri($record->reply->parent); if (!in_array($item['thr-parent'], [$uri, $item['parent-uri']])) { - $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $fetch_uid, Item::PR_COMPLETION, $item['contact-id'], $level, $last_poll, $item['parent-uri']); + $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $fetch_uid, Item::PR_FETCHED, $item['contact-id'], $level, $last_poll, $item['parent-uri']); if (empty($item['thr-parent'])) { return []; } @@ -1473,7 +1473,7 @@ function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, int } foreach ($thread->replies ?? [] as $reply) { - $reply_uri = bluesky_process_thread($reply, $uid, $fetch_uid, Item::PR_COMPLETION, $causer, $level, $last_poll); + $reply_uri = bluesky_process_thread($reply, $uid, $fetch_uid, Item::PR_FETCHED, $causer, $level, $last_poll); Logger::debug('Reply has been processed', ['uri' => $uri, 'reply' => $reply_uri]); }