Skip to content

Commit 9fbed2e

Browse files
committed
refactor(api): migrate chat.postMessage endpoint to new API.v1 format
1 parent e41b3af commit 9fbed2e

File tree

3 files changed

+159
-144
lines changed

3 files changed

+159
-144
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@rocket.chat/rest-typings': minor
3+
'@rocket.chat/meteor': minor
4+
---
5+
6+
Migrate chat.postMessage endpoint to make it openAPI and AJV compatible

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

Lines changed: 153 additions & 11 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, IThreadMainMessage } from '@rocket.chat/core-typings';
2+
import type { IMessage, MessageAttachment, IThreadMainMessage } from '@rocket.chat/core-typings';
33
import { MessageTypes } from '@rocket.chat/message-types';
44
import { Messages, Users, Rooms, Subscriptions } from '@rocket.chat/models';
55
import {
@@ -11,7 +11,6 @@ import {
1111
isChatDeleteProps,
1212
isChatSyncMessagesProps,
1313
isChatGetMessageProps,
14-
isChatPostMessageProps,
1514
isChatSearchProps,
1615
isChatSendMessageProps,
1716
isChatIgnoreUserProps,
@@ -119,6 +118,132 @@ const ChatUnfollowMessageLocalSchema = {
119118
additionalProperties: false,
120119
};
121120

121+
//chat.postMessage starts
122+
type ChatPostMessage =
123+
| {
124+
roomId: string | string[];
125+
text?: string;
126+
alias?: string;
127+
emoji?: string;
128+
avatar?: string;
129+
attachments?: MessageAttachment[];
130+
customFields?: IMessage['customFields'];
131+
}
132+
| {
133+
channel: string | string[];
134+
text?: string;
135+
alias?: string;
136+
emoji?: string;
137+
avatar?: string;
138+
attachments?: MessageAttachment[];
139+
customFields?: IMessage['customFields'];
140+
};
141+
142+
const ChatPostMessageSchema = {
143+
oneOf: [
144+
{
145+
type: 'object',
146+
properties: {
147+
roomId: {
148+
oneOf: [
149+
{ type: 'string' },
150+
{
151+
type: 'array',
152+
items: {
153+
type: 'string',
154+
},
155+
},
156+
],
157+
},
158+
text: {
159+
type: 'string',
160+
nullable: true,
161+
},
162+
alias: {
163+
type: 'string',
164+
nullable: true,
165+
},
166+
emoji: {
167+
type: 'string',
168+
nullable: true,
169+
},
170+
avatar: {
171+
type: 'string',
172+
nullable: true,
173+
},
174+
attachments: {
175+
type: 'array',
176+
items: {
177+
type: 'object',
178+
},
179+
nullable: true,
180+
},
181+
tmid: {
182+
type: 'string',
183+
},
184+
customFields: {
185+
type: 'object',
186+
nullable: true,
187+
},
188+
parseUrls: {
189+
type: 'boolean',
190+
},
191+
},
192+
required: ['roomId'],
193+
additionalProperties: false,
194+
},
195+
{
196+
type: 'object',
197+
properties: {
198+
channel: {
199+
oneOf: [
200+
{ type: 'string' },
201+
{
202+
type: 'array',
203+
items: {
204+
type: 'string',
205+
},
206+
},
207+
],
208+
},
209+
text: {
210+
type: 'string',
211+
nullable: true,
212+
},
213+
alias: {
214+
type: 'string',
215+
nullable: true,
216+
},
217+
emoji: {
218+
type: 'string',
219+
nullable: true,
220+
},
221+
avatar: {
222+
type: 'string',
223+
nullable: true,
224+
},
225+
attachments: {
226+
type: 'array',
227+
items: {
228+
type: 'object',
229+
},
230+
nullable: true,
231+
},
232+
customFields: {
233+
type: 'object',
234+
nullable: true,
235+
},
236+
parseUrls: {
237+
type: 'boolean',
238+
},
239+
},
240+
required: ['channel'],
241+
additionalProperties: false,
242+
},
243+
],
244+
};
245+
//chat.postMessage ends
246+
122247
const isChatStarMessageLocalProps = ajv.compile<ChatStarMessageLocal>(ChatStarMessageLocalSchema);
123248

124249
const isChatUnstarMessageLocalProps = ajv.compile<ChatUnstarMessageLocal>(ChatUnstarMessageLocalSchema);
@@ -127,6 +252,8 @@ const isChatFollowMessageLocalProps = ajv.compile<ChatFollowMessageLocal>(ChatFo
127252

128253
const isChatUnfollowMessageLocalProps = ajv.compile<ChatUnfollowMessageLocal>(ChatUnfollowMessageLocalSchema);
129254

255+
const isChatPostMessageLocalProps = ajv.compile<ChatPostMessage>(ChatPostMessageSchema);
256+
130257
API.v1.addRoute(
131258
'chat.delete',
132259
{ authRequired: true, validateParams: isChatDeleteProps },
@@ -558,13 +685,29 @@ const chatEndpoints = API.v1
558685

559686
return API.v1.success();
560687
},
561-
);
562-
563-
API.v1.addRoute(
564-
'chat.postMessage',
565-
{ authRequired: true, validateParams: isChatPostMessageProps },
566-
{
567-
async post() {
688+
)
689+
.post(
690+
'chat.postMessage',
691+
{
692+
authRequired: true,
693+
body: isChatPostMessageLocalProps,
694+
response: {
695+
400: validateBadRequestErrorResponse,
696+
401: validateUnauthorizedErrorResponse,
697+
200: ajv.compile({
698+
type: 'object',
699+
properties: {
700+
ts: { type: 'number' },
701+
channel: { type: 'string' },
702+
message: { $ref: '#/components/schemas/IMessage' },
703+
success: { type: 'boolean', enum: [true] },
704+
},
705+
required: ['ts', 'channel', 'message', 'success'],
706+
additionalProperties: false,
707+
}),
708+
},
709+
},
710+
async function action() {
568711
const { text, attachments } = this.bodyParams;
569712
const maxAllowedSize = settings.get<number>('Message_MaxAllowedSize') ?? 0;
570713

@@ -594,8 +737,7 @@ API.v1.addRoute(
594737
message,
595738
});
596739
},
597-
},
598-
);
740+
);
599741

600742
API.v1.addRoute(
601743
'chat.search',

packages/rest-typings/src/v1/chat.ts

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -739,132 +739,6 @@ const ChatGetDeletedMessagesSchema = {
739739

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

742-
type ChatPostMessage =
743-
| {
744-
roomId: string | string[];
745-
text?: string;
746-
alias?: string;
747-
emoji?: string;
748-
avatar?: string;
749-
attachments?: MessageAttachment[];
750-
customFields?: IMessage['customFields'];
751-
}
752-
| {
753-
channel: string | string[];
754-
text?: string;
755-
alias?: string;
756-
emoji?: string;
757-
avatar?: string;
758-
attachments?: MessageAttachment[];
759-
customFields?: IMessage['customFields'];
760-
};
761-
762-
const ChatPostMessageSchema = {
763-
oneOf: [
764-
{
765-
type: 'object',
766-
properties: {
767-
roomId: {
768-
oneOf: [
769-
{ type: 'string' },
770-
{
771-
type: 'array',
772-
items: {
773-
type: 'string',
774-
},
775-
},
776-
],
777-
},
778-
text: {
779-
type: 'string',
780-
nullable: true,
781-
},
782-
alias: {
783-
type: 'string',
784-
nullable: true,
785-
},
786-
emoji: {
787-
type: 'string',
788-
nullable: true,
789-
},
790-
avatar: {
791-
type: 'string',
792-
nullable: true,
793-
},
794-
attachments: {
795-
type: 'array',
796-
items: {
797-
type: 'object',
798-
},
799-
nullable: true,
800-
},
801-
tmid: {
802-
type: 'string',
803-
},
804-
customFields: {
805-
type: 'object',
806-
nullable: true,
807-
},
808-
parseUrls: {
809-
type: 'boolean',
810-
},
811-
},
812-
required: ['roomId'],
813-
additionalProperties: false,
814-
},
815-
{
816-
type: 'object',
817-
properties: {
818-
channel: {
819-
oneOf: [
820-
{ type: 'string' },
821-
{
822-
type: 'array',
823-
items: {
824-
type: 'string',
825-
},
826-
},
827-
],
828-
},
829-
text: {
830-
type: 'string',
831-
nullable: true,
832-
},
833-
alias: {
834-
type: 'string',
835-
nullable: true,
836-
},
837-
emoji: {
838-
type: 'string',
839-
nullable: true,
840-
},
841-
avatar: {
842-
type: 'string',
843-
nullable: true,
844-
},
845-
attachments: {
846-
type: 'array',
847-
items: {
848-
type: 'object',
849-
},
850-
nullable: true,
851-
},
852-
customFields: {
853-
type: 'object',
854-
nullable: true,
855-
},
856-
parseUrls: {
857-
type: 'boolean',
858-
},
859-
},
860-
required: ['channel'],
861-
additionalProperties: false,
862-
},
863-
],
864-
};
865-
866-
export const isChatPostMessageProps = ajv.compile<ChatPostMessage>(ChatPostMessageSchema);
867-
868742
type ChatGetURLPreview = {
869743
roomId: IRoom['_id'];
870744
url: string;
@@ -982,13 +856,6 @@ export type ChatEndpoints = {
982856
};
983857
};
984858
};
985-
'/v1/chat.postMessage': {
986-
POST: (params: ChatPostMessage) => {
987-
ts: number;
988-
channel: IRoom;
989-
message: IMessage;
990-
};
991-
};
992859
'/v1/chat.syncThreadMessages': {
993860
GET: (params: ChatSyncThreadMessages) => {
994861
messages: {

0 commit comments

Comments
 (0)