Replace all ChatSubscriptions.find and update

pull/822/head
Rodrigo Nascimento 10 years ago
parent 599c650880
commit e03e929a0b
  1. 214
      packages/rocketchat-lib/server/models/Subscriptions.coffee
  2. 9
      server/methods/archiveRoom.coffee
  3. 2
      server/methods/deleteUser.coffee
  4. 8
      server/methods/hideRoom.coffee
  5. 6
      server/methods/leaveRoom.coffee
  6. 7
      server/methods/openRoom.coffee
  7. 10
      server/methods/readMessages.coffee
  8. 11
      server/methods/saveRoomName.coffee
  9. 7
      server/methods/toogleFavorite.coffee
  10. 9
      server/methods/unarchiveRoom.coffee
  11. 4
      server/publications/subscription.coffee
  12. 4
      server/publications/userChannels.coffee
  13. 6
      server/startup/migrations/v3.coffee
  14. 6
      server/startup/migrations/v4.coffee
  15. 8
      server/startup/migrations/v5.coffee
  16. 2
      server/startup/migrations/v6.coffee

@ -0,0 +1,214 @@
RocketChat.models.Subscriptions = new class asd extends RocketChat.models._Base
RocketChat.models.Subscriptions = new class asd extends RocketChat.models._Base
constructor: ->
@model = new Meteor.Collection 'rocketchat_subscription'
# FIND
findByUserId: (userId, options) ->
query =
"u._id": userId
return @find query, options
# UPDATE
archiveByRoomIdAndUserId: (roomId, userId) ->
query =
rid: roomId
'u._id': userId
update =
$set:
alert: false
open: false
archived: true
return @update query, update
unarchiveByRoomIdAndUserId: (roomId, userId) ->
query =
rid: roomId
'u._id': userId
update =
$set:
alert: false
open: false
archived: false
return @update query, update
hideByRoomIdAndUserId: (roomId, userId) ->
query =
rid: roomId
'u._id': userId
update =
$set:
alert: false
open: false
return @update query, update
openByRoomIdAndUserId: (roomId, userId) ->
query =
rid: roomId
'u._id': userId
update =
$set:
open: true
return @update query, update
setAsReadByRoomIdAndUserId: (roomId, userId) ->
query =
rid: roomId
'u._id': userId
update =
$set:
open: true
alert: false
unread: 0
ls: new Date
return @update query, update
setFavoriteByRoomIdAndUserId: (roomId, userId, favorite=true) ->
query =
rid: roomId
'u._id': userId
update =
$set:
f: favorite
return @update query, update
updateNameByRoomId: (roomId, name) ->
query =
rid: roomId
update =
$set:
name: name
alert: true
return @update query, update, { multi: true }
setServiceId: (_id, serviceName, serviceId) ->
update =
$set: {}
serviceIdKey = "services.#{serviceName}.id"
update.$set[serviceIdKey] = serviceData.id
return @update _id, update
setUsername: (_id, username) ->
update =
$set: username: username
return @update _id, update
setName: (_id, name) ->
update =
$set:
name: name
return @update _id, update
setAvatarOrigin: (_id, origin) ->
update =
$set:
avatarOrigin: origin
return @update _id, update
unsetAvatarOrigin: (_id) ->
update =
$unset:
avatarOrigin: 1
return @update _id, update
setUserActive: (_id, active=true) ->
update =
$set:
active: active
return @update _id, update
setAllUsersActive: (active) ->
update =
$set:
active: active
return @update {}, update, { multi: true }
unsetLoginTokens: (_id) ->
update =
$set:
"services.resume.loginTokens" : []
return @update _id, update
setLanguage: (_id, language) ->
update =
$set:
language: language
return @update _id, update
setProfile: (_id, profile) ->
update =
$set:
"settings.profile": profile
return @update _id, update
setPreferences: (_id, preferences) ->
update =
$set:
"settings.preferences": preferences
return @update _id, update
setUtcOffset: (_id, utcOffset) ->
query =
_id: _id
utcOffset:
$ne: utcOffset
update =
$set:
utcOffset: utcOffset
return @update query, update
# INSERT
create: (data) ->
user =
createdAt: new Date
avatarOrigin: 'none'
_.extend user, data
return @insert user
# REMOVE
removeById: (_id) ->
return @remove _id
removeByUnverifiedEmail: (email) ->
query =
emails:
$elemMatch:
address: email
verified: false
return @remove query

