From 0de8e4db080b5739d77d6394eb5c2904e5d1b66f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 27 Jan 2020 20:01:32 -0500 Subject: [PATCH 1/4] Move Mastodon API entities to src/Object --- src/{Api => }/BaseEntity.php | 2 +- src/Factory/Mastodon/Account.php | 4 ++-- src/Factory/Mastodon/FollowRequest.php | 4 ++-- src/Factory/Mastodon/Relationship.php | 2 +- src/Module/Api/Mastodon/FollowRequests.php | 4 ++-- src/Module/Api/Mastodon/Instance.php | 2 +- src/{Api/Entity => Object/Api}/Mastodon/Account.php | 4 ++-- src/{Api/Entity => Object/Api}/Mastodon/Emoji.php | 8 ++++---- src/{Api/Entity => Object/Api}/Mastodon/Field.php | 4 ++-- src/{Api/Entity => Object/Api}/Mastodon/FollowRequest.php | 3 +-- src/{Api/Entity => Object/Api}/Mastodon/Instance.php | 4 ++-- src/{Api/Entity => Object/Api}/Mastodon/Relationship.php | 4 ++-- src/{Api/Entity => Object/Api}/Mastodon/Stats.php | 4 ++-- 13 files changed, 24 insertions(+), 25 deletions(-) rename src/{Api => }/BaseEntity.php (95%) rename src/{Api/Entity => Object/Api}/Mastodon/Account.php (98%) rename src/{Api/Entity => Object/Api}/Mastodon/Emoji.php (68%) rename src/{Api/Entity => Object/Api}/Mastodon/Field.php (78%) rename src/{Api/Entity => Object/Api}/Mastodon/FollowRequest.php (93%) rename src/{Api/Entity => Object/Api}/Mastodon/Instance.php (97%) rename src/{Api/Entity => Object/Api}/Mastodon/Relationship.php (95%) rename src/{Api/Entity => Object/Api}/Mastodon/Stats.php (93%) diff --git a/src/Api/BaseEntity.php b/src/BaseEntity.php similarity index 95% rename from src/Api/BaseEntity.php rename to src/BaseEntity.php index 4bc15943d..9f0cb31f8 100644 --- a/src/Api/BaseEntity.php +++ b/src/BaseEntity.php @@ -1,6 +1,6 @@ baseUrl, $publicContact, $apcontact, $userContact); + return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $apcontact, $userContact); } } diff --git a/src/Factory/Mastodon/FollowRequest.php b/src/Factory/Mastodon/FollowRequest.php index bbaa3135c..3aabe413d 100644 --- a/src/Factory/Mastodon/FollowRequest.php +++ b/src/Factory/Mastodon/FollowRequest.php @@ -24,7 +24,7 @@ class FollowRequest extends BaseFactory /** * @param Introduction $introduction - * @return \Friendica\Api\Entity\Mastodon\FollowRequest + * @return \Friendica\Object\Api\Mastodon\FollowRequest * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ @@ -42,6 +42,6 @@ class FollowRequest extends BaseFactory $apcontact = APContact::getByURL($publicContact['url'], false); - return new \Friendica\Api\Entity\Mastodon\FollowRequest($this->baseUrl, $introduction->id, $publicContact, $apcontact, $userContact); + return new \Friendica\Object\Api\Mastodon\FollowRequest($this->baseUrl, $introduction->id, $publicContact, $apcontact, $userContact); } } diff --git a/src/Factory/Mastodon/Relationship.php b/src/Factory/Mastodon/Relationship.php index 25c9d4c8c..7a8b216d4 100644 --- a/src/Factory/Mastodon/Relationship.php +++ b/src/Factory/Mastodon/Relationship.php @@ -2,7 +2,7 @@ namespace Friendica\Factory\Mastodon; -use Friendica\Api\Entity\Mastodon\Relationship as RelationshipEntity; +use Friendica\Object\Api\Mastodon\Relationship as RelationshipEntity; use Friendica\BaseFactory; use Friendica\Model\Contact; diff --git a/src/Module/Api/Mastodon/FollowRequests.php b/src/Module/Api/Mastodon/FollowRequests.php index fc384f797..79fe48783 100644 --- a/src/Module/Api/Mastodon/FollowRequests.php +++ b/src/Module/Api/Mastodon/FollowRequests.php @@ -2,8 +2,8 @@ namespace Friendica\Module\Api\Mastodon; -use Friendica\Api\Entity\Mastodon; -use Friendica\Api\Entity\Mastodon\Relationship; +use Friendica\Object\Api\Mastodon; +use Friendica\Object\Api\Mastodon\Relationship; use Friendica\Core\System; use Friendica\DI; use Friendica\Model\Contact; diff --git a/src/Module/Api/Mastodon/Instance.php b/src/Module/Api/Mastodon/Instance.php index cc7639f46..c3d1b93ac 100644 --- a/src/Module/Api/Mastodon/Instance.php +++ b/src/Module/Api/Mastodon/Instance.php @@ -2,7 +2,7 @@ namespace Friendica\Module\Api\Mastodon; -use Friendica\Api\Entity\Mastodon\Instance as InstanceEntity; +use Friendica\Object\Api\Mastodon\Instance as InstanceEntity; use Friendica\Core\System; use Friendica\Module\Base\Api; diff --git a/src/Api/Entity/Mastodon/Account.php b/src/Object/Api/Mastodon/Account.php similarity index 98% rename from src/Api/Entity/Mastodon/Account.php rename to src/Object/Api/Mastodon/Account.php index 8123cf595..38eefee3d 100644 --- a/src/Api/Entity/Mastodon/Account.php +++ b/src/Object/Api/Mastodon/Account.php @@ -1,9 +1,9 @@ Date: Mon, 27 Jan 2020 21:18:42 -0500 Subject: [PATCH 2/4] Move Module\Base\Api to Module\BaseApi --- src/Module/Api/Mastodon/FollowRequests.php | 7 ++----- src/Module/Api/Mastodon/Instance.php | 6 +++--- src/Module/Api/Mastodon/Instance/Peers.php | 4 ++-- src/Module/{Base/Api.php => BaseApi.php} | 6 +++--- 4 files changed, 10 insertions(+), 13 deletions(-) rename src/Module/{Base/Api.php => BaseApi.php} (95%) diff --git a/src/Module/Api/Mastodon/FollowRequests.php b/src/Module/Api/Mastodon/FollowRequests.php index 79fe48783..3b2de61bc 100644 --- a/src/Module/Api/Mastodon/FollowRequests.php +++ b/src/Module/Api/Mastodon/FollowRequests.php @@ -2,18 +2,15 @@ namespace Friendica\Module\Api\Mastodon; -use Friendica\Object\Api\Mastodon; -use Friendica\Object\Api\Mastodon\Relationship; use Friendica\Core\System; use Friendica\DI; -use Friendica\Model\Contact; -use Friendica\Module\Base\Api; +use Friendica\Module\BaseApi; use Friendica\Network\HTTPException; /** * @see https://docs.joinmastodon.org/methods/accounts/follow_requests */ -class FollowRequests extends Api +class FollowRequests extends BaseApi { public static function init(array $parameters = []) { diff --git a/src/Module/Api/Mastodon/Instance.php b/src/Module/Api/Mastodon/Instance.php index c3d1b93ac..927642887 100644 --- a/src/Module/Api/Mastodon/Instance.php +++ b/src/Module/Api/Mastodon/Instance.php @@ -2,14 +2,14 @@ namespace Friendica\Module\Api\Mastodon; -use Friendica\Object\Api\Mastodon\Instance as InstanceEntity; use Friendica\Core\System; -use Friendica\Module\Base\Api; +use Friendica\Module\BaseApi; +use Friendica\Object\Api\Mastodon\Instance as InstanceEntity; /** * @see https://docs.joinmastodon.org/api/rest/instances/ */ -class Instance extends Api +class Instance extends BaseApi { /** * @param array $parameters diff --git a/src/Module/Api/Mastodon/Instance/Peers.php b/src/Module/Api/Mastodon/Instance/Peers.php index 5eaf355cc..99c878fcb 100644 --- a/src/Module/Api/Mastodon/Instance/Peers.php +++ b/src/Module/Api/Mastodon/Instance/Peers.php @@ -5,14 +5,14 @@ namespace Friendica\Module\Api\Mastodon\Instance; use Friendica\Core\Protocol; use Friendica\Core\System; use Friendica\Database\DBA; -use Friendica\Module\Base\Api; +use Friendica\Module\BaseApi; use Friendica\Network\HTTPException; use Friendica\Util\Network; /** * Undocumented API endpoint that is implemented by both Mastodon and Pleroma */ -class Peers extends Api +class Peers extends BaseApi { /** * @param array $parameters diff --git a/src/Module/Base/Api.php b/src/Module/BaseApi.php similarity index 95% rename from src/Module/Base/Api.php rename to src/Module/BaseApi.php index 7349f45fa..81814bb0d 100644 --- a/src/Module/Base/Api.php +++ b/src/Module/BaseApi.php @@ -1,14 +1,14 @@ Date: Tue, 28 Jan 2020 07:33:37 -0500 Subject: [PATCH 3/4] Move Mastodon API factories to Factory\Api\Mastodon --- src/DI.php | 18 ++++---- src/Factory/{ => Api}/Mastodon/Account.php | 4 +- src/Factory/Api/Mastodon/Emoji.php | 46 +++++++++++++++++++ .../{ => Api}/Mastodon/FollowRequest.php | 4 +- .../{ => Api}/Mastodon/Relationship.php | 2 +- 5 files changed, 60 insertions(+), 14 deletions(-) rename src/Factory/{ => Api}/Mastodon/Account.php (96%) create mode 100644 src/Factory/Api/Mastodon/Emoji.php rename src/Factory/{ => Api}/Mastodon/FollowRequest.php (96%) rename src/Factory/{ => Api}/Mastodon/Relationship.php (95%) diff --git a/src/DI.php b/src/DI.php index 6eb1ed4b6..2c4163722 100644 --- a/src/DI.php +++ b/src/DI.php @@ -141,7 +141,7 @@ abstract class DI } /** - * @return \Friendica\Core\PConfig\IPConfig + * @return Core\PConfig\IPConfig */ public static function pConfig() { @@ -221,31 +221,31 @@ abstract class DI // /** - * @return Factory\Mastodon\Account + * @return Factory\Api\Mastodon\Account */ public static function mstdnAccount() { - return self::$dice->create(Factory\Mastodon\Account::class); + return self::$dice->create(Factory\Api\Mastodon\Account::class); } /** - * @return Factory\Mastodon\FollowRequest + * @return Factory\Api\Mastodon\FollowRequest */ public static function mstdnFollowRequest() { - return self::$dice->create(Factory\Mastodon\FollowRequest::class); + return self::$dice->create(Factory\Api\Mastodon\FollowRequest::class); } /** - * @return Factory\Mastodon\Relationship + * @return Factory\Api\Mastodon\Relationship */ public static function mstdnRelationship() { - return self::$dice->create(Factory\Mastodon\Relationship::class); + return self::$dice->create(Factory\Api\Mastodon\Relationship::class); } /** - * @return \Friendica\Factory\Notification\Notification + * @return Factory\Notification\Notification */ public static function notification() { @@ -253,7 +253,7 @@ abstract class DI } /** - * @return \Friendica\Factory\Notification\Introduction + * @return Factory\Notification\Introduction */ public static function notificationIntro() { diff --git a/src/Factory/Mastodon/Account.php b/src/Factory/Api/Mastodon/Account.php similarity index 96% rename from src/Factory/Mastodon/Account.php rename to src/Factory/Api/Mastodon/Account.php index e777e76fe..60f9a76e1 100644 --- a/src/Factory/Mastodon/Account.php +++ b/src/Factory/Api/Mastodon/Account.php @@ -1,12 +1,12 @@ baseUrl = $baseURL; + } + + /** + * @param int $contactId + * @param int $uid User Id + * @return \Friendica\Api\Entity\Mastodon\Account + * @throws HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public function createFromContactId(int $contactId, $uid = 0) + { + $cdata = Contact::getPublicAndUserContacID($contactId, $uid); + if (!empty($cdata)) { + $publicContact = Contact::getById($cdata['public']); + $userContact = Contact::getById($cdata['user']); + } else { + $publicContact = Contact::getById($contactId); + $userContact = []; + } + + $apcontact = APContact::getByURL($publicContact['url'], false); + + return new \Friendica\Api\Entity\Mastodon\Account($this->baseUrl, $publicContact, $apcontact, $userContact); + } +} diff --git a/src/Factory/Mastodon/FollowRequest.php b/src/Factory/Api/Mastodon/FollowRequest.php similarity index 96% rename from src/Factory/Mastodon/FollowRequest.php rename to src/Factory/Api/Mastodon/FollowRequest.php index 3aabe413d..e0125613a 100644 --- a/src/Factory/Mastodon/FollowRequest.php +++ b/src/Factory/Api/Mastodon/FollowRequest.php @@ -1,13 +1,13 @@ Date: Mon, 27 Jan 2020 21:33:51 -0500 Subject: [PATCH 4/4] Add custom emojis Mastodon API endpoint --- doc/API-Mastodon.md | 4 +++ src/Collection/Api/Mastodon/Emojis.php | 10 ++++++ src/DI.php | 8 +++++ src/Factory/Api/Mastodon/Emoji.php | 45 ++++++++++++------------ src/Module/Api/Mastodon/CustomEmojis.php | 28 +++++++++++++++ src/Object/Api/Mastodon/Emoji.php | 44 ++++++++++++++++++++--- static/routes.config.php | 5 +-- 7 files changed, 114 insertions(+), 30 deletions(-) create mode 100644 src/Collection/Api/Mastodon/Emojis.php create mode 100644 src/Module/Api/Mastodon/CustomEmojis.php diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index e34ec4cc9..d9e6f22b6 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -15,6 +15,10 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en ## Implemented endpoints +- [`GET /api/v1/custom_emojis`](https://docs.joinmastodon.org/methods/instance/custom_emojis/) + - Doesn't return unicode emojis since they aren't using an image URL + + - [`GET /api/v1/follow_requests`](https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows) - Returned IDs are specific to follow requests - [`POST /api/v1/follow_requests/:id/authorize`](https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow) diff --git a/src/Collection/Api/Mastodon/Emojis.php b/src/Collection/Api/Mastodon/Emojis.php new file mode 100644 index 000000000..235b3dfbb --- /dev/null +++ b/src/Collection/Api/Mastodon/Emojis.php @@ -0,0 +1,10 @@ +create(Factory\Api\Mastodon\Account::class); } + /** + * @return Factory\Api\Mastodon\Emoji + */ + public static function mstdnEmoji() + { + return self::$dice->create(Factory\Api\Mastodon\Emoji::class); + } + /** * @return Factory\Api\Mastodon\FollowRequest */ diff --git a/src/Factory/Api/Mastodon/Emoji.php b/src/Factory/Api/Mastodon/Emoji.php index 770966a77..42c500d42 100644 --- a/src/Factory/Api/Mastodon/Emoji.php +++ b/src/Factory/Api/Mastodon/Emoji.php @@ -4,6 +4,7 @@ namespace Friendica\Factory\Api\Mastodon; use Friendica\App\BaseURL; use Friendica\BaseFactory; +use Friendica\Collection\Api\Mastodon\Emojis; use Friendica\Model\APContact; use Friendica\Model\Contact; use Friendica\Network\HTTPException; @@ -11,36 +12,34 @@ use Psr\Log\LoggerInterface; class Emoji extends BaseFactory { - /** @var BaseURL */ - protected $baseUrl; - - public function __construct(LoggerInterface $logger, BaseURL $baseURL) + public function create(string $shortcode, string $url) { - parent::__construct($logger); - - $this->baseUrl = $baseURL; + return new \Friendica\Object\Api\Mastodon\Emoji($shortcode, $url); } /** - * @param int $contactId - * @param int $uid User Id - * @return \Friendica\Api\Entity\Mastodon\Account - * @throws HTTPException\InternalServerErrorException - * @throws \ImagickException + * @param array $smilies + * @return Emojis */ - public function createFromContactId(int $contactId, $uid = 0) + public function createCollectionFromSmilies(array $smilies) { - $cdata = Contact::getPublicAndUserContacID($contactId, $uid); - if (!empty($cdata)) { - $publicContact = Contact::getById($cdata['public']); - $userContact = Contact::getById($cdata['user']); - } else { - $publicContact = Contact::getById($contactId); - $userContact = []; + $prototype = null; + + $emojis = []; + + foreach ($smilies['texts'] as $key => $shortcode) { + if (preg_match('/src="(.+?)"/', $smilies['icons'][$key], $matches)) { + $url = $matches[1]; + + if ($prototype === null) { + $prototype = $this->create($shortcode, $url); + $emojis[] = $prototype; + } else { + $emojis[] = \Friendica\Object\Api\Mastodon\Emoji::createFromPrototype($prototype, $shortcode, $url); + } + }; } - $apcontact = APContact::getByURL($publicContact['url'], false); - - return new \Friendica\Api\Entity\Mastodon\Account($this->baseUrl, $publicContact, $apcontact, $userContact); + return new Emojis($emojis); } } diff --git a/src/Module/Api/Mastodon/CustomEmojis.php b/src/Module/Api/Mastodon/CustomEmojis.php new file mode 100644 index 000000000..58b0c96ee --- /dev/null +++ b/src/Module/Api/Mastodon/CustomEmojis.php @@ -0,0 +1,28 @@ +createCollectionFromSmilies(Smilies::getList()); + + System::jsonExit($emojis->getArrayCopy()); + } +} diff --git a/src/Object/Api/Mastodon/Emoji.php b/src/Object/Api/Mastodon/Emoji.php index 725a72a68..658ce282f 100644 --- a/src/Object/Api/Mastodon/Emoji.php +++ b/src/Object/Api/Mastodon/Emoji.php @@ -7,16 +7,50 @@ use Friendica\BaseEntity; /** * Class Emoji * - * @see https://docs.joinmastodon.org/api/entities/#emoji + * @see https://docs.joinmastodon.org/entities/emoji/ */ class Emoji extends BaseEntity { + //Required attributes /** @var string */ protected $shortcode; - /** @var string (URL) */ + /** @var string (URL)*/ protected $static_url; - /** @var string (URL) */ + /** @var string (URL)*/ protected $url; - /** @var bool */ - protected $visible_in_picker; + /** + * Unsupported + * @var bool + */ + protected $visible_in_picker = true; + + // Optional attributes + /** + * Unsupported + * @var string + */ + //protected $category; + + public function __construct(string $shortcode, string $url) + { + $this->shortcode = $shortcode; + $this->url = $url; + $this->static_url = $url; + } + + /** + * @param Emoji $prototype + * @param string $shortcode + * @param string $url + * @return Emoji + */ + public static function createFromPrototype(Emoji $prototype, string $shortcode, string $url) + { + $emoji = clone $prototype; + $emoji->shortcode = $shortcode; + $emoji->url = $url; + $emoji->static_url = $url; + + return $emoji; + } } diff --git a/static/routes.config.php b/static/routes.config.php index 17a2b5bf5..c081dba66 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -29,10 +29,11 @@ return [ '/api' => [ '/v1' => [ + '/custom_emojis' => [Module\Api\Mastodon\CustomEmojis::class, [R::GET ]], '/follow_requests' => [Module\Api\Mastodon\FollowRequests::class, [R::GET ]], '/follow_requests/{id:\d+}/{action}' => [Module\Api\Mastodon\FollowRequests::class, [ R::POST]], - '/instance' => [Module\Api\Mastodon\Instance::class, [R::GET]], - '/instance/peers' => [Module\Api\Mastodon\Instance\Peers::class, [R::GET]], + '/instance' => [Module\Api\Mastodon\Instance::class, [R::GET ]], + '/instance/peers' => [Module\Api\Mastodon\Instance\Peers::class, [R::GET ]], ], ],