Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into feat…
Browse files Browse the repository at this point in the history
…/new-threads

* 'develop' of github.com:RocketChat/Rocket.Chat:
  [FIX] Bot Agents not being able to get Omnichannel Inquiries (#17404)
  [FIX] Allowing blocking a user on channels (#17406)
  Fix moving-to-a-single-codebase link in README (#17297)
  Regression: Fix mem usage with more than one argument (#17391)
  • Loading branch information
ggazzo committed Apr 24, 2020
2 parents 70b809b + 9dc5fab commit 71eb1ce
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[![Code Climate](https://codeclimate.com/github/RocketChat/Rocket.Chat/badges/gpa.svg)](https://codeclimate.com/github/RocketChat/Rocket.Chat)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/RocketChat/Rocket.Chat/raw/master/LICENSE)

* [**NEW!** Rocket.Chat Moving to a Single Codebase](#moving-to-one-codebase)
* [**NEW!** Rocket.Chat Moving to a Single Codebase](#moving-to-a-single-codebase)
* [Community](#community)
* [Mobile Apps](#mobile-apps)
* [Desktop Apps](#desktop-apps)
Expand Down
9 changes: 6 additions & 3 deletions app/authorization/server/functions/canSendMessage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { canAccessRoomAsync } from './canAccessRoom';
import { hasPermissionAsync } from './hasPermission';
import { Subscriptions, Rooms } from '../../../models/server/raw';
import { roomTypes, RoomMemberActions } from '../../../utils/server';

const subscriptionOptions = {
projection: {
Expand All @@ -16,9 +17,11 @@ export const canSendMessageAsync = async (rid, { uid, username, type }, extraDat
throw new Error('error-not-allowed');
}

const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, uid, subscriptionOptions);
if (subscription && (subscription.blocked || subscription.blocker)) {
throw new Error('room_is_blocked');
if (roomTypes.getConfig(room.t).allowMemberAction(room, RoomMemberActions.BLOCK)) {
const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, uid, subscriptionOptions);
if (subscription && (subscription.blocked || subscription.blocker)) {
throw new Error('room_is_blocked');
}
}

if (room.ro === true && !await hasPermissionAsync(uid, 'post-readonly', rid)) {
Expand Down
13 changes: 8 additions & 5 deletions app/authorization/server/functions/hasPermission.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import mem from 'mem';
import { Permissions, Users, Subscriptions } from '../../../models/server/raw';

const rolesHasPermission = mem(async (permission, roles) => {
const result = await Permissions.findOne({ _id: permission, roles: { $in: roles } });
const result = await Permissions.findOne({ _id: permission, roles: { $in: roles } }, { projection: { _id: 1 } });
return !!result;
}, process.env.TEST_MODE === 'true' ? { maxAge: 0 } : undefined);
}, {
cacheKey: JSON.stringify,
...process.env.TEST_MODE === 'true' && { maxAge: 0 },
});

const getRoles = mem(async (uid, scope) => {
const { roles: userRoles = [] } = await Users.findOne({ _id: uid });
const { roles: subscriptionsRoles = [] } = (scope && await Subscriptions.findOne({ rid: scope, 'u._id': uid }, { fields: { roles: 1 } })) || {};
const { roles: userRoles = [] } = await Users.findOne({ _id: uid }, { projection: { roles: 1 } });
const { roles: subscriptionsRoles = [] } = (scope && await Subscriptions.findOne({ rid: scope, 'u._id': uid }, { projection: { roles: 1 } })) || {};
return [...userRoles, ...subscriptionsRoles].sort((a, b) => a.localeCompare(b));
}, { maxAge: 1000 });
}, { maxAge: 1000, cacheKey: JSON.stringify });

export const clearCache = () => {
mem.clear(getRoles);
Expand Down
11 changes: 8 additions & 3 deletions app/lib/lib/roomTypes/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChatRoom, ChatSubscription } from '../../../models';
import { openRoom } from '../../../ui-utils';
import { settings } from '../../../settings';
import { hasAtLeastOnePermission, hasPermission } from '../../../authorization';
import { getUserPreference, RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext, roomTypes } from '../../../utils';
import { getUserPreference, RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext, roomTypes, RoomMemberActions } from '../../../utils';
import { getRoomAvatarURL } from '../../../utils/lib/getRoomAvatarURL';
import { getAvatarURL } from '../../../utils/lib/getAvatarURL';

Expand Down Expand Up @@ -98,8 +98,13 @@ export class PrivateRoomType extends RoomTypeConfig {
}
}

allowMemberAction(/* room, action */) {
return true;
allowMemberAction(room, action) {
switch (action) {
case RoomMemberActions.BLOCK:
return false;
default:
return true;
}
}

enableMembersListProfile() {
Expand Down
11 changes: 8 additions & 3 deletions app/lib/lib/roomTypes/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { openRoom } from '../../../ui-utils';
import { ChatRoom, ChatSubscription } from '../../../models';
import { settings } from '../../../settings';
import { hasAtLeastOnePermission } from '../../../authorization';
import { getUserPreference, RoomTypeConfig, RoomTypeRouteConfig, RoomSettingsEnum, UiTextContext } from '../../../utils';
import { getUserPreference, RoomTypeConfig, RoomTypeRouteConfig, RoomSettingsEnum, UiTextContext, RoomMemberActions } from '../../../utils';
import { getAvatarURL } from '../../../utils/lib/getAvatarURL';

export class PublicRoomRoute extends RoomTypeRouteConfig {
Expand Down Expand Up @@ -113,8 +113,13 @@ export class PublicRoomType extends RoomTypeConfig {
}
}

allowMemberAction(/* room, action */) {
return true;
allowMemberAction(room, action) {
switch (action) {
case RoomMemberActions.BLOCK:
return false;
default:
return true;
}
}

getUiText(context) {
Expand Down
12 changes: 12 additions & 0 deletions app/livechat/server/lib/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,15 @@ export const checkServiceStatus = ({ guest, agent }) => {

return Livechat.online(guest.department);
};

export const userCanTakeInquiry = (user) => {
check(user, Match.ObjectIncluding({
status: String,
statusLivechat: String,
roles: [String],
}));

const { roles, status, statusLivechat } = user;
// TODO: hasRole when the user has already been fetched from DB
return (status !== 'offline' && statusLivechat === 'available') || roles.includes('bot');
};
8 changes: 4 additions & 4 deletions app/livechat/server/methods/takeInquiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Meteor } from 'meteor/meteor';
import { hasPermission } from '../../../authorization';
import { Users, LivechatInquiry } from '../../../models/server';
import { RoutingManager } from '../lib/RoutingManager';
import { userCanTakeInquiry } from '../lib/Helper';

Meteor.methods({
'livechat:takeInquiry'(inquiryId) {
Expand All @@ -16,10 +17,9 @@ Meteor.methods({
throw new Meteor.Error('error-not-allowed', 'Inquiry already taken', { method: 'livechat:takeInquiry' });
}

const user = Users.findOneById(Meteor.userId());
const { status, statusLivechat } = user;
if (status === 'offline' || statusLivechat !== 'available') {
throw new Meteor.Error('error-agent-offline', 'Agent offline', { method: 'livechat:takeInquiry' });
const user = Users.findOneById(Meteor.userId(), { fields: { _id: 1, username: 1, roles: 1, status: 1, statusLivechat: 1 } });
if (!userCanTakeInquiry(user)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:takeInquiry' });
}

const agent = {
Expand Down
4 changes: 2 additions & 2 deletions app/models/client/models/Subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Object.assign(Subscriptions, {
const subscription = this.findOne(query, { fields: { roles: 1 } });

return subscription && Array.isArray(subscription.roles) && subscription.roles.includes(roleName);
}, { maxAge: 1000 }),
}, { maxAge: 1000, cacheKey: JSON.stringify }),

findUsersInRoles: mem(function(roles, scope, options) {
roles = [].concat(roles);
Expand All @@ -41,7 +41,7 @@ Object.assign(Subscriptions, {
}));

return Users.find({ _id: { $in: users } }, options);
}, { maxAge: 1000 }),
}, { maxAge: 1000, cacheKey: JSON.stringify }),
});

export { Subscriptions };
2 changes: 1 addition & 1 deletion app/ui-utils/client/lib/MessageAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const MessageAction = new class {
}

if (config.condition) {
config.condition = mem(config.condition, { maxAge: 1000 });
config.condition = mem(config.condition, { maxAge: 1000, cacheKey: JSON.stringify });
}

return Tracker.nonreactive(() => {
Expand Down

0 comments on commit 71eb1ce

Please sign in to comment.