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 58aa949
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
5 changes: 3 additions & 2 deletions css/icons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
@include icon-black-white('language', 'contacts', 2);
@include icon-black-white('clone', 'contacts', 2);
@include icon-black-white('sync', 'contacts', 2);

// social network icons:
@include icon-black-white('Facebook', 'contacts', 2);
@include icon-black-white('Instagram', 'contacts', 2);
@include icon-black-white('Twitter', 'contacts', 2);
@include icon-black-white('Mastodon', 'contacts', 2);
@include icon-black-white('Tumblr', 'contacts', 2);
@include icon-black-white('Twitter', 'contacts', 2);

.icon-up-force-white {
// using #fffffe to trick the accessibility dark theme icon invert
Expand Down
3 changes: 3 additions & 0 deletions img/Mastodon.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 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 58aa949

Please sign in to comment.