Skip to content

Commit

Permalink
added Mastodon
Browse files Browse the repository at this point in the history
  • Loading branch information
call-me-matt committed May 17, 2020
1 parent c10dc11 commit 0527ede
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Service/Social/CompositeSocialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ class CompositeSocialProvider {
private $providers;

public function __construct(InstagramProvider $instagramProvider,
MastodonProvider $mastodonProvider,
FacebookProvider $facebookProvider,
TwitterProvider $twitterProvider,
TumblrProvider $tumblrProvider) {

// This determines the priority of known providers
$this->providers = [
'instagram' => $instagramProvider,
'mastodon' => $mastodonProvider,
'twitter' => $twitterProvider,
'facebook' => $facebookProvider,
'tumblr' => $tumblrProvider,
Expand Down
95 changes: 95 additions & 0 deletions lib/Service/Social/MastodonProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @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 MastodonProvider implements ISocialProvider {

public function __construct() {
}

/**
* Returns the profile-id
*
* @param {string} the value from the contact's x-socialprofile
*
* @return string
*/
public function cleanupId(string $candidate):?string {
return $candidate;
}

/**
* Returns the profile-picture url
*
* @param {string} profileId the profile-id
*
* @return string|null
*/
public function getImageUrl(string $profileId):?string {
try {
if (strpos($profileId, '@') === 0) {
$profile_parts = explode ('@', $profileId);
$profileId = 'https://' . $profile_parts[2] . '/@' . $profile_parts[1];
}
$connector = $this->getFromHtml($profileId);
}
catch (Exception $e) {
$connector = null;
}
return $connector;

}

/**
* extracts desired value from an html page
*
* @param {string} url the target from where to fetch the content
* @param {String} the desired catchword to filter for
*
* @returns {String} the extracted value (first match) or null if not present
*/
protected function getFromHtml(string $url) : ?string {
try {
$opts = [
"http" => [
"method" => "GET",
"header" => "User-Agent: Nextcloud Contacts App",
]
];
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);

$htmlResult = new \DOMDocument();
$htmlResult->loadHTML($result);
$img = $htmlResult->getElementById('profile_page_avatar');
if (!is_null($img)) {
return $img->getAttribute("data-original");
}
return null;
}
catch (Exception $e) {
return null;
}
}
}

0 comments on commit 0527ede

Please sign in to comment.