Merge pull request 'Bluesky: Provide the correct user id while fetching content' (#1439) from heluecht/friendica-addons:bluesky-notices into 2023.09-rc

Reviewed-on: #1439
This commit is contained in:
Hypolite Petovan 2023-11-25 23:06:40 +01:00
commit ed8c5945da

View file

@ -41,6 +41,7 @@ use Friendica\Model\Item;
use Friendica\Model\ItemURI; use Friendica\Model\ItemURI;
use Friendica\Model\Photo; use Friendica\Model\Photo;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\Tag;
use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Object\Image; use Friendica\Object\Image;
@ -174,7 +175,7 @@ function bluesky_item_by_link(array &$hookData)
$uri = 'at://' . $did . '/app.bsky.feed.post/' . $matches[2]; $uri = 'at://' . $did . '/app.bsky.feed.post/' . $matches[2];
$uri = bluesky_fetch_missing_post($uri, $hookData['uid'], 0, 0); $uri = bluesky_fetch_missing_post($uri, $hookData['uid'], $hookData['uid'], 0, 0);
Logger::debug('Got post', ['profile' => $matches[1], 'cid' => $matches[2], 'result' => $uri]); Logger::debug('Got post', ['profile' => $matches[1], 'cid' => $matches[2], 'result' => $uri]);
if (!empty($uri)) { if (!empty($uri)) {
$item = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $hookData['uid']]); $item = Post::selectFirst(['id'], ['uri' => $uri, 'uid' => $hookData['uid']]);
@ -950,7 +951,7 @@ function bluesky_fetch_notifications(int $uid)
$item['gravity'] = Item::GRAVITY_ACTIVITY; $item['gravity'] = Item::GRAVITY_ACTIVITY;
$item['body'] = $item['verb'] = Activity::LIKE; $item['body'] = $item['verb'] = Activity::LIKE;
$item['thr-parent'] = bluesky_get_uri($notification->record->subject); $item['thr-parent'] = bluesky_get_uri($notification->record->subject);
$item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $item['contact-id'], 0); $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $uid, $item['contact-id'], 0);
if (!empty($item['thr-parent'])) { if (!empty($item['thr-parent'])) {
$data = Item::insert($item); $data = Item::insert($item);
Logger::debug('Got like', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); Logger::debug('Got like', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
@ -964,7 +965,7 @@ function bluesky_fetch_notifications(int $uid)
$item['gravity'] = Item::GRAVITY_ACTIVITY; $item['gravity'] = Item::GRAVITY_ACTIVITY;
$item['body'] = $item['verb'] = Activity::ANNOUNCE; $item['body'] = $item['verb'] = Activity::ANNOUNCE;
$item['thr-parent'] = bluesky_get_uri($notification->record->subject); $item['thr-parent'] = bluesky_get_uri($notification->record->subject);
$item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $item['contact-id'], 0); $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $uid, $item['contact-id'], 0);
if (!empty($item['thr-parent'])) { if (!empty($item['thr-parent'])) {
$data = Item::insert($item); $data = Item::insert($item);
Logger::debug('Got repost', ['uid' => $uid, 'result' => $data, 'uri' => $uri]); Logger::debug('Got repost', ['uid' => $uid, 'result' => $data, 'uri' => $uri]);
@ -1059,7 +1060,7 @@ function bluesky_process_post(stdClass $post, int $uid, int $post_reason, $level
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, $uid);
$item = bluesky_get_content($item, $post->record, $uri, $uid, $level); $item = bluesky_get_content($item, $post->record, $uri, $uid, $uid, $level);
if (empty($item)) { if (empty($item)) {
return 0; return 0;
} }
@ -1110,7 +1111,7 @@ function bluesky_get_header(stdClass $post, string $uri, int $uid, int $fetch_ui
return $item; return $item;
} }
function bluesky_get_content(array $item, stdClass $record, string $uri, int $uid, int $level): array function bluesky_get_content(array $item, stdClass $record, string $uri, int $uid, int $fetch_uid, int $level): array
{ {
if (empty($item)) { if (empty($item)) {
return []; return [];
@ -1119,7 +1120,7 @@ function bluesky_get_content(array $item, stdClass $record, string $uri, int $ui
if (!empty($record->reply)) { if (!empty($record->reply)) {
$item['parent-uri'] = bluesky_get_uri($record->reply->root); $item['parent-uri'] = bluesky_get_uri($record->reply->root);
if ($item['parent-uri'] != $uri) { if ($item['parent-uri'] != $uri) {
$item['parent-uri'] = bluesky_fetch_missing_post($item['parent-uri'], $uid, $item['contact-id'], $level); $item['parent-uri'] = bluesky_fetch_missing_post($item['parent-uri'], $uid, $fetch_uid, $item['contact-id'], $level);
if (empty($item['parent-uri'])) { if (empty($item['parent-uri'])) {
return []; return [];
} }
@ -1127,20 +1128,20 @@ function bluesky_get_content(array $item, stdClass $record, string $uri, int $ui
$item['thr-parent'] = bluesky_get_uri($record->reply->parent); $item['thr-parent'] = bluesky_get_uri($record->reply->parent);
if (!in_array($item['thr-parent'], [$uri, $item['parent-uri']])) { if (!in_array($item['thr-parent'], [$uri, $item['parent-uri']])) {
$item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $item['contact-id'], $level, $item['parent-uri']); $item['thr-parent'] = bluesky_fetch_missing_post($item['thr-parent'], $uid, $fetch_uid, $item['contact-id'], $level, $item['parent-uri']);
if (empty($item['thr-parent'])) { if (empty($item['thr-parent'])) {
return []; return [];
} }
} }
} }
$item['body'] = bluesky_get_text($record); $item['body'] = bluesky_get_text($record, $item['uri-id']);
$item['created'] = DateTimeFormat::utc($record->createdAt, DateTimeFormat::MYSQL); $item['created'] = DateTimeFormat::utc($record->createdAt, DateTimeFormat::MYSQL);
$item['transmitted-languages'] = $record->langs ?? []; $item['transmitted-languages'] = $record->langs ?? [];
return $item; return $item;
} }
function bluesky_get_text(stdClass $record): string function bluesky_get_text(stdClass $record, int $uri_id): string
{ {
$text = $record->text ?? ''; $text = $record->text ?? '';
@ -1180,6 +1181,7 @@ function bluesky_get_text(stdClass $record): string
break; break;
case 'app.bsky.richtext.facet#tag'; case 'app.bsky.richtext.facet#tag';
Tag::store($uri_id, Tag::HASHTAG, $feature->tag);
$url = DI::baseUrl() . '/search?tag=' . urlencode($feature->tag); $url = DI::baseUrl() . '/search?tag=' . urlencode($feature->tag);
$linktext = '#' . $feature->tag; $linktext = '#' . $feature->tag;
break; break;
@ -1233,7 +1235,7 @@ function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $le
break; break;
} }
$shared = bluesky_get_header($embed->record, $uri, 0, $fetch_uid); $shared = bluesky_get_header($embed->record, $uri, 0, $fetch_uid);
$shared = bluesky_get_content($shared, $embed->record->value, $uri, $item['uid'], $level); $shared = bluesky_get_content($shared, $embed->record->value, $uri, $item['uid'], $fetch_uid, $level);
if (!empty($shared)) { if (!empty($shared)) {
if (!empty($embed->record->embeds)) { if (!empty($embed->record->embeds)) {
foreach ($embed->record->embeds as $single) { foreach ($embed->record->embeds as $single) {
@ -1253,7 +1255,7 @@ function bluesky_add_media(stdClass $embed, array $item, int $fetch_uid, int $le
$shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]); $shared = Post::selectFirst(['uri-id'], ['uri' => $uri, 'uid' => $item['uid']]);
if (empty($shared)) { if (empty($shared)) {
$shared = bluesky_get_header($embed->record->record, $uri, 0, $fetch_uid); $shared = bluesky_get_header($embed->record->record, $uri, 0, $fetch_uid);
$shared = bluesky_get_content($shared, $embed->record->record->value, $uri, $item['uid'], $level); $shared = bluesky_get_content($shared, $embed->record->record->value, $uri, $item['uid'], $fetch_uid, $level);
if (!empty($shared)) { if (!empty($shared)) {
if (!empty($embed->record->record->embeds)) { if (!empty($embed->record->record->embeds)) {
foreach ($embed->record->record->embeds as $single) { foreach ($embed->record->record->embeds as $single) {
@ -1331,7 +1333,7 @@ function bluesky_get_uri_parts(string $uri): ?stdClass
return $class; return $class;
} }
function bluesky_fetch_missing_post(string $uri, int $uid, int $causer, int $level, string $fallback = ''): string function bluesky_fetch_missing_post(string $uri, int $uid, int $fetch_uid, int $causer, int $level, string $fallback = ''): string
{ {
$fetched_uri = bluesky_fetch_post($uri, $uid); $fetched_uri = bluesky_fetch_post($uri, $uid);
if (!empty($fetched_uri)) { if (!empty($fetched_uri)) {
@ -1349,7 +1351,7 @@ function bluesky_fetch_missing_post(string $uri, int $uid, int $causer, int $lev
$fetch_uri = $class->uri; $fetch_uri = $class->uri;
Logger::debug('Fetch missing post', ['level' => $level, 'uid' => $uid, 'uri' => $uri]); Logger::debug('Fetch missing post', ['level' => $level, 'uid' => $uid, 'uri' => $uri]);
$data = bluesky_xrpc_get($uid, 'app.bsky.feed.getPostThread', ['uri' => $fetch_uri]); $data = bluesky_xrpc_get($fetch_uid, 'app.bsky.feed.getPostThread', ['uri' => $fetch_uri]);
if (empty($data)) { if (empty($data)) {
Logger::info('Thread was not fetched', ['level' => $level, 'uid' => $uid, 'uri' => $uri, 'fallback' => $fallback]); Logger::info('Thread was not fetched', ['level' => $level, 'uid' => $uid, 'uri' => $uri, 'fallback' => $fallback]);
return $fallback; return $fallback;
@ -1363,7 +1365,7 @@ function bluesky_fetch_missing_post(string $uri, int $uid, int $causer, int $lev
$cdata = []; $cdata = [];
} }
return bluesky_process_thread($data->thread, $uid, $cdata, $level); return bluesky_process_thread($data->thread, $uid, $fetch_uid, $cdata, $level);
} }
function bluesky_fetch_post(string $uri, int $uid): string function bluesky_fetch_post(string $uri, int $uid): string
@ -1381,7 +1383,7 @@ function bluesky_fetch_post(string $uri, int $uid): string
return ''; return '';
} }
function bluesky_process_thread(stdClass $thread, int $uid, array $cdata, int $level): string function bluesky_process_thread(stdClass $thread, int $uid, int $fetch_uid, array $cdata, int $level): string
{ {
if (empty($thread->post)) { if (empty($thread->post)) {
Logger::info('Invalid post', ['post' => $thread]); Logger::info('Invalid post', ['post' => $thread]);
@ -1393,7 +1395,7 @@ function bluesky_process_thread(stdClass $thread, int $uid, array $cdata, int $l
if (empty($fetched_uri)) { if (empty($fetched_uri)) {
Logger::debug('Process missing post', ['uri' => $uri]); Logger::debug('Process missing post', ['uri' => $uri]);
$item = bluesky_get_header($thread->post, $uri, $uid, $uid); $item = bluesky_get_header($thread->post, $uri, $uid, $uid);
$item = bluesky_get_content($item, $thread->post->record, $uri, $uid, $level); $item = bluesky_get_content($item, $thread->post->record, $uri, $uid, $fetch_uid, $level);
if (!empty($item)) { if (!empty($item)) {
$item['post-reason'] = Item::PR_FETCHED; $item['post-reason'] = Item::PR_FETCHED;
@ -1420,7 +1422,7 @@ function bluesky_process_thread(stdClass $thread, int $uid, array $cdata, int $l
} }
foreach ($thread->replies ?? [] as $reply) { foreach ($thread->replies ?? [] as $reply) {
$reply_uri = bluesky_process_thread($reply, $uid, $cdata, $level); $reply_uri = bluesky_process_thread($reply, $uid, $fetch_uid, $cdata, $level);
Logger::debug('Reply has been processed', ['uri' => $uri, 'reply' => $reply_uri]); Logger::debug('Reply has been processed', ['uri' => $uri, 'reply' => $reply_uri]);
} }
@ -1578,7 +1580,12 @@ function bluesky_get_user_pds(int $uid): string
if (!empty($pds)) { if (!empty($pds)) {
return $pds; return $pds;
} }
$pds = bluesky_get_pds(DI::pConfig()->get($uid, 'bluesky', 'did')); $did = DI::pConfig()->get($uid, 'bluesky', 'did');
if (empty($did)) {
Logger::notice('Empty did for user', ['uid' => $uid]);
return '';
}
$pds = bluesky_get_pds($did);
DI::pConfig()->set($uid, 'bluesky', 'pds', $pds); DI::pConfig()->set($uid, 'bluesky', 'pds', $pds);
return $pds; return $pds;
} }