Skip to content

Commit 290c1aa

Browse files
refactor: update ChatPinMessage to the new pattern
1 parent 8f7076b commit 290c1aa

File tree

3 files changed

+88
-38
lines changed

3 files changed

+88
-38
lines changed

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

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { Message } from '@rocket.chat/core-services';
22
import type { IMessage, IThreadMainMessage } from '@rocket.chat/core-typings';
33
import { Messages, Users, Rooms, Subscriptions } from '@rocket.chat/models';
44
import {
5+
ajv,
56
isChatReportMessageProps,
67
isChatGetURLPreviewProps,
78
isChatUpdateProps,
89
isChatGetThreadsListProps,
910
isChatDeleteProps,
1011
isChatSyncMessagesProps,
1112
isChatGetMessageProps,
12-
isChatPinMessageProps,
1313
isChatPostMessageProps,
1414
isChatSearchProps,
1515
isChatSendMessageProps,
@@ -56,6 +56,7 @@ import { followMessage } from '../../../threads/server/methods/followMessage';
5656
import { unfollowMessage } from '../../../threads/server/methods/unfollowMessage';
5757
import { MessageTypes } from '../../../ui-utils/server';
5858
import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser';
59+
import type { ExtractRoutesFromAPI } from '../ApiClass';
5960
import { API } from '../api';
6061
import { getPaginationItems } from '../helpers/getPaginationItems';
6162
import { findDiscussionsFromRoom, findMentionedMessages, findStarredMessages } from '../lib/messages';
@@ -172,25 +173,87 @@ API.v1.addRoute(
172173
},
173174
);
174175

175-
API.v1.addRoute(
176+
type ChatPinMessage = {
177+
messageId: IMessage['_id'];
178+
};
179+
180+
const ChatPinMessageSchema = {
181+
type: 'object',
182+
properties: {
183+
messageId: {
184+
type: 'string',
185+
minLength: 1,
186+
},
187+
},
188+
required: ['messageId'],
189+
additionalProperties: false,
190+
};
191+
192+
const isChatPinMessageProps = ajv.compile<ChatPinMessage>(ChatPinMessageSchema);
193+
194+
const chatPinMessageEndpoints = API.v1.post(
176195
'chat.pinMessage',
177-
{ authRequired: true, validateParams: isChatPinMessageProps },
178196
{
179-
async post() {
180-
const msg = await Messages.findOneById(this.bodyParams.messageId);
197+
authRequired: true,
198+
body: isChatPinMessageProps,
199+
response: {
200+
400: ajv.compile<{
201+
error?: string;
202+
errorType?: string;
203+
stack?: string;
204+
details?: string;
205+
}>({
206+
type: 'object',
207+
properties: {
208+
success: { type: 'boolean', enum: [false] },
209+
stack: { type: 'string' },
210+
error: { type: 'string' },
211+
errorType: { type: 'string' },
212+
details: { type: 'string' },
213+
},
214+
required: ['success'],
215+
additionalProperties: false,
216+
}),
217+
401: ajv.compile({
218+
type: 'object',
219+
properties: {
220+
success: { type: 'boolean', enum: [false] },
221+
status: { type: 'string' },
222+
message: { type: 'string' },
223+
error: { type: 'string' },
224+
errorType: { type: 'string' },
225+
},
226+
required: ['success'],
227+
additionalProperties: false,
228+
}),
229+
200: ajv.compile<{ message: IMessage }>({
230+
type: 'object',
231+
properties: {
232+
message: { $ref: '#/components/schemas/IMessage' },
233+
success: {
234+
type: 'boolean',
235+
enum: [true],
236+
},
237+
},
238+
required: ['message', 'success'],
239+
additionalProperties: false,
240+
}),
241+
},
242+
},
243+
async function action() {
244+
const msg = await Messages.findOneById(this.bodyParams.messageId);
181245

182-
if (!msg) {
183-
throw new Meteor.Error('error-message-not-found', 'The provided "messageId" does not match any existing message.');
184-
}
246+
if (!msg) {
247+
throw new Meteor.Error('error-message-not-found', 'The provided "messageId" does not match any existing message.');
248+
}
185249

186-
const pinnedMessage = await pinMessage(msg, this.userId);
250+
const pinnedMessage = await pinMessage(msg, this.userId);
187251

188-
const [message] = await normalizeMessagesForUser([pinnedMessage], this.userId);
252+
const [message] = await normalizeMessagesForUser([pinnedMessage], this.userId);
189253

190-
return API.v1.success({
191-
message,
192-
});
193-
},
254+
return API.v1.success({
255+
message,
256+
});
194257
},
195258
);
196259

@@ -845,3 +908,12 @@ API.v1.addRoute(
845908
},
846909
},
847910
);
911+
912+
type ChatPinMessageEndpoints = ExtractRoutesFromAPI<typeof chatPinMessageEndpoints>;
913+
914+
export type ChatEndpoints = ChatPinMessageEndpoints;
915+
916+
declare module '@rocket.chat/rest-typings' {
917+
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
918+
interface Endpoints extends ChatPinMessageEndpoints {}
919+
}

packages/core-typings/src/Ajv.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import typia from 'typia';
22

33
import type { ICustomSound } from './ICustomSound';
44
import type { IInvite } from './IInvite';
5+
import type { IMessage } from './IMessage';
56
import type { ISubscription } from './ISubscription';
67

7-
export const schemas = typia.json.schemas<[ISubscription | IInvite | ICustomSound], '3.0'>();
8+
export const schemas = typia.json.schemas<[ISubscription | IInvite | ICustomSound | IMessage], '3.0'>();

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,6 @@ const ChatUnstarMessageSchema = {
175175

176176
export const isChatUnstarMessageProps = ajv.compile<ChatUnstarMessage>(ChatUnstarMessageSchema);
177177

178-
type ChatPinMessage = {
179-
messageId: IMessage['_id'];
180-
};
181-
182-
const ChatPinMessageSchema = {
183-
type: 'object',
184-
properties: {
185-
messageId: {
186-
type: 'string',
187-
minLength: 1,
188-
},
189-
},
190-
required: ['messageId'],
191-
additionalProperties: false,
192-
};
193-
194-
export const isChatPinMessageProps = ajv.compile<ChatPinMessage>(ChatPinMessageSchema);
195-
196178
type ChatUnpinMessage = {
197179
messageId: IMessage['_id'];
198180
};
@@ -952,11 +934,6 @@ export type ChatEndpoints = {
952934
'/v1/chat.unStarMessage': {
953935
POST: (params: ChatUnstarMessage) => void;
954936
};
955-
'/v1/chat.pinMessage': {
956-
POST: (params: ChatPinMessage) => {
957-
message: IMessage;
958-
};
959-
};
960937
'/v1/chat.unPinMessage': {
961938
POST: (params: ChatUnpinMessage) => void;
962939
};

0 commit comments

Comments
 (0)