Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment backend overhaul #5944

Merged
merged 12 commits into from
Jun 17, 2024
1 change: 0 additions & 1 deletion com.woltlab.wcf/templates/comments.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script data-relocate="true">
require(['Language', 'WoltLabSuite/Core/Component/Comment/List'], (Language, { setup }) => {
Language.addObject({
'wcf.comment.guestDialog.title': '{jslang}wcf.comment.guestDialog.title{/jslang}',
'wcf.comment.more': '{jslang}wcf.comment.more{/jslang}',
'wcf.comment.response.more': '{jslang}wcf.comment.response.more{/jslang}',
});
Expand Down
41 changes: 41 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/CreateComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Creates a new comment.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
* @woltlabExcludeBundle tiny
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../Result";

type Response = {
commentID: number;
};

export async function createComment(
objectTypeId: number,
objectId: number,
message: string,
guestToken: string = "",
): Promise<ApiResult<Response>> {
const url = new URL(`${window.WSC_API_URL}index.php?api/rpc/core/comments`);

const payload = {
objectTypeID: objectTypeId,
objectID: objectId,
message,
guestToken,
};

let response: Response;
try {
response = (await prepareRequest(url).post(payload).fetchAsJson()) as Response;
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue(response);
}
22 changes: 22 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/DeleteComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Deletes a comment.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
* @woltlabExcludeBundle tiny
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../Result";

export async function deleteComment(commentId: number): Promise<ApiResult<[]>> {
try {
await prepareRequest(`${window.WSC_API_URL}index.php?api/rpc/core/comments/${commentId}`).delete().fetchAsJson();
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue([]);
}
28 changes: 28 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/EditComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Gets the html code for the editing of a comment.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../Result";

type Response = {
template: string;
};

export async function editComment(commentId: number): Promise<ApiResult<Response>> {
const url = new URL(`${window.WSC_API_URL}index.php?api/rpc/core/comments/${commentId}/edit`);

let response: Response;
try {
response = (await prepareRequest(url).get().fetchAsJson()) as Response;
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue(response);
}
24 changes: 24 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/EnableComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Enables a comment.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
* @woltlabExcludeBundle tiny
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../Result";

export async function enableComment(commentId: number): Promise<ApiResult<[]>> {
try {
await prepareRequest(`${window.WSC_API_URL}index.php?api/rpc/core/comments/${commentId}/enable`)
.post()
.fetchAsJson();
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue([]);
}
41 changes: 41 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/RenderComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Gets the html code for the rendering of a comment.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../Result";

type Response = {
template: string;
response: string | undefined;
};

export async function renderComment(
commentId: number,
responseId: number | undefined = undefined,
messageOnly: boolean = false,
objectTypeId: number | undefined = undefined,
): Promise<ApiResult<Response>> {
const url = new URL(`${window.WSC_API_URL}index.php?api/rpc/core/comments/${commentId}/render`);
url.searchParams.set("messageOnly", messageOnly.toString());
if (responseId !== undefined) {
url.searchParams.set("responseID", responseId.toString());
}
if (objectTypeId !== undefined) {
url.searchParams.set("objectTypeID", objectTypeId.toString());
}

let response: Response;
try {
response = (await prepareRequest(url).get().fetchAsJson()) as Response;
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue(response);
}
36 changes: 36 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/RenderComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Gets the html code for the rendering of comments.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../Result";

type Response = {
template: string;
lastCommentTime: number;
};

export async function renderComments(
objectTypeId: number,
objectId: number,
lastCommentTime: number = 0,
): Promise<ApiResult<Response>> {
const url = new URL(`${window.WSC_API_URL}index.php?api/rpc/core/comments/render`);
url.searchParams.set("objectTypeID", objectTypeId.toString());
url.searchParams.set("objectID", objectId.toString());
url.searchParams.set("lastCommentTime", lastCommentTime.toString());

let response: Response;
try {
response = (await prepareRequest(url).get().fetchAsJson()) as Response;
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue(response);
}
39 changes: 39 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/Responses/CreateResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Creates a new comment response.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
* @woltlabExcludeBundle tiny
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../../Result";

type Response = {
responseID: number;
};

export async function createResponse(
commentId: number,
message: string,
guestToken: string = "",
): Promise<ApiResult<Response>> {
const url = new URL(`${window.WSC_API_URL}index.php?api/rpc/core/comments/responses`);

const payload = {
commentID: commentId,
message,
guestToken,
};

let response: Response;
try {
response = (await prepareRequest(url).post(payload).fetchAsJson()) as Response;
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue(response);
}
24 changes: 24 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/Responses/DeleteResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Deletes a comment response.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
* @woltlabExcludeBundle tiny
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../../Result";

export async function deleteResponse(responseId: number): Promise<ApiResult<[]>> {
try {
await prepareRequest(`${window.WSC_API_URL}index.php?api/rpc/core/comments/responses/${responseId}`)
.delete()
.fetchAsJson();
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue([]);
}
28 changes: 28 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/Responses/EditResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Gets the html code for the editing of a comment response.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../../Result";

type Response = {
template: string;
};

export async function editResponse(responseId: number): Promise<ApiResult<Response>> {
const url = new URL(`${window.WSC_API_URL}index.php?api/rpc/core/comments/responses/${responseId}/edit`);

let response: Response;
try {
response = (await prepareRequest(url).get().fetchAsJson()) as Response;
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue(response);
}
24 changes: 24 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/Responses/EnableResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Enables a comment response.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
* @woltlabExcludeBundle tiny
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../../Result";

export async function enableResponse(responseId: number): Promise<ApiResult<[]>> {
try {
await prepareRequest(`${window.WSC_API_URL}index.php?api/rpc/core/comments/responses/${responseId}/enable`)
.post()
.fetchAsJson();
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue([]);
}
36 changes: 36 additions & 0 deletions ts/WoltLabSuite/Core/Api/Comments/Responses/RenderResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Gets the html code for the rendering of a response.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
*/

import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "../../Result";

type Response = {
template: string;
};

export async function renderResponse(
responseId: number,
messageOnly: boolean = false,
objectTypeId: number | undefined = undefined,
): Promise<ApiResult<Response>> {
const url = new URL(`${window.WSC_API_URL}index.php?api/rpc/core/comments/responses/${responseId}/render`);
url.searchParams.set("messageOnly", messageOnly.toString());
if (objectTypeId !== undefined) {
url.searchParams.set("objectTypeID", objectTypeId.toString());
}

let response: Response;
try {
response = (await prepareRequest(url).get().fetchAsJson()) as Response;
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue(response);
}
Loading
Loading