Skip to content

Commit

Permalink
Merge pull request #9009 from RocketChat/assistify-core/#8264-room-types
Browse files Browse the repository at this point in the history
[NEW] Improve room types API and usages
  • Loading branch information
rodrigok authored Dec 4, 2017
2 parents f18e601 + 5f068ca commit 9ed7613
Show file tree
Hide file tree
Showing 37 changed files with 1,059 additions and 439 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import toastr from 'toastr';
import s from 'underscore.string';

import { RocketChat, RoomSettingsEnum } from 'meteor/rocketchat:lib';

Template.channelSettings.helpers({
toArray(obj) {
return Object.keys(obj).map((key) => {
Expand All @@ -22,12 +24,6 @@ Template.channelSettings.helpers({
}
return obj && obj[key];
},
showSetting(setting, room) {
if (setting.showInDirect === false) {
return room.t !== 'd';
}
return true;
},
settings() {
return Template.instance().settings;
},
Expand All @@ -53,15 +49,17 @@ Template.channelSettings.helpers({
t: 1
}
});

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, {
fields: {
ro: 1
}
});

return room && room.ro;
},
has(v, key) {
Expand All @@ -73,6 +71,7 @@ Template.channelSettings.helpers({
ro: 1
}
});

return t(room && room.ro ? 'True' : 'False');
},
showingValue(field) {
Expand Down Expand Up @@ -164,7 +163,7 @@ Template.channelSettings.onCreated(function() {
type: 'text',
label: 'Name',
canView(room) {
return room.t !== 'd';
return RocketChat.roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.NAME);
},
canEdit(room) {
return RocketChat.authz.hasAllPermission('edit-room', room._id);
Expand All @@ -173,19 +172,18 @@ Template.channelSettings.onCreated(function() {
if (RocketChat.settings.get('UI_Allow_room_names_with_special_chars')) {
return room.fname || room.name;
}

return room.name;
},
save(value, room) {
let nameValidation;
if (!RocketChat.authz.hasAllPermission('edit-room', room._id) || (room.t !== 'c' && room.t !== 'p')) {
return toastr.error(t('error-not-allowed'));
}
if (!RocketChat.settings.get('UI_Allow_room_names_with_special_chars')) {
try {
nameValidation = new RegExp(`^${ RocketChat.settings.get('UTF8_Names_Validation') }$`);
} catch (error1) {
nameValidation = new RegExp('^[0-9a-zA-Z-_.]+$');
}

if (!nameValidation.test(value)) {
return toastr.error(t('error-invalid-room-name', {
room_name: {
Expand All @@ -194,32 +192,36 @@ Template.channelSettings.onCreated(function() {
}));
}
}
Meteor.call('saveRoomSettings', room._id, 'roomName', value, function(err) {

Meteor.call('saveRoomSettings', room._id, RoomSettingsEnum.NAME, value, function(err) {
if (err) {
return handleError(err);
}

RocketChat.callbacks.run('roomNameChanged', {
_id: room._id,
name: value
});

return toastr.success(TAPi18n.__('Room_name_changed_successfully'));
});
}
},
topic: {
type: 'markdown',
label: 'Topic',
canView() {
return true;
canView(room) {
return RocketChat.roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.TOPIC);
},
canEdit(room) {
return RocketChat.authz.hasAllPermission('edit-room', room._id);
},
save(value, room) {
return Meteor.call('saveRoomSettings', room._id, 'roomTopic', value, function(err) {
return Meteor.call('saveRoomSettings', room._id, RoomSettingsEnum.TOPIC, value, function(err) {
if (err) {
return handleError(err);
}

toastr.success(TAPi18n.__('Room_topic_changed_successfully'));
return RocketChat.callbacks.run('roomTopicChanged', room);
});
Expand All @@ -228,17 +230,18 @@ Template.channelSettings.onCreated(function() {
announcement: {
type: 'markdown',
label: 'Announcement',
canView() {
return true;
canView(room) {
return RocketChat.roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.ANNOUNCEMENT);
},
canEdit(room) {
return RocketChat.authz.hasAllPermission('edit-room', room._id);
},
save(value, room) {
return Meteor.call('saveRoomSettings', room._id, 'roomAnnouncement', value, function(err) {
return Meteor.call('saveRoomSettings', room._id, RoomSettingsEnum.ANNOUNCEMENT, value, function(err) {
if (err) {
return handleError(err);
}

toastr.success(TAPi18n.__('Room_announcement_changed_successfully'));
return RocketChat.callbacks.run('roomAnnouncementChanged', room);
});
Expand All @@ -248,16 +251,17 @@ Template.channelSettings.onCreated(function() {
type: 'text',
label: 'Description',
canView(room) {
return room.t !== 'd';
return RocketChat.roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.DESCRIPTION);
},
canEdit(room) {
return RocketChat.authz.hasAllPermission('edit-room', room._id);
},
save(value, room) {
return Meteor.call('saveRoomSettings', room._id, 'roomDescription', value, function(err) {
return Meteor.call('saveRoomSettings', room._id, RoomSettingsEnum.DESCRIPTION, value, function(err) {
if (err) {
return handleError(err);
}

return toastr.success(TAPi18n.__('Room_description_changed_successfully'));
});
}
Expand All @@ -278,7 +282,7 @@ Template.channelSettings.onCreated(function() {
}
},
canView(room) {
if (['c', 'p'].includes(room.t) === false) {
if (!['c', 'p'].includes(room.t)) {
return false;
} else if (room.t === 'p' && !RocketChat.authz.hasAllPermission('create-c')) {
return false;
Expand Down Expand Up @@ -332,17 +336,18 @@ Template.channelSettings.onCreated(function() {
isToggle: true,
processing: new ReactiveVar(false),
canView(room) {
return room.t !== 'd';
return RocketChat.roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.READ_ONLY);
},
canEdit(room) {
return RocketChat.authz.hasAllPermission('set-readonly', room._id);
},
save(value, room) {
this.processing.set(true);
return Meteor.call('saveRoomSettings', room._id, 'readOnly', value, (err) => {
return Meteor.call('saveRoomSettings', room._id, RoomSettingsEnum.READ_ONLY, value, (err) => {
if (err) {
return handleError(err);
}

this.processing.set(false);
return toastr.success(TAPi18n.__('Read_only_changed_successfully'));
});
Expand All @@ -354,7 +359,7 @@ Template.channelSettings.onCreated(function() {
isToggle: true,
processing: new ReactiveVar(false),
canView(room) {
return room.t !== 'd' && room.ro;
return RocketChat.roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.REACT_WHEN_READ_ONLY) && room.ro;
},
canEdit(room) {
return RocketChat.authz.hasAllPermission('set-react-when-readonly', room._id);
Expand All @@ -365,6 +370,7 @@ Template.channelSettings.onCreated(function() {
if (err) {
return handleError(err);
}

this.processing.set(false);
return toastr.success(TAPi18n.__('React_when_read_only_changed_successfully'));
});
Expand All @@ -376,7 +382,7 @@ Template.channelSettings.onCreated(function() {
isToggle: true,
processing: new ReactiveVar(false),
canView(room) {
return room.t !== 'd';
return RocketChat.roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.ARCHIVE_OR_UNARCHIVE);
},
canEdit(room) {
return RocketChat.authz.hasAtLeastOnePermission(['archive-room', 'unarchive-room'], room._id);
Expand All @@ -400,13 +406,15 @@ Template.channelSettings.onCreated(function() {
swal.enableButtons();
handleError(err);
}

swal({
title: value ? t('Room_archived') : t('Room_has_been_archived'),
text: value ? t('Room_has_been_archived') : t('Room_has_been_unarchived'),
type: 'success',
timer: 2000,
showConfirmButton: false
});

return RocketChat.callbacks.run(action, room);
});
} else {
Expand All @@ -421,7 +429,7 @@ Template.channelSettings.onCreated(function() {
showingValue: new ReactiveVar(false),
realValue: null,
canView(room) {
return room.t === 'c' && RocketChat.authz.hasAllPermission('edit-room', room._id);
return RocketChat.roomTypes.roomTypes[room.t].allowRoomSettingChange(room, RoomSettingsEnum.JOIN_CODE) && RocketChat.authz.hasAllPermission('edit-room', room._id);
},
canEdit(room) {
return RocketChat.authz.hasAllPermission('edit-room', room._id);
Expand Down Expand Up @@ -462,6 +470,7 @@ Template.channelSettings.onCreated(function() {
if (err) {
return handleError(err);
}

toastr.success(TAPi18n.__('Room_password_changed_successfully'));
return RocketChat.callbacks.run('roomCodeChanged', room);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

RocketChat.saveRoomName = function(rid, displayName, user, sendMessage = true) {
const room = RocketChat.models.Rooms.findOneById(rid);
if (room.t !== 'c' && room.t !== 'p') {
if (RocketChat.roomTypes.roomTypes[room.t].preventRenaming()) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
'function': 'RocketChat.saveRoomdisplayName'
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* globals RocketChatTabBar */
import s from 'underscore.string';

import { RocketChatTabBar } from 'meteor/rocketchat:lib';

Template.adminSounds.helpers({
isReady() {
if (Template.instance().ready != null) {
Expand Down
3 changes: 2 additions & 1 deletion packages/rocketchat-emoji-custom/admin/adminEmoji.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* globals RocketChatTabBar */
import s from 'underscore.string';

import { RocketChatTabBar } from 'meteor/rocketchat:lib';

Template.adminEmoji.helpers({
isReady() {
if (Template.instance().ready != null) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9ed7613

Please sign in to comment.