Changed edit to editedAt and editedBy

pull/1361/head
Marcelo Schmidt 10 years ago
parent 787db34516
commit 0c27f0a616
  1. 14
      client/methods/updateMessage.coffee
  2. 23
      client/views/app/message.coffee
  3. 1
      packages/rocketchat-lib/server/functions/setUsername.coffee
  4. 41
      packages/rocketchat-lib/server/models/Messages.coffee
  5. 2
      server/methods/loadHistory.coffee
  6. 2
      server/methods/loadMissedMessages.coffee
  7. 9
      server/methods/updateMessage.coffee
  8. 10
      server/startup/migrations/v22.coffee
  9. 2
      server/stream/messages.coffee

@ -9,6 +9,8 @@ Meteor.methods
editAllowed = RocketChat.settings.get 'Message_AllowEditing'
editOwn = originalMessage?.u?._id is Meteor.userId()
me = Meteor.users.findOne 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')
@ -23,9 +25,11 @@ Meteor.methods
Tracker.nonreactive ->
message.edit =
by: Meteor.userId()
at: new Date(Date.now() + TimeSync.serverOffset())
message.editedAt = new Date(Date.now() + TimeSync.serverOffset())
message.editedBy =
_id: Meteor.userId()
username: me.username
message = RocketChat.callbacks.run 'beforeSaveMessage', message
ChatMessage.update
@ -33,6 +37,6 @@ Meteor.methods
'u._id': Meteor.userId()
,
$set:
"edit.by": message.edit.by
"edit.at": message.edit.at
"editedAt": message.editedAt
"editedBy": message.editedBy
msg: message.msg

@ -1,5 +1,3 @@
wasEdited = (msg) ->
msg.edit?.at? and msg.t not in ['s', 'p', 'f', 'r', 'au', 'ru', 'ul', 'nu', 'wm', 'uj', 'rm']
Template.message.helpers
actions: ->
return RocketChat.MessageAction.getButtons(this)
@ -48,23 +46,16 @@ Template.message.helpers
system: ->
return 'system' if this.t in ['s', 'p', 'f', 'r', 'au', 'ru', 'ul', 'nu', 'wm', 'uj', 'rm']
edited: -> wasEdited(@)
edited: -> Template.instance().wasEdited?(@)
editTime: ->
return "" unless wasEdited(@)
moment(@edit.at).format('LL hh:mma') #TODO profile pref for 12hr/24hr clock?
return "" unless Template.instance().wasEdited?(@)
moment(@editedAt).format('LL hh:mma') #TODO profile pref for 12hr/24hr clock?
editedBy: ->
return "" unless wasEdited(@)
return "" unless Template.instance().wasEdited?(@)
# try to return the username of the editor,
# otherwise a special "?" character that will be
# rendered as a special avatar
if @edit.by
user = Meteor.users.findOne(@edit.by)
if user?
user.username
else
"?"
else
"?"
return @editedBy?.username or "?"
pinned: ->
return this.pinned
canEdit: ->
@ -99,6 +90,10 @@ Template.message.helpers
else if @label
return @label
Template.message.onCreated ->
@wasEdited = (msg) ->
msg.editedAt? and msg.t not in ['s', 'p', 'f', 'r', 'au', 'ru', 'ul', 'nu', 'wm', 'uj', 'rm']
Template.message.onViewRendered = (context) ->
view = this
this._domrange.onAttached (domRange) ->

@ -19,6 +19,7 @@ RocketChat.setUsername = (user, username) ->
# Username is available; if coming from old username, update all references
if previousUsername
RocketChat.models.Messages.updateAllUsernamesByUserId user._id, username
RocketChat.models.Messages.updateUsernameOfEditByUserId user._id, username
RocketChat.models.Messages.findByMention(previousUsername).forEach (msg) ->
updatedMsg = msg.msg.replace(new RegExp("@#{previousUsername}", "ig"), "@#{username}")

