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

Replace all ChatMessage.find

parent 001fdb26
No related branches found
No related tags found
No related merge requests found
......@@ -20,7 +20,7 @@ RocketChat.setUsername = (user, username) ->
if previousUsername
ChatMessage.update { "u._id": user._id }, { $set: { "u.username": username } }, { multi: true }
ChatMessage.find({ "mentions.username": previousUsername }).forEach (msg) ->
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 } }
......
......@@ -17,12 +17,71 @@ RocketChat.models.Messages = new class asd extends RocketChat.models._Base
return @findOne query, options
# # FIND
# findByType: (type, options) ->
# query =
# t: type
# FIND
findByMention: (username, options) ->
query =
"mentions.username": username
return @find query, options
findVisibleByRoomId: (roomId, options) ->
query =
_hidden:
$ne: true
rid: roomId
return @find query, options
findInvisibleByRoomId: (roomId, options) ->
query =
_hidden: true
rid: roomId
return @find query, options
# return @find query, options
findVisibleByRoomIdAfterTimestamp: (roomId, timestamp, options) ->
query =
_hidden:
$ne: true
rid: roomId
ts:
$gt: timestamp
return @find query, options
findVisibleByRoomIdBeforeTimestamp: (roomId, timestamp, options) ->
query =
_hidden:
$ne: true
rid: roomId
ts:
$lt: timestamp
return @find query, options
findVisibleByRoomIdBetweenTimestamps: (roomId, afterTimestamp, beforeTimestamp, options) ->
query =
_hidden:
$ne: true
rid: roomId
ts:
$gt = afterTimestamp
$lt = beforeTimestamp
return @find query, options
findVisibleCreatedOrEditedAfterTimestamp: (timestamp) ->
query =
_hidden: { $ne: true }
$or: [
ts:
$gt: timestamp
,
ets:
$gt: timestamp
]
return @find query, options
# # UPDATE
......
......@@ -21,7 +21,7 @@ RocketChat.statistics.get = ->
statistics.totalDirect = RocketChat.models.Rooms.findByType('d').count()
# Message statistics
statistics.totalMessages = ChatMessage.find().count()
statistics.totalMessages = RocketChat.models.Messages.find().count()
m = ->
emit 1,
......
......@@ -20,16 +20,14 @@ Meteor.methods
if not RocketChat.settings.get 'Message_ShowEditedStatus'
options.fields = { ets: 0 }
messages = ChatMessage.find(query, options).fetch()
messages = RocketChat.models.Messages.findVisibleByRoomIdBeforeTimestamp(rid, end, options).fetch()
unreadNotLoaded = 0
if ls?
fistMessage = messages[messages.length - 1]
if fistMessage?.ts > ls
query.ts.$lt = fistMessage.ts
query.ts.$gt = ls
firstMessage = messages[messages.length - 1]
if firstMessage?.ts > ls
delete options.limit
unreadNotLoaded = ChatMessage.find(query, options).count()
unreadNotLoaded = RocketChat.models.Messages.findVisibleByRoomIdBetweenTimestamps(rid, ls, firstMessage.ts).count()
return {
messages: messages
......
......@@ -6,12 +6,6 @@ Meteor.methods
unless Meteor.call 'canAccessRoom', rid, fromId
return false
query =
_hidden: { $ne: true }
rid: rid
ts:
$gt: start
options =
sort:
ts: -1
......@@ -19,4 +13,4 @@ Meteor.methods
if not RocketChat.settings.get 'Message_ShowEditedStatus'
options.fields = { ets: 0 }
return ChatMessage.find(query, options).fetch()
return RocketChat.models.Messages.findVisibleByRoomIdAfterTimestamp(rid, start, options).fetch()
......@@ -61,7 +61,7 @@ Meteor.methods
query.rid = rid
try
if Meteor.call('canAccessRoom', rid, this.userId) isnt false
result.messages = ChatMessage.find(query, options).fetch()
result.messages = RocketChat.models.Messages.find(query, options).fetch()
# ###
......
......@@ -12,11 +12,7 @@ Meteor.publish 'messages', (rid, start) ->
if not Meteor.call 'canAccessRoom', rid, this.userId
return this.ready()
cursor = ChatMessage.find
rid: rid
_hidden:
$ne: true
,
cursor = RocketChat.models.Messages.findVisibleByRoomId rid,
sort:
ts: -1
limit: 50
......@@ -28,10 +24,7 @@ Meteor.publish 'messages', (rid, start) ->
changed: (_id, record) ->
publication.changed('rocketchat_message', _id, record)
cursorDelete = ChatMessage.find
rid: rid
_hidden: true
,
cursorDelete = RocketChat.models.Messages.findInvisibleByRoomId rid,
fields:
_id: 1
......
......@@ -35,7 +35,7 @@ Api.addRoute 'rooms/:id/messages', authRequired: true,
get: ->
try
if Meteor.call('canAccessRoom', @urlParams.id, this.userId)
msgs = ChatMessage.find({rid: @urlParams.id, _hidden: {$ne: true}}, {sort: {ts: -1}}, {limit: 50}).fetch()
msgs = RocketChat.models.Messages.findVisibleByRoomId(@urlParams.id, {sort: {ts: -1}, limit: 50}).fetch()
status: 'success', messages: msgs
else
statusCode: 403 # forbidden
......
......@@ -59,7 +59,7 @@ Meteor.startup ->
n: oldChunk.n
data: oldChunk.data
ChatMessage.find({$or: [{ 'urls.url': "https://demo.rocket.chat/cfs/files/Files/#{cfsRecord._id}" }, { 'urls.url': "https://rocket.chat/cfs/files/Files/#{cfsRecord._id}" }]}).forEach (message) ->
RocketChat.models.Messages.find({$or: [{ 'urls.url': "https://demo.rocket.chat/cfs/files/Files/#{cfsRecord._id}" }, { 'urls.url': "https://rocket.chat/cfs/files/Files/#{cfsRecord._id}" }]}).forEach (message) ->
for urlsItem in message.urls
if urlsItem.url is "https://demo.rocket.chat/cfs/files/Files/#{cfsRecord._id}" or urlsItem.url is "https://rocket.chat/cfs/files/Files/#{cfsRecord._id}"
urlsItem.url = Meteor.absoluteUrl() + url
......
......@@ -58,7 +58,7 @@ Meteor.startup ->
console.log 'Fixing ChatMessage uid'
ChatMessage.find({uid: {$exists: true}}, {nonreactive: true}).forEach (message) ->
RocketChat.models.Messages.find({uid: {$exists: true}}, {nonreactive: true}).forEach (message) ->
update = {}
user = RocketChat.models.Users.findOneById(message.uid, {fields: {username: 1}})
if user?
......
......@@ -4,7 +4,7 @@ Meteor.startup ->
up: ->
console.log 'Populate urls in messages'
query = ChatMessage.find({ 'urls.0': { $exists: true } })
query = RocketChat.models.Messages.find({ 'urls.0': { $exists: true } })
count = query.count()
query.forEach (message, index) ->
console.log "#{index + 1} / #{count}"
......
......@@ -35,7 +35,7 @@ Meteor.startup ->
if not RocketChat.settings.get 'Message_ShowEditedStatus'
options.fields = { ets: 0 }
ChatMessage.find(filter, options).observe
RocketChat.models.Messages.findVisibleCreatedOrEditedAfterTimestamp(new Date(), options).observe
added: (record) ->
msgStream.emit record.rid, record
......
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