The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/packages/rocketchat-channel-settings/server/methods/saveRoomSettings.js

146 lines
4.3 KiB

const fields = ['roomName', 'roomTopic', 'roomAnnouncement', 'roomCustomFields', 'roomDescription', 'roomType', 'readOnly', 'reactWhenReadOnly', 'systemMessages', 'default', 'joinCode', 'tokenpass', 'streamingOptions'];
9 years ago
Meteor.methods({
saveRoomSettings(rid, settings, value) {
9 years ago
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
'function': 'RocketChat.saveRoomName'
});
}
if (!Match.test(rid, String)) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
method: 'saveRoomSettings'
});
}
if (typeof settings !== 'object') {
settings = {
[settings] : value
};
}
if (!Object.keys(settings).every(key => fields.includes(key))) {
9 years ago
throw new Meteor.Error('error-invalid-settings', 'Invalid settings provided', {
method: 'saveRoomSettings'
});
}
9 years ago
if (!RocketChat.authz.hasPermission(Meteor.userId(), 'edit-room', rid)) {
throw new Meteor.Error('error-action-not-allowed', 'Editing room is not allowed', {
method: 'saveRoomSettings',
action: 'Editing_room'
});
}
const room = RocketChat.models.Rooms.findOneById(rid);
if (room.broadcast && (settings.readOnly || settings.reactWhenReadOnly)) {
throw new Meteor.Error('error-action-not-allowed', 'Editing readOnly/reactWhenReadOnly are not allowed for broadcast rooms', {
method: 'saveRoomSettings',
action: 'Editing_room'
});
}
if (!room) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
method: 'saveRoomSettings'
9 years ago
});
}
const user = Meteor.user();
Object.keys(settings).forEach(setting => {
const value = settings[setting];
if (settings === 'default' && !RocketChat.authz.hasPermission(this.userId, 'view-room-administration')) {
throw new Meteor.Error('error-action-not-allowed', 'Viewing room administration is not allowed', {
method: 'saveRoomSettings',
action: 'Viewing_room_administration'
});
}
9 years ago
if (setting === 'roomType' && value !== room.t && value === 'c' && !RocketChat.authz.hasPermission(this.userId, 'create-c')) {
throw new Meteor.Error('error-action-not-allowed', 'Changing a private group to a public channel is not allowed', {
method: 'saveRoomSettings',
action: 'Change_Room_Type'
});
}
if (setting === 'roomType' && value !== room.t && value === 'p' && !RocketChat.authz.hasPermission(this.userId, 'create-p')) {
throw new Meteor.Error('error-action-not-allowed', 'Changing a public channel to a private room is not allowed', {
method: 'saveRoomSettings',
action: 'Change_Room_Type'
});
}
});
Object.keys(settings).forEach(setting => {
const value = settings[setting];
9 years ago
switch (setting) {
case 'roomName':
RocketChat.saveRoomName(rid, value, user);
9 years ago
break;
case 'roomTopic':
if (value !== room.topic) {
RocketChat.saveRoomTopic(rid, value, user);
9 years ago
}
break;
case 'roomAnnouncement':
if (value !== room.announcement) {
RocketChat.saveRoomAnnouncement(rid, value, user);
9 years ago
}
break;
case 'roomCustomFields':
if (value !== room.customFields) {
RocketChat.saveRoomCustomFields(rid, value);
}
break;
9 years ago
case 'roomDescription':
if (value !== room.description) {
RocketChat.saveRoomDescription(rid, value, user);
9 years ago
}
break;
case 'roomType':
if (value !== room.t) {
RocketChat.saveRoomType(rid, value, user);
9 years ago
}
break;
case 'tokenpass':
check(value, {
require: String,
tokens: [{
token: String,
balance: String
}]
});
RocketChat.saveRoomTokenpass(rid, value);
break;
case 'streamingOptions':
RocketChat.saveStreamingOptions(rid, value);
break;
9 years ago
case 'readOnly':
if (value !== room.ro) {
RocketChat.saveRoomReadOnly(rid, value, user);
9 years ago
}
break;
case 'reactWhenReadOnly':
if (value !== room.reactWhenReadOnly) {
RocketChat.saveReactWhenReadOnly(rid, value, user);
9 years ago
}
break;
case 'systemMessages':
if (value !== room.sysMes) {
RocketChat.saveRoomSystemMessages(rid, value, user);
9 years ago
}
break;
case 'joinCode':
RocketChat.models.Rooms.setJoinCodeById(rid, String(value));
break;
case 'default':
RocketChat.models.Rooms.saveDefaultById(rid, value);
}
});
9 years ago
return {
result: true,
rid: room._id
};
}
});