Replace subs of popups with methods

pull/4343/head
Rodrigo Nascimento 9 years ago
parent 44ebe17991
commit e06fb74e97
No known key found for this signature in database
GPG Key ID: 2C85B3AFE75D23F9
  1. 35
      packages/rocketchat-ui-message/message/popup/messagePopup.coffee
  2. 3
      packages/rocketchat-ui-message/message/popup/messagePopupChannel.html
  3. 5
      packages/rocketchat-ui-message/message/popup/messagePopupChannel.js
  4. 105
      packages/rocketchat-ui-message/message/popup/messagePopupConfig.coffee
  5. 2
      packages/rocketchat-ui-message/message/popup/messagePopupConfig.html
  6. 1
      packages/rocketchat-ui-message/package.js
  7. 94
      packages/rocketchat-ui/views/app/spotlight/spotlight.coffee
  8. 36
      server/publications/channelAutocomplete.coffee
  9. 34
      server/publications/filteredUsers.coffee
  10. 9
      server/publications/spotlight.coffee

@ -47,13 +47,13 @@ Template.messagePopup.onCreated ->
template.value = new ReactiveVar
template.trigger = val(template.data.trigger, '@')
template.trigger = val(template.data.trigger, '')
template.triggerAnywhere = val(template.data.triggerAnywhere, true)
template.prefix = val(template.data.prefix, template.trigger)
template.suffix = val(template.data.suffix, ' ')
template.suffix = val(template.data.suffix, '')
if template.triggerAnywhere is true
template.matchSelectorRegex = val(template.data.matchSelectorRegex, new RegExp "(?:^| )#{template.trigger}[^\\s]*$")
@ -147,8 +147,12 @@ Template.messagePopup.onCreated ->
caret = getCursorPosition(template.input)
firstPartValue = value.substr 0, caret
lastPartValue = value.substr caret
getValue = this.getValue(template.value.curValue, template.data.collection, template.records.get(), firstPartValue)
firstPartValue = firstPartValue.replace(template.selectorRegex, template.prefix + this.getValue(template.value.curValue, template.data.collection, firstPartValue) + template.suffix)
if not getValue
return
firstPartValue = firstPartValue.replace(template.selectorRegex, template.prefix + getValue + template.suffix)
template.input.value = firstPartValue + lastPartValue
@ -161,20 +165,27 @@ Template.messagePopup.onCreated ->
filter = template.textFilter.get()
if filter?
result = template.data.getFilter template.data.collection, filter
if (template.data.collection instanceof Meteor.Collection and result.count? and result.count() is 0) or result?.length is 0
template.hasData.set false
else
template.hasData.set true
filterCallback = (result) =>
template.hasData.set result?.length > 0
template.records.set result
template.records.set result
Meteor.defer =>
template.verifySelection()
Meteor.defer =>
template.verifySelection()
result = template.data.getFilter(template.data.collection, filter, filterCallback)
if result?
filterCallback result
Template.messagePopup.onRendered ->
this.input = this.data.getInput?()
if this.data.getInput?
this.input = this.data.getInput?()
else if this.data.input
this.input = this.parentTemplate().find(this.data.input)
if not this.input?
console.error 'Input not found for popup'
$(this.input).on 'keyup', this.onInputKeyup.bind this
$(this.input).on 'keydown', this.onInputKeydown.bind this

@ -1,3 +1,4 @@
<template name="messagePopupChannel">
<i class="{{icon}}"></i>
{{name}}
</template>
</template>

@ -0,0 +1,5 @@
Template.messagePopupChannel.helpers({
icon() {
return RocketChat.roomTypes.getIcon(this.t);
}
});

