Bluesky: Several improvements and fixes

pull/1469/head
Michael 2024-02-23 13:32:24 +00:00 committed by Hypolite Petovan
parent 52f7910f4c
commit e4bb463b5b
1 changed files with 79 additions and 92 deletions

View File

@ -141,7 +141,7 @@ function bluesky_probe_detect(array &$hookData)
return; 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); $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) { 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)) { if (!empty($entry->reason)) {
bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid); 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; break;
case 'mention': case 'mention':
$data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0, $last_poll); $contact = bluesky_get_contact($notification->author, 0, $uid);
Logger::debug('Got mention', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); $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; break;
case 'reply': case 'reply':
$data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0, $last_poll); $contact = bluesky_get_contact($notification->author, 0, $uid);
Logger::debug('Got reply', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); $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; break;
case 'quote': case 'quote':
$data = bluesky_process_post($notification, $uid, Item::PR_PUSHED, 0, $last_poll); $contact = bluesky_get_contact($notification->author, 0, $uid);
Logger::debug('Got quote', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); $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; break;
default: 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]); Logger::debug('Unwanted language detected', ['text' => $entry->post->record->text]);
continue; continue;
} }
$id = bluesky_process_post($entry->post, $uid, Item::PR_TAG, 0, $last_poll); $uri_id = bluesky_process_post($entry->post, $uid, $uid, Item::PR_TAG, 0, 0, $last_poll);
if (!empty($id)) { if (!empty($uri_id)) {
$post = Post::selectFirst(['uri-id'], ['id' => $id]); $stored = Post\Category::storeFileByURIId($uri_id, $uid, Post\Category::SUBCRIPTION, $feedname, $feedurl);
if (!empty($post['uri-id'])) { Logger::debug('Stored tag subscription for user', ['uri-id' => $uri_id, 'uid' => $uid, 'name' => $feedname, 'url' => $feedurl, 'stored' => $stored]);
$stored = Post\Category::storeFileByURIId($post['uri-id'], $uid, Post\Category::SUBCRIPTION, $feedname, $feedurl); } else {
Logger::debug('Stored tag subscription for user', ['uri-id' => $post['uri-id'], 'uid' => $uid, 'name' => $feedname, 'url' => $feedurl, 'stored' => $stored]); Logger::notice('Post not found', ['entry' => $entry]);
} else {
Logger::notice('Post not found', ['id' => $id, 'entry' => $entry]);
}
} }
if (!empty($entry->reason)) { if (!empty($entry->reason)) {
bluesky_process_reason($entry->reason, bluesky_get_uri($entry->post), $uid); 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); $uri = bluesky_get_uri($post);
if ($id = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $uid])) { if ($uri_id = bluesky_fetch_uri_id($uri, $uid)) {
return $id['id']; return $uri_id;
} }
if ($id = Post::selectFirst(['id'], ['extid' => $uri, 'uid' => $uid])) { if (empty($post->record)) {
return $id['id']; 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 ?? '']); 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_header($post, $uri, $uid, $fetch_uid);
$item = bluesky_get_content($item, $post->record, $uri, $uid, $uid, $level, $last_poll); $item = bluesky_get_content($item, $post->record, $uri, $uid, $fetch_uid, $level, $last_poll);
if (empty($item)) { if (empty($item)) {
return 0; return 0;
} }
@ -1134,13 +1135,18 @@ function bluesky_process_post(stdClass $post, int $uid, int $post_reason, int $l
$item['post-reason'] = $post_reason; $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 function bluesky_get_header(stdClass $post, string $uri, int $uid, int $fetch_uid): array
{ {
$parts = bluesky_get_uri_parts($uri); $parts = bluesky_get_uri_parts($uri);
if (empty($post->author)) { if (empty($post->author) || empty($post->cid) || empty($parts->rkey)) {
return []; return [];
} }
$contact = bluesky_get_contact($post->author, $uid, $fetch_uid); $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; break;
case 'app.bsky.embed.record#view': case 'app.bsky.embed.record#view':
$uri = bluesky_get_uri($embed->record); $original_uri = $uri = bluesky_get_uri($embed->record);
$shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]); $uri = bluesky_fetch_missing_post($uri, $item['uid'], $fetch_uid, $item['contact-id'], $level, $last_poll);
if (empty($shared)) { if ($uri) {
if (empty($embed->record->value)) { $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => [$item['uid'], 0]]);
Logger::info('Record has got no value', ['record' => $embed->record]); $uri_id = $shared['uri-id'] ?? 0;
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);
}
} }
if (!empty($shared['uri-id'])) { if (!empty($uri_id)) {
$item['quote-uri-id'] = $shared['uri-id']; $item['quote-uri-id'] = $uri_id;
} else {
Logger::debug('Quoted post could not be fetched', ['original-uri' => $original_uri, 'uri' => $uri]);
} }
break; break;
case 'app.bsky.embed.recordWithMedia#view': case 'app.bsky.embed.recordWithMedia#view':
$uri = bluesky_get_uri($embed->record->record); $original_uri = $uri = bluesky_get_uri($embed->record->record);
$shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]); $uri = bluesky_fetch_missing_post($uri, $item['uid'], $fetch_uid, $item['contact-id'], $level, $last_poll);
if (empty($shared)) { if ($uri) {
$shared = bluesky_get_header($embed->record->record, $uri, 0, $fetch_uid); $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => [$item['uid'], 0]]);
$shared = bluesky_get_content($shared, $embed->record->record->value, $uri, $item['uid'], $fetch_uid, $level, $last_poll); $uri_id = $shared['uri-id'] ?? 0;
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);
}
} }
if (!empty($shared['uri-id'])) { if (!empty($uri_id)) {
$item['quote-uri-id'] = $shared['uri-id']; $item['quote-uri-id'] = $uri_id;
} } else {
Logger::debug('Quoted post could not be fetched', ['original-uri' => $original_uri, 'uri' => $uri]);
if (!empty($embed->media)) {
$item = bluesky_add_media($embed->media, $item, $fetch_uid, $level, $last_poll);
} }
break; break;
default: 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; break;
} }
return $item; return $item;
@ -1449,6 +1435,21 @@ function bluesky_fetch_post(string $uri, int $uid): string
return ''; 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 function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, array $cdata, int $level, int $last_poll): string
{ {
if (empty($thread->post)) { 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); $fetched_uri = bluesky_fetch_post($uri, $uid);
if (empty($fetched_uri)) { if (empty($fetched_uri)) {
Logger::debug('Process missing post', ['uri' => $uri]); $uri_id = bluesky_process_post($thread->post, $uid, $fetch_uid, Item::PR_FETCHED, $cdata['public'], $level, $last_poll);
$item = bluesky_get_header($thread->post, $uri, $uid, $uid); if ($uri_id) {
$item = bluesky_get_content($item, $thread->post->record, $uri, $uid, $fetch_uid, $level, $last_poll); Logger::debug('Post has been processed and stored', ['uri-id' => $uri_id, 'uri' => $uri]);
if (!empty($item)) { return $uri;
$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]);
} else { } else {
Logger::info('Post has not not been fetched', ['uri' => $uri]); Logger::info('Post has not not been stored', ['uri' => $uri]);
return ''; return '';
} }
} else { } 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'); $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['uid'] = 0;
$public_fields['rel'] = Contact::NOTHING; $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); 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 = [ $fields = [
'uid' => $uid, 'uid' => $uid,
'network' => Protocol::BLUESKY, 'network' => Protocol::BLUESKY,
@ -1555,10 +1542,10 @@ function bluesky_get_contact_fields(stdClass $author, int $uid, bool $update): a
'pending' => false, 'pending' => false,
'url' => $author->did, 'url' => $author->did,
'nurl' => $author->did, 'nurl' => $author->did,
'alias' => BLUESKY_WEB . '/profile/' . $author->handle, 'alias' => BLUESKY_WEB . '/profile/' . $nick,
'name' => $author->displayName ?? $author->handle, 'name' => $author->displayName ?? $nick,
'nick' => $author->handle, 'nick' => $nick,
'addr' => $author->handle, 'addr' => $nick,
]; ];
if (!$update) { 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); $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)) { if (empty($data)) {
Logger::debug('Error fetching contact fields', ['uid' => $uid, 'url' => $fields['url']]); Logger::debug('Error fetching contact fields', ['uid' => $uid, 'url' => $fields['url']]);
return $fields; return $fields;