Bluesky: probing for bluesky handles

This commit is contained in:
Michael 2024-09-03 11:23:14 +00:00
parent 28ab491bfb
commit f3b4e21006

View file

@ -130,7 +130,12 @@ function bluesky_probe_detect(array &$hookData)
if (parse_url($hookData['uri'], PHP_URL_SCHEME) == 'did') { if (parse_url($hookData['uri'], PHP_URL_SCHEME) == 'did') {
$did = $hookData['uri']; $did = $hookData['uri'];
} elseif (preg_match('#^' . BLUESKY_WEB . '/profile/(.+)#', $hookData['uri'], $matches)) { } elseif (preg_match('#^' . BLUESKY_WEB . '/profile/(.+)#', $hookData['uri'], $matches)) {
$did = bluesky_get_did($matches[1]); $did = bluesky_get_did($matches[1], $pconfig['uid']);
if (empty($did)) {
return;
}
} elseif (parse_url($hookData['uri'], PHP_URL_PATH) == $hookData['uri'] && strpos($hookData['uri'], '@') === false) {
$did = bluesky_get_did($hookData['uri'], $pconfig['uid']);
if (empty($did)) { if (empty($did)) {
return; return;
} }
@ -181,7 +186,7 @@ function bluesky_item_by_link(array &$hookData)
return; return;
} }
$did = bluesky_get_did($matches[1]); $did = bluesky_get_did($matches[1], $hookData['uid']);
if (empty($did)) { if (empty($did)) {
return; return;
} }
@ -1757,7 +1762,6 @@ function bluesky_get_did_by_wellknown(string $handle): string
Logger::notice('Invalid DID', ['handle' => $handle, 'did' => $did]); Logger::notice('Invalid DID', ['handle' => $handle, 'did' => $did]);
return ''; return '';
} }
Logger::debug('Got DID by wellknown', ['handle' => $handle, 'did' => $did]);
return $did; return $did;
} }
return ''; return '';
@ -1776,14 +1780,13 @@ function bluesky_get_did_by_dns(string $handle): string
Logger::notice('Invalid DID', ['handle' => $handle, 'did' => $did]); Logger::notice('Invalid DID', ['handle' => $handle, 'did' => $did]);
return ''; return '';
} }
Logger::debug('Got DID by DNS', ['handle' => $handle, 'did' => $did]);
return $did; return $did;
} }
} }
return ''; return '';
} }
function bluesky_get_did(string $handle): string function bluesky_get_did(string $handle, int $uid): string
{ {
if ($handle == '') { if ($handle == '') {
return ''; return '';
@ -1793,19 +1796,39 @@ function bluesky_get_did(string $handle): string
$handle .= '.' . BLUESKY_HOSTNAME; $handle .= '.' . BLUESKY_HOSTNAME;
} }
$data = bluesky_get(BLUESKY_PDS . '/xrpc/com.atproto.identity.resolveHandle?handle=' . urlencode($handle)); // At first we use the user PDS. That should cover most cases.
$pds = DI::pConfig()->get($uid, 'bluesky', 'pds');
if (!empty($pds)) {
$data = bluesky_get($pds . '/xrpc/com.atproto.identity.resolveHandle?handle=' . urlencode($handle));
if (!empty($data) && !empty($data->did)) { if (!empty($data) && !empty($data->did)) {
Logger::debug('Got DID by PDS call', ['handle' => $handle, 'did' => $data->did]); Logger::debug('Got DID by user PDS call', ['handle' => $handle, 'did' => $data->did]);
return $data->did; return $data->did;
} }
}
// Possibly a custom PDS. // Then we query the DNS, which is used for third party handles (DNS should be faster than wellknown)
$did = bluesky_get_did_by_dns($handle); $did = bluesky_get_did_by_dns($handle);
if ($did != '') { if ($did != '') {
Logger::debug('Got DID by DNS', ['handle' => $handle, 'did' => $did]);
return $did; return $did;
} }
return bluesky_get_did_by_wellknown($handle); // Then we query wellknown, which should mostly cover the rest.
$did = bluesky_get_did_by_wellknown($handle);
if ($did != '') {
Logger::debug('Got DID by wellknown', ['handle' => $handle, 'did' => $did]);
return $did;
}
// And finally we use the default PDS from Bluesky.
$data = bluesky_get(BLUESKY_PDS . '/xrpc/com.atproto.identity.resolveHandle?handle=' . urlencode($handle));
if (!empty($data) && !empty($data->did)) {
Logger::debug('Got DID by system PDS call', ['handle' => $handle, 'did' => $data->did]);
return $data->did;
}
Logger::notice('No DID detected', ['handle' => $handle]);
return '';
} }
function bluesky_get_user_did(int $uid, bool $refresh = false): ?string function bluesky_get_user_did(int $uid, bool $refresh = false): ?string
@ -1822,7 +1845,7 @@ function bluesky_get_user_did(int $uid, bool $refresh = false): ?string
return null; return null;
} }
$did = bluesky_get_did($handle); $did = bluesky_get_did($handle, $uid);
if (empty($did)) { if (empty($did)) {
return null; return null;
} }