@ -1,6 +1,4 @@
@filteredUsers = new Mongo.Collection 'filtered-users'
@filteredUsersMemory = new Mongo.Collection null
@channelAutocomplete = new Mongo.Collection 'channel-autocomplete'
Meteor.startup ->
Tracker.autorun ->
@ -20,17 +18,55 @@ Meteor.startup ->
ts: messageUser.ts
getUsersFromServer = (filter, records, cb) =>
messageUsers = _.pluck(records, 'username')
Meteor.call 'spotlight', filter, messageUsers, { users: true }, (err, results) ->
if err?
return console.error err
if results.users.length > 0
for result in results
if records.length < 5
records.push
_id: result.username
username: result.username
status: 'offline'
sort: 3
records = _.sortBy(records, 'sort')
cb(records)
getRoomsFromServer = (filter, records, cb) =>
Meteor.call 'spotlight', filter, null, { rooms: true }, (err, results) ->
if err?
return console.error err
if results.rooms.length > 0
for room in results.rooms
if records.length < 5
records.push room
cb(records)
getUsersFromServerDelayed = _.throttle getUsersFromServer, 500
getRoomsFromServerDelayed = _.throttle getRoomsFromServer, 500
Template.messagePopupConfig.helpers
popupUserConfig: ->
self = this
template = Template.instance()
config =
title: t('People')
collection: filteredUsersMemory
template: 'messagePopupUser'
getInput: self.getInput
textFilterDelay: 200
getFilter: (collection, filter) ->
trigger: '@'
suffix: ' '
getFilter: (collection, filter, cb) ->
exp = new RegExp("#{RegExp.escape filter}", 'i')
# Get users from messages
@ -44,6 +80,7 @@ Template.messagePopupConfig.helpers
_id: item.username
username: item.username
status: item.status
sort: 1
# Get users of room
if items.length < 5 and filter?.trim() isnt ''
@ -56,23 +93,14 @@ Template.messagePopupConfig.helpers
_id: roomUsername
username: roomUsername
status: Session.get('user_' + roomUsername + '_status') or 'offline'
sort: 2
if items.length >= 5
break
# Get users from db
if items.length < 5 and filter?.trim() isnt ''
messageUsers = _.pluck(items, 'username')
template.userFilter.set
name: filter
except: messageUsers
if template.subscriptionsReady()
filteredUsers.find({username: exp}, {limit: 5 - messageUsers.length}).fetch().forEach (item) ->
items.push
_id: item.username
username: item.username
status: 'offline'
getUsersFromServerDelayed filter, items, cb
all =
_id: '@all'
@ -80,12 +108,12 @@ Template.messagePopupConfig.helpers
system: true
name: t 'Notify_all_in_this_room'
compatibility: 'channel group'
sort: 4
exp = new RegExp("(^|\\s)#{RegExp.escape filter}", 'i')
if exp.test(all.username) or exp.test(all.compatibility)
items.unshift all
items.push all
template.resultsLength.set items.length
return items
getValue: (_id, collection, firstPartValue) ->
@ -105,26 +133,26 @@ Template.messagePopupConfig.helpers
popupChannelConfig: ->
self = this
template = Template.instance()
config =
title: t('Channels')
collection: channelAutocomplete
collection: RocketChat.models.Subscriptions
trigger: '#'
suffix: ' '
template: 'messagePopupChannel'
getInput: self.getInput
textFilterDelay: 200
getFilter: (collection, filter) ->
getFilter: (collection, filter, cb) ->
exp = new RegExp(filter, 'i')
template.channelFilter.set filter
if template.channelSubscription.ready()
results = collection.find( { name: exp }, { limit: 5 }).fetch()
else
results = []
template.resultsLength.set results.length
return results
records = collection.find({name: exp, t: {$in: ['c', 'p']}}, {limit: 5, sort: {ls: -1}}).fetch()
if records.length < 5 and filter?.trim() isnt ''
getRoomsFromServerDelayed filter, records, cb
getValue: (_id, collection) ->
return collection.findOne(_id)?.name
return records
getValue: (_id, collection, records) ->
return _.findWhere(records, {_id: _id})?.name
return config
@ -136,6 +164,7 @@ Template.messagePopupConfig.helpers
title: t('Commands')
collection: RocketChat.slashCommands.commands
trigger: '/'
suffix: ' '
triggerAnywhere: false
template: 'messagePopupSlashCommand'
getInput: self.getInput
@ -153,7 +182,6 @@ Template.messagePopupConfig.helpers
commands = commands[0..10]
template.resultsLength.set commands.length
return commands
return config
@ -172,7 +200,7 @@ Template.messagePopupConfig.helpers
trigger: ':'
prefix: ''
getInput: self.getInput
getFilter: (collection, filter) ->
getFilter: (collection, filter, cb) ->
results = []
key = ':' + filter
@ -197,23 +225,6 @@ Template.messagePopupConfig.helpers
return 1
return 0
template.resultsLength.set results.length
return results
return config
subscriptionNotReady: ->
template = Template.instance()
return 'notready' if template.resultsLength.get() is 0 and not template.subscriptionsReady()
Template.messagePopupConfig.onCreated ->
@userFilter = new ReactiveVar {}
@channelFilter = new ReactiveVar ''
@resultsLength = new ReactiveVar 0
template = @
@autorun ->
template.userSubscription = template.subscribe 'filteredUsers', template.userFilter.get()
@autorun ->
template.channelSubscription = template.subscribe 'channelAutocomplete', template.channelFilter.get()

