2020-01-05 23:29:54 +01:00
|
|
|
<?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/>.
|
|
|
|
*
|
|
|
|
*/
|
2020-01-05 23:29:54 +01:00
|
|
|
|
2020-01-28 13:33:37 +01:00
|
|
|
namespace Friendica\Factory\Api\Mastodon;
|
2020-01-05 23:29:54 +01:00
|
|
|
|
|
|
|
use Friendica\App\BaseURL;
|
2020-01-28 13:33:37 +01:00
|
|
|
use Friendica\BaseFactory;
|
2020-01-23 15:06:50 +01:00
|
|
|
use Friendica\Collection\Api\Mastodon\Fields;
|
2022-06-10 20:49:03 +02:00
|
|
|
use Friendica\Database\DBA;
|
2020-01-05 23:29:54 +01:00
|
|
|
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;
|
2020-01-05 23:29:54 +01:00
|
|
|
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;
|
2020-01-23 15:06:50 +01:00
|
|
|
/** @var Field */
|
2021-06-05 22:36:45 +02:00
|
|
|
private $mstdnFieldFactory;
|
2020-01-05 23:29:54 +01:00
|
|
|
|
2021-10-21 23:18:08 +02:00
|
|
|
public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileFieldRepository $profileFieldRepo, Field $mstdnFieldFactory)
|
2020-01-05 23:29:54 +01:00
|
|
|
{
|
|
|
|
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;
|
2020-01-05 23:29:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $contactId
|
2020-01-23 15:06:50 +01:00
|
|
|
* @param int $uid Public contact (=0) or owner user id
|
2021-06-09 00:09:32 +02:00
|
|
|
*
|
2020-01-28 02:01:32 +01:00
|
|
|
* @return \Friendica\Object\Api\Mastodon\Account
|
2020-01-05 23:29:54 +01:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2021-06-09 00:09:32 +02:00
|
|
|
* @throws ImagickException|HTTPException\NotFoundException
|
2020-01-05 23:29:54 +01:00
|
|
|
*/
|
2022-06-16 20:01:09 +02:00
|
|
|
public function createFromContactId(int $contactId, int $uid = 0): \Friendica\Object\Api\Mastodon\Account
|
2020-01-05 23:29:54 +01:00
|
|
|
{
|
2022-06-10 20:49:03 +02:00
|
|
|
$contact = Contact::getById($contactId, ['uri-id']);
|
2022-07-05 18:51:29 +02:00
|
|
|
|
2022-06-10 20:49:03 +02:00
|
|
|
if (empty($contact)) {
|
|
|
|
throw new HTTPException\NotFoundException('Contact ' . $contactId . ' not found');
|
2020-01-05 23:29:54 +01:00
|
|
|
}
|
2022-07-05 18:51:29 +02:00
|
|
|
if (empty($contact['uri-id'])) {
|
|
|
|
throw new HTTPException\NotFoundException('Contact ' . $contactId . ' has no uri-id set');
|
|
|
|
}
|
|
|
|
|
2022-06-10 20:49:03 +02:00
|
|
|
return self::createFromUriId($contact['uri-id'], $uid);
|
|
|
|
}
|
2020-01-05 23:29:54 +01:00
|
|
|
|
2022-06-10 20:49:03 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2022-06-16 20:01:09 +02:00
|
|
|
public function createFromUriId(int $contactUriId, int $uid = 0): \Friendica\Object\Api\Mastodon\Account
|
2022-06-10 20:49:03 +02:00
|
|
|
{
|
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)) {
|
2022-06-10 20:49:03 +02:00
|
|
|
throw new HTTPException\NotFoundException('Contact ' . $contactUriId . ' not found');
|
2020-12-29 23:14:54 +01:00
|
|
|
}
|
|
|
|
|
2022-06-10 20:49:03 +02:00
|
|
|
$fields = new Fields();
|
2020-01-05 23:29:54 +01:00
|
|
|
|
2022-06-10 22:32:21 +02:00
|
|
|
if (Contact::isLocal($account['url'])) {
|
|
|
|
$self_contact = Contact::selectFirst(['uid'], ['nurl' => $account['nurl'], 'self' => true]);
|
2022-06-10 20:49:03 +02:00
|
|
|
if (!empty($self_contact['uid'])) {
|
|
|
|
$profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($self_contact['uid']);
|
|
|
|
$fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
|
|
|
|
}
|
2020-10-25 21:32:45 +01:00
|
|
|
}
|
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);
|
2020-01-23 15:06:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $userId
|
|
|
|
* @return \Friendica\Object\Api\Mastodon\Account
|
2021-06-09 00:09:32 +02:00
|
|
|
* @throws ImagickException|HTTPException\InternalServerErrorException
|
2020-01-23 15:06:50 +01:00
|
|
|
*/
|
2021-06-09 00:09:32 +02:00
|
|
|
public function createFromUserId(int $userId): \Friendica\Object\Api\Mastodon\Account
|
2020-01-23 15:06:50 +01:00
|
|
|
{
|
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);
|
2020-01-23 15:06:50 +01:00
|
|
|
|
2022-06-10 22:32:21 +02:00
|
|
|
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $account, $fields);
|
2020-01-05 23:29:54 +01:00
|
|
|
}
|
|
|
|
}
|