Merge pull request #8191 from MrPetovan/task/7967-mastodon-api-custom_emojis

Add custom emojis Mastodon API endpoint
This commit is contained in:
Philipp 2020-01-28 17:12:00 +01:00 committed by GitHub
commit 977248f510
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 195 additions and 69 deletions

View 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)

View File

@ -1,22 +0,0 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
use Friendica\Api\BaseEntity;
/**
* Class Emoji
*
* @see https://docs.joinmastodon.org/api/entities/#emoji
*/
class Emoji extends BaseEntity
{
/** @var string */
protected $shortcode;
/** @var string (URL)*/
protected $static_url;
/** @var string (URL)*/
protected $url;
/** @var bool */
protected $visible_in_picker;
}

View File

@ -1,6 +1,6 @@
<?php
namespace Friendica\Api;
namespace Friendica;
/**
* The API entity classes are meant as data transfer objects. As such, their member should be protected.

View File

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

View File

@ -141,7 +141,7 @@ abstract class DI
}
/**
* @return \Friendica\Core\PConfig\IPConfig
* @return Core\PConfig\IPConfig
*/
public static function pConfig()
{
@ -221,31 +221,39 @@ 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\Emoji
*/
public static function mstdnEmoji()
{
return self::$dice->create(Factory\Api\Mastodon\Emoji::class);
}
/**
* @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 +261,7 @@ abstract class DI
}
/**
* @return \Friendica\Factory\Notification\Introduction
* @return Factory\Notification\Introduction
*/
public static function notificationIntro()
{

View File

@ -1,12 +1,12 @@
<?php
namespace Friendica\Factory\Mastodon;
namespace Friendica\Factory\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Model\APContact;
use Friendica\Model\Contact;
use Friendica\Network\HTTPException;
use Friendica\BaseFactory;
use Psr\Log\LoggerInterface;
class Account extends BaseFactory
@ -24,7 +24,7 @@ class Account extends BaseFactory
/**
* @param int $contactId
* @param int $uid User Id
* @return \Friendica\Api\Entity\Mastodon\Account
* @return \Friendica\Object\Api\Mastodon\Account
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
@ -41,6 +41,6 @@ class Account extends BaseFactory
$apcontact = APContact::getByURL($publicContact['url'], false);
return new \Friendica\Api\Entity\Mastodon\Account($this->baseUrl, $publicContact, $apcontact, $userContact);
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $apcontact, $userContact);
}
}

View File

@ -0,0 +1,45 @@
<?php
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;
use Psr\Log\LoggerInterface;
class Emoji extends BaseFactory
{
public function create(string $shortcode, string $url)
{
return new \Friendica\Object\Api\Mastodon\Emoji($shortcode, $url);
}
/**
* @param array $smilies
* @return Emojis
*/
public function createCollectionFromSmilies(array $smilies)
{
$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);
}
};
}
return new Emojis($emojis);
}
}

View File

@ -1,13 +1,13 @@
<?php
namespace Friendica\Factory\Mastodon;
namespace Friendica\Factory\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Model\APContact;
use Friendica\Model\Contact;
use Friendica\Model\Introduction;
use Friendica\Network\HTTPException;
use Friendica\BaseFactory;
use Psr\Log\LoggerInterface;
class FollowRequest extends BaseFactory
@ -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);
}
}

View File

@ -1,8 +1,8 @@
<?php
namespace Friendica\Factory\Mastodon;
namespace Friendica\Factory\Api\Mastodon;
use Friendica\Api\Entity\Mastodon\Relationship as RelationshipEntity;
use Friendica\Object\Api\Mastodon\Relationship as RelationshipEntity;
use Friendica\BaseFactory;
use Friendica\Model\Contact;

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

@ -2,18 +2,15 @@
namespace Friendica\Module\Api\Mastodon;
use Friendica\Api\Entity\Mastodon;
use Friendica\Api\Entity\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 = [])
{

View File

@ -2,14 +2,14 @@
namespace Friendica\Module\Api\Mastodon;
use Friendica\Api\Entity\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

View File

@ -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

View File

@ -1,14 +1,14 @@
<?php
namespace Friendica\Module\Base;
namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\DI;
use Friendica\Network\HTTPException;
require_once __DIR__ . '/../../../include/api.php';
require_once __DIR__ . '/../../include/api.php';
class Api extends BaseModule
class BaseApi extends BaseModule
{
/**
* @var string json|xml|rss|atom

View File

@ -1,9 +1,9 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
namespace Friendica\Object\Api\Mastodon;
use Friendica\Api\BaseEntity;
use Friendica\App\BaseURL;
use Friendica\BaseEntity;
use Friendica\Content\Text\BBCode;
use Friendica\Database\DBA;
use Friendica\Model\Contact;

View File

@ -0,0 +1,56 @@
<?php
namespace Friendica\Object\Api\Mastodon;
use Friendica\BaseEntity;
/**
* Class Emoji
*
* @see https://docs.joinmastodon.org/entities/emoji/
*/
class Emoji extends BaseEntity
{
//Required attributes
/** @var string */
protected $shortcode;
/** @var string (URL)*/
protected $static_url;
/** @var string (URL)*/
protected $url;
/**
* 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

@ -1,8 +1,8 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
namespace Friendica\Object\Api\Mastodon;
use Friendica\Api\BaseEntity;
use Friendica\BaseEntity;
/**
* Class Field

View File

@ -1,9 +1,8 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
namespace Friendica\Object\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\Model\Introduction;
/**
* Virtual entity to separate Accounts from Follow Requests.

View File

@ -1,8 +1,8 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
namespace Friendica\Object\Api\Mastodon;
use Friendica\Api\BaseEntity;
use Friendica\BaseEntity;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\User;

View File

@ -1,8 +1,8 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
namespace Friendica\Object\Api\Mastodon;
use Friendica\Api\BaseEntity;
use Friendica\BaseEntity;
use Friendica\Model\Contact;
use Friendica\Util\Network;

View File

@ -1,8 +1,8 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
namespace Friendica\Object\Api\Mastodon;
use Friendica\Api\BaseEntity;
use Friendica\BaseEntity;
use Friendica\Core\Protocol;
use Friendica\Database\DBA;
use Friendica\DI;

View File

@ -29,6 +29,7 @@ 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 ]],