Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/healthy-dolls-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": minor
"@rocket.chat/rest-typings": minor
---

Add OpenAPI support for the Rocket.Chat chat.postMessage API endpoints by migrating to a modern chained route definition syntax and utilizing shared AJV schemas for validation to enhance API documentation and ensure type safety through response validation.
166 changes: 161 additions & 5 deletions apps/meteor/app/api/server/v1/chat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Message } from '@rocket.chat/core-services';
import type { IMessage, IThreadMainMessage } from '@rocket.chat/core-typings';
import type { IMessage, IUser, IThreadMainMessage, MessageAttachment, RequiredField } from '@rocket.chat/core-typings';
import { MessageTypes } from '@rocket.chat/message-types';
import { Messages, Users, Rooms, Subscriptions } from '@rocket.chat/models';
import {
Expand All @@ -11,7 +11,6 @@ import {
isChatDeleteProps,
isChatSyncMessagesProps,
isChatGetMessageProps,
isChatPostMessageProps,
isChatSearchProps,
isChatSendMessageProps,
isChatIgnoreUserProps,
Expand Down Expand Up @@ -247,6 +246,26 @@ type ChatUnpinMessage = {
messageId: IMessage['_id'];
};

type ChatPostMessage =
| {
roomId: string | string[];
text?: string;
alias?: string;
emoji?: string;
avatar?: string;
attachments?: MessageAttachment[];
customFields?: IMessage['customFields'];
}
| {
channel: string | string[];
text?: string;
alias?: string;
emoji?: string;
avatar?: string;
attachments?: MessageAttachment[];
customFields?: IMessage['customFields'];
};

const ChatPinMessageSchema = {
type: 'object',
properties: {
Expand All @@ -271,10 +290,116 @@ const ChatUnpinMessageSchema = {
additionalProperties: false,
};

const ChatPostMessageSchema = {
oneOf: [
{
type: 'object',
properties: {
roomId: {
oneOf: [
{ type: 'string' },
{
type: 'array',
items: {
type: 'string',
},
},
],
},
text: {
type: 'string',
nullable: true,
},
alias: {
type: 'string',
nullable: true,
},
emoji: {
type: 'string',
nullable: true,
},
avatar: {
type: 'string',
nullable: true,
},
attachments: {
type: 'array',
items: {
type: 'object',
},
nullable: true,
},
tmid: {
type: 'string',
},
customFields: {
type: 'object',
nullable: true,
},
parseUrls: {
type: 'boolean',
},
},
required: ['roomId'],
additionalProperties: false,
},
{
type: 'object',
properties: {
channel: {
oneOf: [
{ type: 'string' },
{
type: 'array',
items: {
type: 'string',
},
},
],
},
text: {
type: 'string',
nullable: true,
},
alias: {
type: 'string',
nullable: true,
},
emoji: {
type: 'string',
nullable: true,
},
avatar: {
type: 'string',
nullable: true,
},
attachments: {
type: 'array',
items: {
type: 'object',
},
nullable: true,
},
customFields: {
type: 'object',
nullable: true,
},
parseUrls: {
type: 'boolean',
},
},
required: ['channel'],
additionalProperties: false,
},
],
};

const isChatPinMessageProps = ajv.compile<ChatPinMessage>(ChatPinMessageSchema);

const isChatUnpinMessageProps = ajv.compile<ChatUnpinMessage>(ChatUnpinMessageSchema);

const isChatPostMessageProps = ajv.compile<ChatPostMessage>(ChatPostMessageSchema);

const chatEndpoints = API.v1
.post(
'chat.pinMessage',
Expand Down Expand Up @@ -420,6 +545,30 @@ const chatEndpoints = API.v1
},
)
.post(
'chat.postMessage',
{
authRequired: true,
body: isChatPostMessageProps,
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
200: ajv.compile<{
ts: number;
channel: string;
message: IMessage;
}>({
type: 'object',
properties: {
ts: { type: 'number' },
channel: { type: 'string' },
message: { $ref: '#/components/schemas/IMessage' },
success: {
type: 'boolean',
enum: [true],
description: 'Indicates if the request was successful.',
},
},
required: ['ts', 'channel', 'message', 'success'],
'chat.starMessage',
{
authRequired: true,
Expand Down Expand Up @@ -580,7 +729,15 @@ API.v1.addRoute(
}
}

const messageReturn = (await applyAirGappedRestrictionsValidation(() => processWebhookMessage(this.bodyParams, this.user)))[0];
if (!this.user.username) {
return API.v1.failure('Invalid user');
}

const messageReturn = (
await applyAirGappedRestrictionsValidation(() =>
processWebhookMessage({ ...this.bodyParams, separateResponse: true }, this.user as RequiredField<IUser, 'username'>),
)
)[0];

if (!messageReturn?.message) {
return API.v1.failure('unknown-error');
Expand All @@ -594,8 +751,7 @@ API.v1.addRoute(
message,
});
},
},
);
);

API.v1.addRoute(
'chat.search',
Expand Down
135 changes: 1 addition & 134 deletions packages/rest-typings/src/v1/chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IMessage, IRoom, MessageAttachment, IReadReceiptWithUser, MessageUrl, IThreadMainMessage } from '@rocket.chat/core-typings';
import type { IMessage, IRoom, IReadReceiptWithUser, MessageUrl, IThreadMainMessage } from '@rocket.chat/core-typings';

import { ajv } from './Ajv';
import type { PaginatedRequest } from '../helpers/PaginatedRequest';
Expand Down Expand Up @@ -739,132 +739,6 @@ const ChatGetDeletedMessagesSchema = {

export const isChatGetDeletedMessagesProps = ajv.compile<ChatGetDeletedMessages>(ChatGetDeletedMessagesSchema);

type ChatPostMessage =
| {
roomId: string | string[];
text?: string;
alias?: string;
emoji?: string;
avatar?: string;
attachments?: MessageAttachment[];
customFields?: IMessage['customFields'];
}
| {
channel: string | string[];
text?: string;
alias?: string;
emoji?: string;
avatar?: string;
attachments?: MessageAttachment[];
customFields?: IMessage['customFields'];
};

const ChatPostMessageSchema = {
oneOf: [
{
type: 'object',
properties: {
roomId: {
oneOf: [
{ type: 'string' },
{
type: 'array',
items: {
type: 'string',
},
},
],
},
text: {
type: 'string',
nullable: true,
},
alias: {
type: 'string',
nullable: true,
},
emoji: {
type: 'string',
nullable: true,
},
avatar: {
type: 'string',
nullable: true,
},
attachments: {
type: 'array',
items: {
type: 'object',
},
nullable: true,
},
tmid: {
type: 'string',
},
customFields: {
type: 'object',
nullable: true,
},
parseUrls: {
type: 'boolean',
},
},
required: ['roomId'],
additionalProperties: false,
},
{
type: 'object',
properties: {
channel: {
oneOf: [
{ type: 'string' },
{
type: 'array',
items: {
type: 'string',
},
},
],
},
text: {
type: 'string',
nullable: true,
},
alias: {
type: 'string',
nullable: true,
},
emoji: {
type: 'string',
nullable: true,
},
avatar: {
type: 'string',
nullable: true,
},
attachments: {
type: 'array',
items: {
type: 'object',
},
nullable: true,
},
customFields: {
type: 'object',
nullable: true,
},
parseUrls: {
type: 'boolean',
},
},
required: ['channel'],
additionalProperties: false,
},
],
};

export const isChatPostMessageProps = ajv.compile<ChatPostMessage>(ChatPostMessageSchema);

type ChatGetURLPreview = {
roomId: IRoom['_id'];
url: string;
Expand Down Expand Up @@ -982,13 +856,6 @@ export type ChatEndpoints = {
};
};
};
'/v1/chat.postMessage': {
POST: (params: ChatPostMessage) => {
ts: number;
channel: IRoom;
message: IMessage;
};
};
'/v1/chat.syncThreadMessages': {
GET: (params: ChatSyncThreadMessages) => {
messages: {
Expand Down