Skip to content
Snippets Groups Projects
channelSettings.coffee 7.29 KiB
Newer Older
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import toastr from 'toastr'
Template.channelSettings.helpers
	toArray: (obj) ->
		arr = []
		for key, value of obj
			arr.push
				$key: key
				$value: value
		return arr

	valueOf: (obj, key) ->
		return obj?[key]

	showSetting: (setting, room) ->
		if setting.showInDirect is false
			return room.t isnt 'd'
		return true

	settings: ->
		return Template.instance().settings

	getRoom: ->
		return ChatRoom.findOne(@rid)

Marcelo Schmidt's avatar
Marcelo Schmidt committed
	editing: (field) ->
		return Template.instance().editing.get() is field
Marcelo Schmidt's avatar
Marcelo Schmidt committed
	channelSettings: ->
		return RocketChat.ChannelSettings.getOptions()

	unscape: (value) ->
		return s.unescapeHTML value

	canDeleteRoom: ->
		roomType = ChatRoom.findOne(@rid, { fields: { t: 1 }})?.t
		return roomType? and RocketChat.authz.hasAtLeastOnePermission("delete-#{roomType}", @rid)
Karl Prieb's avatar
Karl Prieb committed

	readOnly: ->
		return  ChatRoom.findOne(@rid, { fields: { ro: 1 }})?.ro
Karl Prieb's avatar
Karl Prieb committed

	readOnlyDescription: ->
		readOnly = ChatRoom.findOne(@rid, { fields: { ro: 1 }})?.ro
		if readOnly is true
			return t('True')
		else
			return t('False')
Marcelo Schmidt's avatar
Marcelo Schmidt committed
Template.channelSettings.events
	'click .delete': ->
		swal {
			title: t('Are_you_sure')
			text: t('Delete_Room_Warning')
			type: 'warning'
			showCancelButton: true
			confirmButtonColor: '#DD6B55'
			confirmButtonText: t('Yes_delete_it')
			cancelButtonText: t('Cancel')
			closeOnConfirm: false
			html: false
		}, =>
			swal.disableButtons()

			Meteor.call 'eraseRoom', @rid, (error, result) ->
				if error
					handleError(error)
					swal.enableButtons()
				else
					swal
						title: t('Deleted')
						text: t('Room_has_been_deleted')
						type: 'success'
						timer: 2000
						showConfirmButton: false

	'keydown input[type=text]': (e, t) ->
		if e.keyCode is 13
			e.preventDefault()
			t.saveSetting()
Marcelo Schmidt's avatar
Marcelo Schmidt committed

	'click [data-edit]': (e, t) ->
		e.preventDefault()
		t.editing.set($(e.currentTarget).data('edit'))
		setTimeout (-> t.$('input.editing').focus().select()), 100

Karl Prieb's avatar
Karl Prieb committed
	'change [type="radio"]': (e, t) ->
		t.editing.set($(e.currentTarget).attr('name'))

	'change [type="checkbox"]': (e, t) ->
		t.editing.set($(e.currentTarget).attr('name'))
		t.saveSetting()

	'click .cancel': (e, t) ->
		e.preventDefault()
		t.editing.set()
Marcelo Schmidt's avatar
Marcelo Schmidt committed
	'click .save': (e, t) ->
		e.preventDefault()
		t.saveSetting()
Template.channelSettings.onCreated ->
	@editing = new ReactiveVar

	@settings =
		name:
			type: 'text'
			label: 'Name'
			canView: (room) => room.t isnt 'd'
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
				if not RocketChat.authz.hasAllPermission('edit-room', room._id) or room.t not in ['c', 'p']
					return toastr.error t('error-not-allowed')

				try
					nameValidation = new RegExp '^' + RocketChat.settings.get('UTF8_Names_Validation') + '$'
				catch
					nameValidation = new RegExp '^[0-9a-zA-Z-_.]+$'

				if not nameValidation.test value
					return toastr.error t('error-invalid-room-name', { room_name: name: value })

Maki Nishifuji's avatar
Maki Nishifuji committed
				RocketChat.callbacks.run 'roomNameChanged', { _id: room._id, name: value }
				Meteor.call 'saveRoomSettings', room._id, 'roomName', value, (err, result) ->
					return handleError err if err
					toastr.success TAPi18n.__ 'Room_name_changed_successfully'

		topic:
			type: 'markdown'
			label: 'Topic'
			canView: (room) => true
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
				Meteor.call 'saveRoomSettings', room._id, 'roomTopic', value, (err, result) ->
					return handleError err if err
					toastr.success TAPi18n.__ 'Room_topic_changed_successfully'
					RocketChat.callbacks.run 'roomTopicChanged', room

		description:
			type: 'text'
			label: 'Description'
			canView: (room) => room.t isnt 'd'
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
				Meteor.call 'saveRoomSettings', room._id, 'roomDescription', value, (err, result) ->
					return handleError err if err
					toastr.success TAPi18n.__ 'Room_description_changed_successfully'

		t:
