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

Replace all ChatRooms.find

parent d7f78988
No related branches found
No related tags found
No related merge requests found
Showing
with 167 additions and 87 deletions
......@@ -231,15 +231,12 @@ class IrcClient
msg = "PRIVMSG #{target} :#{message.msg}\r\n"
@sendRawMessage msg
initRoomList: () ->
roomsCursor = ChatRoom.find
usernames:
$in: [@user.username]
t: 'c'
,
initRoomList: ->
roomsCursor = RocketChat.models.Rooms.findByTypeContainigUsername 'c', @user.username,
fields:
name: 1
t: 1
rooms = roomsCursor.fetch()
for room in rooms
@joinRoom(room)
......
......@@ -9,7 +9,7 @@ Meteor.methods
RocketChat.callbacks.run 'beforeJoinDefaultChannels', user
ChatRoom.find({default: true, t: {$in: ['c', 'p']}}).forEach (room) ->
RocketChat.models.Rooms.findByDefaultAndTypes(true, ['c', 'p']).forEach (room) ->
# put user in default rooms
ChatRoom.update room._id,
......
RocketChat.models.Rooms = new class asd extends RocketChat.models._Base
constructor: ->
@model = new Meteor.Collection 'rocketchat_room'
# @model = new Meteor.Collection 'rocketchat_room'
@model = @ChatRoom
@tryEnsureIndex { 'name': 1 }, { unique: 1, sparse: 1 }
@tryEnsureIndex { 'u._id': 1 }
......@@ -42,12 +43,106 @@ RocketChat.models.Rooms = new class asd extends RocketChat.models._Base
return @findOne query, options
# # FIND
# findByUserId: (userId, options) ->
# query =
# "u._id": userId
# return @find query, options
# FIND
findByType: (type, options) ->
query =
t: type
return @find query, options
findByTypes: (types, options) ->
query =
t:
$in: types
return @find query, options
findByUserId: (userId, options) ->
query =
"u._id": userId
return @find query, options
findByNameContaining: (name, options) ->
nameRegex = new RegExp name, "i"
query =
$or: [
name: nameRegex
,
t: 'd'
usernames: nameRegex
]
return @find query, options
findByNameContainingAndTypes: (name, types, options) ->
nameRegex = new RegExp name, "i"
query =
t:
$in: types
$or: [
name: nameRegex
,
t: 'd'
usernames: nameRegex
]
return @find query, options
findByDefaultAndTypes: (defaultValue, types, options) ->
query =
default: defaultValue
t:
$in: types
return @find query, options
findByTypeContainigUsername: (type, username, options) ->
query =
t: type
usernames: username
return @find query, options
findByTypesAndNotUserIdContainingUsername: (types, userId, username, options) ->
query =
t:
$in: types
uid:
$ne: userId
usernames: username
return @find query, options
findByContainigUsername: (username, options) ->
query =
usernames: username
return @find query, options
findByTypeAndName: (type, name, options) ->
query =
t: type
name: name
return @find query, options
findByTypeAndNameContainigUsername: (type, name, username, options) ->
query =
t: type
name: name
usernames: username
return @find query, options
findByVisitorToken: (visitorToken, options) ->
query =
"v.token": visitorToken
return @find query, options
# # UPDATE
......
Meteor.publish 'visitorRoom', (visitorToken) ->
return ChatRoom.find
"v.token": visitorToken
,
return RocketChat.models.Rooms.findByVisitorToken visitorToken,
fields:
name: 1
t: 1
......
RocketChat.statistics.get = ->
statistics = {}
# Version
statistics.uniqueId = Settings.findOne({ _id: "uniqueID" })?.value
statistics.version = BuildInfo?.commit?.hash
......@@ -15,16 +15,16 @@ RocketChat.statistics.get = ->
statistics.offlineUsers = statistics.totalUsers - statistics.onlineUsers - statistics.awayUsers
# Room statistics
statistics.totalRooms = ChatRoom.find().count()
statistics.totalChannels = ChatRoom.find({ t: 'c' }).count()
statistics.totalPrivateGroups = ChatRoom.find({ t: 'p' }).count()
statistics.totalDirect = ChatRoom.find({ t: 'd' }).count()
statistics.totalRooms = RocketChat.models.Rooms.find().count()
statistics.totalChannels = RocketChat.models.Rooms.findByType('c').count()
statistics.totalPrivateGroups = RocketChat.models.Rooms.findByType('p').count()
statistics.totalDirect = RocketChat.models.Rooms.findByType('d').count()
# Message statistics
statistics.totalMessages = ChatMessage.find().count()
m = ->
emit 1,
emit 1,
sum: this.usernames.length or 0
min: this.usernames.length or 0
max: this.usernames.length or 0
......@@ -44,20 +44,20 @@ RocketChat.statistics.get = ->
a.max = Math.max a.max, b.max
a.count += b.count
return a
f = (k, v) ->
v.avg = v.sum / v.count
return v
result = ChatRoom.mapReduce(m, r, { finalize: f, out: "rocketchat_mr_statistics" })
statistics.maxRoomUsers = 0
statistics.maxRoomUsers = 0
statistics.avgChannelUsers = 0
statistics.avgPrivateGroupUsers = 0
if MapReducedStatistics.findOne({ _id: 1 })
statistics.maxRoomUsers = MapReducedStatistics.findOne({ _id: 1 }).value.max
else
else
console.log 'max room user statistic not found'.red
if MapReducedStatistics.findOne({ _id: 'c' })
......@@ -66,12 +66,12 @@ RocketChat.statistics.get = ->
console.log 'channel user statistic not found'.red
if MapReducedStatistics.findOne({ _id: 'p' })
statistics.avgPrivateGroupUsers = MapReducedStatistics.findOne({ _id: 'p' }).value.avg
statistics.avgPrivateGroupUsers = MapReducedStatistics.findOne({ _id: 'p' }).value.avg
else
console.log 'private group user statistic not found'.red
os = Npm.require('os')
statistics.os =
statistics.os =
type: os.type()
platform: os.platform()
arch: os.arch()
......@@ -82,4 +82,4 @@ RocketChat.statistics.get = ->
freemem: os.freemem()
cpus: os.cpus()
return statistics
\ No newline at end of file
return statistics
Meteor.methods
channelsList: ->
return { channels: ChatRoom.find({ t: 'c' }, { sort: { msgs:-1 } }).fetch() }
return { channels: RocketChat.models.Rooms.findByType('c', { sort: { msgs:-1 } }).fetch() }
......@@ -23,7 +23,7 @@ Meteor.methods
RocketChat.models.Subscriptions.removeByUserId userId # Remove user subscriptions
rooms = ChatRoom.find({ "u._id": userId }).fetch()
rooms = RocketChat.models.Rooms.findByUserId(userId).fetch()
ChatRoom.remove { t: 'd', usernames: user.username } # Remove direct rooms with the user
......
......@@ -8,22 +8,7 @@ Meteor.publish 'adminRooms', (filter, types, limit) ->
unless _.isArray types
types = []
query = {}
filter = _.trim filter
if filter
if limit is 1
query = { name: filter }
else
filterReg = new RegExp filter, "i"
query = { $or: [ { name: filterReg }, { t: 'd', usernames: filterReg } ] }
if types.length
query['t'] = { $in: types }
console.log '[publish] adminRooms'.green, filter, types, limit, query
ChatRoom.find query,
options =
fields:
name: 1
t: 1
......@@ -31,4 +16,18 @@ Meteor.publish 'adminRooms', (filter, types, limit) ->
u: 1
usernames: 1
limit: limit
sort: { name: 1 }
sort:
name: 1
filter = _.trim filter
console.log '[publish] adminRooms'.green, filter, types, limit
if filter and types.length
return RocketChat.models.Rooms.findByNameContainingAndTypes filter, types, options
if filter
return RocketChat.models.Rooms.findByNameContaining filter, options
if types.length
return RocketChat.models.Rooms.findByTypes types, options
......@@ -4,9 +4,7 @@ Meteor.publish 'privateHistory', ->
console.log '[publish] privateHistory'.green
ChatRoom.find
usernames: RocketChat.models.Users.findOneById(this.userId).username
,
RocketChat.models.Rooms.findByContainigUsername RocketChat.models.Users.findOneById(this.userId).username,
fields:
t: 1
name: 1
......
......@@ -10,32 +10,26 @@ Meteor.publish 'room', (typeName) ->
type = typeName.substr(0, 1)
name = typeName.substr(1)
query = {}
options =
fields:
name: 1
t: 1
cl: 1
u: 1
usernames: 1
if type in ['c', 'p']
query =
t: type
name: name
switch type
when 'c'
return RocketChat.models.Rooms.findByTypeContainigUsername 'c', name, options
if type is 'p'
when 'p'
user = RocketChat.models.Users.findOneById this.userId, fields: username: 1
query.usernames = user.username
return RocketChat.models.Rooms.findByTypeAndNameContainigUsername 'p', name, user.username, options
else if type is 'd'
user = RocketChat.models.Users.findOneById this.userId, fields: username: 1
query =
t: 'd'
usernames:
$all: [user.username, name]
when 'd'
user = RocketChat.models.Users.findOneById this.userId, fields: username: 1
return RocketChat.models.Rooms.findByTypeContainigUsername 'd', user.username, options
# Change to validate access manualy
# if not Meteor.call 'canAccessRoom', rid, this.userId
# return this.ready()
ChatRoom.find query,
fields:
name: 1
t: 1
cl: 1
u: 1
usernames: 1
......@@ -24,8 +24,7 @@ Meteor.publish 'roomSearch', (selector, options, collName) ->
self.removed("autocompleteRecords", id)
if not searchType? or searchType is 'r'
roomSelector = _.extend { t: { $in: ['c','p'] }, usernames: RocketChat.models.Users.findOneById(this.userId).username }, selector
subHandleRooms = ChatRoom.find(roomSelector, { limit: 10, fields: { t: 1, name: 1 } }).observeChanges
subHandleRooms = RocketChat.models.Rooms.findByTypesAndNotUserIdContainingUsername(['c', 'p'], selector.uid?.$ne, RocketChat.models.Users.findOneById(this.userId).username, { limit: 10, fields: { t: 1, name: 1 } }).observeChanges
added: (id, fields) ->
data = { type: 'r', rid: id, name: fields.name, t: fields.t }
self.added("autocompleteRecords", id, data)
......
......@@ -11,7 +11,7 @@ Api.addRoute 'version', authRequired: false,
Api.addRoute 'publicRooms', authRequired: true,
get: ->
rooms = ChatRoom.find({ t: 'c' }, { sort: { msgs:-1 } }).fetch()
rooms = RocketChat.models.Rooms.findByType('c', { sort: { msgs:-1 } }).fetch()
status: 'success', rooms: rooms
# join a room
......
......@@ -7,10 +7,10 @@ Meteor.startup ->
###
count = 0
ChatRoom.find({'usernames.0': {$exists: true}}, {fields: {usernames: 1}}).forEach (room) ->
RocketChat.models.Rooms.find({'usernames.0': {$exists: true}}, {fields: {usernames: 1}}).forEach (room) ->
newUsernames = _.uniq room.usernames
if newUsernames.length isnt room.usernames.length
count++
ChatRoom.update {_id: room._id}, {$set: {usernames: newUsernames}}
console.log "Removed duplicated usernames from #{count} rooms"
\ No newline at end of file
console.log "Removed duplicated usernames from #{count} rooms"
......@@ -18,9 +18,9 @@ Meteor.startup ->
usernames = _.pluck( users, 'username').join(', ')
console.log "Add #{usernames} to 'user' role".green
# Add 'moderator' role to channel/group creators
rooms = ChatRoom.find({t: {$in : ['c','p']}}).fetch()
_.each( rooms, (room) ->
# Add 'moderator' role to channel/group creators
rooms = RocketChat.models.Rooms.findByTypes(['c','p']).fetch()
_.each( rooms, (room) ->
creator = room?.u?._id
if creator
RocketChat.authz.addUsersToRoles( creator, ['moderator'], room._id)
......
......@@ -24,7 +24,7 @@ Meteor.startup ->
console.log 'Fixing ChatRoom uids'
ChatRoom.find({'uids.0': {$exists: true}}, {nonreactive: true}).forEach (room) ->
RocketChat.models.Rooms.find({'uids.0': {$exists: true}}, {nonreactive: true}).forEach (room) ->
update = {}
users = RocketChat.models.Users.find {_id: {$in: room.uids}, username: {$exists: true}}, {fields: {username: 1}}
usernames = users.map (user) ->
......
......@@ -12,15 +12,15 @@ Meteor.startup ->
console.log 'Adding names to rooms without name'
ChatRoom.find({name: ''}).forEach (item) ->
RocketChat.models.Rooms.find({name: ''}).forEach (item) ->
name = Random.id().toLowerCase()
ChatRoom.update item._id, {$set: {name: name}}
RocketChat.models.Subscriptions.update {rid: item._id}, {$set: {name: name}}, {multi: true}
console.log 'Making room names unique'
ChatRoom.find().forEach (room) ->
ChatRoom.find({name: room.name, _id: {$ne: room._id}}).forEach (item) ->
RocketChat.models.Rooms.find().forEach (room) ->
RocketChat.models.Rooms.find({name: room.name, _id: {$ne: room._id}}).forEach (item) ->
name = room.name + '-' + Random.id(2).toLowerCase()
ChatRoom.update item._id, {$set: {name: name}}
RocketChat.models.Subscriptions.update {rid: item._id}, {$set: {name: name}}, {multi: true}
......
......@@ -4,7 +4,7 @@ Meteor.startup ->
up: ->
console.log 'Dropping test rooms with less than 2 messages'
ChatRoom.find({msgs: {'$lt': 2}}).forEach (room) ->
RocketChat.models.Rooms.find({msgs: {'$lt': 2}}).forEach (room) ->
console.log 'Dropped: ', room.name
ChatRoom.remove room._id
ChatMessage.remove {rid: room._id}
......@@ -12,7 +12,7 @@ Meteor.startup ->
console.log 'Dropping test rooms with less than 2 user'
ChatRoom.find({usernames: {'$size':1}}).forEach (room) ->
RocketChat.models.Rooms.find({usernames: {'$size':1}}).forEach (room) ->
console.log 'Dropped: ', room.name
ChatRoom.remove room._id
ChatMessage.remove {rid: room._id}
......@@ -33,7 +33,7 @@ Meteor.startup ->
console.log 'Fixing _id of direct messages rooms'
ChatRoom.find({'t': 'd'}).forEach (room) ->
RocketChat.models.Rooms.findByType('d').forEach (room) ->
newId = ''
id0 = RocketChat.models.Users.findOneByUsername(room.usernames[0])._id
id1 = RocketChat.models.Users.findOneByUsername(room.usernames[1])._id
......
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