Files
upstream-rocketchat/apps/meteor/ee/server/api/lib/canned-responses.js
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

169 lines
3.8 KiB
JavaScript

import { CannedResponse } from '@rocket.chat/models';
import { escapeRegExp } from '@rocket.chat/tools';
import { hasPermissionAsync } from '../../../../server/lib/authorization/hasPermission';
import { getDepartmentsWhichUserCanAccess } from '../v1/omnichannel/lib/departments';
export async function findAllCannedResponses({ userId }) {
// If the user is an admin or livechat manager, get his own responses and all responses from all departments
if (await hasPermissionAsync(userId, 'view-all-canned-responses')) {
const cannedResponses = await CannedResponse.find({
$or: [
{
scope: 'user',
userId,
},
{
scope: 'department',
},
{
scope: 'global',
},
],
}).toArray();
return cannedResponses;
}
// Last scenario: user is an agent, so get his own responses and those from the departments he is in
const accessibleDepartments = await getDepartmentsWhichUserCanAccess(userId, true);
const cannedResponses = await CannedResponse.find({
$or: [
{
scope: 'user',
userId,
},
{
scope: 'department',
departmentId: {
$in: accessibleDepartments,
},
},
{
scope: 'global',
},
],
}).toArray();
return cannedResponses;
}
/**
* @param {Object} param0
* @param {String} param0.userId
* @param {String} [param0.shortcut]
* @param {String} [param0.text]
* @param {String} [param0.departmentId]
* @param {String} [param0.scope]
* @param {String} [param0.createdBy]
* @param {String[]} [param0.tags]
* @param {Object} param0.options
* @param {Number} param0.options.offset
* @param {Number} param0.options.count
* @param {Object} param0.options.sort
* @param {Object} param0.options.fields
*/
export async function findAllCannedResponsesFilter({ userId, shortcut, text, departmentId, scope, createdBy, tags = [], options = {} }) {
let extraFilter = [];
// if user cannot see all, filter to private + public + departments user is in
if (!(await hasPermissionAsync(userId, 'view-all-canned-responses'))) {
const accessibleDepartments = await getDepartmentsWhichUserCanAccess(userId, true);
const isDepartmentInScope = (departmentId) => !!accessibleDepartments.includes(departmentId);
const departmentIds = departmentId && isDepartmentInScope(departmentId) ? [departmentId] : accessibleDepartments;
extraFilter = [
{
$or: [
{
scope: 'user',
userId,
},
{
scope: 'department',
departmentId: {
$in: departmentIds,
},
},
{
scope: 'global',
},
],
},
];
}
if (departmentId) {
extraFilter = [
{
departmentId,
},
];
}
const textFilter = new RegExp(escapeRegExp(text), 'i');
let filter = {
$and: [
...(shortcut ? [{ shortcut }] : []),
...(text ? [{ $or: [{ shortcut: textFilter }, { text: textFilter }] }] : []),
...(scope ? [{ scope }] : []),
...(createdBy ? [{ 'createdBy._id': createdBy }] : []),
...(tags.length
? [
{
tags: {
$in: tags,
},
},
]
: []),
...extraFilter,
],
};
if (!filter.$and.length) {
filter = {};
}
const { cursor, totalCount } = CannedResponse.findPaginated(filter, {
sort: options.sort || { shortcut: 1 },
skip: options.offset,
limit: options.count,
});
const [cannedResponses, total] = await Promise.all([cursor.toArray(), totalCount]);
return {
cannedResponses,
total,
};
}
export async function findOneCannedResponse({ userId, _id }) {
if (await hasPermissionAsync(userId, 'view-all-canned-responses')) {
return CannedResponse.findOneById(_id);
}
const accessibleDepartments = await getDepartmentsWhichUserCanAccess(userId, true);
const filter = {
_id,
$or: [
{
scope: 'user',
userId,
},
{
scope: 'department',
departmentId: {
$in: accessibleDepartments,
},
},
{
scope: 'global',
},
],
};
return CannedResponse.findOne(filter);
}