1
1
Fork 0

Introduce Mastodon entity factories and API\Entity

This commit is contained in:
Hypolite Petovan 2020-01-05 17:29:54 -05:00
parent c748a82e8f
commit 5a1abb8c7d
16 changed files with 411 additions and 224 deletions

18
src/Api/BaseEntity.php Normal file
View file

@ -0,0 +1,18 @@
<?php
namespace Friendica\Api;
/**
* The API entity classes are meant as data transfer objects. As such, their member should be protected.
* Then the JsonSerializable interface ensures the protected members will be included in a JSON encode situation.
*
* Constructors are supposed to take as arguments the Friendica dependencies/model/collection/data it needs to
* populate the class members.
*/
abstract class BaseEntity implements \JsonSerializable
{
public function jsonSerialize()
{
return get_object_vars($this);
}
}

View file

@ -0,0 +1,108 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
use Friendica\Api\BaseEntity;
use Friendica\App\BaseURL;
use Friendica\Content\Text\BBCode;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Util\DateTimeFormat;
/**
* Class Account
*
* @see https://docs.joinmastodon.org/entities/account
*/
class Account extends BaseEntity
{
/** @var string */
protected $id;
/** @var string */
protected $username;
/** @var string */
protected $acct;
/** @var string */
protected $display_name;
/** @var bool */
protected $locked;
/** @var string (Datetime) */
protected $created_at;
/** @var int */
protected $followers_count;
/** @var int */
protected $following_count;
/** @var int */
protected $statuses_count;
/** @var string */
protected $note;
/** @var string (URL)*/
protected $url;
/** @var string (URL) */
protected $avatar;
/** @var string (URL) */
protected $avatar_static;
/** @var string (URL) */
protected $header;
/** @var string (URL) */
protected $header_static;
/** @var Emoji[] */
protected $emojis;
/** @var Account|null */
protected $moved = null;
/** @var Field[]|null */
protected $fields = null;
/** @var bool|null */
protected $bot = null;
/** @var bool */
protected $group;
/** @var bool */
protected $discoverable;
/** @var string|null (Datetime) */
protected $last_status_at = null;
/**
* Creates an account record from a public contact record. Expects all contact table fields to be set.
*
* @param BaseURL $baseUrl
* @param array $publicContact Full contact table record with uid = 0
* @param array $apcontact Optional full apcontact table record
* @param array $userContact Optional full contact table record with uid != 0
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(BaseURL $baseUrl, array $publicContact, array $apcontact = [], array $userContact = [])
{
$this->id = $publicContact['id'];
$this->username = $publicContact['nick'];
$this->acct =
strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ?
$publicContact['nick'] :
$publicContact['addr'];
$this->display_name = $publicContact['name'];
$this->locked = !empty($apcontact['manually-approve']);
$this->created_at = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::ATOM);
$this->followers_count = $apcontact['followers_count'] ?? 0;
$this->following_count = $apcontact['following_count'] ?? 0;
$this->statuses_count = $apcontact['statuses_count'] ?? 0;
$this->note = BBCode::convert($publicContact['about'], false);
$this->url = $publicContact['url'];
$this->avatar = $userContact['avatar'] ?? $publicContact['avatar'];
$this->avatar_static = $userContact['avatar'] ?? $publicContact['avatar'];
// No header picture in Friendica
$this->header = '';
$this->header_static = '';
// No custom emojis per account in Friendica
$this->emojis = [];
// No metadata fields in Friendica
$this->fields = [];
$this->bot = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
$this->group = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
$this->discoverable = !$publicContact['unsearchable'];
$publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
$userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
$lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
$this->last_status_at = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, DateTimeFormat::ATOM) : null;
}
}

View file

@ -0,0 +1,22 @@
<?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,18 +1,20 @@
<?php <?php
namespace Friendica\Api\Mastodon; namespace Friendica\Api\Entity\Mastodon;
use Friendica\Api\BaseEntity;
/** /**
* Class Field * Class Field
* *
* @see https://docs.joinmastodon.org/api/entities/#field * @see https://docs.joinmastodon.org/api/entities/#field
*/ */
class Field class Field extends BaseEntity
{ {
/** @var string */ /** @var string */
var $name; protected $name;
/** @var string (HTML) */ /** @var string (HTML) */
var $value; protected $value;
/** @var string (Datetime)*/ /** @var string (Datetime)*/
var $verified_at; protected $verified_at;
} }

View file

@ -0,0 +1,32 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
use Friendica\App\BaseURL;
use Friendica\Model\Introduction;
/**
* Virtual entity to separate Accounts from Follow Requests.
* In the Mastodon API they are one and the same.
*/
class FollowRequest extends Account
{
/**
* Creates a follow request entity from an introduction record.
*
* The account ID is set to the Introduction ID to allow for later interaction with follow requests.
*
* @param BaseURL $baseUrl
* @param int $introduction_id Introduction record id
* @param array $publicContact Full contact table record with uid = 0
* @param array $apcontact Optional full apcontact table record
* @param array $userContact Optional full contact table record with uid != 0
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(BaseURL $baseUrl, int $introduction_id, array $publicContact, array $apcontact = [], array $userContact = [])
{
parent::__construct($baseUrl, $publicContact, $apcontact, $userContact);
$this->id = $introduction_id;
}
}

View file

@ -1,12 +1,11 @@
<?php <?php
namespace Friendica\Api\Mastodon; namespace Friendica\Api\Entity\Mastodon;
use Friendica\App; use Friendica\Api\BaseEntity;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\APContact;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\Register; use Friendica\Module\Register;
@ -15,42 +14,41 @@ use Friendica\Module\Register;
* *
* @see https://docs.joinmastodon.org/api/entities/#instance * @see https://docs.joinmastodon.org/api/entities/#instance
*/ */
class Instance class Instance extends BaseEntity
{ {
/** @var string (URL) */ /** @var string (URL) */
var $uri; protected $uri;
/** @var string */ /** @var string */
var $title; protected $title;
/** @var string */ /** @var string */
var $description; protected $description;
/** @var string */ /** @var string */
var $email; protected $email;
/** @var string */ /** @var string */
var $version; protected $version;
/** @var array */ /** @var array */
var $urls; protected $urls;
/** @var Stats */ /** @var Stats */
var $stats; protected $stats;
/** @var string */ /** @var string|null */
var $thumbnail; protected $thumbnail = null;
/** @var array */ /** @var array */
var $languages; protected $languages;
/** @var int */ /** @var int */
var $max_toot_chars; protected $max_toot_chars;
/** @var bool */ /** @var bool */
var $registrations; protected $registrations;
/** @var bool */ /** @var bool */
var $approval_required; protected $approval_required;
/** @var Account|null */ /** @var Account|null */
var $contact_account; protected $contact_account = null;
/** /**
* Creates an instance record * Creates an instance record
* *
* @param App $app
*
* @return Instance * @return Instance
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/ */
public static function get() public static function get()
{ {
@ -77,9 +75,8 @@ class Instance
$adminList = explode(',', str_replace(' ', '', Config::get('config', 'admin_email'))); $adminList = explode(',', str_replace(' ', '', Config::get('config', 'admin_email')));
$administrator = User::getByEmail($adminList[0], ['nickname']); $administrator = User::getByEmail($adminList[0], ['nickname']);
if (!empty($administrator)) { if (!empty($administrator)) {
$adminContact = DBA::selectFirst('contact', [], ['nick' => $administrator['nickname'], 'self' => true]); $adminContact = DBA::selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]);
$apcontact = APContact::getByURL($adminContact['url'], false); $instance->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']);
$instance->contact_account = Account::create($baseUrl, $adminContact, $apcontact);
} }
} }

View file

@ -0,0 +1,60 @@
<?php
namespace Friendica\Api\Entity\Mastodon;
use Friendica\Api\BaseEntity;
use Friendica\Model\Contact;
use Friendica\Util\Network;
/**
* Class Relationship
*
* @see https://docs.joinmastodon.org/api/entities/#relationship
*/
class Relationship extends BaseEntity
{
/** @var int */
protected $id;
/** @var bool */
protected $following = false;
/** @var bool */
protected $followed_by = false;
/** @var bool */
protected $blocking = false;
/** @var bool */
protected $muting = false;
/** @var bool */
protected $muting_notifications = false;
/** @var bool */
protected $requested = false;
/** @var bool */
protected $domain_blocking = false;
/**
* Unsupported
* @var bool
*/
protected $showing_reblogs = true;
/**
* Unsupported
* @var bool
*/
protected $endorsed = false;
/**
* @param int $userContactId Contact row Id with uid != 0
* @param array $userContact Full Contact table record with uid != 0
*/
public function __construct(int $userContactId, array $userContact = [])
{
$this->id = $userContactId;
$this->following = in_array($userContact['rel'] ?? 0, [Contact::SHARING, Contact::FRIEND]);
$this->followed_by = in_array($userContact['rel'] ?? 0, [Contact::FOLLOWER, Contact::FRIEND]);
$this->blocking = (bool)$userContact['blocked'] ?? false;
$this->muting = (bool)$userContact['readonly'] ?? false;
$this->muting_notifications = (bool)$userContact['readonly'] ?? false;
$this->requested = (bool)$userContact['pending'] ?? false;
$this->domain_blocking = Network::isUrlBlocked($userContact['url'] ?? '');
return $this;
}
}

View file

@ -1,7 +1,8 @@
<?php <?php
namespace Friendica\Api\Mastodon; namespace Friendica\Api\Entity\Mastodon;
use Friendica\Api\BaseEntity;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Database\DBA; use Friendica\Database\DBA;
@ -11,14 +12,14 @@ use Friendica\Database\DBA;
* *
* @see https://docs.joinmastodon.org/api/entities/#stats * @see https://docs.joinmastodon.org/api/entities/#stats
*/ */
class Stats class Stats extends BaseEntity
{ {
/** @var int */ /** @var int */
var $user_count; protected $user_count = 0;
/** @var int */ /** @var int */
var $status_count; protected $status_count = 0;
/** @var int */ /** @var int */
var $domain_count; protected $domain_count = 0;
/** /**
* Creates a stats record * Creates a stats record

View file

@ -1,111 +0,0 @@
<?php
namespace Friendica\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\Content\Text\BBCode;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Util\DateTimeFormat;
/**
* Class Account
*
* @see https://docs.joinmastodon.org/entities/account
*/
class Account
{
/** @var string */
var $id;
/** @var string */
var $username;
/** @var string */
var $acct;
/** @var string */
var $display_name;
/** @var bool */
var $locked;
/** @var string (Datetime) */
var $created_at;
/** @var int */
var $followers_count;
/** @var int */
var $following_count;
/** @var int */
var $statuses_count;
/** @var string */
var $note;
/** @var string (URL)*/
var $url;
/** @var string (URL) */
var $avatar;
/** @var string (URL) */
var $avatar_static;
/** @var string (URL) */
var $header;
/** @var string (URL) */
var $header_static;
/** @var Emoji[] */
var $emojis;
/** @var Account|null */
var $moved = null;
/** @var Field[]|null */
var $fields = null;
/** @var bool|null */
var $bot = null;
/** @var bool */
var $group;
/** @var bool */
var $discoverable;
/** @var string|null (Datetime) */
var $last_status_at = null;
/**
* Creates an account record from a public contact record. Expects all contact table fields to be set.
*
* @param BaseURL $baseUrl
* @param array $publicContact Full contact table record with uid = 0
* @param array $apcontact Optional full apcontact table record
* @param array $userContact Optional full contact table record with uid = local_user()
* @return Account
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function create(BaseURL $baseUrl, array $publicContact, array $apcontact = [], array $userContact = [])
{
$account = new Account();
$account->id = $publicContact['id'];
$account->username = $publicContact['nick'];
$account->acct =
strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ?
$publicContact['nick'] :
$publicContact['addr'];
$account->display_name = $publicContact['name'];
$account->locked = !empty($apcontact['manually-approve']);
$account->created_at = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::ATOM);
$account->followers_count = $apcontact['followers_count'] ?? 0;
$account->following_count = $apcontact['following_count'] ?? 0;
$account->statuses_count = $apcontact['statuses_count'] ?? 0;
$account->note = BBCode::convert($publicContact['about'], false);
$account->url = $publicContact['url'];
$account->avatar = $userContact['avatar'] ?? $publicContact['avatar'];
$account->avatar_static = $userContact['avatar'] ?? $publicContact['avatar'];
// No header picture in Friendica
$account->header = '';
$account->header_static = '';
// No custom emojis per account in Friendica
$account->emojis = [];
// No metadata fields in Friendica
$account->fields = [];
$account->bot = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
$account->group = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
$account->discoverable = !$publicContact['unsearchable'];
$publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
$userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
$lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
$account->last_status_at = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, DateTimeFormat::ATOM) : null;
return $account;
}
}

View file

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

View file

@ -1,59 +0,0 @@
<?php
namespace Friendica\Api\Mastodon;
use Friendica\Model\Contact;
use Friendica\Util\Network;
/**
* Class Relationship
*
* @see https://docs.joinmastodon.org/api/entities/#relationship
*/
class Relationship
{
/** @var int */
var $id;
/** @var bool */
var $following = false;
/** @var bool */
var $followed_by = false;
/** @var bool */
var $blocking = false;
/** @var bool */
var $muting = false;
/** @var bool */
var $muting_notifications = false;
/** @var bool */
var $requested = false;
/** @var bool */
var $domain_blocking = false;
/** @var bool */
var $showing_reblogs = false;
/** @var bool */
var $endorsed = false;
/**
* @param array $contact Full Contact table record
* @return Relationship
*/
public static function createFromContact(array $contact)
{
$relationship = new self();
$relationship->id = $contact['id'];
$relationship->following = in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]);
$relationship->followed_by = in_array($contact['rel'], [Contact::FOLLOWER, Contact::FRIEND]);
$relationship->blocking = (bool)$contact['blocked'];
$relationship->muting = (bool)$contact['readonly'];
$relationship->muting_notifications = (bool)$contact['readonly'];
$relationship->requested = (bool)$contact['pending'];
$relationship->domain_blocking = Network::isUrlBlocked($contact['url']);
// Unsupported
$relationship->showing_reblogs = true;
// Unsupported
$relationship->endorsed = false;
return $relationship;
}
}

View file

@ -28,6 +28,9 @@ use Psr\Log\LoggerInterface;
* @method static Core\Process process() * @method static Core\Process process()
* @method static Core\Session\ISession session() * @method static Core\Session\ISession session()
* @method static Database\Database dba() * @method static Database\Database dba()
* @method static Factory\Mastodon\Account mstdnAccount()
* @method static Factory\Mastodon\FollowRequest mstdnFollowRequest()
* @method static Factory\Mastodon\Relationship mstdnRelationship()
* @method static Model\User\Cookie cookie() * @method static Model\User\Cookie cookie()
* @method static Model\Notify notify() * @method static Model\Notify notify()
* @method static Model\Introduction intro() * @method static Model\Introduction intro()
@ -62,6 +65,9 @@ abstract class DI
'process' => Core\Process::class, 'process' => Core\Process::class,
'session' => Core\Session\ISession::class, 'session' => Core\Session\ISession::class,
'dba' => Database\Database::class, 'dba' => Database\Database::class,
'mstdnAccount' => Factory\Mastodon\Account::class,
'mstdnFollowRequest' => Factory\Mastodon\FollowRequest::class,
'mstdnRelationship' => Factory\Mastodon\Relationship::class,
'cookie' => Model\User\Cookie::class, 'cookie' => Model\User\Cookie::class,
'notify' => Model\Notify::class, 'notify' => Model\Notify::class,
'intro' => Model\Introduction::class, 'intro' => Model\Introduction::class,

View file

@ -0,0 +1,46 @@
<?php
namespace Friendica\Factory\Mastodon;
use Friendica\App\BaseURL;
use Friendica\Model\APContact;
use Friendica\Model\Contact;
use Friendica\Network\HTTPException;
use Friendica\BaseFactory;
use Psr\Log\LoggerInterface;
class Account extends BaseFactory
{
/** @var BaseURL */
protected $baseUrl;
public function __construct(LoggerInterface $logger, BaseURL $baseURL)
{
parent::__construct($logger);
$this->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);
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace Friendica\Factory\Mastodon;
use Friendica\App\BaseURL;
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
{
/** @var BaseURL */
protected $baseUrl;
public function __construct(LoggerInterface $logger, BaseURL $baseURL)
{
parent::__construct($logger);
$this->baseUrl = $baseURL;
}
/**
* @param Introduction $Introduction
* @return \Friendica\Api\Entity\Mastodon\FollowRequest
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public function createFromIntroduction(Introduction $Introduction)
{
$cdata = Contact::getPublicAndUserContacID($Introduction->{'contact-id'}, $Introduction->uid);
if (empty($cdata)) {
$this->logger->warning('Wrong introduction data', ['Introduction' => $Introduction]);
throw new HTTPException\InternalServerErrorException('Wrong introduction data');
}
$publicContact = Contact::getById($cdata['public']);
$userContact = Contact::getById($cdata['user']);
$apcontact = APContact::getByURL($publicContact['url'], false);
return new \Friendica\Api\Entity\Mastodon\FollowRequest($this->baseUrl, $Introduction->id, $publicContact, $apcontact, $userContact);
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Friendica\Factory\Mastodon;
use Friendica\Api\Entity\Mastodon\Relationship as RelationshipEntity;
use Friendica\BaseFactory;
use Friendica\Model\Contact;
class Relationship extends BaseFactory
{
/**
* @param int $userContactId Contact row id with uid != 0
* @return RelationshipEntity
* @throws \Exception
*/
public function createFromContactId(int $userContactId)
{
return $this->createFromContact(Contact::getById($userContactId));
}
/**
* @param array $userContact Full contact row record with uid != 0
* @return RelationshipEntity
*/
public function createFromContact(array $userContact)
{
return new RelationshipEntity($userContact['id'], $userContact);
}
/**
* @param int $userContactId Contact row id with uid != 0
* @return RelationshipEntity
*/
public function createDefaultFromContactId(int $userContactId)
{
return new RelationshipEntity($userContactId);
}
}

View file

@ -2,7 +2,7 @@
namespace Friendica\Module\Api\Mastodon; namespace Friendica\Module\Api\Mastodon;
use Friendica\Api\Mastodon\Instance as InstanceEntity; use Friendica\Api\Entity\Mastodon\Instance as InstanceEntity;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Module\Base\Api; use Friendica\Module\Base\Api;