Skip to content

Commit

Permalink
Merge pull request #1906 from eleith/support-gravatar
Browse files Browse the repository at this point in the history
add support for downloading avatars from gravatar
  • Loading branch information
eleith authored Nov 5, 2020
2 parents 0466dd9 + ffddb70 commit 9226cb1
Show file tree
Hide file tree
Showing 24 changed files with 1,890 additions and 189 deletions.
1 change: 1 addition & 0 deletions css/icons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
@include icon-black-white('twitter', 'contacts', 2); // “twitter (fab)” by fontawesome.com is licensed under CC BY 4.0. (https://fontawesome.com/icons/twitter?style=brands)
@include icon-black-white('diaspora', 'contacts', 2); // “diaspora (fab)” by fontawesome.com is licensed under CC BY 4.0. (https://fontawesome.com/icons/diaspora?style=brands)
@include icon-black-white('xing', 'contacts', 2); // “xing (fab)” by fontawesome.com is licensed under CC BY 4.0. (https://fontawesome.com/icons/xing?style=brands)
@include icon-black-white('gravatar', 'contacts', 2); // “gravatar (fab)” by svgrepo.com is licensed under public domain CCO 1.0. (https://www.svgrepo.com/page/licensing)

.icon-up-force-white {
// using #fffffe to trick the accessibility dark theme icon invert
Expand Down
1 change: 1 addition & 0 deletions img/gravatar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions img/license.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
* “twitter (fab)” by fontawesome.com is licensed under CC BY 4.0. (https://fontawesome.com/icons/twitter?style=brands)
* “diaspora (fab)” by fontawesome.com is licensed under CC BY 4.0. (https://fontawesome.com/icons/diaspora?style=brands)
* “xing (fab)” by fontawesome.com is licensed under CC BY 4.0. (https://fontawesome.com/icons/xing?style=brands)
* “gravatar (fab)” by svgrepo.com is licensed under public domain CCO 1.0. (https://www.svgrepo.com/page/licensing)

56 changes: 23 additions & 33 deletions lib/Service/Social/CompositeSocialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ public function __construct(InstagramProvider $instagramProvider,
TwitterProvider $twitterProvider,
TumblrProvider $tumblrProvider,
DiasporaProvider $diasporaProvider,
XingProvider $xingProvider) {
XingProvider $xingProvider,
GravatarProvider $gravatarProvider) {

// This determines the priority of known providers
$this->providers = [
'instagram' => $instagramProvider,
'mastodon' => $mastodonProvider,
'twitter' => $twitterProvider,
'facebook' => $facebookProvider,
'tumblr' => $tumblrProvider,
'diaspora' => $diasporaProvider,
'xing' => $xingProvider,
$instagramProvider->name => $instagramProvider,
$mastodonProvider->name => $mastodonProvider,
$twitterProvider->name => $twitterProvider,
$facebookProvider->name => $facebookProvider,
$tumblrProvider->name => $tumblrProvider,
$diasporaProvider->name => $diasporaProvider,
$xingProvider->name => $xingProvider,
$gravatarProvider->name => $gravatarProvider
];
}

Expand All @@ -60,40 +62,28 @@ public function getSupportedNetworks() : array {
return array_keys($this->providers);
}


/**
* generate download url for a social entry
*
* @param array socialEntries all social data from the contact
* @param String network the choice which network to use (fallback: take first available)
* @param String network the choice which network to use
*
* @returns String the url to the requested information or null in case of errors
* @return ISocialProvider if provider of 'network' is found, otherwise null
*/
public function getSocialConnector(array $socialEntries, string $network) : ?string {
public function getSocialConnector(string $network) : ?ISocialProvider {
$connector = null;
$selection = $this->providers;
// check if dedicated network selected
if (isset($this->providers[$network])) {
$selection = [$network => $this->providers[$network]];
$connector = $this->providers[$network];
}
return $connector;
}

// check selected providers in order
foreach ($selection as $type => $socialProvider) {

// search for this network in user's profile
foreach ($socialEntries as $socialEntry) {
if (strtolower($type) === strtolower($socialEntry['type'])) {
$profileId = $socialProvider->cleanupId($socialEntry['value']);
if (!is_null($profileId)) {
$connector = $socialProvider->getImageUrl($profileId);
}
break;
}
}
if ($connector) {
break;
}
}
return ($connector);
/**
* generate download url for a social entry
*
* @return ISocialProvider[] all social providers
*/
public function getSocialConnectors() : array {
return array_values($this->providers);
}
}
90 changes: 75 additions & 15 deletions lib/Service/Social/DiasporaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,48 @@ class DiasporaProvider implements ISocialProvider {
/** @var IClientService */
private $httpClient;

/** @var boolean */
/** @var bool */
private $looping;

/** @var string */
public $name = "diaspora";

public function __construct(IClientService $httpClient) {
$this->httpClient = $httpClient->NewClient();
$this->looping = false;
}

/**
* Returns the profile-id
* Returns if this provider supports this contact
*
* @param {string} the value from the contact's x-socialprofile
* @param {array} contact info
*
* @return string
* @return bool
*/
public function cleanupId(string $candidate):string {
try {
if (strpos($candidate, 'http') !== 0) {
$user_server = explode('@', $candidate);
$candidate = 'https://' . array_pop($user_server) . '/public/' . array_pop($user_server) . '.atom';
public function supportsContact(array $contact):bool {
$socialprofiles = $this->getProfileIds($contact);
return isset($socialprofiles) && count($socialprofiles) > 0;
}

/**
* Returns all possible profile-picture urls
*
* @param {array} contact information
*
* @return array
*/
public function getImageUrls(array $contact):array {
$profileIds = $this->getProfileIds($contact);
$urls = [];

foreach ($profileIds as $profileId) {
$url = $this->getImageUrl($profileId);
if (isset($url)) {
$urls[] = $url;
}
} catch (Exception $e) {
$candidate = null;
}
return $candidate;

return $urls;
}

/**
Expand All @@ -64,7 +81,7 @@ public function cleanupId(string $candidate):string {
*
* @return string|null
*/
public function getImageUrl(string $profileUrl):?string {
protected function getImageUrl(string $profileUrl):?string {
try {
$result = $this->httpClient->get($profileUrl);
$htmlResult = $result->getBody();
Expand All @@ -82,8 +99,51 @@ public function getImageUrl(string $profileUrl):?string {
}
}
return null;
} catch (Exception $e) {
} catch (\Exception $e) {
return null;
}
}

/**
* Returns all possible profile ids for contact
*
* @param {array} contact information
*
* @return array
*/
protected function getProfileIds($contact):array {
$socialprofiles = $contact['X-SOCIALPROFILE'];
$profileIds = [];

if (isset($socialprofiles)) {
foreach ($socialprofiles as $profile) {
if (strtolower($profile['type']) == $this->name) {
$profileId = $this->cleanupId($profile['value']);
if (isset($profileId)) {
$profileIds[] = $profileId;
}
}
}
}
return $profileIds;
}

/**
* Returns the profile-id
*
* @param {string} the value from the contact's x-socialprofile
*
* @return string
*/
protected function cleanupId(string $candidate):?string {
try {
if (strpos($candidate, 'http') !== 0) {
$user_server = explode('@', $candidate);
$candidate = 'https://' . array_pop($user_server) . '/public/' . array_pop($user_server) . '.atom';
}
} catch (\Exception $e) {
$candidate = null;
}
return $candidate;
}
}
74 changes: 65 additions & 9 deletions lib/Service/Social/FacebookProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,51 @@ class FacebookProvider implements ISocialProvider {
/** @var IClientService */
private $httpClient;

/** @var string */
public $name = "facebook";

public function __construct(IClientService $httpClient) {
$this->httpClient = $httpClient->NewClient();
}


/**
* Returns if this provider supports this contact
*
* @param {array} contact info
*
* @return bool
*/
public function supportsContact(array $contact):bool {
$socialprofiles = $this->getProfiles($contact);
return isset($socialprofiles) && count($socialprofiles) > 0;
}

/**
* Returns the profile-picture url
*
* @param {array} contact information
*
* @return array
*/
public function getImageUrls(array $contact):array {
$profileIds = $this->getProfileIds($contact);
$urls = [];
foreach ($profileIds as $profileId) {
$recipe = 'https://graph.facebook.com/{socialId}/picture?width=720';
$connector = str_replace("{socialId}", $profileId, $recipe);
$urls[] = $connector;
}
return $urls;
}

/**
* Returns the profile-id
*
* @param {string} the value from the contact's x-socialprofile
*
* @return string
*/
public function cleanupId(string $candidate):string {
protected function cleanupId(string $candidate):string {
$candidate = basename($candidate);
if (!is_numeric($candidate)) {
$candidate = $this->findFacebookId($candidate);
Expand All @@ -50,16 +83,39 @@ public function cleanupId(string $candidate):string {
}

/**
* Returns the profile-picture url
* Returns all possible profile ids for contact
*
* @param {string} profileId the profile-id
* @param {array} contact information
*
* @return string
* @return array of string profile ids
*/
public function getImageUrl(string $profileId):string {
$recipe = 'https://graph.facebook.com/{socialId}/picture?width=720';
$connector = str_replace("{socialId}", $profileId, $recipe);
return $connector;
protected function getProfiles(array $contact):array {
$socialprofiles = $contact['X-SOCIALPROFILE'];
$profiles = [];
if (isset($socialprofiles)) {
foreach ($socialprofiles as $profile) {
if (strtolower($profile['type']) == $this->name) {
$profiles[] = $profile['value'];
}
}
}
return $profiles;
}

/**
* Returns all possible profile ids for contact
*
* @param {array} contact information
*
* @return array of string profile ids
*/
protected function getProfileIds(array $contact):array {
$profiles = $this->getProfiles($contact);
$profileIds = [];
foreach ($profiles as $profile) {
$profileIds[] = $this->cleanupId($profile);
}
return $profileIds;
}

/**
Expand Down
65 changes: 65 additions & 0 deletions lib/Service/Social/GravatarProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @author leith <online-nextcloud@eleith.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Contacts\Service\Social;

class GravatarProvider implements ISocialProvider {
/** @var string */
public $name = "gravatar";

public function __construct() {
}

/**
* Returns if this provider supports this contact
*
* @param {array} contact info
*
* @return bool
*/
public function supportsContact(array $contact):bool {
$emails = $contact['EMAIL'];
return isset($emails) && count($emails);
}

/**
* Returns the profile-picture url
*
* @param {array} contact information
*
* @return array
*/
public function getImageUrls(array $contact):array {
$urls = [];
$emails = $contact['EMAIL'];
if (isset($emails)) {
foreach ($emails as $email) {
$hash = md5(strtolower(trim($email['value'])));
$recipe = 'https://www.gravatar.com/avatar/{hash}?s=720&d=404';
$connector = str_replace("{hash}", $hash, $recipe);
$urls[] = $connector;
}
}
return $urls;
}
}
Loading

0 comments on commit 9226cb1

Please sign in to comment.