Replace hard-coded room types with "interface"-method-calls

pull/8629/head
Oliver Jägle 8 years ago
parent 5b6b5f80f7
commit 06226f238f
  1. 2
      packages/rocketchat-channel-settings/client/views/channelSettings.js
  2. 12
      packages/rocketchat-channel-settings/server/functions/saveRoomName.js
  3. 16
      packages/rocketchat-lib/lib/roomTypesCommon.js
  4. 67
      packages/rocketchat-lib/startup/defaultRoomTypes.js
  5. 31
      packages/rocketchat-ui-admin/client/rooms/adminRooms.js
  6. 16
      packages/rocketchat-ui-flextab/client/tabs/membersList.js
  7. 14
      packages/rocketchat-ui/client/views/app/createChannel.js
  8. 5
      server/methods/eraseRoom.js

@ -54,7 +54,7 @@ Template.channelSettings.helpers({
}
});
const roomType = room && room.t;
return roomType && RocketChat.authz.hasAtLeastOnePermission(`delete-${ roomType }`, this.rid);
return roomType && RocketChat.roomTypes.roomTypes[room.t].canBeDeleted(room);
},
readOnly() {
const room = ChatRoom.findOne(this.rid, {

@ -1,12 +1,10 @@
RocketChat.saveRoomName = function(rid, displayName, user, sendMessage = true) {
const room = RocketChat.models.Rooms.findOneById(rid);
if (room.t !== 'c' && room.t !== 'p') {
// custom room types can explicitly state they don't want to be renamed
if (!(RocketChat.roomTypes[room.t].preventRenaming && RocketChat.roomTypes[room.t].preventRenaming())) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
'function': 'RocketChat.saveRoomName'
});
}
if (RocketChat.roomTypes.roomTypes[room.t].preventRenaming()) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
'function': 'RocketChat.saveRoomdisplayName'
});
}
if (displayName === room.name) {
return;

@ -112,12 +112,10 @@ this.roomTypesCommon = class {
}
if (!config.canBeDeleted) {
config.canBeDeleted = function(userId, room) {
/* this implementation is actually not necessary, since the generic authorisation check on delete-{identifier}
is already performed in /server/methods/eraseRoom.js. However, in order not to rely on that remaining as-is,
code the authorization check redundantly here. This shall also make consumption more easy and transparent
*/
return RocketChat.authz.hasPermission(userId, `delete-${ room.t }`, room._id);
config.canBeDeleted = function(room) {
return Meteor.isServer ?
RocketChat.authz.hasAtLeastOnePermission(Meteor.userId(), [`delete-${ room.t }`], room._id) :
RocketChat.authz.hasAtLeastOnePermission([`delete-${ room.t }`], room._id);
};
}
@ -129,13 +127,13 @@ this.roomTypesCommon = class {
if (!config.isGroupChat) {
config.isGroupChat = function() {
return true;
return false;
};
}
if (!config.canAddUser) {
config.canAddUser = function() {
return true;
return false;
};
}
@ -159,7 +157,7 @@ this.roomTypesCommon = class {
if (!config.includeInRoomSearch) {
config.includeInRoomSearch = function() {
return true;
return false;
};
}
}

@ -6,19 +6,13 @@ RocketChat.roomTypes.add('unread', 10, {
const preferences = (user && user.settings && user.settings.preferences && user.settings.preferences) || {};
return preferences.roomsListExhibitionMode === 'unread';
},
label: 'Unread',
includeInRoomSearch() {
return false;
}
label: 'Unread'
});
RocketChat.roomTypes.add('f', 20, {
header: 'favorite',
icon: 'star',
label: 'Favorites',
includeInRoomSearch() {
return false;
}
label: 'Favorites'
});
// activity
@ -28,10 +22,7 @@ RocketChat.roomTypes.add('activity', 30, {
const preferences = (user && user.settings && user.settings.preferences && user.settings.preferences) || {};
return preferences.roomsListExhibitionMode === 'activity';
},
label: 'Conversations',
includeInRoomSearch() {
return false;
}
label: 'Conversations'
});
RocketChat.roomTypes.add('channels', 30, {
@ -77,6 +68,30 @@ RocketChat.roomTypes.add('c', 30, {
showJoinLink(roomId) {
return !!ChatRoom.findOne({_id: roomId, t: 'c'});
},
includeInRoomSearch() {
return true;
},
isGroupChat() {
return true;
},
canAddUser(room) {
return RocketChat.authz.hasAtLeastOnePermission(['add-user-to-any-c-room', 'add-user-to-joined-room'], room._id)
},
userDetailShowAll() {
return true;
},
userDetailShowAdmin() {
return true;
},
getDisplayName(room) {
return room.name;
}
});
@ -113,8 +128,24 @@ RocketChat.roomTypes.add('p', 40, {
return !preferences.roomsListExhibitionMode || ['unread', 'category'].includes(preferences.roomsListExhibitionMode) && !preferences.mergeChannels && RocketChat.authz.hasAllPermission('view-p-room');
},
includeInRoomSearch() {
return false;
isGroupChat() {
return true;
},
canAddUser(room) {
return RocketChat.authz.hasAtLeastOnePermission(['add-user-to-any-p-room', 'add-user-to-joined-room'], room._id)
},
userDetailShowAll() {
return true;
},
userDetailShowAdmin() {
return true;
},
getDisplayName(room) {
return room.name;
}
});
@ -180,7 +211,11 @@ RocketChat.roomTypes.add('d', 50, {
return Session.get(`user_${ subscription.name }_status`);
},
includeInRoomSearch() {
return false;
userDetailShowAdmin() {
return true;
},
getDisplayName(room) {
return room.usernames.join(' x ');
}
});

@ -27,28 +27,10 @@ Template.adminRooms.helpers({
return rooms && rooms.count();
},
name() {
if (this.t === 'c' || this.t === 'p') {
return this.name;
} else if (this.t === 'd') {
return this.usernames.join(' x ');
} else {
// custom room type
RocketChat.roomTypes[this.t].getDisplayName(this);
}
RocketChat.roomTypes.roomTypes[this.t].getDisplayName(this);
},
type() {
if (this.t === 'c') {
return TAPi18n.__('Channel');
} else if (this.t === 'd') {
return TAPi18n.__('Direct_Messages');
}
if (this.t === 'p') {
return TAPi18n.__('Private_Groups');
}
// custom room type
return TAPi18n.__(RocketChat.roomTypes[this.t].label);
return TAPi18n.__(RocketChat.roomTypes.roomTypes[this.t].label);
},
'default'() {
if (this['default']) {
@ -62,8 +44,7 @@ Template.adminRooms.helpers({
tabBar: Template.instance().tabBar
};
}
})
;
});
Template.adminRooms.onCreated(function() {
const instance = this;
@ -115,13 +96,13 @@ Template.adminRooms.onCreated(function() {
filter = _.trim(filter);
if (filter) {
const filterReg = new RegExp(s.escapeRegExp(filter), 'i');
query = {$or: [{name: filterReg}, {t: 'd', usernames: filterReg}]};
query = { $or: [{ name: filterReg }, { t: 'd', usernames: filterReg } ]};
}
if (types.length) {
query['t'] = {$in: types};
query['t'] = { $in: types };
}
const limit = instance.limit && instance.limit.get();
return AdminChatRoom.find(query, {limit, sort: {'default': -1, name: 1}});
return AdminChatRoom.find(query, { limit, sort: { 'default': -1, name: 1}});
};
this.getSearchTypes = function() {
return _.map($('[name=room-type]:checked'), function(input) {

@ -7,8 +7,7 @@ Template.membersList.helpers({
isGroupChat() {
const room = ChatRoom.findOne(this.rid, { reactive: false });
return ['c', 'p'].includes(room.t)
|| (RocketChat.roomTypes[room.t].isGroupChat && RocketChat.roomTypes[room.t].isGroupChat());
return RocketChat.roomTypes.roomTypes[room.t].isGroupChat();
},
isDirectChat() {
@ -88,12 +87,7 @@ Template.membersList.helpers({
const roomData = Session.get(`roomData${ this._id }`);
if (!roomData) { return ''; }
return (() => {
switch (roomData.t) {
case 'p': return RocketChat.authz.hasAtLeastOnePermission(['add-user-to-any-p-room', 'add-user-to-joined-room'], this._id);
case 'c': return RocketChat.authz.hasAtLeastOnePermission(['add-user-to-any-c-room', 'add-user-to-joined-room'], this._id);
default:
return (RocketChat.roomTypes[roomData.t].canAddUser && RocketChat.roomTypes[roomData.t].canAddUser(roomData)) || false;
}
return RocketChat.roomTypes.roomTypes[roomData.t].canAddUser(roomData);
})();
},
@ -139,8 +133,8 @@ Template.membersList.helpers({
tabBar: Template.currentData().tabBar,
username: Template.instance().userDetail.get(),
clear: Template.instance().clearUserDetail,
showAll: ['c', 'p'].includes(room != null ? room.t : undefined) || (RocketChat.roomTypes[room.t].userDetailShowAll && RocketChat.roomTypes[room.t].userDetailShowAll(room)),
hideAdminControls: ['c', 'p', 'd'].includes(room != null ? room.t : undefined) || !(RocketChat.roomTypes[room.t].userDetailShowAdmin && RocketChat.roomTypes[room.t].userDetailShowAdmin(room)),
showAll: RocketChat.roomTypes.roomTypes[room.t].userDetailShowAll(room) || false,
hideAdminControls: RocketChat.roomTypes.roomTypes[room.t].userDetailShowAdmin(room) || false,
video: ['d'].includes(room != null ? room.t : undefined)
};
},
@ -166,7 +160,7 @@ Template.membersList.events({
const roomData = Session.get(`roomData${ template.data.rid }`);
if (['c', 'p'].includes(roomData.t) || (RocketChat.roomTypes[roomData.t].canAddUser && RocketChat.roomTypes[roomData.t].canAddUser(roomData))) {
if (RocketChat.roomTypes.roomTypes[roomData.t].canAddUser(roomData)) {
return Meteor.call('addUserToRoom', { rid: roomData._id, username: doc.username }, function(error) {
if (error) {
return handleError(error);

@ -44,7 +44,7 @@ Template.createChannel.helpers({
autocomplete(key) {
const instance = Template.instance();
const param = instance.ac[key];
return typeof param === 'function' ? param.apply(instance.ac) : param;
return typeof param === 'function' ? param.apply(instance.ac): param;
},
items() {
return Template.instance().ac.filteredList();
@ -180,10 +180,10 @@ Template.createChannel.events({
}
if (!isPrivate) {
RocketChat.callbacks.run('aftercreateCombined', {_id: result.rid, name: result.name});
RocketChat.callbacks.run('aftercreateCombined', { _id: result.rid, name: result.name });
}
return FlowRouter.go(isPrivate ? 'group' : 'channel', {name: result.name}, FlowRouter.current().queryParams);
return FlowRouter.go(isPrivate ? 'group' : 'channel', { name: result.name }, FlowRouter.current().queryParams);
});
return false;
}
@ -205,7 +205,7 @@ Template.createChannel.onRendered(function() {
Template.createChannel.onCreated(function() {
this.selectedUsers = new ReactiveVar([]);
const filter = {exceptions: [Meteor.user().username].concat(this.selectedUsers.get().map(u => u.username))};
const filter = {exceptions :[Meteor.user().username].concat(this.selectedUsers.get().map(u => u.username))};
// this.onViewRead:??y(function() {
Deps.autorun(() => {
filter.exceptions = [Meteor.user().username].concat(this.selectedUsers.get().map(u => u.username));
@ -231,7 +231,7 @@ Template.createChannel.onCreated(function() {
this.ac = new AutoComplete(
{
selector: {
selector:{
item: '.rc-popup-list__item',
container: '.rc-popup-list__list'
},
@ -240,7 +240,7 @@ Template.createChannel.onCreated(function() {
inputDelay: 300,
rules: [
{
// @TODO maybe change this 'collection' and/or template
// @TODO maybe change this 'collection' and/or template
collection: 'UserAndRoom',
subscription: 'userAutocomplete',
field: 'username',
@ -248,7 +248,7 @@ Template.createChannel.onCreated(function() {
filter,
doNotChangeWidth: false,
selector(match) {
return {term: match};
return { term: match };
},
sort: 'username'
}

@ -10,7 +10,6 @@ Meteor.methods({
});
}
const fromId = Meteor.userId();
const room = RocketChat.models.Rooms.findOneById(rid);
if (!room) {
@ -19,9 +18,7 @@ Meteor.methods({
});
}
if (RocketChat.authz.hasPermission(fromId, `delete-${ room.t }`, rid)
//support custom room types verifying deletion differently
|| (RocketChat.roomTypes[room.t].canBeDeleted && RocketChat.roomTypes[room.t].canBeDeleted(fromId, room))) {
if (RocketChat.roomTypes.roomTypes[room.t].canBeDeleted(room)) {
RocketChat.models.Messages.removeByRoomId(rid);
RocketChat.models.Subscriptions.removeByRoomId(rid);
return RocketChat.models.Rooms.removeById(rid);

Loading…
Cancel
Save