@ -3,8 +3,8 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
@_initModel 'message'
@tryEnsureIndex { 'rid': 1, 'ts': 1 }
@tryEnsureIndex { 'edit.at': 1 }, { sparse: 1 }
@tryEnsureIndex { 'edit.by': 1 }, { sparse: 1 }
@tryEnsureIndex { 'editedAt': 1 }, { sparse: 1 }
@tryEnsureIndex { 'editedBy._id': 1 }, { sparse: 1 }
@tryEnsureIndex { 'rid': 1, 't': 1, 'u._id': 1 }
@tryEnsureIndex { 'expireAt': 1 }, { expireAfterSeconds: 0 }
@tryEnsureIndex { 'msg': 'text' }
@ -77,7 +77,7 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
ts:
$gt: timestamp
,
'edit.at':
'editedAt':
$gt: timestamp
]
@ -94,12 +94,14 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
return @find query, options
cloneAndSaveAsHistoryById: (_id) ->
me = RocketChat.models.Users.findOneById Meteor.userId()
record = @findOneById _id
record._hidden = true
record.parent = record._id
record.edit =
at: new Date()
by: Meteor.userId()
record.editedAt = new Date
record.editedBy =
_id: Meteor.userId()
username: me.username
delete record._id
return @insert record
@ -117,6 +119,7 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
return @update query, update
setAsDeletedById: (_id) ->
me = RocketChat.models.Users.findOneById Meteor.userId()
query =
_id: _id
@ -124,8 +127,10 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
$set:
msg: ''
t: 'rm'
'edit.at': new Date()
'edit.by': Meteor.userId()
editedAt: new Date()
editedBy:
_id: Meteor.userId()
username: me.username
return @update query, update
@ -161,6 +166,16 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
return @update query, update, { multi: true }
updateUsernameOfEditByUserId: (userId, username) ->
query =
'editedBy._id': userId
update =
$set:
"editedBy.username": username
return @update query, update, { multi: true }
updateUsernameAndMessageOfMentionByIdAndOldUsername: (_id, oldUsername, newUsername, newMessage) ->
query =
_id: _id
@ -188,6 +203,16 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
return @update query, update
upgradeEtsToEditAt: ->
query =
ets: { $exists: 1 }
update =
$rename:
"ets": "editedAt"
return @update query, update, { multi: true }
# INSERT
createWithTypeRoomIdMessageAndUser: (type, roomId, message, user, extraData) ->
record =

@ -12,7 +12,7 @@ Meteor.methods
limit: limit
if not RocketChat.settings.get 'Message_ShowEditedStatus'
options.fields = { 'edit.at': 0 }
options.fields = { 'editedAt': 0 }
if end?
records = RocketChat.models.Messages.findVisibleByRoomIdBeforeTimestamp(rid, end, options).fetch()

@ -11,6 +11,6 @@ Meteor.methods
ts: -1
if not RocketChat.settings.get 'Message_ShowEditedStatus'
options.fields = { 'edit.at': 0 }
options.fields = { 'editedAt': 0 }
return RocketChat.models.Messages.findVisibleByRoomIdAfterTimestamp(rid, start, options).fetch()

@ -12,6 +12,8 @@ Meteor.methods
editAllowed = RocketChat.settings.get 'Message_AllowEditing'
editOwn = originalMessage?.u?._id is Meteor.userId()
me = RocketChat.models.Users.findOneById Meteor.userId()
unless hasPermission or (editAllowed and editOwn)
throw new Meteor.Error 'message-editing-not-allowed', "[methods] updateMessage -> Message editing not allowed"
@ -28,9 +30,10 @@ Meteor.methods
if RocketChat.settings.get 'Message_KeepHistory'
RocketChat.models.Messages.cloneAndSaveAsHistoryById originalMessage._id
message.edit =
at: new Date()
by: Meteor.userId()
message.editedAt = new Date()
message.editedBy =
_id: Meteor.userId()
username: me.username
if urls = message.msg.match /([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\w]+)?\??([-\+=&!:;%@\/\.\,\w]+)?#?([\w]+)?)?/g
message.urls = urls.map (url) -> url: url

@ -0,0 +1,10 @@
Meteor.startup ->
Migrations.add
version: 22
up: ->
###
# Update message edit field
###
RocketChat.models.Messages.upgradeEtsToEditAt()
console.log 'Updated old messages\' ets edited timestamp to new editedAt timestamp.'

@ -23,7 +23,7 @@ Meteor.startup ->
options = {}
if not RocketChat.settings.get 'Message_ShowEditedStatus'
options.fields = { 'edit.at': 0 }
options.fields = { 'editedAt': 0 }
RocketChat.models.Messages.findVisibleCreatedOrEditedAfterTimestamp(new Date(), options).observe
added: (record) ->

Loading…
Cancel
Save