From 079a2be292c7880413dcc644b645b9548bd4aa99 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 12 Mar 2024 17:28:55 +0100 Subject: [PATCH] Add an API wrapper for editor mentions --- .../Core/Api/Messages/MentionSuggestions.ts | 30 +++++++++++++++++++ .../Core/Component/Ckeditor/Mention.ts | 28 ++--------------- .../Core/Api/Messages/MentionSuggestions.js | 18 +++++++++++ .../Core/Component/Ckeditor/Mention.js | 12 ++------ .../lib/action/DeleteSessionAction.class.php | 1 + .../core/sessions/DeleteSession.class.php | 4 +-- 6 files changed, 55 insertions(+), 38 deletions(-) create mode 100644 ts/WoltLabSuite/Core/Api/Messages/MentionSuggestions.ts create mode 100644 wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/MentionSuggestions.js diff --git a/ts/WoltLabSuite/Core/Api/Messages/MentionSuggestions.ts b/ts/WoltLabSuite/Core/Api/Messages/MentionSuggestions.ts new file mode 100644 index 0000000000..01d61a1d38 --- /dev/null +++ b/ts/WoltLabSuite/Core/Api/Messages/MentionSuggestions.ts @@ -0,0 +1,30 @@ +import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend"; +import { ApiResult, apiResultFromError, apiResultFromValue } from "../Result"; + +type Item = + | { + avatarTag: string; + username: string; + userID: number; + type: "user"; + } + | { + name: string; + groupID: string; + type: "group"; + }; +type Response = Item[]; + +export async function mentionSuggestions(query: string): Promise> { + const url = new URL(window.WSC_API_URL + "index.php?api/rpc/core/messages/mentionsuggestions"); + url.searchParams.set("query", query); + + let response: Response; + try { + response = (await prepareRequest(url).get().allowCaching().disableLoadingIndicator().fetchAsJson()) as Response; + } catch (e) { + return apiResultFromError(e); + } + + return apiResultFromValue(response); +} diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor/Mention.ts b/ts/WoltLabSuite/Core/Component/Ckeditor/Mention.ts index 2cc92f6d79..203e5bc53e 100644 --- a/ts/WoltLabSuite/Core/Component/Ckeditor/Mention.ts +++ b/ts/WoltLabSuite/Core/Component/Ckeditor/Mention.ts @@ -9,23 +9,9 @@ */ import type { CKEditor5 } from "@woltlab/editor"; -import { prepareRequest } from "../../Ajax/Backend"; import { createFragmentFromHtml } from "../../Dom/Util"; import { listenToCkeditor } from "./Event"; - -type SearchResultItem = - | { - avatarTag: string; - username: string; - userID: number; - type: "user"; - } - | { - name: string; - groupID: string; - type: "group"; - }; -type ResultGetSearchResultList = SearchResultItem[]; +import { mentionSuggestions } from "WoltLabSuite/Core/Api/Messages/MentionSuggestions"; type Mention = { id: string; @@ -39,17 +25,7 @@ async function getPossibleMentions(query: string): Promise { return []; } - // TODO: Provide the URL as a parameter. - const url = new URL(window.WSC_API_URL + "index.php?api/rpc/core/messages/mentionsuggestions"); - url.searchParams.set("query", query); - - const result = (await prepareRequest(url.toString()) - .get() - .allowCaching() - .disableLoadingIndicator() - .fetchAsJson()) as ResultGetSearchResultList; - - return result.map((item) => { + return (await mentionSuggestions(query)).unwrap().map((item) => { if (item.type === "user") { return { id: `@${item.username}`, diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/MentionSuggestions.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/MentionSuggestions.js new file mode 100644 index 0000000000..d2f0251ddd --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/MentionSuggestions.js @@ -0,0 +1,18 @@ +define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend", "../Result"], function (require, exports, Backend_1, Result_1) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.mentionSuggestions = void 0; + async function mentionSuggestions(query) { + const url = new URL(window.WSC_API_URL + "index.php?api/rpc/core/messages/mentionsuggestions"); + url.searchParams.set("query", query); + let response; + try { + response = (await (0, Backend_1.prepareRequest)(url).get().allowCaching().disableLoadingIndicator().fetchAsJson()); + } + catch (e) { + return (0, Result_1.apiResultFromError)(e); + } + return (0, Result_1.apiResultFromValue)(response); + } + exports.mentionSuggestions = mentionSuggestions; +}); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Mention.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Mention.js index 5efaec501e..9c7ae3f8f9 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Mention.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Mention.js @@ -7,7 +7,7 @@ * @since 6.0 * @woltlabExcludeBundle tiny */ -define(["require", "exports", "../../Ajax/Backend", "../../Dom/Util", "./Event"], function (require, exports, Backend_1, Util_1, Event_1) { +define(["require", "exports", "../../Dom/Util", "./Event", "WoltLabSuite/Core/Api/Messages/MentionSuggestions"], function (require, exports, Util_1, Event_1, MentionSuggestions_1) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setup = void 0; @@ -16,15 +16,7 @@ define(["require", "exports", "../../Ajax/Backend", "../../Dom/Util", "./Event"] if (query.length > 24) { return []; } - // TODO: Provide the URL as a parameter. - const url = new URL(window.WSC_API_URL + "index.php?api/rpc/core/messages/mentionsuggestions"); - url.searchParams.set("query", query); - const result = (await (0, Backend_1.prepareRequest)(url.toString()) - .get() - .allowCaching() - .disableLoadingIndicator() - .fetchAsJson()); - return result.map((item) => { + return (await (0, MentionSuggestions_1.mentionSuggestions)(query)).unwrap().map((item) => { if (item.type === "user") { return { id: `@${item.username}`, diff --git a/wcfsetup/install/files/lib/action/DeleteSessionAction.class.php b/wcfsetup/install/files/lib/action/DeleteSessionAction.class.php index b304f038c8..4ad4ae103b 100644 --- a/wcfsetup/install/files/lib/action/DeleteSessionAction.class.php +++ b/wcfsetup/install/files/lib/action/DeleteSessionAction.class.php @@ -15,6 +15,7 @@ use wcf\util\StringUtil; * @author Joshua Ruesweg * @copyright 2001-2020 WoltLab GmbH * @license GNU Lesser General Public License + * @deprecated 6.1 */ final class DeleteSessionAction extends AbstractSecureAction { diff --git a/wcfsetup/install/files/lib/system/endpoint/controller/core/sessions/DeleteSession.class.php b/wcfsetup/install/files/lib/system/endpoint/controller/core/sessions/DeleteSession.class.php index aabf40ba60..2920e40d73 100644 --- a/wcfsetup/install/files/lib/system/endpoint/controller/core/sessions/DeleteSession.class.php +++ b/wcfsetup/install/files/lib/system/endpoint/controller/core/sessions/DeleteSession.class.php @@ -7,7 +7,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use wcf\system\endpoint\DeleteRequest; use wcf\system\endpoint\IController; -use wcf\system\exception\IllegalLinkException; +use wcf\system\exception\UserInputException; use wcf\system\session\SessionHandler; use wcf\system\WCF; @@ -20,7 +20,7 @@ final class DeleteSession implements IController $sessionID = $variables['id']; if (!$this->isOwnSessionID($sessionID)) { - throw new IllegalLinkException(); + throw new UserInputException('sessionID'); } SessionHandler::getInstance()->deleteUserSession($sessionID); -- 2.20.1