From 115b1b47335c79ee5b8f11c8e2c8f1f45da7e357 Mon Sep 17 00:00:00 2001 From: call-me-matt Date: Fri, 7 Jul 2023 00:45:04 +0200 Subject: change Mastodon tree for social avatar Signed-off-by: call-me-matt --- lib/Service/Social/MastodonProvider.php | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/Service/Social/MastodonProvider.php b/lib/Service/Social/MastodonProvider.php index 37f20862..fb7f5cf2 100644 --- a/lib/Service/Social/MastodonProvider.php +++ b/lib/Service/Social/MastodonProvider.php @@ -79,9 +79,9 @@ class MastodonProvider implements ISocialProvider { */ public function getImageUrl(string $profileUrl):?string { try { - $result = $this->httpClient->get($profileUrl); - $jsonResult = json_decode($result->getBody()); - return $jsonResult->avatar; + $result = $this->httpClient->get($profileUrl, ['headers' => ['Accept' => 'application/json']]); + $jsonResult = json_decode($result->getBody(), true); + return $jsonResult["icon"]["url"] ?? null; } catch (\Exception $e) { return null; } @@ -104,14 +104,18 @@ class MastodonProvider implements ISocialProvider { if (isset($masto_user_server)) { try { [$masto_user, $masto_server] = $masto_user_server; - # search for user on Mastodon - $search = $masto_server . '/api/v2/search?q=' . $masto_user . '@' . parse_url($masto_server)["host"]; - $result = $this->httpClient->get($search); - $jsonResult = json_decode($result->getBody()); - # take first search result - $masto_id = $jsonResult->accounts[0]->id; - $profileId = $masto_server . "/api/v1/accounts/" . $masto_id; - $profileIds[] = $profileId; + # search for user webfinger + $webfinger = $masto_server . '/.well-known/webfinger?resource=acct:' . $masto_user . '@' . parse_url($masto_server)["host"]; + $result = $this->httpClient->get($webfinger); + $jsonResult = json_decode($result->getBody(), null, 512, JSON_THROW_ON_ERROR); + # find account link + foreach ($jsonResult->links as $link) { + if (($link->rel == "self") and ($link->type == "application/activity+json")) { + $profileId = $link->href; + $profileIds[] = $profileId; + break; + } + } } catch (\Exception $e) { continue; } @@ -143,6 +147,8 @@ class MastodonProvider implements ISocialProvider { if ((empty($masto_server)) || (empty($masto_user))) { return null; } + $masto_user = trim($masto_user, '/'); + $masto_server = trim($masto_server, '/'); return array($masto_user, $masto_server); } catch (\Exception $e) { return null; -- cgit v1.2.3