@ -1,5 +1,5 @@
<template name="messagePopupConfig">
<div class="message-popup-results {{subscriptionNotReady}}">
<div class="message-popup-results">
{{#if emojiEnabled}}
{{> messagePopup popupEmojiConfig}}
{{/if}}

@ -34,6 +34,7 @@ Package.onUse(function(api) {
api.addFiles('message/message.coffee', 'client');
api.addFiles('message/messageBox.coffee', 'client');
api.addFiles('message/popup/messagePopup.coffee', 'client');
api.addFiles('message/popup/messagePopupChannel.js', 'client');
api.addFiles('message/popup/messagePopupConfig.coffee', 'client');
api.addFiles('message/popup/messagePopupEmoji.coffee', 'client');

@ -7,80 +7,58 @@
$('.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)
getFromServer = (filter, records, cb) =>
Meteor.call 'spotlight', filter, Meteor.user().username, (err, results) ->
if err?
return console.log err
server = []
if results?.users?.length > 0
for user in results.users when not _.findWhere(records, {t: 'd', name: user.username})?
server.push({
_id: user._id
t: 'd',
name: user.username
})
if results?.rooms?.length > 0
for room in results.rooms
server.push({
_id: room._id
t: 'c',
name: room.name
})
if server.length > 0
cb(records.concat(server))
getFromServerDelayed = _.throttle getFromServer, 500
Template.spotlight.helpers
popupConfig: ->
self = this
template = Template.instance()
config =
cls: 'popup-down'
collection: RocketChat.models.Subscriptions
template: 'spotlightTemplate'
trigger: ''
suffix: ''
getInput: () =>
template.find('[name=spotlight]')
textFilterDelay: 200
getFilter: (collection, filter) ->
input: '[name=spotlight]'
getFilter: (collection, filter, cb) ->
exp = new RegExp("#{RegExp.escape filter}", 'i')
serverSearch.set(filter)
memory = collection.find({name: exp, rid: {$ne: Session.get('openedRoom')}}, {limit: 10, sort: {unread: -1, ls: -1}}).fetch()
server = serverResults.get()
records = collection.find({name: exp, rid: {$ne: Session.get('openedRoom')}}, {limit: 10, sort: {unread: -1, ls: -1}}).fetch()
if server?.users?.length > 0
for user in server.users when not _.findWhere(memory, {t: 'd', name: user.username})?
memory.push({
_id: user._id
t: 'd',
name: user.username
})
cb(records)
if server?.rooms?.length > 0
for room in server.rooms
memory.push({
_id: room._id
t: 'c',
name: room.name
})
if filter?.trim().length < 1 or records.length >= 5
return
return memory
getFromServerDelayed(filter, records, cb)
getValue: (_id, collection, firstPartValue) ->
doc = collection.findOne(_id)
if not doc?
server = serverResults.get()
if server?.users?.length > 0
doc = _.findWhere(server.users, {_id: _id})
if doc?
doc.name = doc.username
doc.t = 'd'
if not doc? and server?.rooms?.length > 0
doc = _.findWhere(server.rooms, {_id: _id})
getValue: (_id, collection, records, firstPartValue) ->
doc = _.findWhere(records, {_id: _id})
FlowRouter.go FlowRouter.path RocketChat.roomTypes.getRouteLink doc.t, doc
spotlight.hide()
return ''
return config

@ -1,36 +0,0 @@
Meteor.publish 'channelAutocomplete', (name) ->
unless this.userId
return this.ready()
pub = this
options =
fields:
_id: 1
name: 1
limit: 5
sort:
name: 1
roomIds = []
if not RocketChat.authz.hasPermission(this.userId, 'view-c-room') and RocketChat.authz.hasPermission(this.userId, 'view-joined-room')
roomIds = _.pluck RocketChat.models.Subscriptions.findByUserId(this.userId).fetch(), 'rid'
hasPermission = (_id) =>
return RocketChat.authz.hasPermission(this.userId, 'view-c-room') or (RocketChat.authz.hasPermission(this.userId, 'view-joined-room') and roomIds.indexOf(_id) isnt -1)
cursorHandle = RocketChat.models.Rooms.findByNameContainingAndTypes(name, ['c'], options).observeChanges
added: (_id, record) ->
if hasPermission(_id)
pub.added('channel-autocomplete', _id, record)
changed: (_id, record) ->
if hasPermission(_id)
pub.changed('channel-autocomplete', _id, record)
removed: (_id, record) ->
if hasPermission(_id)
pub.removed('channel-autocomplete', _id, record)
@ready()
@onStop ->
cursorHandle.stop()
return

@ -1,34 +0,0 @@
Meteor.publish 'filteredUsers', (selector) ->
unless this.userId
return this.ready()
if not _.isObject selector
return this.ready()
options =
fields:
name: 1
username: 1
sort:
username: 1
limit: 5
pub = this
exceptions = selector.except or []
cursorHandle = RocketChat.models.Users.findByActiveUsersUsernameExcept(selector.name, exceptions, options).observeChanges
added: (_id, record) ->
pub.added('filtered-users', _id, record)
changed: (_id, record) ->
pub.changed('filtered-users', _id, record)
removed: (_id, record) ->
pub.removed('filtered-users', _id, record)
@ready()
@onStop ->
cursorHandle.stop()
return

@ -1,5 +1,5 @@
Meteor.methods
spotlight: (text, username) ->
spotlight: (text, usernames, type = { users: true, rooms: true }) ->
result =
users: []
rooms: []
@ -9,10 +9,11 @@ Meteor.methods
regex = new RegExp s.trim(s.escapeRegExp(text)), "i"
if RocketChat.authz.hasPermission this.userId, 'view-d-room'
result.users = RocketChat.models.Users.findByActiveUsersUsernameExcept(text, [ username ], { limit: 5, fields: { username: 1, status: 1 }, sort: { username: 1 } }).fetch()
if type.users is true and RocketChat.authz.hasPermission this.userId, 'view-d-room'
result.users = RocketChat.models.Users.findByActiveUsersUsernameExcept(text, usernames, { limit: 5, fields: { username: 1, status: 1 }, sort: { username: 1 } }).fetch()
if RocketChat.authz.hasPermission this.userId, 'view-c-room'
if type.rooms is true and RocketChat.authz.hasPermission this.userId, 'view-c-room'
username = RocketChat.models.Users.findOne(this.userId, {username: 1}).username
result.rooms = RocketChat.models.Rooms.findByNameAndTypeNotContainingUsername(regex, 'c', username, { limit: 5, fields: { t: 1, name: 1 }, sort: { name: 1 } }).fetch()
return result

Loading…
Cancel
Save