From 945f482ba3c03603ab9dd702583501c8e57053fe Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 10 Jun 2021 19:10:33 +0000 Subject: [PATCH 1/9] API: Some more apps are now supported --- doc/API-Mastodon.md | 10 +++++----- src/Object/Api/Mastodon/Conversation.php | 2 +- src/Object/Api/Mastodon/Instance.php | 9 ++++++++- src/Object/Api/Mastodon/Mention.php | 4 +++- src/Object/Api/Mastodon/Relationship.php | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index d453883b0e..d76b751a0f 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -17,6 +17,7 @@ Authentication is the same as described in [Using the APIs](help/api#Authenticat - [AndStatus](http://andstatus.org) - [B4X for Pleroma & Mastodon](https://github.com/AnywhereSoftware/B4X-Pleroma) +- [Fedi](https://play.google.com/store/apps/details?id=com.fediverse.app) - [Husky](https://husky.fwgs.ru) - [Roma](https://play.google.com/store/apps/details?id=tech.bigfig.roma) - [Subway Tooter](https://github.com/tateisu/SubwayTooter) @@ -28,27 +29,26 @@ Authentication is the same as described in [Using the APIs](help/api#Authenticat #### iOS - [Amaroq](https://github.com/ReticentJohn/Amaroq/tree/master) - [B4X for Pleroma & Mastodon](https://github.com/AnywhereSoftware/B4X-Pleroma) +- [Fedi](https://apps.apple.com/de/app/fedi-for-pleroma-and-mastodon/id1478806281) +- [Roma](https://apps.apple.com/de/app/roma-for-pleroma-and-mastodon/id1445328699) - [StarPterano](https://apps.apple.com/de/app/starpterano/id1436972796) Uses an OAuth method where you have to manually copy and paste the provided code. - [Stella](https://apps.apple.com/us/app/stella-for-mastodon-twitter/id921372048?l=ms) +- [Tootle](https://apps.apple.com/de/app/tootle-for-mastodon/id1236013466) entered hostname must match in upper/lower case. Currently crashes on "Status" type notifications. ### Unsupported apps #### Android -- [Fedi](https://play.google.com/store/apps/details?id=com.fediverse.app) Authenticates, but doesn't load posts and throws several errors - [Fedilab](https://framagit.org/tom79/fedilab) Automatically uses the legacy API, see issue: https://framagit.org/tom79/fedilab/-/issues/520 - [Mammut](https://github.com/jamiesanson/Mammut) There are problems with the token request, see issue https://github.com/jamiesanson/Mammut/issues/19 #### iOS -- [Fedi](https://apps.apple.com/de/app/fedi-for-pleroma-and-mastodon/id1478806281) Authenticates, but doesn't load posts and throws several errors - [Mast](https://github.com/Beesitech/Mast) Doesn't accept the entered instance name. Claims that it is invalid (Message is: "Not a valid instance (may be closed or dead)") -- [Mastonaut](https://mastonaut.app/) -- [Roma](https://apps.apple.com/de/app/roma-for-pleroma-and-mastodon/id1445328699) Authenticates, but doesn't load content - [Toot!](https://apps.apple.com/app/toot/id1229021451) -- [Tootle](https://apps.apple.com/de/app/tootle-for-mastodon/id1236013466) Doesn't recognize server, loads /api/v1/instance in some endless loop #### Other - [Halycon](https://www.halcyon.social/) Doesn't load content, creates masses of HTTP requests +- [Mastonaut](https://mastonaut.app/) - [Pinafore](https://github.com/nolanlawson/pinafore) Returns message "Error: NetworkError when attempting to fetch resource.. Is this a valid Mastodon instance?" ## Entities diff --git a/src/Object/Api/Mastodon/Conversation.php b/src/Object/Api/Mastodon/Conversation.php index 0b0136044f..e0950e555e 100644 --- a/src/Object/Api/Mastodon/Conversation.php +++ b/src/Object/Api/Mastodon/Conversation.php @@ -46,7 +46,7 @@ class Conversation extends BaseDataTransferObject public function __construct(string $id, array $accounts, bool $unread, \Friendica\Object\Api\Mastodon\Status $last_status) { - $this->id = $id; + $this->id = (string)$id; $this->accounts = $accounts; $this->unread = $unread; $this->last_status = $last_status; diff --git a/src/Object/Api/Mastodon/Instance.php b/src/Object/Api/Mastodon/Instance.php index 031e5bcb06..82f30390d7 100644 --- a/src/Object/Api/Mastodon/Instance.php +++ b/src/Object/Api/Mastodon/Instance.php @@ -39,6 +39,8 @@ class Instance extends BaseDataTransferObject /** @var string */ protected $title; /** @var string */ + protected $short_description; + /** @var string */ protected $description; /** @var string */ protected $email; @@ -58,8 +60,12 @@ class Instance extends BaseDataTransferObject protected $registrations; /** @var bool */ protected $approval_required; + /** @var bool */ + protected $invites_enabled; /** @var Account|null */ protected $contact_account = null; + /** @var array */ + protected $rules = []; /** * Creates an instance record @@ -77,7 +83,7 @@ class Instance extends BaseDataTransferObject $instance = new Instance(); $instance->uri = $baseUrl->get(); $instance->title = DI::config()->get('config', 'sitename'); - $instance->description = DI::config()->get('config', 'info'); + $instance->short_description = $instance->description = DI::config()->get('config', 'info'); $instance->email = DI::config()->get('config', 'admin_email'); $instance->version = FRIENDICA_VERSION; $instance->urls = null; // Not supported @@ -87,6 +93,7 @@ class Instance extends BaseDataTransferObject $instance->max_toot_chars = (int)DI::config()->get('config', 'api_import_size', DI::config()->get('config', 'max_import_size')); $instance->registrations = ($register_policy != Register::CLOSED); $instance->approval_required = ($register_policy == Register::APPROVE); + $instance->invites_enabled = false; $instance->contact_account = []; if (!empty(DI::config()->get('config', 'admin_email'))) { diff --git a/src/Object/Api/Mastodon/Mention.php b/src/Object/Api/Mastodon/Mention.php index d64fe6b5fc..3fd2507c4a 100644 --- a/src/Object/Api/Mastodon/Mention.php +++ b/src/Object/Api/Mastodon/Mention.php @@ -50,7 +50,7 @@ class Mention extends BaseDataTransferObject */ public function __construct(BaseURL $baseUrl, array $tag, array $contact) { - $this->id = $contact['id'] ?? 0; + $this->id = (string)($contact['id'] ?? 0); $this->username = $tag['name']; $this->url = $tag['url']; @@ -59,6 +59,8 @@ class Mention extends BaseDataTransferObject strpos($contact['url'], $baseUrl->get() . '/') === 0 ? $contact['nick'] : $contact['addr']; + + $this->username = $contact['nick']; } else { $this->acct = ''; } diff --git a/src/Object/Api/Mastodon/Relationship.php b/src/Object/Api/Mastodon/Relationship.php index 870acb5446..5d7169b893 100644 --- a/src/Object/Api/Mastodon/Relationship.php +++ b/src/Object/Api/Mastodon/Relationship.php @@ -79,7 +79,7 @@ class Relationship extends BaseDataTransferObject */ public function __construct(int $contactId, array $contactRecord = [], bool $blocked = false, bool $muted = false) { - $this->id = $contactId; + $this->id = (string)$contactId; $this->following = false; $this->requested = false; $this->endorsed = false; From 4ea30af752fe0fbb025593c95f30786482dce533 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 10 Jun 2021 19:55:43 +0000 Subject: [PATCH 2/9] Videos are now working as well --- src/Factory/Api/Mastodon/Attachment.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Factory/Api/Mastodon/Attachment.php b/src/Factory/Api/Mastodon/Attachment.php index 1345da9e85..f7c0328f94 100644 --- a/src/Factory/Api/Mastodon/Attachment.php +++ b/src/Factory/Api/Mastodon/Attachment.php @@ -86,8 +86,8 @@ class Attachment extends BaseFactory $preview = Proxy::proxifyUrl($attachment['url'], false, Proxy::SIZE_SMALL); } } else { - $url = ''; - $preview = ''; + $url = $attachment['url']; + $preview = $attachment['preview'] ?? ''; } $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote); From 84028d7342431d134df5f5c4de4e47107abadc6d Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 11 Jun 2021 03:51:11 +0000 Subject: [PATCH 3/9] Proxify the media url --- src/Factory/Api/Mastodon/Attachment.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Factory/Api/Mastodon/Attachment.php b/src/Factory/Api/Mastodon/Attachment.php index f7c0328f94..f039c1a233 100644 --- a/src/Factory/Api/Mastodon/Attachment.php +++ b/src/Factory/Api/Mastodon/Attachment.php @@ -86,8 +86,8 @@ class Attachment extends BaseFactory $preview = Proxy::proxifyUrl($attachment['url'], false, Proxy::SIZE_SMALL); } } else { - $url = $attachment['url']; - $preview = $attachment['preview'] ?? ''; + $url = Proxy::proxifyUrl($attachment['url']); + $preview = Proxy::proxifyUrl($attachment['preview'] ?? ''); } $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote); From e9832e3729054f94c98127d79326748c6d184215 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 11 Jun 2021 03:56:06 +0000 Subject: [PATCH 4/9] Avoid notice "Undefined index: uid" --- include/api.php | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/include/api.php b/include/api.php index 421086002a..df92e9960b 100644 --- a/include/api.php +++ b/include/api.php @@ -56,6 +56,7 @@ use Friendica\Network\HTTPException\UnauthorizedException; use Friendica\Object\Image; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; +use Friendica\Security\BasicAuth; use Friendica\Security\FKOAuth1; use Friendica\Security\OAuth; use Friendica\Security\OAuth1\OAuthRequest; @@ -94,8 +95,9 @@ function api_user() return $user; } - if (!empty($_SESSION['allow_api'])) { - return local_user(); + $user = BasicAuth::getCurrentUserID(false); + if (!empty($user)) { + return $user; } return false; @@ -115,22 +117,11 @@ function api_user() */ function api_source() { - if (requestdata('source')) { - return requestdata('source'); + $application = OAuth::getCurrentApplicationToken(); + if (empty($application)) { + $application = BasicAuth::getCurrentApplicationToken(); } - - // Support for known clients that doesn't send a source name - if (!empty($_SERVER['HTTP_USER_AGENT'])) { - if(strpos($_SERVER['HTTP_USER_AGENT'], "Twidere") !== false) { - return "Twidere"; - } - - Logger::info(API_LOG_PREFIX . 'Unrecognized user-agent', ['module' => 'api', 'action' => 'source', 'http_user_agent' => $_SERVER['HTTP_USER_AGENT']]); - } else { - Logger::info(API_LOG_PREFIX . 'Empty user-agent', ['module' => 'api', 'action' => 'source']); - } - - return "api"; + return $application['name'] ?? 'api'; } /** @@ -181,7 +172,6 @@ function api_register_func($path, $func, $auth = false, $method = API_METHOD_ANY * Simple Auth allow username in form of
user@server
, ignoring server part * * @param App $a App - * @param bool $do_login try to log in when not logged in, otherwise quit silently * @throws ForbiddenException * @throws InternalServerErrorException * @throws UnauthorizedException @@ -192,7 +182,7 @@ function api_register_func($path, $func, $auth = false, $method = API_METHOD_ANY * 'authenticated' => return status, * 'user_record' => return authenticated user record */ -function api_login(App $a, bool $do_login = true) +function api_login(App $a) { $_SESSION["allow_api"] = false; @@ -225,10 +215,6 @@ function api_login(App $a, bool $do_login = true) Logger::warning(API_LOG_PREFIX . 'OAuth error', ['module' => 'api', 'action' => 'login', 'exception' => $e->getMessage()]); } - if (!$do_login) { - return; - } - Logger::debug(API_LOG_PREFIX . 'failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]); header('WWW-Authenticate: Basic realm="Friendica"'); throw new UnauthorizedException("This API requires login"); @@ -270,9 +256,6 @@ function api_login(App $a, bool $do_login = true) } if (!DBA::isResult($record)) { - if (!$do_login) { - return; - } Logger::debug(API_LOG_PREFIX . 'failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]); header('WWW-Authenticate: Basic realm="Friendica"'); //header('HTTP/1.0 401 Unauthorized'); @@ -608,7 +591,7 @@ function api_get_user(App $a, $contact_id = null) api_login($a); return false; } else { - $user = $_SESSION['uid']; + $user = api_user(); $extra_query = "AND `contact`.`uid` = %d AND `contact`.`self` "; } } From e492650d1b47ddc6c4c8251ca4e57fc6f8438577 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 11 Jun 2021 04:26:24 +0000 Subject: [PATCH 5/9] Added whalebird as working client --- doc/API-Mastodon.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index d76b751a0f..975fc786c9 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -35,6 +35,9 @@ Authentication is the same as described in [Using the APIs](help/api#Authenticat - [Stella](https://apps.apple.com/us/app/stella-for-mastodon-twitter/id921372048?l=ms) - [Tootle](https://apps.apple.com/de/app/tootle-for-mastodon/id1236013466) entered hostname must match in upper/lower case. Currently crashes on "Status" type notifications. +#### Desktop +- [Whalebird](https://whalebird.social) + ### Unsupported apps #### Android From 0ecef260644058728988c035cc9a8c66efa7cd17 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 11 Jun 2021 06:02:56 +0000 Subject: [PATCH 6/9] Making the tests work --- include/api.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/api.php b/include/api.php index df92e9960b..42b25a6c1b 100644 --- a/include/api.php +++ b/include/api.php @@ -100,6 +100,10 @@ function api_user() return $user; } + if (!empty($_SESSION['allow_api'])) { + return local_user(); + } + return false; } From 5960582dcf8e7584a526b6f86c1d36171f580fd8 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 11 Jun 2021 06:05:42 +0000 Subject: [PATCH 7/9] Tests ... --- include/api.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/include/api.php b/include/api.php index 42b25a6c1b..92230cc611 100644 --- a/include/api.php +++ b/include/api.php @@ -95,11 +95,6 @@ function api_user() return $user; } - $user = BasicAuth::getCurrentUserID(false); - if (!empty($user)) { - return $user; - } - if (!empty($_SESSION['allow_api'])) { return local_user(); } From 246f3c5a8fd477fbe94068b884166ff55069a442 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 11 Jun 2021 06:09:08 +0000 Subject: [PATCH 8/9] Making the tests happy --- include/api.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/include/api.php b/include/api.php index 92230cc611..0ebd9b7514 100644 --- a/include/api.php +++ b/include/api.php @@ -116,11 +116,22 @@ function api_user() */ function api_source() { - $application = OAuth::getCurrentApplicationToken(); - if (empty($application)) { - $application = BasicAuth::getCurrentApplicationToken(); + if (requestdata('source')) { + return requestdata('source'); } - return $application['name'] ?? 'api'; + + // Support for known clients that doesn't send a source name + if (!empty($_SERVER['HTTP_USER_AGENT'])) { + if(strpos($_SERVER['HTTP_USER_AGENT'], "Twidere") !== false) { + return "Twidere"; + } + + Logger::info(API_LOG_PREFIX . 'Unrecognized user-agent', ['module' => 'api', 'action' => 'source', 'http_user_agent' => $_SERVER['HTTP_USER_AGENT']]); + } else { + Logger::info(API_LOG_PREFIX . 'Empty user-agent', ['module' => 'api', 'action' => 'source']); + } + + return "api"; } /** From 4ea92d0afcdd6e8e6d18ef709964953994ce1647 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 11 Jun 2021 06:11:37 +0000 Subject: [PATCH 9/9] Removed unneeded use --- include/api.php | 1 - 1 file changed, 1 deletion(-) diff --git a/include/api.php b/include/api.php index 0ebd9b7514..7da32f36dd 100644 --- a/include/api.php +++ b/include/api.php @@ -56,7 +56,6 @@ use Friendica\Network\HTTPException\UnauthorizedException; use Friendica\Object\Image; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; -use Friendica\Security\BasicAuth; use Friendica\Security\FKOAuth1; use Friendica\Security\OAuth; use Friendica\Security\OAuth1\OAuthRequest;