]> git.mxchange.org Git - friendica.git/commitdiff
API: add list members editing
authormarcin mikołajczak <git@mkljczk.pl>
Sat, 19 Feb 2022 22:16:21 +0000 (23:16 +0100)
committermarcin mikołajczak <git@mkljczk.pl>
Sat, 19 Feb 2022 22:22:54 +0000 (23:22 +0100)
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
src/Model/Group.php
src/Module/Api/Mastodon/Lists/Accounts.php

index 58c59268e77ba2ef9d54c977eedfa7dabed40eff..da77335f12ed944a515bf26d74be052aadde0644 100644 (file)
@@ -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
         *
index 6dcde12b7d4aa651f7aaff9fd2b04fd3ad5a0a37..8d27207fc4762b933d2288283bbdd7e298c74df3 100644 (file)
@@ -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']);
        }
 
        /**