From: Alexander Ebert Date: Sat, 9 Mar 2024 23:47:57 +0000 (+0100) Subject: Migrate the endpoint for mention suggestions X-Git-Tag: 6.1.0_Alpha_1~148^2~22 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=0e18180bb90867c8539cea41fbc02c98570af9e8;p=GitHub%2FWoltLab%2FWCF.git Migrate the endpoint for mention suggestions --- diff --git a/wcfsetup/install/files/lib/action/EditorGetMentionSuggestionsAction.class.php b/wcfsetup/install/files/lib/action/EditorGetMentionSuggestionsAction.class.php index 0e56965860..4cc3790b1b 100644 --- a/wcfsetup/install/files/lib/action/EditorGetMentionSuggestionsAction.class.php +++ b/wcfsetup/install/files/lib/action/EditorGetMentionSuggestionsAction.class.php @@ -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 * @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 - */ - 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 - */ - 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); } } diff --git a/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php b/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php index 945248053c..9b42e05f44 100644 --- a/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php +++ b/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php @@ -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 index 0000000000..a415b39922 --- /dev/null +++ b/wcfsetup/install/files/lib/system/endpoint/controller/core/messages/MentionSuggestions.class.php @@ -0,0 +1,96 @@ +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 + */ + 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 + */ + 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; + } +}