Bluesky: Improved fetching of the user DID

This commit is contained in:
Michael 2024-04-28 10:36:47 +00:00
parent d4abc9bac8
commit b9d0cece0a

View file

@ -97,7 +97,7 @@ function bluesky_load_config(ConfigFileManager $loader)
function bluesky_check_item_notification(array &$notification_data) function bluesky_check_item_notification(array &$notification_data)
{ {
$did = DI::pConfig()->get($notification_data['uid'], 'bluesky', 'did'); $did = bluesky_get_user_did($notification_data['uid']);
if (!empty($did)) { if (!empty($did)) {
$notification_data['profiles'][] = $did; $notification_data['profiles'][] = $did;
@ -342,7 +342,7 @@ function bluesky_settings(array &$data)
$def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default') ?? false; $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default') ?? false;
$pds = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'pds'); $pds = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'pds');
$handle = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle'); $handle = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle');
$did = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'did'); $did = bluesky_get_user_did(DI::userSession()->getLocalUserId());
$token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'access_token'); $token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'access_token');
$import = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'import') ?? false; $import = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'import') ?? false;
$import_feeds = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'import_feeds') ?? false; $import_feeds = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'import_feeds') ?? false;
@ -438,15 +438,7 @@ function bluesky_settings_post(array &$b)
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'friendica_handle', intval($_POST['bluesky_friendica_handle'])); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'friendica_handle', intval($_POST['bluesky_friendica_handle']));
if (!empty($handle)) { if (!empty($handle)) {
if (empty($old_did) || $old_handle != $handle) { $did = bluesky_get_user_did(DI::userSession()->getLocalUserId(), empty($old_did) || $old_handle != $handle);
$did = bluesky_get_did(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle'));
if (empty($did)) {
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'status', BLUEKSY_STATUS_DID_FAIL);
}
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'did', $did);
} else {
$did = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
}
if (!empty($did) && (empty($old_pds) || $old_handle != $handle)) { if (!empty($did) && (empty($old_pds) || $old_handle != $handle)) {
$pds = bluesky_get_pds($did); $pds = bluesky_get_pds($did);
if (empty($pds)) { if (empty($pds)) {
@ -664,7 +656,7 @@ function bluesky_create_activity(array $item, stdClass $parent = null)
return; return;
} }
$did = DI::pConfig()->get($uid, 'bluesky', 'did'); $did = bluesky_get_user_did($uid);
if ($item['verb'] == Activity::LIKE) { if ($item['verb'] == Activity::LIKE) {
$record = [ $record = [
@ -1775,18 +1767,41 @@ function bluesky_get_did(string $handle): string
return $data->did; return $data->did;
} }
function bluesky_get_user_did(int $uid, bool $refresh = false): string
{
if (!$refresh) {
$did = DI::pConfig()->get($uid, 'bluesky', 'did');
if (!empty($did)) {
return $did;
}
}
$handle = DI::pConfig()->get($uid, 'bluesky', 'handle');
$did = bluesky_get_did($handle);
if (empty($did)) {
Logger::notice('Error fetching DID for handle', ['uid' => $uid, 'handle' => $handle]);
DI::pConfig()->set($uid, 'bluesky', 'status', BLUEKSY_STATUS_DID_FAIL);
}
Logger::debug('Got DID for user', ['uid' => $uid, 'handle' => $handle, 'did' => $did]);
DI::pConfig()->set($uid, 'bluesky', 'did', $did);
return $did;
}
function bluesky_get_user_pds(int $uid): string function bluesky_get_user_pds(int $uid): string
{ {
$pds = DI::pConfig()->get($uid, 'bluesky', 'pds'); $pds = DI::pConfig()->get($uid, 'bluesky', 'pds');
if (!empty($pds)) { if (!empty($pds)) {
return $pds; return $pds;
} }
$did = DI::pConfig()->get($uid, 'bluesky', 'did'); $did = bluesky_get_user_did($uid);
if (empty($did)) { if (!empty($did)) {
Logger::notice('Empty did for user', ['uid' => $uid]);
return '';
}
$pds = bluesky_get_pds($did); $pds = bluesky_get_pds($did);
} else {
Logger::notice('Empty did for user', ['uid' => $uid]);
}
if (empty($pds)) {
return BLUESKY_PDS;
}
DI::pConfig()->set($uid, 'bluesky', 'pds', $pds); DI::pConfig()->set($uid, 'bluesky', 'pds', $pds);
return $pds; return $pds;
} }
@ -1849,7 +1864,7 @@ function bluesky_refresh_token(int $uid): string
function bluesky_create_token(int $uid, string $password): string function bluesky_create_token(int $uid, string $password): string
{ {
$did = DI::pConfig()->get($uid, 'bluesky', 'did'); $did = bluesky_get_user_did($uid);
$data = bluesky_post($uid, '/xrpc/com.atproto.server.createSession', json_encode(['identifier' => $did, 'password' => $password]), ['Content-type' => 'application/json']); $data = bluesky_post($uid, '/xrpc/com.atproto.server.createSession', json_encode(['identifier' => $did, 'password' => $password]), ['Content-type' => 'application/json']);
if (empty($data)) { if (empty($data)) {
@ -1906,12 +1921,12 @@ function bluesky_get(string $url, string $accept_content = HttpClientAccept::DEF
try { try {
$curlResult = DI::httpClient()->get($url, $accept_content, $opts); $curlResult = DI::httpClient()->get($url, $accept_content, $opts);
} catch (\Exception $e) { } catch (\Exception $e) {
Logger::notice('Exception on get', ['exception' => $e]); Logger::notice('Exception on get', ['url' => $url, 'exception' => $e]);
return null; return null;
} }
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
Logger::notice('API Error', ['error' => json_decode($curlResult->getBodyString()) ?: $curlResult->getBodyString()]); Logger::notice('API Error', ['url' => $url, 'error' => json_decode($curlResult->getBodyString()) ?: $curlResult->getBodyString()]);
return null; return null;
} }