Skip to content
Snippets Groups Projects
Commit db82bfcb authored by Gabriel Engel's avatar Gabriel Engel
Browse files

several performance improvements

parent 95c69b1d
No related branches found
No related tags found
No related merge requests found
Meteor.methods
canAccessRoom: (roomId, userId) ->
console.log '[methods] canAccessRoom -> '.green, 'userId:', userId, 'roomId:', roomId
canAccessRoom: (rid, userId) ->
console.log '[methods] canAccessRoom -> '.green, 'userId:', userId, 'rid:', rid
user = Meteor.users.findOne userId, fields: username: 1
......@@ -8,12 +8,10 @@ Meteor.methods
unless user?.username
throw new Meteor.Error 'not-logged-user', "[methods] canAccessRoom -> User doesn't have enough permissions"
unless roomId
unless rid
throw new Meteor.Error 'invalid-room', '[methods] canAccessRoom -> Cannot access empty room'
room = ChatRoom.findOne roomId, { fields: { usernames: 1, t: 1 } }
canAccess = false
room = ChatRoom.findOne rid, { fields: { usernames: 1, t: 1 } }
if room.t is 'c'
canAccess = true
......@@ -22,9 +20,12 @@ Meteor.methods
if canAccess isnt true
throw new Meteor.Error 'without-permission', "[methods] canAccessRoom -> User doesn't have enough permissions"
return false
else
return room
# # create room subscription
# ChatSubscription.upsert { rid: roomId, $and: [{'u._id': Meteor.userId()}] },
# ChatSubscription.upsert { rid: rid, $and: [{'u._id': Meteor.userId()}] },
# $setOnInsert:
# 'u._id': Meteor.userId()
# name: room.name
......@@ -33,5 +34,3 @@ Meteor.methods
# $set:
# ls: (new Date())
# ts: (new Date())
return canAccess
......@@ -3,7 +3,9 @@ Meteor.methods
if not Meteor.userId()
throw new Meteor.Error('invalid-user', "[methods] sendMessage -> Invalid user")
if not Meteor.call 'canAccessRoom', message.rid, Meteor.userId()
room = Meteor.call 'canAccessRoom', message.rid, Meteor.userId()
if not room
return false
console.log '[methods] sendMessage -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments
......@@ -12,7 +14,7 @@ Meteor.methods
message.ts = new Date()
message = RocketChat.callbacks.run 'beforeSaveMessage', message
console.log "message", message
# console.log "message", message
###
Defer other updated as their return is not interesting to the user
......@@ -33,28 +35,49 @@ Meteor.methods
$inc:
msgs: 1
message.mentions?.forEach (mention) ->
# increment unread couter if direct messages
if room.t is 'd'
###
Update all other subscriptions of mentioned users to alert their owners and incrementing
the unread counter for mentions and direct messages
Update the other subscriptions
###
ChatSubscription.update
# only subscriptions to the same room
rid: message.rid
# not the msg owner
'u._id': mention._id
'u._id':
$ne: message.u._id
,
$set:
# alert de user
alert: true
# open the room for the user
open: true
# increment undear couter
# increment unread couter
$inc:
unread: 1
,
# make sure we alert all matching subscription
multi: true
else
message.mentions?.forEach (mention) ->
console.log mention
###
Update all other subscriptions of mentioned users to alert their owners and incrementing
the unread counter for mentions and direct messages
###
ChatSubscription.update
# only subscriptions to the same room
rid: message.rid
# the mentioned user
'u._id': mention._id
,
$set:
# alert de user
alert: true
# open the room for the user
open: true
# increment unread couter
$inc:
unread: 1
###
Update all other subscriptions to alert their owners but witout incrementing
......
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