Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] New specific endpoint for contactChatHistoryMessages with right permissions #23533

Merged
merged 20 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Template.contactChatHistoryMessages.onCreated(function() {
return this.loadMessages(`chat.search/?roomId=${ this.rid }&searchText=${ searchTerm }&count=${ limit }&offset=${ offset }&sort={"ts": 1}`);
}

this.loadMessages(`channels.messages/?roomId=${ this.rid }&count=${ limit }&offset=${ offset }&sort={"ts": 1}&query={"$or": [ {"t": {"$exists": false} }, {"t": "livechat-close"} ] }`);
this.loadMessages(`livechat/${ this.rid }/messages?count=${ limit }&offset=${ offset }&sort={"ts": 1}&query={"$or": [ {"t": {"$exists": false} }, {"t": "livechat-close"} ] }`);
});

this.autorun(() => {
Expand Down
47 changes: 47 additions & 0 deletions app/livechat/imports/server/rest/visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import { Match, check } from 'meteor/check';

import { API } from '../../../../api/server';
import { LivechatRooms, Messages, Users } from '../../../../models/server';
import { normalizeMessagesForUser } from '../../../../utils/server/lib/normalizeMessagesForUser';
import { canAccessRoom, hasPermission } from '../../../../authorization';
import { findVisitorInfo, findVisitedPages, findChatHistory, searchChats, findVisitorsToAutocomplete, findVisitorsByEmailOrPhoneOrNameOrUsername } from '../../../server/api/lib/visitors';

API.v1.addRoute('livechat/visitors.info', { authRequired: true }, {
Expand Down Expand Up @@ -62,6 +65,50 @@ API.v1.addRoute('livechat/visitors.chatHistory/room/:roomId/visitor/:visitorId',
},
});

API.v1.addRoute('livechat/:rid/messages', { authRequired: true }, {
get() {
check(this.urlParams, {
rid: String,
});
const { offset, count } = this.getPaginationItems();
const { sort, query } = this.parseJsonQuery();

if (!hasPermission(this.userId, 'view-l-room')) {
tiagoevanp marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('error-not-authorized');
}

const room = LivechatRooms.findOneById(this.urlParams.rid);
const user = Users.findOneById(this.userId, { fields: { _id: 1 } });

if (!room) {
throw new Error('invalid-room');
}

if (!canAccessRoom(room, user)) {
throw new Error('error-not-allowed');
}

const ourQuery = { ...query, rid: this.urlParams.rid };
tiagoevanp marked this conversation as resolved.
Show resolved Hide resolved

const cursor = Messages.find(ourQuery, {
sort: sort || { ts: -1 },
skip: offset,
limit: count,
});

const total = cursor.count();

const messages = cursor.fetch();

return API.v1.success({
messages: normalizeMessagesForUser(messages, this.userId),
offset,
count,
total,
});
},
});

API.v1.addRoute('livechat/visitors.searchChats/room/:roomId/visitor/:visitorId', { authRequired: true }, {
get() {
check(this.urlParams, {
Expand Down
5 changes: 3 additions & 2 deletions app/livechat/server/api/lib/visitors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission';
import { LivechatVisitors, Messages, LivechatRooms } from '../../../../models/server/raw';
import { LivechatVisitors, Messages as MessagesRaw, LivechatRooms } from '../../../../models/server/raw';
tiagoevanp marked this conversation as resolved.
Show resolved Hide resolved
import { canAccessRoomAsync } from '../../../../authorization/server/functions/canAccessRoom';

export async function findVisitorInfo({ userId, visitorId }) {
Expand All @@ -25,7 +25,7 @@ export async function findVisitedPages({ userId, roomId, pagination: { offset, c
if (!room) {
throw new Error('invalid-room');
}
const cursor = Messages.findByRoomIdAndType(room._id, 'livechat_navigation_history', {
const cursor = MessagesRaw.findByRoomIdAndType(room._id, 'livechat_navigation_history', {
sort: sort || { ts: -1 },
skip: offset,
limit: count,
Expand Down Expand Up @@ -72,6 +72,7 @@ export async function findChatHistory({ userId, roomId, visitorId, pagination: {
total,
};
}

export async function searchChats({ userId, roomId, visitorId, searchText, closedChatsOnly, servedChatsOnly: served, pagination: { offset, count, sort } }) {
if (!await hasPermissionAsync(userId, 'view-l-room')) {
throw new Error('error-not-authorized');
Expand Down
9 changes: 9 additions & 0 deletions client/contexts/ServerContext/endpoints/v1/omnichannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ILivechatDepartment } from '../../../../../definition/ILivechatDepartment';
import { ILivechatMonitor } from '../../../../../definition/ILivechatMonitor';
import { ILivechatTag } from '../../../../../definition/ILivechatTag';
import { IMessage } from '../../../../../definition/IMessage';
import { IOmnichannelCannedResponse } from '../../../../../definition/IOmnichannelCannedResponse';
import { IOmnichannelRoom, IRoom } from '../../../../../definition/IRoom';
import { ISetting } from '../../../../../definition/ISetting';
Expand Down Expand Up @@ -90,6 +91,14 @@ export type OmnichannelEndpoints = {
total: number;
};
};
'livechat/:rid/messages': {
GET: (params: { query: string; sort?: string; offset?: number; count?: number }) => {
messages: IMessage[];
count: number;
offset: number;
total: number;
};
};
'livechat/users/agent': {
GET: (params: { text?: string; offset?: number; count?: number; sort?: string }) => {
users: {
Expand Down