@@ -2,14 +2,14 @@ import { Message } from '@rocket.chat/core-services';
22import type { IMessage , IThreadMainMessage } from '@rocket.chat/core-typings' ;
33import { Messages , Users , Rooms , Subscriptions } from '@rocket.chat/models' ;
44import {
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';
5656import { unfollowMessage } from '../../../threads/server/methods/unfollowMessage' ;
5757import { MessageTypes } from '../../../ui-utils/server' ;
5858import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser' ;
59+ import type { ExtractRoutesFromAPI } from '../ApiClass' ;
5960import { API } from '../api' ;
6061import { getPaginationItems } from '../helpers/getPaginationItems' ;
6162import { 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+ }
0 commit comments