Skip to content

Commit 2c5740f

Browse files
committed
chore: migrate chat.starMessage and chat.unStarMessage to OpenAPI
1 parent 3c30636 commit 2c5740f

File tree

3 files changed

+116
-86
lines changed

3 files changed

+116
-86
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@rocket.chat/meteor": minor
3+
"@rocket.chat/rest-typings": minor
4+
---
5+
6+
Add OpenAPI support for the chat.starMessage and chat.unStarMessage API endpoints by migrating to a modern chained route definition syntax and utilizing AJV schemas for body and response validation.

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

Lines changed: 110 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import {
1414
isChatPostMessageProps,
1515
isChatSearchProps,
1616
isChatSendMessageProps,
17-
isChatStarMessageProps,
18-
isChatUnstarMessageProps,
1917
isChatIgnoreUserProps,
2018
isChatGetPinnedMessagesProps,
2119
isChatFollowMessageProps,
@@ -59,6 +57,42 @@ import { API } from '../api';
5957
import { getPaginationItems } from '../helpers/getPaginationItems';
6058
import { findDiscussionsFromRoom, findMentionedMessages, findStarredMessages } from '../lib/messages';
6159

60+
type ChatStarMessageLocal = {
61+
messageId: IMessage['_id'];
62+
};
63+
64+
type ChatUnstarMessageLocal = {
65+
messageId: IMessage['_id'];
66+
};
67+
68+
const ChatStarMessageLocalSchema = {
69+
type: 'object',
70+
properties: {
71+
messageId: {
72+
type: 'string',
73+
minLength: 1,
74+
},
75+
},
76+
required: ['messageId'],
77+
additionalProperties: false,
78+
};
79+
80+
const ChatUnstarMessageLocalSchema = {
81+
type: 'object',
82+
properties: {
83+
messageId: {
84+
type: 'string',
85+
minLength: 1,
86+
},
87+
},
88+
required: ['messageId'],
89+
additionalProperties: false,
90+
};
91+
92+
const isChatStarMessageLocalProps = ajv.compile<ChatStarMessageLocal>(ChatStarMessageLocalSchema);
93+
94+
const isChatUnstarMessageLocalProps = ajv.compile<ChatUnstarMessageLocal>(ChatUnstarMessageLocalSchema);
95+
6296
API.v1.addRoute(
6397
'chat.delete',
6498
{ authRequired: true, validateParams: isChatDeleteProps },
@@ -350,6 +384,80 @@ const chatEndpoints = API.v1
350384
message,
351385
});
352386
},
387+
)
388+
.post(
389+
'chat.starMessage',
390+
{
391+
authRequired: true,
392+
body: isChatStarMessageLocalProps,
393+
response: {
394+
400: validateBadRequestErrorResponse,
395+
401: validateUnauthorizedErrorResponse,
396+
200: ajv.compile<void>({
397+
type: 'object',
398+
properties: {
399+
success: {
400+
type: 'boolean',
401+
enum: [true],
402+
},
403+
},
404+
required: ['success'],
405+
additionalProperties: false,
406+
}),
407+
},
408+
},
409+
async function action() {
410+
const msg = await Messages.findOneById(this.bodyParams.messageId);
411+
412+
if (!msg) {
413+
throw new Meteor.Error('error-message-not-found', 'The provided "messageId" does not match any existing message.');
414+
}
415+
416+
await starMessage(this.user, {
417+
_id: msg._id,
418+
rid: msg.rid,
419+
starred: true,
420+
});
421+
422+
return API.v1.success();
423+
},
424+
)
425+
.post(
426+
'chat.unStarMessage',
427+
{
428+
authRequired: true,
429+
body: isChatUnstarMessageLocalProps,
430+
response: {
431+
400: validateBadRequestErrorResponse,
432+
401: validateUnauthorizedErrorResponse,
433+
200: ajv.compile<void>({
434+
type: 'object',
435+
properties: {
436+
success: {
437+
type: 'boolean',
438+
enum: [true],
439+
},
440+
},
441+
required: ['success'],
442+
additionalProperties: false,
443+
}),
444+
},
445+
},
446+
async function action() {
447+
const msg = await Messages.findOneById(this.bodyParams.messageId);
448+
449+
if (!msg) {
450+
throw new Meteor.Error('error-message-not-found', 'The provided "messageId" does not match any existing message.');
451+
}
452+
453+
await starMessage(this.user, {
454+
_id: msg._id,
455+
rid: msg.rid,
456+
starred: false,
457+
});
458+
459+
return API.v1.success();
460+
},
353461
);
354462