Karl Prieb's avatar
Karl Prieb committed
			type: 'boolean'
			label: 'Private'
			isToggle: true
			processing: new ReactiveVar(false)
			canView: (room) ->
				if not room.t in ['c', 'p']
					return false
				else if room.t is 'p' and not RocketChat.authz.hasAllPermission('create-c')
					return false
				else if room.t is 'c' and not RocketChat.authz.hasAllPermission('create-p')
					return false
				return true
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
Karl Prieb's avatar
Karl Prieb committed
				@processing.set(true)
				value = if value then 'p' else 'c'
				RocketChat.callbacks.run 'roomTypeChanged', room
Karl Prieb's avatar
Karl Prieb committed
				Meteor.call 'saveRoomSettings', room._id, 'roomType', value, (err, result) =>
					return handleError err if err
Karl Prieb's avatar
Karl Prieb committed
					@processing.set(false)
					toastr.success TAPi18n.__ 'Room_type_changed_successfully'

		ro:
			type: 'boolean'
			label: 'Read_only'
Karl Prieb's avatar
Karl Prieb committed
			isToggle: true
			processing: new ReactiveVar(false)
			canView: (room) => room.t isnt 'd'
			canEdit: (room) => RocketChat.authz.hasAllPermission('set-readonly', room._id)
			save: (value, room) ->
Karl Prieb's avatar
Karl Prieb committed
				@processing.set(true)
				Meteor.call 'saveRoomSettings', room._id, 'readOnly', value, (err, result) =>
					return handleError err if err
Karl Prieb's avatar
Karl Prieb committed
					@processing.set(false)
Maki Nishifuji's avatar
Maki Nishifuji committed
					toastr.success TAPi18n.__ 'Read_only_changed_successfully'
		archived:
			type: 'boolean'
			label: 'Room_archivation_state_true'
Karl Prieb's avatar
Karl Prieb committed
			isToggle: true,
			canView: (room) => room.t isnt 'd'
			canEdit: (room) => RocketChat.authz.hasAtLeastOnePermission(['archive-room', 'unarchive-room'], room._id)
Karl Prieb's avatar
Karl Prieb committed
			save: (value, room) =>
				swal {
					title: t('Are_you_sure')
					type: 'warning'
					showCancelButton: true
					confirmButtonColor: '#DD6B55'
					confirmButtonText: if value then t('Yes_archive_it') else t('Yes_unarchive_it')
					cancelButtonText: t('Cancel')
					closeOnConfirm: false
					html: false
				}, (confirmed) =>
					swal.disableButtons()
					if (confirmed)
						action = if value then 'archiveRoom' else 'unarchiveRoom'
						Meteor.call action, room._id, (err, results) =>
							if err
								swal.enableButtons()
								handleError err
							swal
								title: if value then t('Room_archived') else t('Room_has_been_archived')
								text: if value then t('Room_has_been_archived') else t('Room_has_been_unarchived')
								type: 'success'
								timer: 2000
								showConfirmButton: false
							RocketChat.callbacks.run action, room
					else
						$(".channel-settings form [name='archived']").prop('checked', room.archived)
		joinCode:
			type: 'text'
			label: 'Code'
			canView: (room) => room.t is 'c' and RocketChat.authz.hasAllPermission('edit-room', room._id)
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
				Meteor.call 'saveRoomSettings', room._id, 'joinCode', value, (err, result) ->
					return handleError err if err
					toastr.success TAPi18n.__ 'Room_code_changed_successfully'
					RocketChat.callbacks.run 'roomCodeChanged', room
	@saveSetting = =>
		room = ChatRoom.findOne @data?.rid
		field = @editing.get()
		if @settings[field].type is 'select'
			value = @$(".channel-settings form [name=#{field}]:checked").val()
		else if @settings[field].type is 'boolean'
Karl Prieb's avatar
Karl Prieb committed
			value = @$(".channel-settings form [name=#{field}]").is(":checked")
		else
			value = @$(".channel-settings form [name=#{field}]").val()
		if value isnt room[field]
			@settings[field].save(value, room)
		@editing.set()