@@ -2,8 +2,18 @@ import fs from 'fs';
2
2
import path from 'path' ;
3
3
4
4
import type { Credentials } from '@rocket.chat/api-client' ;
5
- import type { IMessage , IRole , IRoom , ITeam , IUpload , IUser , ImageAttachmentProps , SettingValue } from '@rocket.chat/core-typings' ;
6
- import { TEAM_TYPE } from '@rocket.chat/core-typings' ;
5
+ import type {
6
+ IMessage ,
7
+ IRole ,
8
+ IRoom ,
9
+ ITeam ,
10
+ IUpload ,
11
+ IUser ,
12
+ ImageAttachmentProps ,
13
+ MessageAttachment ,
14
+ SettingValue ,
15
+ } from '@rocket.chat/core-typings' ;
16
+ import { isFileAttachment , isQuoteAttachment , TEAM_TYPE } from '@rocket.chat/core-typings' ;
7
17
import { Random } from '@rocket.chat/random' ;
8
18
import { assert , expect } from 'chai' ;
9
19
import { after , afterEach , before , beforeEach , describe , it } from 'mocha' ;
@@ -1176,6 +1186,116 @@ describe('[Rooms]', () => {
1176
1186
} )
1177
1187
. end ( done ) ;
1178
1188
} ) ;
1189
+
1190
+ it ( 'should remove only files and file attachments when filesOnly is set to true' , async ( ) => {
1191
+ const message1Response = await sendSimpleMessage ( { roomId : publicChannel . _id } ) ;
1192
+
1193
+ const mediaUploadResponse = await request
1194
+ . post ( api ( `rooms.media/${ publicChannel . _id } ` ) )
1195
+ . set ( credentials )
1196
+ . attach ( 'file' , imgURL )
1197
+ . expect ( 200 ) ;
1198
+
1199
+ const message2Response = await request
1200
+ . post ( api ( `rooms.mediaConfirm/${ publicChannel . _id } /${ mediaUploadResponse . body . file . _id } ` ) )
1201
+ . set ( credentials )
1202
+ . send ( { msg : 'message with file only' } )
1203
+ . expect ( 200 ) ;
1204
+
1205
+ await request
1206
+ . post ( api ( 'rooms.cleanHistory' ) )
1207
+ . set ( credentials )
1208
+ . send ( {
1209
+ roomId : publicChannel . _id ,
1210
+ latest : '9999-12-31T23:59:59.000Z' ,
1211
+ oldest : '0001-01-01T00:00:00.000Z' ,
1212
+ filesOnly : true ,
1213
+ } )
1214
+ . expect ( 200 ) ;
1215
+
1216
+ const res = await request . get ( api ( 'channels.messages' ) ) . set ( credentials ) . query ( { roomId : publicChannel . _id } ) . expect ( 200 ) ;
1217
+
1218
+ expect ( res . body . messages ) . to . be . an ( 'array' ) ;
1219
+ const messageIds = res . body . messages . map ( ( m : IMessage ) => m . _id ) ;
1220
+ expect ( messageIds ) . to . contain ( message1Response . body . message . _id ) ;
1221
+ expect ( messageIds ) . to . contain ( message2Response . body . message . _id ) ;
1222
+ const cleanedMessage = res . body . messages . find ( ( m : { _id : any } ) => m . _id === message2Response . body . message . _id ) ;
1223
+ expect ( cleanedMessage ) . to . exist ;
1224
+ expect ( cleanedMessage . file ) . to . be . undefined ;
1225
+ expect ( cleanedMessage . files ?. length ?? 0 ) . to . equal ( 0 ) ;
1226
+ expect ( ( cleanedMessage . attachments ?? [ ] ) . find ( ( a : MessageAttachment ) => isFileAttachment ( a ) ) ) . to . be . undefined ;
1227
+
1228
+ await request
1229
+ . get ( api ( 'channels.files' ) )
1230
+ . set ( credentials )
1231
+ . query ( {
1232
+ roomId : publicChannel . _id ,
1233
+ } )
1234
+ . expect ( 'Content-Type' , 'application/json' )
1235
+ . expect ( 200 )
1236
+ . expect ( ( res ) => {
1237
+ expect ( res . body ) . to . have . property ( 'success' , true ) ;
1238
+ expect ( res . body ) . to . have . property ( 'files' ) . and . to . be . an ( 'array' ) ;
1239
+ expect ( res . body . files ) . to . have . lengthOf ( 0 ) ;
1240
+ } ) ;
1241
+ } ) ;
1242
+
1243
+ it ( 'should not remove quote attachments when filesOnly is set to true' , async ( ) => {
1244
+ const siteUrl = await getSettingValueById ( 'Site_Url' ) ;
1245
+ const message1Response = await sendSimpleMessage ( { roomId : publicChannel . _id } ) ;
1246
+ const mediaResponse = await request
1247
+ . post ( api ( `rooms.media/${ publicChannel . _id } ` ) )
1248
+ . set ( credentials )
1249
+ . attach ( 'file' , imgURL )
1250
+ . expect ( 'Content-Type' , 'application/json' )
1251
+ . expect ( 200 ) ;
1252
+
1253
+ const message2Response = await request
1254
+ . post ( api ( `rooms.mediaConfirm/${ publicChannel . _id } /${ mediaResponse . body . file . _id } ` ) )
1255
+ . set ( credentials )
1256
+ . send ( {
1257
+ msg : new URL ( `/${ publicChannel . fname } ?msg=${ message1Response . body . message . _id } ` , siteUrl as string ) . toString ( ) ,
1258
+ } )
1259
+ . expect ( 200 ) ;
1260
+
1261
+ await request
1262
+ . post ( api ( 'rooms.cleanHistory' ) )
1263
+ . set ( credentials )
1264
+ . send ( {
1265
+ roomId : publicChannel . _id ,
1266
+ latest : '9999-12-31T23:59:59.000Z' ,
1267
+ oldest : '0001-01-01T00:00:00.000Z' ,
1268
+ filesOnly : true ,
1269
+ } )
1270
+ . expect ( 'Content-Type' , 'application/json' )
1271
+ . expect ( 200 )
1272
+ . expect ( ( res ) => {
1273
+ expect ( res . body ) . to . have . property ( 'success' , true ) ;
1274
+ } ) ;
1275
+
1276
+ await request
1277
+ . get ( api ( 'channels.messages' ) )
1278
+ . set ( credentials )
1279
+ . query ( {
1280
+ roomId : publicChannel . _id ,
1281
+ } )
1282
+ . expect ( 'Content-Type' , 'application/json' )
1283
+ . expect ( 200 )
1284
+ . expect ( ( res ) => {
1285
+ expect ( res . body ) . to . have . property ( 'success' , true ) ;
1286
+ expect ( res . body ) . to . have . property ( 'messages' ) . and . to . be . an ( 'array' ) ;
1287
+ const message = ( res . body . messages . find ( ( m : { _id : any } ) => m . _id === message2Response . body . message . _id ) as IMessage ) || null ;
1288
+ expect ( message ) . not . to . be . null ;
1289
+ expect ( message ) . to . have . property ( 'attachments' ) ;
1290
+ const fileAttachment = message . attachments ?. find ( ( f ) => isFileAttachment ( f ) ) || null ;
1291
+ expect ( fileAttachment , 'Expected file attachments to be removed' ) . to . be . null ;
1292
+ const quoteAttachment = message . attachments ?. find ( ( f ) => isQuoteAttachment ( f ) ) || null ;
1293
+ expect ( quoteAttachment , 'Expected quote attachments to be present' ) . not . to . be . null ;
1294
+ expect ( message . file ) . to . be . undefined ;
1295
+ expect ( message . files ) . to . satisfy ( ( files : IMessage [ 'files' ] ) => files === undefined || files . length === 0 ) ;
1296
+ } ) ;
1297
+ } ) ;
1298
+
1179
1299
it ( 'should return success when send a valid private channel' , ( done ) => {
1180
1300
void request
1181
1301
. post ( api ( 'rooms.cleanHistory' ) )
0 commit comments