several performance improvements

pull/173/head
Gabriel Engel 11 years ago
parent 95c69b1d95
commit db82bfcb7e
  1. 17
      server/methods/canAccessRoom.coffee
  2. 43
      server/methods/sendMessage.coffee

@ -1,6 +1,6 @@
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

Loading…
Cancel
Save