Skip to content

Commit

Permalink
Improve room types usage (#16753)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Mar 17, 2020
1 parent 6914d11 commit 0ace4aa
Show file tree
Hide file tree
Showing 30 changed files with 139 additions and 62 deletions.
7 changes: 3 additions & 4 deletions app/api/server/v1/im.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { Meteor } from 'meteor/meteor';

import { getRoomByNameOrIdWithOptionToJoin } from '../../../lib';
import { Subscriptions, Uploads, Users, Messages, Rooms } from '../../../models';
import { hasPermission } from '../../../authorization';
import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser';
import { settings } from '../../../settings';
import { API } from '../api';
import { getDirectMessageByNameOrIdWithOptionToJoin } from '../../../lib/server/functions/getDirectMessageByNameOrIdWithOptionToJoin';

function findDirectMessageRoom(params, user) {
if ((!params.roomId || !params.roomId.trim()) && (!params.username || !params.username.trim())) {
throw new Meteor.Error('error-room-param-not-provided', 'Body param "roomId" or "username" is required');
}

const room = getRoomByNameOrIdWithOptionToJoin({
const room = getDirectMessageByNameOrIdWithOptionToJoin({
currentUserId: user._id,
nameOrId: params.username || params.roomId,
type: 'd',
});

const canAccess = Meteor.call('canAccessRoom', room._id, user._id);
if (!canAccess || !room || room.t !== 'd') {
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "username" param provided does not match any dirct message');
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "username" param provided does not match any direct message');
}

const subscription = Subscriptions.findOneByRoomIdAndUserId(room._id, user._id);
Expand Down
24 changes: 12 additions & 12 deletions app/channel-settings/client/views/channelSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const common = {
});

const roomType = room && room.t;
return roomType && roomTypes.roomTypes[roomType].canBeDeleted(hasPermission, room);
return roomType && roomTypes.getConfig(roomType).canBeDeleted(hasPermission, room);
},
canEditRoom() {
const { _id } = Template.instance().room;
Expand Down Expand Up @@ -223,7 +223,7 @@ Template.channelSettingsEditing.onCreated(function() {
type: 'text',
label: 'Name',
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.NAME);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.NAME);
},
canEdit() {
return hasAllPermission('edit-room', room._id);
Expand Down Expand Up @@ -270,7 +270,7 @@ Template.channelSettingsEditing.onCreated(function() {
type: 'markdown',
label: 'Topic',
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.TOPIC);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.TOPIC);
},
canEdit() {
return hasAllPermission('edit-room', room._id);
Expand All @@ -289,7 +289,7 @@ Template.channelSettingsEditing.onCreated(function() {
return Template.instance().room.announcement;
},
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.ANNOUNCEMENT);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.ANNOUNCEMENT);
},
canEdit() {
return hasAllPermission('edit-room', room._id);
Expand All @@ -305,7 +305,7 @@ Template.channelSettingsEditing.onCreated(function() {
type: 'text',
label: 'Description',
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.DESCRIPTION);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.DESCRIPTION);
},
canEdit() {
return hasAllPermission('edit-room', room._id);
Expand Down Expand Up @@ -386,7 +386,7 @@ Template.channelSettingsEditing.onCreated(function() {
isToggle: true,
processing: new ReactiveVar(false),
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.READ_ONLY);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.READ_ONLY);
},
canEdit() {
return !room.broadcast && hasAllPermission('set-readonly', room._id);
Expand All @@ -401,7 +401,7 @@ Template.channelSettingsEditing.onCreated(function() {
isToggle: true,
processing: new ReactiveVar(false),
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.REACT_WHEN_READ_ONLY);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.REACT_WHEN_READ_ONLY);
},
canEdit() {
return !room.broadcast && hasAllPermission('set-react-when-readonly', room._id);
Expand All @@ -418,7 +418,7 @@ Template.channelSettingsEditing.onCreated(function() {
isToggle: true,
processing: new ReactiveVar(false),
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(
return roomTypes.getConfig(room.t).allowRoomSettingChange(
room,
RoomSettingsEnum.SYSTEM_MESSAGES,
);
Expand Down Expand Up @@ -460,7 +460,7 @@ Template.channelSettingsEditing.onCreated(function() {
isToggle: true,
processing: new ReactiveVar(false),
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.ARCHIVE_OR_UNARCHIVE);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.ARCHIVE_OR_UNARCHIVE);
},
canEdit() {
return hasAtLeastOnePermission(['archive-room', 'unarchive-room'], room._id);
Expand Down Expand Up @@ -501,7 +501,7 @@ Template.channelSettingsEditing.onCreated(function() {
isToggle: true,
processing: new ReactiveVar(false),
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.BROADCAST);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.BROADCAST);
},
canEdit() {
return false;
Expand All @@ -516,7 +516,7 @@ Template.channelSettingsEditing.onCreated(function() {
showingValue: new ReactiveVar(false),
realValue: null,
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.JOIN_CODE) && hasAllPermission('edit-room', room._id);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.JOIN_CODE) && hasAllPermission('edit-room', room._id);
},
canEdit() {
return hasAllPermission('edit-room', room._id);
Expand Down Expand Up @@ -679,7 +679,7 @@ Template.channelSettingsEditing.onCreated(function() {
isToggle: true,
processing: new ReactiveVar(false),
canView() {
return roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.E2E);
return roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.E2E);
},
canEdit() {
return hasAllPermission('edit-room', room._id);
Expand Down
2 changes: 1 addition & 1 deletion app/channel-settings/server/functions/saveRoomName.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const updateRoomName = (rid, displayName, isDiscussion) => {

export const saveRoomName = function(rid, displayName, user, sendMessage = true) {
const room = Rooms.findOneById(rid);
if (roomTypes.roomTypes[room.t].preventRenaming()) {
if (roomTypes.getConfig(room.t).preventRenaming()) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
function: 'RocketChat.saveRoomdisplayName',
});
Expand Down
5 changes: 4 additions & 1 deletion app/channel-settings/server/functions/saveRoomType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TAPi18n } from 'meteor/rocketchat:tap-i18n';

import { Rooms, Subscriptions, Messages } from '../../../models';
import { settings } from '../../../settings';
import { roomTypes, RoomSettingsEnum } from '../../../utils';

export const saveRoomType = function(rid, roomType, user, sendMessage = true) {
if (!Match.test(rid, String)) {
Expand All @@ -24,11 +25,13 @@ export const saveRoomType = function(rid, roomType, user, sendMessage = true) {
_id: rid,
});
}
if (room.t === 'd') {

if (!roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.TYPE)) {
throw new Meteor.Error('error-direct-room', 'Can\'t change type of direct rooms', {
function: 'RocketChat.saveRoomType',
});
}

const result = Rooms.setTypeById(rid, roomType) && Subscriptions.updateTypeByRoomId(rid, roomType);
if (result && sendMessage) {
let message;
Expand Down
3 changes: 2 additions & 1 deletion app/channel-settings/server/methods/saveRoomSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { saveReactWhenReadOnly } from '../functions/saveReactWhenReadOnly';
import { saveRoomSystemMessages } from '../functions/saveRoomSystemMessages';
import { saveRoomTokenpass } from '../functions/saveRoomTokens';
import { saveStreamingOptions } from '../functions/saveStreamingOptions';
import { RoomSettingsEnum, roomTypes } from '../../../utils';

const fields = ['roomName', 'roomTopic', 'roomAnnouncement', 'roomCustomFields', 'roomDescription', 'roomType', 'readOnly', 'reactWhenReadOnly', 'systemMessages', 'default', 'joinCode', 'tokenpass', 'streamingOptions', 'retentionEnabled', 'retentionMaxAge', 'retentionExcludePinned', 'retentionFilesOnly', 'retentionOverrideGlobal', 'encrypted'];
Meteor.methods({
Expand Down Expand Up @@ -90,7 +91,7 @@ Meteor.methods({
action: 'Change_Room_Type',
});
}
if (setting === 'encrypted' && value !== room.encrypted && (room.t !== 'd' && room.t !== 'p')) {
if (setting === 'encrypted' && value !== room.encrypted && !roomTypes.getConfig(room.t).allowRoomSettingChange(room, RoomSettingsEnum.E2E)) {
throw new Meteor.Error('error-action-not-allowed', 'Only groups or direct channels can enable encryption', {
method: 'saveRoomSettings',
action: 'Change_Room_Encrypted',
Expand Down
3 changes: 2 additions & 1 deletion app/e2e/client/rocketchat.e2e.room.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { Notifications } from '../../notifications';
import { Rooms, Subscriptions } from '../../models';
import { call } from '../../ui-utils';
import { roomTypes, RoomSettingsEnum } from '../../utils';

export class E2ERoom {
constructor(userId, roomId, t) {
Expand Down Expand Up @@ -97,7 +98,7 @@ export class E2ERoom {
}

isSupportedRoomType(type) {
return ['d', 'p'].includes(type);
return roomTypes.getConfig(type).allowRoomSettingChange({}, RoomSettingsEnum.E2E);
}

async importGroupKey(groupKey) {
Expand Down
4 changes: 2 additions & 2 deletions app/file-upload/server/lib/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const FileUpload = {
const user = file.userId ? Meteor.users.findOne(file.userId) : null;

const room = Rooms.findOneById(file.rid);
const directMessageAllow = settings.get('FileUpload_Enabled_Direct');
const directMessageAllowed = settings.get('FileUpload_Enabled_Direct');
const fileUploadAllowed = settings.get('FileUpload_Enabled');
if (canAccessRoom(room, user, file) !== true) {
return false;
Expand All @@ -75,7 +75,7 @@ export const FileUpload = {
throw new Meteor.Error('error-file-upload-disabled', reason);
}

if (!directMessageAllow && room.t === 'd') {
if (!directMessageAllowed && room.t === 'd') {
const reason = TAPi18n.__('File_not_allowed_direct_messages', language);
throw new Meteor.Error('error-direct-message-file-upload-not-allowed', reason);
}
Expand Down
5 changes: 5 additions & 0 deletions app/lib/lib/roomTypes/direct.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class DirectMessageRoomType extends RoomTypeConfig {

allowRoomSettingChange(room, setting) {
switch (setting) {
case RoomSettingsEnum.TYPE:
case RoomSettingsEnum.NAME:
case RoomSettingsEnum.SYSTEM_MESSAGES:
case RoomSettingsEnum.DESCRIPTION:
Expand All @@ -118,6 +119,10 @@ export class DirectMessageRoomType extends RoomTypeConfig {
}
}

allowMemberAction(/* room, action */) {
return false;
}

enableMembersListProfile() {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions app/lib/lib/roomTypes/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export class PrivateRoomType extends RoomTypeConfig {
}
}

allowMemberAction(/* room, action */) {
return true;
}

enableMembersListProfile() {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions app/lib/lib/roomTypes/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export class PublicRoomType extends RoomTypeConfig {
}
}

allowMemberAction(/* room, action */) {
return true;
}

getUiText(context) {
switch (context) {
case UiTextContext.HIDE_WARNING:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getRoomByNameOrIdWithOptionToJoin } from './getRoomByNameOrIdWithOptionToJoin';

export const getDirectMessageByNameOrIdWithOptionToJoin = (args) =>
getRoomByNameOrIdWithOptionToJoin({ ...args, type: 'd' });

export const getDirectMessageByIdWithOptionToJoin = (args) =>
getDirectMessageByNameOrIdWithOptionToJoin({ ...args, tryDirectByUserIdOnly: true });
5 changes: 3 additions & 2 deletions app/lib/server/functions/processWebhookMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import s from 'underscore.string';
import { getRoomByNameOrIdWithOptionToJoin } from './getRoomByNameOrIdWithOptionToJoin';
import { sendMessage } from './sendMessage';
import { Subscriptions } from '../../../models';
import { getDirectMessageByIdWithOptionToJoin, getDirectMessageByNameOrIdWithOptionToJoin } from './getDirectMessageByNameOrIdWithOptionToJoin';

export const processWebhookMessage = function(messageObj, user, defaultValues = { channel: '', alias: '', avatar: '', emoji: '' }, mustBeJoined = false) {
const sentData = [];
Expand All @@ -21,7 +22,7 @@ export const processWebhookMessage = function(messageObj, user, defaultValues =
room = getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, nameOrId: channelValue, joinChannel: true });
break;
case '@':
room = getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, nameOrId: channelValue, type: 'd' });
room = getDirectMessageByNameOrIdWithOptionToJoin({ currentUserId: user._id, nameOrId: channelValue });
break;
default:
channelValue = channelType + channelValue;
Expand All @@ -33,7 +34,7 @@ export const processWebhookMessage = function(messageObj, user, defaultValues =
}

// We didn't get a room, let's try finding direct messages
room = getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, nameOrId: channelValue, type: 'd', tryDirectByUserIdOnly: true });
room = getDirectMessageByIdWithOptionToJoin({ currentUserId: user._id, nameOrId: channelValue });
if (room) {
break;
}
Expand Down
9 changes: 5 additions & 4 deletions app/lib/server/methods/archiveRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { check } from 'meteor/check';
import { Rooms } from '../../../models';
import { hasPermission } from '../../../authorization';
import { archiveRoom } from '../functions';
import { roomTypes, RoomMemberActions } from '../../../utils/server';

Meteor.methods({
archiveRoom(rid) {
Expand All @@ -19,12 +20,12 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'archiveRoom' });
}

if (!hasPermission(Meteor.userId(), 'archive-room', room._id)) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', { method: 'archiveRoom' });
if (!roomTypes.getConfig(room.t).allowMemberAction(room, RoomMemberActions.ARCHIVE)) {
throw new Meteor.Error('error-direct-message-room', `rooms type: ${ room.t } can not be archived`, { method: 'archiveRoom' });
}

if (room.t === 'd') {
throw new Meteor.Error('error-direct-message-room', 'Direct Messages can not be archived', { method: 'archiveRoom' });
if (!hasPermission(Meteor.userId(), 'archive-room', room._id)) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', { method: 'archiveRoom' });
}

return archiveRoom(rid);
Expand Down
5 changes: 5 additions & 0 deletions app/lib/server/methods/joinRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { hasPermission, canAccessRoom } from '../../../authorization';
import { Rooms } from '../../../models';
import { Tokenpass, updateUserTokenpassBalances } from '../../../tokenpass/server';
import { addUserToRoom } from '../functions';
import { roomTypes, RoomMemberActions } from '../../../utils/server';

Meteor.methods({
joinRoom(rid, code) {
Expand All @@ -20,6 +21,10 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'joinRoom' });
}

if (!roomTypes.getConfig(room.t).allowMemberAction(room, RoomMemberActions.JOIN)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'joinRoom' });
}

// TODO we should have a 'beforeJoinRoom' call back so external services can do their own validations
const user = Meteor.user();
if (room.tokenpass && user && user.services && user.services.tokenpass) {
Expand Down
7 changes: 6 additions & 1 deletion app/lib/server/methods/leaveRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { check } from 'meteor/check';
import { hasPermission, hasRole, getUsersInRole } from '../../../authorization';
import { Subscriptions, Rooms } from '../../../models';
import { removeUserFromRoom } from '../functions';
import { roomTypes, RoomMemberActions } from '../../../utils/server';

Meteor.methods({
leaveRoom(rid) {
Expand All @@ -16,7 +17,11 @@ Meteor.methods({
const room = Rooms.findOneById(rid);
const user = Meteor.user();

if (room.t === 'd' || (room.t === 'c' && !hasPermission(user._id, 'leave-c')) || (room.t === 'p' && !hasPermission(user._id, 'leave-p'))) {
if (!roomTypes.getConfig(room.t).allowMemberAction(room, RoomMemberActions.LEAVE)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'leaveRoom' });
}

if ((room.t === 'c' && !hasPermission(user._id, 'leave-c')) || (room.t === 'p' && !hasPermission(user._id, 'leave-p'))) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'leaveRoom' });
}

Expand Down
3 changes: 2 additions & 1 deletion app/slashcommands-unarchiveroom/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { Rooms, Messages } from '../../models';
import { slashCommands } from '../../utils';
import { Notifications } from '../../notifications';
import { roomTypes, RoomMemberActions } from '../../utils/server';

function Unarchive(command, params, item) {
if (command !== 'unarchive' || !Match.test(params, String)) {
Expand Down Expand Up @@ -38,7 +39,7 @@ function Unarchive(command, params, item) {
}

// You can not archive direct messages.
if (room.t === 'd') {
if (!roomTypes.getConfig(room.t).allowMemberAction(room, RoomMemberActions.ARCHIVE)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion app/ui-admin/client/rooms/adminRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Template.adminRooms.helpers({
return Template.instance().rooms.get().length;
},
type() {
return TAPi18n.__(roomTypes.roomTypes[this.t].label);
return TAPi18n.__(roomTypes.getConfig(this.t).label);
},
'default'() {
if (this.default) {
Expand Down
Loading

0 comments on commit 0ace4aa

Please sign in to comment.