diff options
author | eleith <online+github@eleith.com> | 2020-11-01 03:32:54 +0000 |
---|---|---|
committer | leith abdulla <online-nextcloud@eleith.com> | 2020-11-01 16:20:49 +0000 |
commit | 296ebf7c7cbe44d297625e29c681b3a8ffb3dc3c (patch) | |
tree | 8b007f9a0d7874a97597b534e979caf4e159e27c /lib/Service/SocialApiService.php | |
parent | 247b727c440669836c18c765f1f7cbf1f55feced (diff) |
add support for downloading avatars from gravatar
adding support for gravatar to the current social providers/services.
because gravatar relies on email instead of the x-socialprofile carddav
field, this requires re-working the current abstraction for how social
providers work.
also in this change is improved support for when a contact card has
multiple fields of the same type (as might be common with email) thus
they all can be checked for profile photos if one of them doesn't result
in a valid photo
Signed-off-by: leith abdulla <online-nextcloud@eleith.com>
Diffstat (limited to 'lib/Service/SocialApiService.php')
-rw-r--r-- | lib/Service/SocialApiService.php | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/lib/Service/SocialApiService.php b/lib/Service/SocialApiService.php index 03484c99..af07c998 100644 --- a/lib/Service/SocialApiService.php +++ b/lib/Service/SocialApiService.php @@ -50,9 +50,9 @@ class SocialApiService { private $config; /** @var IClientService */ private $clientService; - /** @var IL10N */ + /** @var IL10N */ private $l10n; - /** @var IURLGenerator */ + /** @var IURLGenerator */ private $urlGen; /** @var CardDavBackend */ private $davBackend; @@ -84,7 +84,7 @@ class SocialApiService { /** * returns an array of supported social networks * - * @returns {array} array of the supported social networks + * @return {array} array of the supported social networks */ public function getSupportedNetworks() : array { $syncAllowedByAdmin = $this->config->getAppValue($this->appName, 'allowSocialSync', 'yes'); @@ -168,7 +168,10 @@ class SocialApiService { * @returns {JSONResponse} an empty JSONResponse with respective http status code */ public function updateContact(string $addressbookId, string $contactId, string $network) : JSONResponse { - $url = null; + $socialdata = null; + $imageType = null; + $urls = array(); + $allConnectors = $this->socialProvider->getSocialConnectors(); try { // get corresponding addressbook @@ -179,20 +182,42 @@ class SocialApiService { // search contact in that addressbook, get social data $contact = $addressBook->search($contactId, ['UID'], ['types' => true])[0]; - if (!isset($contact['X-SOCIALPROFILE'])) { + + if (!isset($contact)) { + return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); + } + + if ($network) { + $allConnectors = [$this->socialProvider->getSocialConnector($network)]; + } + + $connectors = array_filter($allConnectors, function($connector) use($contact) { + return $connector->supportsContact($contact); + }); + + if (count($connectors) == 0) { return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); } - $socialprofiles = $contact['X-SOCIALPROFILE']; - // retrieve data - $url = $this->socialProvider->getSocialConnector($socialprofiles, $network); - if (empty($url)) { + foreach($connectors as $connector) { + $urls = array_merge($connector->getImageUrls($contact), $urls); + } + + if (count($urls) == 0) { return new JSONResponse([], Http::STATUS_BAD_REQUEST); } - $httpResult = $this->clientService->NewClient()->get($url); - $socialdata = $httpResult->getBody(); - $imageType = $httpResult->getHeader('content-type'); + foreach($urls as $url) { + try { + $httpResult = $this->clientService->NewClient()->get($url); + $socialdata = $httpResult->getBody(); + $imageType = $httpResult->getHeader('content-type'); + if (isset($socialdata) && isset($imageType)) { + break; + } + } catch(\Exception $e) { + } + } if (!$socialdata || $imageType === null) { return new JSONResponse([], Http::STATUS_NOT_FOUND); |