Skip to content

Commit

Permalink
improve room types usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Feb 28, 2020
1 parent 6f0e929 commit 101f77d
Show file tree
Hide file tree
Showing 25 changed files with 135 additions and 55 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
10 changes: 9 additions & 1 deletion app/lib/lib/roomTypes/direct.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Session } from 'meteor/session';

import { ChatRoom, Subscriptions } from '../../../models';
import { openRoom } from '../../../ui-utils';
import { getUserPreference, RoomTypeConfig, RoomTypeRouteConfig, RoomSettingsEnum, UiTextContext } from '../../../utils';
import { getUserPreference, RoomTypeConfig, RoomTypeRouteConfig, RoomSettingsEnum, UiTextContext, RoomMemberActions } from '../../../utils';
import { hasPermission, hasAtLeastOnePermission } from '../../../authorization';
import { settings } from '../../../settings';
import { getUserAvatarURL } from '../../../utils/lib/getUserAvatarURL';
Expand Down Expand Up @@ -118,6 +118,14 @@ export class DirectMessageRoomType extends RoomTypeConfig {
}
}

allowMemberAction(room, action) {
switch (action) {
case RoomMemberActions.MUTE:
default:
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
10 changes: 9 additions & 1 deletion 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,6 +113,14 @@ export class PublicRoomType extends RoomTypeConfig {
}
}

allowMemberAction(room, action) {
switch (action) {
case RoomMemberActions.MUTE:
default:
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
2 changes: 1 addition & 1 deletion app/lib/server/methods/addUsersToRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Meteor.methods({
const userInRoom = subscription != null;

// Can't add to direct room ever
if (room.t === 'd') {
if (room.t === 'd') { // TODO CHANGE
throw new Meteor.Error('error-cant-invite-for-direct-room', 'Can\'t invite user to direct rooms', {
method: 'addUsersToRoom',
});
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.r } 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
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
16 changes: 8 additions & 8 deletions app/ui-flextab/client/tabs/membersList.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Template.membersList.helpers({

isGroupChat() {
const room = ChatRoom.findOne(this.rid, { reactive: false });
return roomTypes.roomTypes[room.t].isGroupChat();
return roomTypes.getConfig(room.t).isGroupChat();
},

isDirectChat() {
Expand Down Expand Up @@ -98,7 +98,7 @@ Template.membersList.helpers({
canAddUser() {
const roomData = Session.get(`roomData${ this._id }`);
if (!roomData) { return ''; }
return (() => roomTypes.roomTypes[roomData.t].canAddUser(roomData))();
return (() => roomTypes.getConfig(roomData.t).canAddUser(roomData))();
},

canInviteUser() {
Expand All @@ -123,10 +123,10 @@ Template.membersList.helpers({
tabBar: Template.currentData().tabBar,
username: Template.instance().userDetail.get(),
clear: Template.instance().clearUserDetail,
showAll: roomTypes.roomTypes[room.t].userDetailShowAll(room) || false,
hideAdminControls: roomTypes.roomTypes[room.t].userDetailShowAdmin(room) || false,
showAll: roomTypes.getConfig(room.t).userDetailShowAll(room) || false,
hideAdminControls: roomTypes.getConfig(room.t).userDetailShowAdmin(room) || false,
video: ['d'].includes(room && room.t),
showBackButton: roomTypes.roomTypes[room.t].isGroupChat(),
showBackButton: roomTypes.getConfig(room.t).isGroupChat(),
};
},
displayName() {
Expand Down Expand Up @@ -183,8 +183,8 @@ Template.membersList.events({
const room = Session.get(`roomData${ instance.data.rid }`);
const _actions = getActions({
user: this.user.user,
hideAdminControls: roomTypes.roomTypes[room.t].userDetailShowAdmin(room) || false,
directActions: roomTypes.roomTypes[room.t].userDetailShowAll(room) || false,
hideAdminControls: roomTypes.getConfig(room.t).userDetailShowAdmin(room) || false,
directActions: roomTypes.getConfig(room.t).userDetailShowAll(room) || false,
})
.map((action) => (typeof action === 'function' ? action.call(this) : action))
.filter((action) => action && (!action.condition || action.condition.call(this)));
Expand Down Expand Up @@ -235,7 +235,7 @@ Template.membersList.events({
'autocompleteselect #user-add-search'(event, template, doc) {
const roomData = Session.get(`roomData${ template.data.rid }`);

if (roomTypes.roomTypes[roomData.t].canAddUser(roomData)) {
if (roomTypes.getConfig(roomData.t).canAddUser(roomData)) {
return Meteor.call('addUserToRoom', { rid: roomData._id, username: doc.username }, function(error) {
if (error) {
return handleError(error);
Expand Down
Loading

0 comments on commit 101f77d

Please sign in to comment.