]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Account.php
Merge pull request #8222 from annando/ap-gnusocial
[friendica.git] / src / Factory / Api / Mastodon / Account.php
1 <?php
2
3 namespace Friendica\Factory\Api\Mastodon;
4
5 use Friendica\App\BaseURL;
6 use Friendica\BaseFactory;
7 use Friendica\Collection\Api\Mastodon\Fields;
8 use Friendica\Model\APContact;
9 use Friendica\Model\Contact;
10 use Friendica\Network\HTTPException;
11 use Friendica\Repository\PermissionSet;
12 use Friendica\Repository\ProfileField;
13 use Psr\Log\LoggerInterface;
14
15 class Account extends BaseFactory
16 {
17         /** @var BaseURL */
18         protected $baseUrl;
19         /** @var ProfileField */
20         protected $profileField;
21         /** @var Field */
22         protected $mstdnField;
23
24         public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
25         {
26                 parent::__construct($logger);
27
28                 $this->baseUrl = $baseURL;
29                 $this->profileField = $profileField;
30                 $this->mstdnField = $mstdnField;
31         }
32
33         /**
34          * @param int $contactId
35          * @param int $uid        Public contact (=0) or owner user id
36          * @return \Friendica\Object\Api\Mastodon\Account
37          * @throws HTTPException\InternalServerErrorException
38          * @throws \ImagickException
39          */
40         public function createFromContactId(int $contactId, $uid = 0)
41         {
42                 $cdata = Contact::getPublicAndUserContacID($contactId, $uid);
43                 if (!empty($cdata)) {
44                         $publicContact = Contact::getById($cdata['public']);
45                         $userContact = Contact::getById($cdata['user']);
46                 } else {
47                         $publicContact = Contact::getById($contactId);
48                         $userContact = [];
49                 }
50
51                 $apcontact = APContact::getByURL($publicContact['url'], false);
52
53                 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, new Fields(), $apcontact, $userContact);
54         }
55
56         /**
57          * @param int $userId
58          * @return \Friendica\Object\Api\Mastodon\Account
59          * @throws HTTPException\InternalServerErrorException
60          * @throws \ImagickException
61          */
62         public function createFromUserId(int $userId)
63         {
64                 $publicContact = Contact::selectFirst([], ['uid' => $userId, 'self' => true]);
65
66                 $profileFields = $this->profileField->select(['uid' => $userId, 'psid' => PermissionSet::PUBLIC]);
67                 $fields        = $this->mstdnField->createFromProfileFields($profileFields);
68
69                 $apcontact     = APContact::getByURL($publicContact['url'], false);
70
71                 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apcontact);
72         }
73 }