355463
API.v1.addRoute(
@@ -445,50 +553,6 @@ API.v1.addRoute(
445553
},
446554
);
447555

448-
API.v1.addRoute(
449-
'chat.starMessage',
450-
{ authRequired: true, validateParams: isChatStarMessageProps },
451-
{
452-
async post() {
453-
const msg = await Messages.findOneById(this.bodyParams.messageId);
454-
455-
if (!msg) {
456-
throw new Meteor.Error('error-message-not-found', 'The provided "messageId" does not match any existing message.');
457-
}
458-
459-
await starMessage(this.user, {
460-
_id: msg._id,
461-
rid: msg.rid,
462-
starred: true,
463-
});
464-
465-
return API.v1.success();
466-
},
467-
},
468-
);
469-
470-
API.v1.addRoute(
471-
'chat.unStarMessage',
472-
{ authRequired: true, validateParams: isChatUnstarMessageProps },
473-
{
474-
async post() {
475-
const msg = await Messages.findOneById(this.bodyParams.messageId);
476-
477-
if (!msg) {
478-
throw new Meteor.Error('error-message-not-found', 'The provided "messageId" does not match any existing message.');
479-
}
480-
481-
await starMessage(this.user, {
482-
_id: msg._id,
483-
rid: msg.rid,
484-
starred: false,
485-
});
486-
487-
return API.v1.success();
488-
},
489-
},
490-
);
491-
492556
API.v1.addRoute(
493557
'chat.react',
494558
{ authRequired: true, validateParams: isChatReactProps },

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

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -133,40 +133,6 @@ const ChatGetMessageSchema = {
133133

134134
export const isChatGetMessageProps = ajv.compile<ChatGetMessage>(ChatGetMessageSchema);
135135

136-
type ChatStarMessage = {
137-
messageId: IMessage['_id'];
138-
};
139-
140-
const ChatStarMessageSchema = {
141-
type: 'object',
142-
properties: {
143-
messageId: {
144-
type: 'string',
145-
},
146-
},
147-
required: ['messageId'],
148-
additionalProperties: false,
149-
};
150-
151-
export const isChatStarMessageProps = ajv.compile<ChatStarMessage>(ChatStarMessageSchema);
152-
153-
type ChatUnstarMessage = {
154-
messageId: IMessage['_id'];
155-
};
156-
157-
const ChatUnstarMessageSchema = {
158-
type: 'object',
159-
properties: {
160-
messageId: {
161-
type: 'string',
162-
},
163-
},
164-
required: ['messageId'],
165-
additionalProperties: false,
166-
};
167-
168-
export const isChatUnstarMessageProps = ajv.compile<ChatUnstarMessage>(ChatUnstarMessageSchema);
169-
170136
type ChatGetDiscussions = PaginatedRequest<{
171137
roomId: IRoom['_id'];
172138
text?: string;
@@ -973,12 +939,6 @@ export type ChatEndpoints = {
973939
'/v1/chat.unfollowMessage': {
974940
POST: (params: ChatUnfollowMessage) => void;
975941
};
976-
'/v1/chat.starMessage': {
977-
POST: (params: ChatStarMessage) => void;
978-
};
979-
'/v1/chat.unStarMessage': {
980-
POST: (params: ChatUnstarMessage) => void;
981-
};
982942
'/v1/chat.reportMessage': {
983943
POST: (params: ChatReportMessage) => void;
984944
};

0 commit comments

Comments
 (0)