[FIX] New specific endpoint for contactChatHistoryMessages with right permissions (#23533)
Co-authored-by: Kevin Aleman <kevin.aleman@rocket.chat> Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>pull/23675/head^2
parent
34cb351a15
commit
7c8b4e5b11
@ -0,0 +1,47 @@ |
||||
|
||||
import { check } from 'meteor/check'; |
||||
|
||||
import { API } from '../../../../api/server'; |
||||
import { LivechatRooms } from '../../../../models/server'; |
||||
import { Messages } from '../../../../models/server/raw'; |
||||
import { normalizeMessagesForUser } from '../../../../utils/server/lib/normalizeMessagesForUser'; |
||||
import { canAccessRoom } from '../../../../authorization/server'; |
||||
import { IMessage } from '../../../../../definition/IMessage'; |
||||
|
||||
API.v1.addRoute('livechat/:rid/messages', { authRequired: true, permissionsRequired: ['view-l-room'] }, { |
||||
async get() { |
||||
check(this.urlParams, { |
||||
rid: String, |
||||
}); |
||||
|
||||
const { offset, count } = this.getPaginationItems(); |
||||
const { sort } = this.parseJsonQuery(); |
||||
|
||||
const room = LivechatRooms.findOneById(this.urlParams.rid); |
||||
|
||||
if (!room) { |
||||
throw new Error('invalid-room'); |
||||
} |
||||
|
||||
if (!canAccessRoom(room, this.user)) { |
||||
throw new Error('not-allowed'); |
||||
} |
||||
|
||||
const cursor = Messages.findLivechatClosedMessages(this.urlParams.rid, { |
||||
sort: sort || { ts: -1 }, |
||||
skip: offset, |
||||
limit: count, |
||||
}); |
||||
|
||||
const total = await cursor.count(); |
||||
|
||||
const messages = await cursor.toArray() as IMessage[]; |
||||
|
||||
return API.v1.success({ |
||||
messages: normalizeMessagesForUser(messages, this.userId), |
||||
offset, |
||||
count, |
||||
total, |
||||
}); |
||||
}, |
||||
}); |
||||
@ -1,4 +1,5 @@ |
||||
export type PaginatedRequest = { |
||||
count: number; |
||||
offset: number; |
||||
}; |
||||
export type PaginatedRequest<T = {}, S extends string = string> = { |
||||
count?: number; |
||||
offset?: number; |
||||
sort?: `{ ${S}: ${1 | -1} }` | string; |
||||
} & T; |
||||
|
||||
@ -1,5 +1,5 @@ |
||||
export type PaginatedResult = { |
||||
export type PaginatedResult<T = {}> = { |
||||
count: number; |
||||
offset: number; |
||||
total: number; |
||||
}; |
||||
} & T; |
||||
|
||||
Loading…
Reference in new issue