]> git.mxchange.org Git - friendica.git/commitdiff
Add custom emojis Mastodon API endpoint
authorHypolite Petovan <hypolite@mrpetovan.com>
Tue, 28 Jan 2020 02:33:51 +0000 (21:33 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 28 Jan 2020 13:14:48 +0000 (08:14 -0500)
doc/API-Mastodon.md
src/Collection/Api/Mastodon/Emojis.php [new file with mode: 0644]
src/DI.php
src/Factory/Api/Mastodon/Emoji.php
src/Module/Api/Mastodon/CustomEmojis.php [new file with mode: 0644]
src/Object/Api/Mastodon/Emoji.php
static/routes.config.php

index e34ec4cc95a061c6f92cf0990e6818e061eab803..d9e6f22b6633cb5f9c97dde61d7e5e44ec8e71dd 100644 (file)
@@ -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 (file)
index 0000000..235b3df
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+namespace Friendica\Collection\Api\Mastodon;
+
+use Friendica\BaseCollection;
+
+class Emojis extends BaseCollection
+{
+
+}
index 2c4163722ee1ff6e515bda17babdbcd8a9f0a36d..fc18b30b8ea04f3cc9a436973d6baa4e9bf5ca8f 100644 (file)
@@ -228,6 +228,14 @@ abstract class DI
                return self::$dice->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
         */
index 770966a776f150b6dc8a0fbac113f7dd5a4bbe0d..42c500d4243c36ee2d13efa11d39a98922667556 100644 (file)
@@ -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;
 
-               $apcontact = APContact::getByURL($publicContact['url'], false);
+               $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);
+                               }
+                       };
+               }
 
-               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 (file)
index 0000000..58b0c96
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+namespace Friendica\Module\Api\Mastodon;
+
+use Friendica\Content\Smilies;
+use Friendica\Core\System;
+use Friendica\DI;
+use Friendica\Module\BaseApi;
+use Friendica\Network\HTTPException;
+
+/**
+ * @see https://docs.joinmastodon.org/methods/accounts/follow_requests
+ */
+class CustomEmojis extends BaseApi
+{
+       /**
+        * @param array $parameters
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows
+        */
+       public static function rawContent(array $parameters = [])
+       {
+               $emojis = DI::mstdnEmoji()->createCollectionFromSmilies(Smilies::getList());
+
+               System::jsonExit($emojis->getArrayCopy());
+       }
+}
index 725a72a68cf738ac8069c1748b8c7b8e286ae3ba..658ce282f915125ae4029046c883f0d18e6b72b3 100644 (file)
@@ -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;
+       }
 }
index 17a2b5bf58f91cdbc93892f207cd6781bbdfd3aa..c081dba66a1c0687467d021cfa27702bdc16f63c 100644 (file)
@@ -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         ]],
                ],
        ],