Skip to content

Commit

Permalink
Extract canSendMessage function (#14909)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored and sampaiodiego committed Jul 2, 2019
1 parent 7e9ba13 commit 3ce57f4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 35 deletions.
54 changes: 24 additions & 30 deletions app/authorization/server/functions/canSendMessage.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/tap:i18n';
import { Random } from 'meteor/random';

import { canAccessRoom } from './canAccessRoom';
import { hasPermission } from './hasPermission';
import { Notifications } from '../../../notifications';
import { Rooms, Subscriptions } from '../../../models';

import { canAccessRoomAsync } from './canAccessRoom';
import { hasPermissionAsync } from './hasPermission';
import { Subscriptions, Rooms } from '../../../models/server/raw';

const subscriptionOptions = {
projection: {
blocked: 1,
blocker: 1,
},
};

export const canSendMessage = (rid, { uid, username }, extraData) => {
const room = Rooms.findOneById(rid);
export const canSendMessageAsync = async (rid, { uid, username }, extraData) => {
const room = await Rooms.findOneById(rid);

if (!canAccessRoom.call(this, room, { _id: uid, username }, extraData)) {
throw new Meteor.Error('error-not-allowed');
if (!await canAccessRoomAsync(room, { _id: uid, username }, extraData)) {
throw new Error('error-not-allowed');
}

const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, uid);
if (subscription && (subscription.blocked || subscription.blocker)) {
throw new Meteor.Error('room_is_blocked');
const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, uid, subscriptionOptions);
if (subscription.blocked || subscription.blocker) {
throw new Error('room_is_blocked');
}

if (room.ro === true) {
if (!hasPermission(Meteor.userId(), 'post-readonly', room._id)) {
// Unless the user was manually unmuted
if (!(room.unmuted || []).includes(username)) {
Notifications.notifyUser(Meteor.userId(), 'message', {
_id: Random.id(),
rid: room._id,
ts: new Date(),
msg: TAPi18n.__('room_is_read_only'),
});

throw new Meteor.Error('You can\'t send messages because the room is readonly.');
}
if (room.ro === true && !await hasPermissionAsync(uid, 'post-readonly', rid)) {
// Unless the user was manually unmuted
if (!(room.unmuted || []).includes(username)) {
throw new Error('You can\'t send messages because the room is readonly.');
}
}

if ((room.muted || []).includes(username)) {
throw new Meteor.Error('You_have_been_muted');
throw new Error('You_have_been_muted');
}

return room;
};

export const canSendMessage = (rid, { uid, username }, extraData) => Promise.await(canSendMessageAsync(rid, { uid, username }, extraData));
4 changes: 4 additions & 0 deletions app/models/server/raw/BaseRaw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export class BaseRaw {
this.col = col;
}

findOneById(_id, options) {
return this.findOne({ _id }, options);
}

findOne(...args) {
return this.col.findOne(...args);
}
Expand Down
26 changes: 26 additions & 0 deletions app/models/server/raw/Rooms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { BaseRaw } from './BaseRaw';

export class RoomsRaw extends BaseRaw {
findOneByRoomIdAndUserId(rid, uid, options) {
const query = {
rid,
'u._id': uid,
};

return this.col.findOne(query, options);
}

isUserInRole(uid, roleName, rid) {
if (rid == null) {
return;
}

const query = {
'u._id': uid,
rid,
roles: roleName,
};

return this.findOne(query, { fields: { roles: 1 } });
}
}
10 changes: 5 additions & 5 deletions app/models/server/raw/Subscriptions.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { BaseRaw } from './BaseRaw';

export class SubscriptionsRaw extends BaseRaw {
findOneByRoomIdAndUserId(roomId, userId, options) {
findOneByRoomIdAndUserId(rid, uid, options) {
const query = {
rid: roomId,
'u._id': userId,
rid,
'u._id': uid,
};

return this.col.findOne(query, options);
}

isUserInRole(userId, roleName, rid) {
isUserInRole(uid, roleName, rid) {
if (rid == null) {
return;
}

const query = {
'u._id': userId,
'u._id': uid,
rid,
roles: roleName,
};
Expand Down
3 changes: 3 additions & 0 deletions app/models/server/raw/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import SettingsModel from '../models/Settings';
import { SettingsRaw } from './Settings';
import UsersModel from '../models/Users';
import { UsersRaw } from './Users';
import RoomsModel from '../models/Rooms';
import { RoomsRaw } from './Rooms';

export const Permissions = new PermissionsRaw(PermissionsModel.model.rawCollection());
export const Roles = new RolesRaw(RolesModel.model.rawCollection());
export const Subscriptions = new SubscriptionsRaw(SubscriptionsModel.model.rawCollection());
export const Settings = new SettingsRaw(SettingsModel.model.rawCollection());
export const Users = new UsersRaw(UsersModel.model.rawCollection());
export const Rooms = new RoomsRaw(RoomsModel.model.rawCollection());

0 comments on commit 3ce57f4

Please sign in to comment.