Skip to content
Snippets Groups Projects
Commit 831bfd8c authored by Rodrigo Nascimento's avatar Rodrigo Nascimento
Browse files

Replace all ChatMessage.update

parent 6e97e2ba
No related branches found
No related tags found
No related merge requests found
......@@ -18,11 +18,11 @@ RocketChat.setUsername = (user, username) ->
# Username is available; if coming from old username, update all references
if previousUsername
ChatMessage.update { "u._id": user._id }, { $set: { "u.username": username } }, { multi: true }
RocketChat.models.Messages.updateAllUsernamesByUserId user._id, username
RocketChat.models.Messages.findByMention(previousUsername).forEach (msg) ->
updatedMsg = msg.msg.replace(new RegExp("@#{previousUsername}", "ig"), "@#{username}")
ChatMessage.update { _id: msg._id, "mentions.username": previousUsername }, { $set: { "mentions.$.username": username, "msg": updatedMsg } }
RocketChat.models.Messages.updateUsernameAndMessageOfMentionByIdAndOldUsername msg._id, previousUsername, username, updatedMsg
RocketChat.models.Rooms.replaceUsername previousUsername, username
RocketChat.models.Rooms.replaceUsernameOfUserByUserId user._id, username
......
......@@ -103,6 +103,72 @@ RocketChat.models.Messages = new class asd extends RocketChat.models._Base
# return @update query, update
setHiddenById: (_id, hidden=true) ->
query =
_id: _id
update =
$set:
_hidden: hidden
return @update query, update
setAsDeletedById: (_id) ->
query =
_id: _id
update =
$set:
msg: ''
t: 'rm'
ets: new Date()
return @update query, update
setPinnedByIdAndUserId: (_id, userId, pinned=true) ->
query =
_id: _id
'u._id': userId
update =
$set:
pinned: pinned
pts: new Date
return @update query, update
setUrlsById: (_id, urls) ->
query =
_id: _id
update =
$set:
urls: urls
return @update query, update
updateAllUsernamesByUserId: (userId, username) ->
query =
'u._id': userId
update =
$set:
"u.username": username
return @update query, update, { multi: true }
updateUsernameAndMessageOfMentionByIdAndOldUsername: (_id, oldUsername, newUsername, newMessage) ->
query =
_id: _id
"mentions.username": oldUsername
update =
$set:
"mentions.$.username": newUsername
"msg": newMessage
return @update query, update
# INSERT
createWithTypeRoomIdMessageAndUser: (type, roomId, message, user, extraData) ->
......
......@@ -167,7 +167,7 @@ OEmbed.RocketUrlParser = (message) ->
changed = true
if changed is true
ChatMessage.update {_id: message._id}, { $set: { urls: message.urls } }
RocketChat.models.Messages.setUrlsById message._id, message.urls
return message
......
@ChatMessage = RocketChat.models.Messages.model
@MapReducedStatistics = new Mongo.Collection 'rocketchat_mr_statistics'
@ChatReports = new Meteor.Collection 'rocketchat_reports'
......@@ -20,26 +20,17 @@ Meteor.methods
keepHistory = RocketChat.settings.get 'Message_KeepHistory'
showDeletedStatus = RocketChat.settings.get 'Message_ShowDeletedStatus'
deleteQuery =
_id: originalMessage._id
if keepHistory
if showDeletedStatus
RocketChat.models.Messages.cloneAndSaveAsHistoryById originalMessage._id
else
ChatMessage.update deleteQuery,
$set:
_hidden: true
RocketChat.models.Messages.setHiddenById originalMessage._id, true
else
if not showDeletedStatus
RocketChat.models.Messages.removeById originalMessage._id
if showDeletedStatus
ChatMessage.update deleteQuery,
$set:
msg: ''
t: 'rm'
ets: new Date()
RocketChat.models.Messages.setAsDeletedById originalMessage._id
else
RocketChat.Notifications.notifyRoom originalMessage.rid, 'deleteMessage', { _id: originalMessage._id }
......@@ -12,18 +12,11 @@ Meteor.methods
if RocketChat.settings.get 'Message_KeepHistory'
RocketChat.models.Messages.cloneAndSaveAsHistoryById message._id
message.pts = new Date()
message.pinned = true
message = RocketChat.callbacks.run 'beforeSaveMessage', message
ChatMessage.update
_id: message._id
'u._id': Meteor.userId()
,
$set:
pinned: message.pinned
pts : message.pts
RocketChat.models.Messages.setPinnedByIdAndUserId message._id, Meteor.userId(), message.pinned
# Meteor.defer ->
......
......@@ -12,18 +12,11 @@ Meteor.methods
if RocketChat.settings.get 'Message_KeepHistory'
RocketChat.models.Messages.cloneAndSaveAsHistoryById message._id
message.pts = new Date()
message.pinned = false
message = RocketChat.callbacks.run 'beforeSaveMessage', message
ChatMessage.update
_id: message._id
'u._id': Meteor.userId()
,
$set:
pinned: message.pinned
pts : message.pts
RocketChat.models.Messages.setPinnedByIdAndUserId message._id, Meteor.userId(), message.pinned
# Meteor.defer ->
......
......@@ -35,7 +35,7 @@ Meteor.methods
tempid = message._id
delete message._id
ChatMessage.update
RocketChat.models.Messages.update
_id: tempid
,
$set: message
......
......@@ -16,7 +16,7 @@ Meteor.startup ->
}
{
source: new Meteor.Collection 'data.ChatMessage'
target: ChatMessage
target: RocketChat.models.Messages.model
}
{
source: new Meteor.Collection 'settings'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment