Add custom emojis Mastodon API endpoint

This commit is contained in:
Hypolite Petovan 2020-01-27 21:33:51 -05:00
parent 1ac9107e5f
commit bd910342df
7 changed files with 114 additions and 30 deletions

View file

@ -15,6 +15,10 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en
## Implemented endpoints ## 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) - [`GET /api/v1/follow_requests`](https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows)
- Returned IDs are specific to follow requests - Returned IDs are specific to follow requests
- [`POST /api/v1/follow_requests/:id/authorize`](https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow) - [`POST /api/v1/follow_requests/:id/authorize`](https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow)

View file

@ -0,0 +1,10 @@
<?php
namespace Friendica\Collection\Api\Mastodon;
use Friendica\BaseCollection;
class Emojis extends BaseCollection
{
}

View file

@ -228,6 +228,14 @@ abstract class DI
return self::$dice->create(Factory\Api\Mastodon\Account::class); 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 * @return Factory\Api\Mastodon\FollowRequest
*/ */

View file

@ -4,6 +4,7 @@ namespace Friendica\Factory\Api\Mastodon;
use Friendica\App\BaseURL; use Friendica\App\BaseURL;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Collection\Api\Mastodon\Emojis;
use Friendica\Model\APContact; use Friendica\Model\APContact;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
@ -11,36 +12,34 @@ use Psr\Log\LoggerInterface;
class Emoji extends BaseFactory class Emoji extends BaseFactory
{ {
/** @var BaseURL */ public function create(string $shortcode, string $url)
protected $baseUrl;
public function __construct(LoggerInterface $logger, BaseURL $baseURL)
{ {
parent::__construct($logger); return new \Friendica\Object\Api\Mastodon\Emoji($shortcode, $url);
$this->baseUrl = $baseURL;
} }
/** /**
* @param int $contactId * @param array $smilies
* @param int $uid User Id * @return Emojis
* @return \Friendica\Api\Entity\Mastodon\Account
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/ */
public function createFromContactId(int $contactId, $uid = 0) public function createCollectionFromSmilies(array $smilies)
{ {
$cdata = Contact::getPublicAndUserContacID($contactId, $uid); $prototype = null;
if (!empty($cdata)) {
$publicContact = Contact::getById($cdata['public']); $emojis = [];
$userContact = Contact::getById($cdata['user']);
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 { } else {
$publicContact = Contact::getById($contactId); $emojis[] = \Friendica\Object\Api\Mastodon\Emoji::createFromPrototype($prototype, $shortcode, $url);
$userContact = []; }
};
} }
$apcontact = APContact::getByURL($publicContact['url'], false); return new Emojis($emojis);
return new \Friendica\Api\Entity\Mastodon\Account($this->baseUrl, $publicContact, $apcontact, $userContact);
} }
} }

View file

@ -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());
}
}

View file

@ -7,16 +7,50 @@ use Friendica\BaseEntity;
/** /**
* Class Emoji * Class Emoji
* *
* @see https://docs.joinmastodon.org/api/entities/#emoji * @see https://docs.joinmastodon.org/entities/emoji/
*/ */
class Emoji extends BaseEntity class Emoji extends BaseEntity
{ {
//Required attributes
/** @var string */ /** @var string */
protected $shortcode; protected $shortcode;
/** @var string (URL)*/ /** @var string (URL)*/
protected $static_url; protected $static_url;
/** @var string (URL)*/ /** @var string (URL)*/
protected $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;
}
} }

View file

@ -29,6 +29,7 @@ return [
'/api' => [ '/api' => [
'/v1' => [ '/v1' => [
'/custom_emojis' => [Module\Api\Mastodon\CustomEmojis::class, [R::GET ]],
'/follow_requests' => [Module\Api\Mastodon\FollowRequests::class, [R::GET ]], '/follow_requests' => [Module\Api\Mastodon\FollowRequests::class, [R::GET ]],
'/follow_requests/{id:\d+}/{action}' => [Module\Api\Mastodon\FollowRequests::class, [ R::POST]], '/follow_requests/{id:\d+}/{action}' => [Module\Api\Mastodon\FollowRequests::class, [ R::POST]],
'/instance' => [Module\Api\Mastodon\Instance::class, [R::GET ]], '/instance' => [Module\Api\Mastodon\Instance::class, [R::GET ]],