<?php
/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
namespace Friendica\Module\Api\Mastodon;
use Friendica\Core\System;
-use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
+use Friendica\Model\Circle;
/**
* @see https://docs.joinmastodon.org/methods/timelines/lists/
*/
class Lists extends BaseApi
{
- public static function delete(array $parameters = [])
+ protected function delete(array $request = [])
{
- self::unsupported('delete');
+ $this->checkAllowedScope(self::SCOPE_WRITE);
+ $uid = self::getCurrentUserID();
+
+ if (empty($this->parameters['id'])) {
+ $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity());
+ }
+
+ if (!Circle::exists($this->parameters['id'], $uid)) {
+ $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound());
+ }
+
+ if (!Circle::remove($this->parameters['id'])) {
+ DI::mstdnError()->InternalError();
+ }
+
+ $this->jsonExit([]);
}
- public static function post(array $parameters = [])
+ protected function post(array $request = [])
{
- self::unsupported('post');
+ $this->checkAllowedScope(self::SCOPE_WRITE);
+ $uid = self::getCurrentUserID();
+
+ $request = $this->getRequest([
+ 'title' => '',
+ ], $request);
+
+ if (empty($request['title'])) {
+ $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity());
+ }
+
+ Circle::create($uid, $request['title']);
+
+ $id = Circle::getIdByName($uid, $request['title']);
+ if (!$id) {
+ DI::mstdnError()->InternalError();
+ }
+
+ $this->jsonExit(DI::mstdnList()->createFromCircleId($id));
}
- public static function put(array $parameters = [])
+ public function put(array $request = [])
{
- self::unsupported('put');
+ $request = $this->getRequest([
+ 'title' => '', // The title of the list to be updated.
+ 'replies_policy' => '', // One of: "followed", "list", or "none".
+ ], $request);
+
+ if (empty($request['title']) || empty($this->parameters['id'])) {
+ $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity());
+ }
+
+ Circle::update($this->parameters['id'], $request['title']);
}
/**
- * @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- public static function rawContent(array $parameters = [])
+ protected function rawContent(array $request = [])
{
- self::login();
+ $this->checkAllowedScope(self::SCOPE_READ);
$uid = self::getCurrentUserID();
- if (empty($parameters['id'])) {
+ if (empty($this->parameters['id'])) {
$lists = [];
- $groups = DBA::select('group', ['id'], ['uid' => $uid, 'deleted' => false]);
- while ($group = DBA::fetch($groups)) {
- $lists[] = DI::mstdnList()->createFromGroupId($group['id']);
+ foreach (Circle::getByUserId($uid) as $circle) {
+ $lists[] = DI::mstdnList()->createFromCircleId($circle['id']);
}
- DBA::close($groups);
} else {
- $id = $parameters['id'];
- if (!DBA::exists('group',['uid' => $uid, 'deleted' => false])) {
- DI::mstdnError()->RecordNotFound();
+ $id = $this->parameters['id'];
+
+ if (!Circle::exists($id, $uid)) {
+ $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound());
}
- $lists = DI::mstdnList()->createFromGroupId($id);
+ $lists = DI::mstdnList()->createFromCircleId($id);
}
- System::jsonExit($lists);
+ $this->jsonExit($lists);
}
}