@ -19,11 +19,4 @@ Meteor.methods
if not member?
continue
ChatSubscription.update
rid: rid
'u._id': member._id
,
$set:
alert: false
open: false
archived: true
RocketChat.models.Subscriptions.archiveByRoomIdAndUserId rid, member._id

@ -14,7 +14,7 @@ Meteor.methods
ChatMessage.remove { "u._id": userId } # Remove user messages
ChatSubscription.find({ "u._id": userId }).forEach (subscription) ->
RocketChat.models.Subscriptions.findByUserId(userId).forEach (subscription) ->
room = ChatRoom.findOne subscription.rid
if room.t isnt 'c' and room.usernames.length is 1
ChatRoom.remove subscription.rid # Remove non-channel rooms with only 1 user (the one being deleted)

@ -5,10 +5,4 @@ Meteor.methods
console.log '[methods] hideRoom -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments
ChatSubscription.update
rid: rid
'u._id': Meteor.userId()
,
$set:
alert: false
open: false
RocketChat.models.Subscriptions.hideByRoomIdAndUserId rid, Meteor.userId()

@ -15,12 +15,6 @@ Meteor.methods
$pull:
usernames: user.username
ChatSubscription.update { rid: rid },
$set:
name: room.name
,
multi: true
if room.t isnt 'c' and room.usernames.indexOf(user.username) isnt -1
removedUser = user

@ -5,9 +5,4 @@ Meteor.methods
console.log '[methods] openRoom -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments
ChatSubscription.update
rid: rid
'u._id': Meteor.userId()
,
$set:
open: true
RocketChat.models.Subscriptions.openByRoomIdAndUserId rid, Meteor.userId()

@ -5,12 +5,4 @@ Meteor.methods
console.log '[methods] readMessages -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments
ChatSubscription.update
rid: rid
'u._id': Meteor.userId()
,
$set:
open: true
alert: false
unread: 0
ls: (new Date())
RocketChat.models.Subscriptions.setAsReadByRoomIdAndUserId rid, Meteor.userId()

@ -9,7 +9,7 @@ Meteor.methods
throw new Meteor.Error 403, 'Not allowed'
unless RocketChat.authz.hasPermission(Meteor.userId(), 'edit-room', rid)
#if room.u._id isnt Meteor.userId() and not hasPermission
#if room.u._id isnt Meteor.userId() and not hasPermission
throw new Meteor.Error 403, 'Not allowed'
if not /^[0-9a-z-_]+$/.test name
@ -28,14 +28,7 @@ Meteor.methods
$set:
name: name
ChatSubscription.update
rid: rid
,
$set:
name: name
alert: true
,
multi: true
RocketChat.models.Subscriptions.updateNameByRoomId rid, name
ChatMessage.insert
rid: rid

@ -5,9 +5,4 @@ Meteor.methods
console.log '[methods] toogleFavorite -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments
ChatSubscription.update
rid: rid
'u._id': Meteor.userId()
,
$set:
f: f
RocketChat.models.Subscriptions.setFavoriteByRoomIdAndUserId rid, Meteor.userId()

@ -19,11 +19,4 @@ Meteor.methods
if not member?
continue
ChatSubscription.update
rid: rid
'u._id': member._id
,
$set:
alert: false
open: false
archived: false
RocketChat.models.Subscriptions.unarchiveByRoomIdAndUserId rid, member._id

@ -4,9 +4,7 @@ Meteor.publish 'subscription', ->
console.log '[publish] subscription'.green
ChatSubscription.find
'u._id': this.userId
,
RocketChat.models.Subscriptions.findByUserId this.userId,
fields:
t: 1
ts: 1

@ -5,11 +5,9 @@ Meteor.publish 'userChannels', (userId) ->
if RocketChat.authz.hasPermission( @userId, 'view-other-user-channels') isnt true
return this.ready()
query = { "u._id": userId }
console.log '[publish] userChannels'.green, userId
ChatSubscription.find query,
RocketChat.models.Subscriptions.findByUserId userId,
fields:
rid: 1,
name: 1,

