Skip to content

Commit

Permalink
Preventing messages from being edited based on settings (should close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
gmsecrieru committed Sep 18, 2015
1 parent f6bb3a4 commit 8964610
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions client/lib/chatMessages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class @ChatMessages

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

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

this.clearEditing()
this.input.value = message.msg
this.editing.element = element
Expand Down
8 changes: 8 additions & 0 deletions client/methods/updateMessage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ 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')

msgTs = moment(originalMessage.ts) if originalMessage.ts?
currentTsDiff = moment().diff(msgTs, 'minutes') if msgTs?

if currentTsDiff > RocketChat.settings.get 'Message_AllowEditing_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
12 changes: 9 additions & 3 deletions client/views/app/message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ 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)

msgTs = moment(this.ts) if this.ts?
currentTsDiff = moment().diff(msgTs, 'minutes') if msgTs?

return RocketChat.settings.get('Message_AllowEditing') and this.u?._id is Meteor.userId()
return currentTsDiff < RocketChat.settings.get 'Message_AllowEditing_BlockEditInMinutes'

canDelete: ->
if RocketChat.authz.hasAtLeastOnePermission('delete-message', this.rid )
Expand Down
11 changes: 10 additions & 1 deletion packages/rocketchat-lib/client/MessageAction.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ 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)

msgTs = moment(message.ts) if message.ts?
currentTsDiff = moment().diff(msgTs, 'minutes') if msgTs?

return currentTsDiff < RocketChat.settings.get 'Message_AllowEditing_BlockEditInMinutes'
order: 1

RocketChat.MessageAction.addButton
Expand Down
6 changes: 6 additions & 0 deletions server/methods/updateMessage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Meteor.methods
unless hasPermission or (editAllowed and editOwn)
throw new Meteor.Error 'message-editing-not-allowed', "[methods] updateMessage -> Message editing not allowed"

msgTs = moment(originalMessage.ts) if originalMessage.ts?
currentTsDiff = moment().diff(msgTs, 'minutes') if msgTs?

if currentTsDiff > RocketChat.settings.get 'Message_AllowEditing_BlockEditInMinutes'
throw new Meteor.Error 'message-editing-blocked', "[methods] updateMessage -> 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

0 comments on commit 8964610

Please sign in to comment.