44import type { IMessage , IRoom , ISubscription , IUser } from '@rocket.chat/core-typings' ;
55import { Subscriptions , Uploads , Messages , Rooms , Users } from '@rocket.chat/models' ;
66import {
7- isDmDeleteProps ,
7+ ajv ,
8+ validateUnauthorizedErrorResponse ,
9+ validateBadRequestErrorResponse ,
810 isDmFileProps ,
911 isDmMemberProps ,
1012 isDmMessagesProps ,
@@ -26,7 +28,9 @@ import { getRoomByNameOrIdWithOptionToJoin } from '../../../lib/server/functions
2628import { getChannelHistory } from '../../../lib/server/methods/getChannelHistory' ;
2729import { settings } from '../../../settings/server' ;
2830import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser' ;
31+ import type { ExtractRoutesFromAPI } from '../ApiClass' ;
2932import { API } from '../api' ;
33+ import type { TypedAction } from '../definition' ;
3034import { addUserToFileObj } from '../helpers/addUserToFileObj' ;
3135import { composeRoomWithLastMessage } from '../helpers/composeRoomWithLastMessage' ;
3236import { getPaginationItems } from '../helpers/getPaginationItems' ;
@@ -87,28 +91,78 @@ API.v1.addRoute(
8791 } ,
8892) ;
8993
90- API . v1 . addRoute (
91- [ 'dm.delete' , 'im.delete' ] ,
92- {
93- authRequired : true ,
94- validateParams : isDmDeleteProps ,
94+ type DmDeleteProps =
95+ | {
96+ roomId : string ;
97+ }
98+ | {
99+ username : string ;
100+ } ;
101+
102+ const isDmDeleteProps = ajv . compile < DmDeleteProps > ( {
103+ oneOf : [
104+ {
105+ type : 'object' ,
106+ properties : {
107+ roomId : {
108+ type : 'string' ,
109+ } ,
110+ } ,
111+ required : [ 'roomId' ] ,
112+ additionalProperties : false ,
113+ } ,
114+ {
115+ type : 'object' ,
116+ properties : {
117+ username : {
118+ type : 'string' ,
119+ } ,
120+ } ,
121+ required : [ 'username' ] ,
122+ additionalProperties : false ,
123+ } ,
124+ ] ,
125+ } ) ;
126+
127+ const dmDeleteEndpointsProps = {
128+ authRequired : true ,
129+ body : isDmDeleteProps ,
130+ response : {
131+ 400 : validateBadRequestErrorResponse ,
132+ 401 : validateUnauthorizedErrorResponse ,
133+ 200 : ajv . compile < void > ( {
134+ type : 'object' ,
135+ properties : {
136+ success : {
137+ type : 'boolean' ,
138+ enum : [ true ] ,
139+ } ,
140+ } ,
141+ required : [ 'success' ] ,
142+ additionalProperties : false ,
143+ } ) ,
95144 } ,
96- {
97- async post ( ) {
98- const { room } = await findDirectMessageRoom ( this . bodyParams , this . userId ) ;
145+ } as const ;
99146
100- const canAccess =
101- ( await canAccessRoomIdAsync ( room . _id , this . userId ) ) || ( await hasPermissionAsync ( this . userId , 'view-room-administration' ) ) ;
102- if ( ! canAccess ) {
103- throw new Meteor . Error ( 'error-not-allowed' , 'Not allowed' ) ;
104- }
147+ const dmDeleteAction = < Path extends string > ( _path : Path ) : TypedAction < typeof dmDeleteEndpointsProps , Path > =>
148+ async function action ( ) {
149+ const { room } = await findDirectMessageRoom ( this . bodyParams , this . userId ) ;
105150
106- await eraseRoom ( room . _id , this . userId ) ;
151+ const canAccess =
152+ ( await canAccessRoomIdAsync ( room . _id , this . userId ) ) || ( await hasPermissionAsync ( this . userId , 'view-room-administration' ) ) ;
107153
108- return API . v1 . success ( ) ;
109- } ,
110- } ,
111- ) ;
154+ if ( ! canAccess ) {
155+ throw new Meteor . Error ( 'error-not-allowed' , 'Not allowed' ) ;
156+ }
157+
158+ await eraseRoom ( room . _id , this . userId ) ;
159+
160+ return API . v1 . success ( ) ;
161+ } ;
162+
163+ const dmEndpoints = API . v1
164+ . post ( 'im.delete' , dmDeleteEndpointsProps , dmDeleteAction ( 'im.delete' ) )
165+ . post ( 'dm.delete' , dmDeleteEndpointsProps , dmDeleteAction ( 'dm.delete' ) ) ;
112166
113167API . v1 . addRoute (
114168 [ 'dm.close' , 'im.close' ] ,
@@ -589,3 +643,10 @@ API.v1.addRoute(
589643 } ,
590644 } ,
591645) ;
646+
647+ export type DmEndpoints = ExtractRoutesFromAPI < typeof dmEndpoints > ;
648+
649+ declare module '@rocket.chat/rest-typings' {
650+ // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
651+ interface Endpoints extends DmEndpoints { }
652+ }
0 commit comments