Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block message editing based on admin settings #833

Merged
merged 6 commits into from
Sep 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions client/lib/chatMessages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ class @ChatMessages

return unless hasPermission or (editAllowed and editOwn)
return if element.classList.contains("system")

blockEditInMinutes = RocketChat.settings.get 'Message_AllowEditing_BlockEditInMinutes'
if blockEditInMinutes? and blockEditInMinutes isnt 0
msgTs = moment(message.ts) if message.ts?
currentTsDiff = moment().diff(msgTs, 'minutes') if msgTs?
if currentTsDiff > blockEditInMinutes
return

this.clearEditing()
this.input.value = message.msg
this.editing.element = element
Expand Down
9 changes: 9 additions & 0 deletions client/methods/updateMessage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ Meteor.methods
editOwn = originalMessage?.u?._id is Meteor.userId()

unless hasPermission or (editAllowed and editOwn)
toastr.error t('Message_editing_not_allowed')
throw new Meteor.Error 'message-editing-not-allowed', t('Message_editing_not_allowed')

blockEditInMinutes = RocketChat.settings.get 'Message_AllowEditing_BlockEditInMinutes'
if blockEditInMinutes? and blockEditInMinutes isnt 0
msgTs = moment(originalMessage.ts) if originalMessage.ts?
currentTsDiff = moment().diff(msgTs, 'minutes') if msgTs?
if currentTsDiff > blockEditInMinutes
toastr.error t('Message_editing_blocked')
throw new Meteor.Error 'message-editing-blocked'

Tracker.nonreactive ->

message.ets = new Date(Date.now() + TimeSync.serverOffset())
Expand Down
2 changes: 1 addition & 1 deletion client/views/admin/admin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Template.admin.helpers
sections: ->
group = FlowRouter.getParam('group')
group ?= Settings.findOne({ type: 'group' })?._id
settings = Settings.find({ group: group }, {sort: {section: 1}}).fetch()
settings = Settings.find({ group: group }, {sort: {section: 1, i18nLabel: 1}}).fetch()
sections = {}
for setting in settings
sections[setting.section or ''] ?= []
Expand Down
15 changes: 12 additions & 3 deletions client/views/app/message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ Template.message.helpers
pinned: ->
return this.pinned
canEdit: ->
if RocketChat.authz.hasAtLeastOnePermission('edit-message', this.rid )
return true
hasPermission = RocketChat.authz.hasAtLeastOnePermission('edit-message', this.rid)
isEditAllowed = RocketChat.settings.get 'Message_AllowEditing'
editOwn = this.u?._id is Meteor.userId()

return unless hasPermission or (isEditAllowed and editOwn)

return RocketChat.settings.get('Message_AllowEditing') and this.u?._id is Meteor.userId()
blockEditInMinutes = RocketChat.settings.get 'Message_AllowEditing_BlockEditInMinutes'
if blockEditInMinutes? and blockEditInMinutes isnt 0
msgTs = moment(this.ts) if this.ts?
currentTsDiff = moment().diff(msgTs, 'minutes') if msgTs?
return currentTsDiff < blockEditInMinutes
else
return true

canDelete: ->
if RocketChat.authz.hasAtLeastOnePermission('delete-message', this.rid )
Expand Down
2 changes: 2 additions & 0 deletions i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@
"Message" : "Message",
"Message_AllowDeleting" : "Allow Message Deleting",
"Message_AllowEditing" : "Allow Message Editing",
"Message_AllowEditing_BlockEditInMinutes" : "Block message editing after (in minutes - 0 to disable)",
"Message_AllowPinning" : "Allow Message Pinning",
"Message_deleting_not_allowed" : "Message deleting not allowed",
"Message_editing_not_allowed" : "Message editing not allowed",
"Message_editing_blocked" : "This message cannot be edited anymore",
"Message_MaxAllowedSize" : "Message max allowed size",
"Message_pinning_not_allowed" : "Message pinning not allowed",
"Message_KeepHistory" : "Keep Message History",
Expand Down
2 changes: 2 additions & 0 deletions i18n/pt.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@
"Message" : "Mensagem",
"Message_AllowDeleting" : "Permitir Exclusão de Mensagem",
"Message_AllowEditing" : "Permitir Edição de Mensagem",
"Message_AllowEditing_BlockEditInMinutes" : "Bloquear edição de mensagens após (em minutos - 0 para desabilitar)",
"Message_AllowPinning" : "Permitir Fixar Mensagem",
"Message_deleting_not_allowed" : "Exclusão de mensagem não permitido",
"Message_editing_not_allowed" : "Edição de mensagem não permitido",
"Message_editing_blocked" : "Esta mensagem não pode mais ser editada",
"Message_pinning_not_allowed" : "Não Permitir Fixar Mensagem",
"Message_KeepHistory" : "Manter Histórico de Mensagens",
"Message_removed" : "Mensagem removida",
Expand Down
14 changes: 13 additions & 1 deletion packages/rocketchat-lib/client/MessageAction.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,19 @@ Meteor.startup ->
input.focus()
, 200
validation: (message) ->
return RocketChat.authz.hasAtLeastOnePermission('edit-message', message.rid ) or RocketChat.settings.get('Message_AllowEditing') and message.u?._id is Meteor.userId()
hasPermission = RocketChat.authz.hasAtLeastOnePermission('edit-message', message.rid)
isEditAllowed = RocketChat.settings.get 'Message_AllowEditing'
editOwn = message.u?._id is Meteor.userId()

return unless hasPermission or (isEditAllowed and editOwn)

blockEditInMinutes = RocketChat.settings.get 'Message_AllowEditing_BlockEditInMinutes'
if blockEditInMinutes? and blockEditInMinutes isnt 0
msgTs = moment(message.ts) if message.ts?
currentTsDiff = moment().diff(msgTs, 'minutes') if msgTs?
return currentTsDiff < blockEditInMinutes
else
return true
order: 1

RocketChat.MessageAction.addButton
Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-lib/settings/server/startup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Meteor.startup ->

RocketChat.settings.addGroup 'Message'
RocketChat.settings.add 'Message_AllowEditing', true, { type: 'boolean', group: 'Message', public: true }
RocketChat.settings.add 'Message_AllowEditing_BlockEditInMinutes', 0, { type: 'int', group: 'Message', public: true }
RocketChat.settings.add 'Message_AllowDeleting', true, { type: 'boolean', group: 'Message', public: true }
RocketChat.settings.add 'Message_AllowPinning', true, { type: 'boolean', group: 'Message', public: true }
RocketChat.settings.add 'Message_ShowEditedStatus', true, { type: 'boolean', group: 'Message', public: true }
Expand Down
7 changes: 7 additions & 0 deletions server/methods/updateMessage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ Meteor.methods
unless hasPermission or (editAllowed and editOwn)
throw new Meteor.Error 'message-editing-not-allowed', "[methods] updateMessage -> Message editing not allowed"

blockEditInMinutes = RocketChat.settings.get 'Message_AllowEditing_BlockEditInMinutes'
if blockEditInMinutes? and blockEditInMinutes isnt 0
msgTs = moment(originalMessage.ts) if originalMessage.ts?
currentTsDiff = moment().diff(msgTs, 'minutes') if msgTs?
if currentTsDiff > blockEditInMinutes
throw new Meteor.Error 'message-editing-blocked'

console.log '[methods] updateMessage -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments

# If we keep history of edits, insert a new message to store history information
Expand Down