Skip to content

Commit e336a45

Browse files
refactor: Update chat.postMessage endpoint to include validateParams and type assertions
1 parent 19f1f9a commit e336a45

File tree

1 file changed

+6
-4
lines changed
  • apps/meteor/app/api/server/v1

1 file changed

+6
-4
lines changed

apps/meteor/app/api/server/v1/chat.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Message } from '@rocket.chat/core-services';
2-
import type { IMessage, IUser, IRoom, IThreadMainMessage, MessageAttachment, RequiredField } from '@rocket.chat/core-typings';
2+
import type { IMessage, IUser, IThreadMainMessage, MessageAttachment, RequiredField } from '@rocket.chat/core-typings';
33
import { Messages, Users, Rooms, Subscriptions } from '@rocket.chat/models';
44
import {
55
isChatReportMessageProps,
@@ -29,6 +29,7 @@ import {
2929
isChatGetStarredMessagesProps,
3030
isChatGetDiscussionsProps,
3131
} from '@rocket.chat/rest-typings';
32+
import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv';
3233
import { escapeRegExp } from '@rocket.chat/string-helpers';
3334
import { Meteor } from 'meteor/meteor';
3435

@@ -318,6 +319,7 @@ const chatPostMessageEndpoints = API.v1.post(
318319
'chat.postMessage',
319320
{
320321
authRequired: true,
322+
validateParams: isChatPostMessageProps,
321323
body: isChatPostMessageProps,
322324
response: {
323325
400: ajv.compile({
@@ -351,7 +353,7 @@ const chatPostMessageEndpoints = API.v1.post(
351353
}),
352354
200: ajv.compile<{
353355
ts: number;
354-
channel: string | string[];
356+
channel: string;
355357
message: IMessage;
356358
success: boolean;
357359
}>({
@@ -426,7 +428,7 @@ const chatPostMessageEndpoints = API.v1.post(
426428
},
427429
},
428430
async function action() {
429-
const { text, attachments } = this.bodyParams;
431+
const { text, attachments } = this.bodyParams as ChatPostMessage;
430432
const maxAllowedSize = settings.get<number>('Message_MaxAllowedSize') ?? 0;
431433

432434
if (text && text.length > maxAllowedSize) {
@@ -451,7 +453,7 @@ const chatPostMessageEndpoints = API.v1.post(
451453
return API.v1.failure('unknown-error');
452454
}
453455

454-
const [message] = await normalizeMessagesForUser([messageReturn.message], this.userId);
456+
const [message] = (await normalizeMessagesForUser([messageReturn.message], this.userId)) as IMessage[];
455457

456458
return API.v1.success({
457459
ts: Date.now(),

0 commit comments

Comments
 (0)