friendica/src/Factory/Api/Mastodon/Account.php

106 lines
3.6 KiB
PHP
Raw Normal View History

<?php
2020-02-09 15:45:36 +01:00
/**
2021-03-29 08:40:20 +02:00
* @copyright Copyright (C) 2010-2021, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Collection\Api\Mastodon\Fields;
use Friendica\Model\APContact;
use Friendica\Model\Contact;
use Friendica\Network\HTTPException;
use Friendica\Repository\PermissionSet;
use Friendica\Repository\ProfileField;
2021-06-09 00:09:32 +02:00
use ImagickException;
use Psr\Log\LoggerInterface;
class Account extends BaseFactory
{
/** @var BaseURL */
2021-06-05 22:36:45 +02:00
private $baseUrl;
/** @var ProfileField */
2021-06-05 22:36:45 +02:00
private $profileFieldRepo;
/** @var Field */
2021-06-05 22:36:45 +02:00
private $mstdnFieldFactory;
2021-06-05 22:36:45 +02:00
public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileFieldRepo, Field $mstdnFieldFactory)
{
parent::__construct($logger);
2021-06-05 22:36:45 +02:00
$this->baseUrl = $baseURL;
$this->profileFieldRepo = $profileFieldRepo;
$this->mstdnFieldFactory = $mstdnFieldFactory;
}
/**
* @param int $contactId
* @param int $uid Public contact (=0) or owner user id
2021-06-09 00:09:32 +02:00
*
* @return \Friendica\Object\Api\Mastodon\Account
* @throws HTTPException\InternalServerErrorException
2021-06-09 00:09:32 +02:00
* @throws ImagickException|HTTPException\NotFoundException
*/
2021-06-09 00:09:32 +02:00
public function createFromContactId(int $contactId, $uid = 0): \Friendica\Object\Api\Mastodon\Account
{
$cdata = Contact::getPublicAndUserContactID($contactId, $uid);
if (!empty($cdata)) {
$publicContact = Contact::getById($cdata['public']);
2021-05-14 23:51:32 +02:00
$userContact = Contact::getById($cdata['user']);
} else {
$publicContact = Contact::getById($contactId);
2021-06-09 00:09:32 +02:00
$userContact = [];
}
2020-12-29 23:14:54 +01:00
if (empty($publicContact)) {
throw new HTTPException\NotFoundException('Contact ' . $contactId . ' not found');
}
$apcontact = APContact::getByURL($publicContact['url'], false);
2020-10-25 21:40:25 +01:00
$self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]);
if (!empty($self_contact['uid'])) {
2021-06-05 22:36:45 +02:00
$profileFields = $this->profileFieldRepo->select(['uid' => $self_contact['uid'], 'psid' => PermissionSet::PUBLIC]);
$fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
} else {
$fields = new Fields();
}
2020-10-25 19:21:18 +01:00
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apcontact, $userContact);
}
/**
* @param int $userId
* @return \Friendica\Object\Api\Mastodon\Account
2021-06-09 00:09:32 +02:00
* @throws ImagickException|HTTPException\InternalServerErrorException
*/
2021-06-09 00:09:32 +02:00
public function createFromUserId(int $userId): \Friendica\Object\Api\Mastodon\Account
{
$publicContact = Contact::selectFirst([], ['uid' => $userId, 'self' => true]);
2021-06-05 22:36:45 +02:00
$profileFields = $this->profileFieldRepo->select(['uid' => $userId, 'psid' => PermissionSet::PUBLIC]);
$fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
2021-06-09 00:09:32 +02:00
$apContact = APContact::getByURL($publicContact['url'], false);
2021-06-09 00:09:32 +02:00
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apContact);
}
}