summaryrefslogtreecommitdiffstats
path: root/lib/Service
diff options
context:
space:
mode:
authorRichard Steinmetz <richard@steinmetz.cloud>2023-01-09 09:47:22 +0100
committerGitHub <noreply@github.com>2023-01-09 09:47:22 +0100
commit831e25b157fe1c1b7ccc39678d74af1aebdc40f8 (patch)
tree922a49835ca46a4c4af54401bc14eb80445e919d /lib/Service
parente011d765efb5402434913a1a1b378beb91d0a3a6 (diff)
parent887b2aba4ef830d45b7be5a0a8553edd670ff6e7 (diff)
Merge pull request #3152 from nextcloud/3151-mastodon-api
use Mastodon API instead of scraping
Diffstat (limited to 'lib/Service')
-rw-r--r--lib/Service/Social/MastodonProvider.php50
1 files changed, 31 insertions, 19 deletions
diff --git a/lib/Service/Social/MastodonProvider.php b/lib/Service/Social/MastodonProvider.php
index 99eb5a58..a5db02a3 100644
--- a/lib/Service/Social/MastodonProvider.php
+++ b/lib/Service/Social/MastodonProvider.php
@@ -62,7 +62,6 @@ class MastodonProvider implements ISocialProvider {
public function getImageUrls(array $contact):array {
$profileIds = $this->getProfileIds($contact);
$urls = [];
-
foreach ($profileIds as $profileId) {
$url = $this->getImageUrl($profileId);
if (isset($url)) {
@@ -82,21 +81,15 @@ class MastodonProvider implements ISocialProvider {
public function getImageUrl(string $profileUrl):?string {
try {
$result = $this->httpClient->get($profileUrl);
-
- $htmlResult = new \DOMDocument();
- $htmlResult->loadHTML($result->getBody());
- $img = $htmlResult->getElementById('profile_page_avatar');
- if (!is_null($img)) {
- return $img->getAttribute('data-original');
- }
- return null;
+ $jsonResult = json_decode($result->getBody());
+ return $jsonResult->avatar;
} catch (\Exception $e) {
return null;
}
}
/**
- * Returns all possible profile ids for contact
+ * Returns all possible profile URI for contact by searching the mastodon instance
*
* @param {array} contact information
*
@@ -108,9 +101,21 @@ class MastodonProvider implements ISocialProvider {
if (isset($socialprofiles)) {
foreach ($socialprofiles as $profile) {
if (strtolower($profile['type']) == $this->name) {
- $profileId = $this->cleanupId($profile['value']);
- if (isset($profileId)) {
- $profileIds[] = $profileId;
+ $masto_user_server = $this->cleanupId($profile['value']);
+ 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;
+ $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;
+ } catch (\Exception $e) {
+ continue;
+ }
}
}
}
@@ -123,18 +128,25 @@ class MastodonProvider implements ISocialProvider {
*
* @param {string} the value from the contact's x-socialprofile
*
- * @return string
+ * @return array username and server instance
*/
- protected function cleanupId(string $candidate):?string {
+ protected function cleanupId(string $candidate):?array {
$candidate = preg_replace('/^' . preg_quote('x-apple:', '/') . '/', '', $candidate);
try {
+ $user_server = explode('@', $candidate);
if (strpos($candidate, 'http') !== 0) {
- $user_server = explode('@', $candidate);
- $candidate = 'https://' . array_pop($user_server) . '/@' . array_pop($user_server);
+ $masto_server = "https://" . array_pop($user_server);
+ $masto_user = array_pop($user_server);
+ } else {
+ $masto_user = array_pop($user_server);
+ $masto_server = array_pop($user_server);
+ }
+ if ((empty($masto_server)) || (empty($masto_user))) {
+ return null;
}
+ return array($masto_user, $masto_server);
} catch (\Exception $e) {
- $candidate = null;
+ return null;
}
- return $candidate;
}
}