Skip to content

Commit

Permalink
Include all public rooms and users on spotlight search
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Sep 12, 2016
1 parent d0c08a1 commit b3151e6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
9 changes: 9 additions & 0 deletions packages/rocketchat-lib/server/models/Rooms.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ RocketChat.models.Rooms = new class extends RocketChat.models._Base

return @find query, options

findByNameAndTypeNotContainingUsername: (name, type, username, options) ->
query =
t: type
name: name
usernames:
$ne: username

return @find query, options

findByNameStartingAndTypes: (name, types, options) ->
nameRegex = new RegExp "^" + s.trim(s.escapeRegExp(name)), "i"

Expand Down
37 changes: 36 additions & 1 deletion packages/rocketchat-ui/views/app/spotlight/spotlight.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
$('.spotlight').removeClass('hidden')
$('.spotlight input').focus()

serverResults = new ReactiveVar
serverSearch = new ReactiveVar

Tracker.autorun ->
text = serverSearch.get()
serverResults.set()

if text?.trim().length >= 2
Meteor.call 'spotlight', text, Meteor.user().username, (err, results) ->
if err?
return console.log err

serverResults.set(results)


Template.spotlight.helpers
popupConfig: ->
self = this
Expand All @@ -23,7 +38,27 @@ Template.spotlight.helpers
getFilter: (collection, filter) ->
exp = new RegExp("#{RegExp.escape filter}", 'i')

return collection.find({name: exp, rid: {$ne: Session.get('openedRoom')}}, {limit: 10, sort: {unread: -1, ls: -1}}).fetch()
serverSearch.set(filter)

memory = collection.find({name: exp, rid: {$ne: Session.get('openedRoom')}}, {limit: 10, sort: {unread: -1, ls: -1}}).fetch()
server = serverResults.get()

if server?.users?.length > 0
for user in server.users
memory.push({
t: 'd',
name: user.username
})

if server?.rooms?.length > 0
for room in server.rooms
memory.push({
t: 'c',
name: room.name
})

return memory


getValue: (_id, collection, firstPartValue) ->
doc = collection.findOne(_id)
Expand Down
42 changes: 20 additions & 22 deletions server/publications/spotlight.coffee
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
Meteor.publish 'spotlight', (selector, options, collName) ->
if not this.userId? or not selector?.name?.$regex?
return this.ready()
Meteor.methods
spotlight: (text, username) ->
if not this.userId?
return {
users: []
rooms: []
}

self = this
subHandleUsers = null
subHandleRooms = null
regex = new RegExp s.trim(s.escapeRegExp(text)), "i"

subHandleUsers = RocketChat.models.Users.findUsersByNameOrUsername(new RegExp(selector.name.$regex, 'i'), { limit: 10, fields: { name: 1, username: 1, status: 1 }, sort: { name: 1 } }).observeChanges
added: (id, fields) ->
data = { type: 'u', uid: id, username: fields.username, name: fields.username + ' - ' + fields.name, status: fields.status }
self.added("autocompleteRecords", id, data)
removed: (id) ->
self.removed("autocompleteRecords", id)
users = RocketChat.models.Users.findByUsername(regex, { limit: 5, fields: { username: 1, status: 1 }, sort: { username: 1 } }).fetch()

subHandleRooms = RocketChat.models.Rooms.findByNameContainingAndTypes(selector.name.$regex, ['c'], { limit: 10, fields: { t: 1, name: 1 }, sort: {name: 1}}).observeChanges
added: (id, fields) ->
data = { type: 'r', rid: id, name: fields.name, t: fields.t }
self.added("autocompleteRecords", id, data)
removed: (id) ->
self.removed("autocompleteRecords", id)
rooms = RocketChat.models.Rooms.findByNameAndTypeNotContainingUsername(regex, 'c', username, { limit: 5, fields: { t: 1, name: 1 }, sort: { name: 1 } }).fetch()

this.ready()
return {
users: users
rooms: rooms
}

this.onStop ->
subHandleUsers?.stop()
subHandleRooms?.stop()
DDPRateLimiter.addRule
type: 'method'
name: 'spotlight'
userId: (userId) ->
return true
, 10, 10000

0 comments on commit b3151e6

Please sign in to comment.