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

116 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2020-02-09 15:45:36 +01:00
/**
2023-01-01 15:36:24 +01:00
* @copyright Copyright (C) 2010-2023, 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\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Network\HTTPException;
2021-10-21 23:18:08 +02:00
use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository;
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;
2021-10-21 23:18:08 +02:00
/** @var ProfileFieldRepository */
private $profileFieldRepo;
/** @var Field */
2021-06-05 22:36:45 +02:00
private $mstdnFieldFactory;
2021-10-21 23:18:08 +02:00
public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileFieldRepository $profileFieldRepo, Field $mstdnFieldFactory)
{
parent::__construct($logger);
2021-06-05 22:36:45 +02:00
$this->baseUrl = $baseURL;
2021-10-21 23:18:08 +02:00
$this->profileFieldRepo = $profileFieldRepo;
2021-06-05 22:36:45 +02:00
$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
*/
public function createFromContactId(int $contactId, int $uid = 0): \Friendica\Object\Api\Mastodon\Account
{
$contact = Contact::getById($contactId, ['uri-id']);
if (empty($contact)) {
throw new HTTPException\NotFoundException('Contact ' . $contactId . ' not found');
}
if (empty($contact['uri-id'])) {
throw new HTTPException\NotFoundException('Contact ' . $contactId . ' has no uri-id set');
}
return self::createFromUriId($contact['uri-id'], $uid);
}
/**
* @param int $contactUriId
* @param int $uid Public contact (=0) or owner user id
*
* @return \Friendica\Object\Api\Mastodon\Account
* @throws HTTPException\InternalServerErrorException
* @throws ImagickException|HTTPException\NotFoundException
*/
public function createFromUriId(int $contactUriId, int $uid = 0): \Friendica\Object\Api\Mastodon\Account
{
2022-06-10 22:32:21 +02:00
$account = DBA::selectFirst('account-user-view', [], ['uri-id' => $contactUriId, 'uid' => [0, $uid]], ['order' => ['id' => true]]);
if (empty($account)) {
throw new HTTPException\NotFoundException('Contact ' . $contactUriId . ' not found');
2020-12-29 23:14:54 +01:00
}
$fields = new Fields();
2022-06-10 22:32:21 +02:00
if (Contact::isLocal($account['url'])) {
$self_contact = Contact::selectFirst(['uid'], ['nurl' => $account['nurl'], 'self' => true]);
if (!empty($self_contact['uid'])) {
$profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($self_contact['uid']);
$fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
}
}
2020-10-25 19:21:18 +01:00
2022-06-10 22:32:21 +02:00
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $account, $fields);
}
/**
* @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
{
2022-06-10 22:32:21 +02:00
$account = DBA::selectFirst('account-user-view', [], ['uid' => $userId, 'self' => true]);
2021-10-21 23:18:08 +02:00
$profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($userId);
2021-06-05 22:36:45 +02:00
$fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
2022-06-10 22:32:21 +02:00
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $account, $fields);
}
}