friendica/src/Module/Api/Twitter/Lists/Create.php

86 lines
2.8 KiB
PHP
Raw Normal View History

2022-01-15 22:38:19 +01:00
<?php
/**
2023-01-01 15:36:24 +01:00
* @copyright Copyright (C) 2010-2023, the Friendica project
2022-01-15 22:38:19 +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\Module\Api\Twitter\Lists;
2022-01-16 08:07:46 +01:00
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Factory\Api\Friendica\Circle as FriendicaCircle;
2022-01-15 22:38:19 +01:00
use Friendica\Module\BaseApi;
use Friendica\Model\Contact;
use Friendica\Model\Circle;
2022-01-16 08:07:46 +01:00
use Friendica\Module\Api\ApiResponse;
2022-01-15 22:38:19 +01:00
use Friendica\Network\HTTPException;
2022-01-16 08:07:46 +01:00
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
2022-01-15 22:38:19 +01:00
/**
* Update information about a circle.
2022-01-15 22:38:19 +01:00
*
* @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/post-lists-update
*/
class Create extends BaseApi
{
/** @var FriendicaCircle */
private $friendicaCircle;
2022-01-16 08:07:46 +01:00
/** @var Database */
private $dba;
public function __construct(Database $dba, FriendicaCircle $friendicaCircle, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
2022-01-16 08:07:46 +01:00
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->dba = $dba;
$this->friendicaCircle = $friendicaCircle;
2022-01-16 08:07:46 +01:00
}
2022-01-16 08:14:09 +01:00
2022-01-15 22:38:19 +01:00
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();
2022-01-16 08:14:09 +01:00
2022-01-15 22:38:19 +01:00
// params
2022-01-16 15:04:20 +01:00
$name = $this->getRequestValue($request, 'name', '');
2022-01-16 08:14:09 +01:00
2022-01-15 22:38:19 +01:00
if ($name == '') {
throw new HTTPException\BadRequestException('circle name not specified');
2022-01-15 22:38:19 +01:00
}
2022-01-16 08:14:09 +01:00
// error message if specified circle name already exists
2022-01-16 08:07:46 +01:00
if ($this->dba->exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => false])) {
throw new HTTPException\BadRequestException('circle name already exists');
2022-01-15 22:38:19 +01:00
}
2022-01-16 08:14:09 +01:00
$ret = Circle::create($uid, $name);
2022-01-15 22:38:19 +01:00
if ($ret) {
$gid = Circle::getIdByName($uid, $name);
2022-01-15 22:38:19 +01:00
} else {
throw new HTTPException\BadRequestException('other API error');
}
2022-01-16 08:14:09 +01:00
$grp = $this->friendicaCircle->createFromId($gid);
2022-01-16 08:14:09 +01:00
2022-01-15 22:38:19 +01:00
$this->response->exit('statuses', ['lists' => ['lists' => $grp]], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
}
}