Migrate the endpoint for mention suggestions
authorAlexander Ebert <ebert@woltlab.com>
Sat, 9 Mar 2024 23:47:57 +0000 (00:47 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 10 Mar 2024 14:19:39 +0000 (15:19 +0100)
wcfsetup/install/files/lib/action/EditorGetMentionSuggestionsAction.class.php
wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php
wcfsetup/install/files/lib/system/endpoint/controller/core/messages/MentionSuggestions.class.php [new file with mode: 0644]

index 0e56965860aae79b648a83d9fc848793a9bbb941..4cc3790b1b8519224c58fafceb900d50ef7b6d04 100644 (file)
@@ -2,15 +2,10 @@
 
 namespace wcf\action;
 
-use Laminas\Diactoros\Response\JsonResponse;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Server\RequestHandlerInterface;
-use wcf\data\user\group\UserGroup;
-use wcf\data\user\UserProfile;
-use wcf\data\user\UserProfileList;
-use wcf\http\Helper;
-use wcf\system\WCF;
+use wcf\system\endpoint\controller\core\messages\MentionSuggestions;
 
 /**
  * Suggests users that may be mentioned.
@@ -19,85 +14,12 @@ use wcf\system\WCF;
  * @copyright   2001-2023 WoltLab GmbH
  * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @since   6.0
+ * @deprecated 6.1
  */
 final class EditorGetMentionSuggestionsAction implements RequestHandlerInterface
 {
     public function handle(ServerRequestInterface $request): ResponseInterface
     {
-        $parameters = Helper::mapQueryParameters(
-            $request->getQueryParams(),
-            <<<'EOT'
-                    array {
-                        query: non-empty-string
-                    }
-                    EOT,
-        );
-
-        $query = \mb_strtolower($parameters['query']);
-        $matches = [];
-
-        foreach ($this->getGroups($query) as $userGroup) {
-            $matches[] = [
-                'name' => $userGroup->getName(),
-                'groupID' => $userGroup->groupID,
-                'type' => 'group',
-            ];
-        }
-
-        foreach ($this->getUsers($query) as $userProfile) {
-            $matches[] = [
-                'avatarTag' => $userProfile->getAvatar()->getImageTag(16),
-                'username' => $userProfile->getUsername(),
-                'userID' => $userProfile->getObjectID(),
-                'type' => 'user',
-            ];
-        }
-
-        return new JsonResponse(
-            $matches,
-            200,
-            [
-                'cache-control' => [
-                    'max-age=300',
-                ],
-            ]
-        );
-    }
-
-    /**
-     * @return list<UserProfile>
-     */
-    private function getUsers(string $query): array
-    {
-        $userProfileList = new UserProfileList();
-        $userProfileList->getConditionBuilder()->add("username LIKE ?", [$query . '%']);
-
-        $userProfileList->sqlLimit = 10;
-        $userProfileList->readObjects();
-
-        return \array_values($userProfileList->getObjects());
-    }
-
-    /**
-     * @return list<UserGroup>
-     */
-    private function getGroups(string $query): array
-    {
-        $userGroups = UserGroup::getMentionableGroups();
-        if ($userGroups === []) {
-            return [];
-        }
-
-        $userGroups = \array_filter($userGroups, static function (UserGroup $userGroup) use ($query) {
-            return \str_starts_with(\mb_strtolower($userGroup->getName()), $query);
-        });
-
-        $collator = new \Collator(WCF::getLanguage()->getLocale());
-        \usort(
-            $userGroups,
-            static fn (UserGroup $a, UserGroup $b) => $collator->compare($a->getName(), $b->getName())
-        );
-
-        return $userGroups;
+        return (new MentionSuggestions())->mentionSuggestions($request);
     }
 }
index 945248053c65bef85a0695642be35166fd317168..9b42e05f4482ee0b48a40ad2b8c2d70366a04882 100644 (file)
@@ -85,6 +85,7 @@ return static function (): void {
     });
 
     $eventHandler->register(ControllerCollecting::class, static function (ControllerCollecting $event) {
+        $event->register(new \wcf\system\endpoint\controller\core\messages\MentionSuggestions);
         $event->register(new \wcf\system\endpoint\moderationqueues\Assign);
     });
 
diff --git a/wcfsetup/install/files/lib/system/endpoint/controller/core/messages/MentionSuggestions.class.php b/wcfsetup/install/files/lib/system/endpoint/controller/core/messages/MentionSuggestions.class.php
new file mode 100644 (file)
index 0000000..a415b39
--- /dev/null
@@ -0,0 +1,96 @@
+<?php
+
+namespace wcf\system\endpoint\controller\core\messages;
+
+use Laminas\Diactoros\Response\JsonResponse;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use wcf\data\user\group\UserGroup;
+use wcf\data\user\UserProfileList;
+use wcf\http\Helper;
+use wcf\system\endpoint\GetRequest;
+use wcf\system\endpoint\IController;
+use wcf\system\WCF;
+
+final class MentionSuggestions implements IController
+{
+    #[GetRequest('/core/messages/mentionsuggestions')]
+    public function mentionSuggestions(ServerRequestInterface $request): ResponseInterface
+    {
+        $parameters = Helper::mapQueryParameters(
+            $request->getQueryParams(),
+            <<<'EOT'
+                array {
+                    query: non-empty-string
+                }
+                EOT,
+        );
+
+        $query = \mb_strtolower($parameters['query']);
+        $matches = [];
+
+        foreach ($this->getGroups($query) as $userGroup) {
+            $matches[] = [
+                'name' => $userGroup->getName(),
+                'groupID' => $userGroup->groupID,
+                'type' => 'group',
+            ];
+        }
+
+        foreach ($this->getUsers($query) as $userProfile) {
+            $matches[] = [
+                'avatarTag' => $userProfile->getAvatar()->getImageTag(16),
+                'username' => $userProfile->getUsername(),
+                'userID' => $userProfile->getObjectID(),
+                'type' => 'user',
+            ];
+        }
+
+        return new JsonResponse(
+            $matches,
+            200,
+            [
+                'cache-control' => [
+                    'max-age=300',
+                ],
+            ]
+        );
+    }
+
+    /**
+     * @return list<UserProfile>
+     */
+    private function getUsers(string $query): array
+    {
+        $userProfileList = new UserProfileList();
+        $userProfileList->getConditionBuilder()->add("username LIKE ?", [$query . '%']);
+
+        $userProfileList->sqlLimit = 10;
+        $userProfileList->readObjects();
+
+        return \array_values($userProfileList->getObjects());
+    }
+
+    /**
+     * @return list<UserGroup>
+     */
+    private function getGroups(string $query): array
+    {
+        $userGroups = UserGroup::getMentionableGroups();
+        if ($userGroups === []) {
+            return [];
+        }
+
+        $userGroups = \array_filter($userGroups, static function (UserGroup $userGroup) use ($query) {
+            return \str_starts_with(\mb_strtolower($userGroup->getName()), $query);
+        });
+
+        $collator = new \Collator(WCF::getLanguage()->getLocale());
+        \usort(
+            $userGroups,
+            static fn (UserGroup $a, UserGroup $b) => $collator->compare($a->getName(), $b->getName())
+        );
+
+        return $userGroups;
+    }
+}