From 9c6f6bcb95213186a364ba00672c1f3c9556b85d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sat, 19 Feb 2022 23:16:21 +0100 Subject: [PATCH 1/2] API: add list members editing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- src/Model/Group.php | 58 ++++++++++++++++++++++ src/Module/Api/Mastodon/Lists/Accounts.php | 22 +++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/Model/Group.php b/src/Model/Group.php index 58c59268e7..da77335f12 100644 --- a/src/Model/Group.php +++ b/src/Model/Group.php @@ -310,6 +310,64 @@ class Group return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]); } + /** + * Adds contacts to a group + * + * @param int $gid + * @param array $contacts + * @throws \Exception + */ + public static function addMembers(int $gid, array $contacts) + { + if (!$gid || !$contacts) { + return false; + } + + // @TODO Backward compatibility with user contacts, remove by version 2022.03 + $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]); + if (empty($group)) { + throw new HTTPException\NotFoundException('Group not found.'); + } + + foreach ($contacts as $cid) { + $cdata = Contact::getPublicAndUserContactID($cid, $group['uid']); + if (empty($cdata['user'])) { + throw new HTTPException\NotFoundException('Invalid contact.'); + } + + DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE); + } + } + + /** + * Removes contacts from a group + * + * @param int $gid + * @param array $contacts + * @throws \Exception + */ + public static function removeMembers(int $gid, array $contacts) + { + if (!$gid || !$contacts) { + return false; + } + + // @TODO Backward compatibility with user contacts, remove by version 2022.03 + $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]); + if (empty($group)) { + throw new HTTPException\NotFoundException('Group not found.'); + } + + foreach ($contacts as $cid) { + $cdata = Contact::getPublicAndUserContactID($cid, $group['uid']); + if (empty($cdata['user'])) { + throw new HTTPException\NotFoundException('Invalid contact.'); + } + + DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']]); + } + } + /** * Returns the combined list of contact ids from a group id list * diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index 6dcde12b7d..8d27207fc4 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -36,12 +36,30 @@ class Accounts extends BaseApi { protected function delete(array $request = []) { - $this->response->unsupported(Router::DELETE, $request); + self::checkAllowedScope(self::SCOPE_WRITE); + + $request = $this->getRequest([ + 'account_ids' => [], // Array of account IDs to remove from the list + ], $request); + + if (empty($request['account_ids']) || empty($this->parameters['id'])) { + DI::mstdnError()->UnprocessableEntity(); + } + return Group::removeMembers($this->parameters['id'], $request['account_ids']); } protected function post(array $request = []) { - $this->response->unsupported(Router::POST, $request); + self::checkAllowedScope(self::SCOPE_WRITE); + + $request = $this->getRequest([ + 'account_ids' => [], // Array of account IDs to add to the list + ], $request); + + if (empty($request['account_ids']) || empty($this->parameters['id'])) { + DI::mstdnError()->UnprocessableEntity(); + } + return Group::addMembers($this->parameters['id'], $request['account_ids']); } /** From 0209892631892486e4ee000328c7e8e8f15c839c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 20 Feb 2022 14:10:57 +0100 Subject: [PATCH 2/2] Apply suggestions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- src/Model/Group.php | 6 +++++- src/Module/Api/Mastodon/Lists/Accounts.php | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Model/Group.php b/src/Model/Group.php index da77335f12..fa41d26467 100644 --- a/src/Model/Group.php +++ b/src/Model/Group.php @@ -358,14 +358,18 @@ class Group throw new HTTPException\NotFoundException('Group not found.'); } + $contactIds = []; + foreach ($contacts as $cid) { $cdata = Contact::getPublicAndUserContactID($cid, $group['uid']); if (empty($cdata['user'])) { throw new HTTPException\NotFoundException('Invalid contact.'); } - DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']]); + $contactIds[] = $cdata['user']; } + + DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $contactIds]); } /** diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index 8d27207fc4..413cacae29 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -45,6 +45,7 @@ class Accounts extends BaseApi if (empty($request['account_ids']) || empty($this->parameters['id'])) { DI::mstdnError()->UnprocessableEntity(); } + return Group::removeMembers($this->parameters['id'], $request['account_ids']); } @@ -59,6 +60,7 @@ class Accounts extends BaseApi if (empty($request['account_ids']) || empty($this->parameters['id'])) { DI::mstdnError()->UnprocessableEntity(); } + return Group::addMembers($this->parameters['id'], $request['account_ids']); }