@ -10,7 +10,7 @@ Meteor.startup ->
console.log 'Fixing ChatSubscription uid'
ChatSubscription.find({uid: {$exists: true}}, {nonreactive: true}).forEach (sub) ->
RocketChat.models.Subscriptions.find({uid: {$exists: true}}, {nonreactive: true}).forEach (sub) ->
update = {}
user = RocketChat.models.Users.findOneById(sub.uid, {fields: {username: 1}})
if user?
@ -21,7 +21,7 @@ Meteor.startup ->
update.$unset.uid = 1
if Object.keys(update).length > 0
ChatSubscription.update(sub._id, update)
RocketChat.models.Subscriptions.update(sub._id, update)
console.log 'Fixing ChatRoom uids'
@ -52,7 +52,7 @@ Meteor.startup ->
room._id = usernames.sort().join(',')
ChatRoom.insert(room)
ChatRoom.remove({_id: oldId})
ChatSubscription.update({rid: oldId}, {$set: {rid: room._id}}, {multi: true})
RocketChat.models.Subscriptions.update({rid: oldId}, {$set: {rid: room._id}}, {multi: true})
ChatMessage.update({rid: oldId}, {$set: {rid: room._id}}, {multi: true})
else
ChatRoom.update(room._id, update)

@ -8,14 +8,14 @@ Meteor.startup ->
console.log 'Rename rn to name'
ChatSubscription.update({rn: {$exists: true}}, {$rename: {rn: 'name'}}, {multi: true})
RocketChat.models.Subscriptions.update({rn: {$exists: true}}, {$rename: {rn: 'name'}}, {multi: true})
console.log 'Adding names to rooms without name'
ChatRoom.find({name: ''}).forEach (item) ->
name = Random.id().toLowerCase()
ChatRoom.update item._id, {$set: {name: name}}
ChatSubscription.update {rid: item._id}, {$set: {name: name}}, {multi: true}
RocketChat.models.Subscriptions.update {rid: item._id}, {$set: {name: name}}, {multi: true}
console.log 'Making room names unique'
@ -23,7 +23,7 @@ Meteor.startup ->
ChatRoom.find({name: room.name, _id: {$ne: room._id}}).forEach (item) ->
name = room.name + '-' + Random.id(2).toLowerCase()
ChatRoom.update item._id, {$set: {name: name}}
ChatSubscription.update {rid: item._id}, {$set: {name: name}}, {multi: true}
RocketChat.models.Subscriptions.update {rid: item._id}, {$set: {name: name}}, {multi: true}
console.log 'End'

@ -41,20 +41,20 @@ Meteor.startup ->
newId = ids.sort().join('')
if (newId != room._id)
console.log 'Fixing: _id ' + room._id + ' to ' + newId
ChatSubscription.update({'rid':room._id},{'$set':{'rid':newId}},{'multi':1})
RocketChat.models.Subscriptions.update({'rid':room._id},{'$set':{'rid':newId}},{'multi':1})
ChatMessage.update({'rid':room._id},{'$set':{'rid':newId}},{'multi':1})
ChatRoom.remove({'_id':room._id})
room._id = newId
ChatRoom.insert(room)
ChatSubscription.update({'rid':room._id,'u._id':id0},{'$set':{'name':room.usernames[1]}})
ChatSubscription.update({'rid':room._id,'u._id':id1},{'$set':{'name':room.usernames[0]}})
RocketChat.models.Subscriptions.update({'rid':room._id,'u._id':id0},{'$set':{'name':room.usernames[1]}})
RocketChat.models.Subscriptions.update({'rid':room._id,'u._id':id1},{'$set':{'name':room.usernames[0]}})
console.log 'Adding u.username to all documents'
RocketChat.models.Users.find({},{'username':1}).forEach (user) ->
console.log 'Adding: u.username ' + user.username + ' to all document'
ChatRoom.update({'u._id':user._id},{'$set':{'u.username':user.username}},{'multi':1})
ChatSubscription.update({'u._id':user._id},{'$set':{'u.username':user.username}},{'multi':1})
RocketChat.models.Subscriptions.update({'u._id':user._id},{'$set':{'u.username':user.username}},{'multi':1})
ChatMessage.update({'u._id':user._id},{'$set':{'u.username':user.username}},{'multi':1})
ChatMessage.update({'uid':user._id},{'$set':{'u':user}},{'multi':1})
ChatMessage.update({'by':user._id},{'$set':{'u':user}},{'multi':1})

@ -6,7 +6,7 @@ Meteor.startup ->
console.log 'Changin _id of #general channel room from XXX to GENERAL'
room = ChatRoom.findOne('name':'general')
if room?._id is not 'GENERAL'
ChatSubscription.update({'rid':room._id},{'$set':{'rid':'GENERAL'}},{'multi':1})
RocketChat.models.Subscriptions.update({'rid':room._id},{'$set':{'rid':'GENERAL'}},{'multi':1})
ChatMessage.update({'rid':room._id},{'$set':{'rid':'GENERAL'}},{'multi':1})
ChatRoom.remove({'_id':room._id})
delete room._id

Loading…
Cancel
Save