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

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