Skip to content
Snippets Groups Projects
Commit 309e8087 authored by Gabriel Engel's avatar Gabriel Engel Committed by GitHub
Browse files

Merge pull request #4343 from RocketChat/improvements/popup-without-subscriptions

Replace subs of popups with methods
parents 52e72c9a e06fb74e
No related merge requests found
Showing with 131 additions and 193 deletions
...@@ -47,13 +47,13 @@ Template.messagePopup.onCreated -> ...@@ -47,13 +47,13 @@ Template.messagePopup.onCreated ->
template.value = new ReactiveVar template.value = new ReactiveVar
template.trigger = val(template.data.trigger, '@') template.trigger = val(template.data.trigger, '')
template.triggerAnywhere = val(template.data.triggerAnywhere, true) template.triggerAnywhere = val(template.data.triggerAnywhere, true)
template.prefix = val(template.data.prefix, template.trigger) 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 if template.triggerAnywhere is true
template.matchSelectorRegex = val(template.data.matchSelectorRegex, new RegExp "(?:^| )#{template.trigger}[^\\s]*$") template.matchSelectorRegex = val(template.data.matchSelectorRegex, new RegExp "(?:^| )#{template.trigger}[^\\s]*$")
...@@ -147,8 +147,12 @@ Template.messagePopup.onCreated -> ...@@ -147,8 +147,12 @@ Template.messagePopup.onCreated ->
caret = getCursorPosition(template.input) caret = getCursorPosition(template.input)
firstPartValue = value.substr 0, caret firstPartValue = value.substr 0, caret
lastPartValue = value.substr 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 template.input.value = firstPartValue + lastPartValue
...@@ -161,20 +165,27 @@ Template.messagePopup.onCreated -> ...@@ -161,20 +165,27 @@ Template.messagePopup.onCreated ->
filter = template.textFilter.get() filter = template.textFilter.get()
if filter? if filter?
result = template.data.getFilter template.data.collection, filter filterCallback = (result) =>
if (template.data.collection instanceof Meteor.Collection and result.count? and result.count() is 0) or result?.length is 0 template.hasData.set result?.length > 0
template.hasData.set false template.records.set result
else
template.hasData.set true
template.records.set result Meteor.defer =>
template.verifySelection()
Meteor.defer => result = template.data.getFilter(template.data.collection, filter, filterCallback)
template.verifySelection() if result?
filterCallback result
Template.messagePopup.onRendered -> 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 'keyup', this.onInputKeyup.bind this
$(this.input).on 'keydown', this.onInputKeydown.bind this $(this.input).on 'keydown', this.onInputKeydown.bind this
......
<template name="messagePopupChannel"> <template name="messagePopupChannel">
<i class="{{icon}}"></i>
{{name}} {{name}}
</template> </template>
\ No newline at end of file
Template.messagePopupChannel.helpers({
icon() {
return RocketChat.roomTypes.getIcon(this.t);
}
});
@filteredUsers = new Mongo.Collection 'filtered-users'
@filteredUsersMemory = new Mongo.Collection null @filteredUsersMemory = new Mongo.Collection null
@channelAutocomplete = new Mongo.Collection 'channel-autocomplete'
Meteor.startup -> Meteor.startup ->
Tracker.autorun -> Tracker.autorun ->
...@@ -20,17 +18,55 @@ Meteor.startup -> ...@@ -20,17 +18,55 @@ Meteor.startup ->
ts: messageUser.ts 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 Template.messagePopupConfig.helpers
popupUserConfig: -> popupUserConfig: ->
self = this self = this
template = Template.instance() template = Template.instance()
config = config =
title: t('People') title: t('People')
collection: filteredUsersMemory collection: filteredUsersMemory
template: 'messagePopupUser' template: 'messagePopupUser'
getInput: self.getInput getInput: self.getInput
textFilterDelay: 200 textFilterDelay: 200
getFilter: (collection, filter) -> trigger: '@'
suffix: ' '
getFilter: (collection, filter, cb) ->
exp = new RegExp("#{RegExp.escape filter}", 'i') exp = new RegExp("#{RegExp.escape filter}", 'i')
# Get users from messages # Get users from messages
...@@ -44,6 +80,7 @@ Template.messagePopupConfig.helpers ...@@ -44,6 +80,7 @@ Template.messagePopupConfig.helpers
_id: item.username _id: item.username
username: item.username username: item.username
status: item.status status: item.status
sort: 1
# Get users of room # Get users of room
if items.length < 5 and filter?.trim() isnt '' if items.length < 5 and filter?.trim() isnt ''
...@@ -56,23 +93,14 @@ Template.messagePopupConfig.helpers ...@@ -56,23 +93,14 @@ Template.messagePopupConfig.helpers
_id: roomUsername _id: roomUsername
username: roomUsername username: roomUsername
status: Session.get('user_' + roomUsername + '_status') or 'offline' status: Session.get('user_' + roomUsername + '_status') or 'offline'
sort: 2
if items.length >= 5 if items.length >= 5
break break
# Get users from db # Get users from db
if items.length < 5 and filter?.trim() isnt '' if items.length < 5 and filter?.trim() isnt ''
messageUsers = _.pluck(items, 'username') getUsersFromServerDelayed filter, items, cb
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'
all = all =
_id: '@all' _id: '@all'
...@@ -80,12 +108,12 @@ Template.messagePopupConfig.helpers ...@@ -80,12 +108,12 @@ Template.messagePopupConfig.helpers
system: true system: true
name: t 'Notify_all_in_this_room' name: t 'Notify_all_in_this_room'
compatibility: 'channel group' compatibility: 'channel group'
sort: 4
exp = new RegExp("(^|\\s)#{RegExp.escape filter}", 'i') exp = new RegExp("(^|\\s)#{RegExp.escape filter}", 'i')
if exp.test(all.username) or exp.test(all.compatibility) if exp.test(all.username) or exp.test(all.compatibility)
items.unshift all items.push all
template.resultsLength.set items.length
return items return items
getValue: (_id, collection, firstPartValue) -> getValue: (_id, collection, firstPartValue) ->
...@@ -105,26 +133,26 @@ Template.messagePopupConfig.helpers ...@@ -105,26 +133,26 @@ Template.messagePopupConfig.helpers
popupChannelConfig: -> popupChannelConfig: ->
self = this self = this
template = Template.instance() template = Template.instance()
config = config =
title: t('Channels') title: t('Channels')
collection: channelAutocomplete collection: RocketChat.models.Subscriptions
trigger: '#' trigger: '#'
suffix: ' '
template: 'messagePopupChannel' template: 'messagePopupChannel'
getInput: self.getInput getInput: self.getInput
textFilterDelay: 200 getFilter: (collection, filter, cb) ->
getFilter: (collection, filter) ->
exp = new RegExp(filter, 'i') 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 records = collection.find({name: exp, t: {$in: ['c', 'p']}}, {limit: 5, sort: {ls: -1}}).fetch()
return results
if records.length < 5 and filter?.trim() isnt ''
getRoomsFromServerDelayed filter, records, cb
getValue: (_id, collection) -> return records
return collection.findOne(_id)?.name
getValue: (_id, collection, records) ->
return _.findWhere(records, {_id: _id})?.name
return config return config
...@@ -136,6 +164,7 @@ Template.messagePopupConfig.helpers ...@@ -136,6 +164,7 @@ Template.messagePopupConfig.helpers
title: t('Commands') title: t('Commands')
collection: RocketChat.slashCommands.commands collection: RocketChat.slashCommands.commands
trigger: '/' trigger: '/'
suffix: ' '
triggerAnywhere: false triggerAnywhere: false
template: 'messagePopupSlashCommand' template: 'messagePopupSlashCommand'
getInput: self.getInput getInput: self.getInput
...@@ -153,7 +182,6 @@ Template.messagePopupConfig.helpers ...@@ -153,7 +182,6 @@ Template.messagePopupConfig.helpers
commands = commands[0..10] commands = commands[0..10]
template.resultsLength.set commands.length
return commands return commands
return config return config
...@@ -172,7 +200,7 @@ Template.messagePopupConfig.helpers ...@@ -172,7 +200,7 @@ Template.messagePopupConfig.helpers
trigger: ':' trigger: ':'
prefix: '' prefix: ''
getInput: self.getInput getInput: self.getInput
getFilter: (collection, filter) -> getFilter: (collection, filter, cb) ->
results = [] results = []
key = ':' + filter key = ':' + filter
...@@ -197,23 +225,6 @@ Template.messagePopupConfig.helpers ...@@ -197,23 +225,6 @@ Template.messagePopupConfig.helpers
return 1 return 1
return 0 return 0
template.resultsLength.set results.length
return results return results
return config 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()
<template name="messagePopupConfig"> <template name="messagePopupConfig">
<div class="message-popup-results {{subscriptionNotReady}}"> <div class="message-popup-results">
{{#if emojiEnabled}} {{#if emojiEnabled}}
{{> messagePopup popupEmojiConfig}} {{> messagePopup popupEmojiConfig}}
{{/if}} {{/if}}
......
...@@ -34,6 +34,7 @@ Package.onUse(function(api) { ...@@ -34,6 +34,7 @@ Package.onUse(function(api) {
api.addFiles('message/message.coffee', 'client'); api.addFiles('message/message.coffee', 'client');
api.addFiles('message/messageBox.coffee', 'client'); api.addFiles('message/messageBox.coffee', 'client');
api.addFiles('message/popup/messagePopup.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/messagePopupConfig.coffee', 'client');
api.addFiles('message/popup/messagePopupEmoji.coffee', 'client'); api.addFiles('message/popup/messagePopupEmoji.coffee', 'client');
......
...@@ -7,80 +7,58 @@ ...@@ -7,80 +7,58 @@
$('.spotlight').removeClass('hidden') $('.spotlight').removeClass('hidden')
$('.spotlight input').focus() $('.spotlight input').focus()
serverResults = new ReactiveVar getFromServer = (filter, records, cb) =>
serverSearch = new ReactiveVar Meteor.call 'spotlight', filter, Meteor.user().username, (err, results) ->
if err?
Tracker.autorun -> return console.log err
text = serverSearch.get()
serverResults.set() server = []
if text?.trim().length >= 2 if results?.users?.length > 0
Meteor.call 'spotlight', text, Meteor.user().username, (err, results) -> for user in results.users when not _.findWhere(records, {t: 'd', name: user.username})?
if err? server.push({
return console.log err _id: user._id
t: 'd',
serverResults.set(results) 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 Template.spotlight.helpers
popupConfig: -> popupConfig: ->
self = this
template = Template.instance()
config = config =
cls: 'popup-down' cls: 'popup-down'
collection: RocketChat.models.Subscriptions collection: RocketChat.models.Subscriptions
template: 'spotlightTemplate' template: 'spotlightTemplate'
trigger: '' input: '[name=spotlight]'
suffix: '' getFilter: (collection, filter, cb) ->
getInput: () =>
template.find('[name=spotlight]')
textFilterDelay: 200
getFilter: (collection, filter) ->
exp = new RegExp("#{RegExp.escape filter}", 'i') exp = new RegExp("#{RegExp.escape filter}", 'i')
serverSearch.set(filter) records = collection.find({name: exp, rid: {$ne: Session.get('openedRoom')}}, {limit: 10, sort: {unread: -1, ls: -1}}).fetch()
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 cb(records)
for user in server.users when not _.findWhere(memory, {t: 'd', name: user.username})?
memory.push({
_id: user._id
t: 'd',
name: user.username
})
if server?.rooms?.length > 0 if filter?.trim().length < 1 or records.length >= 5
for room in server.rooms return
memory.push({
_id: room._id
t: 'c',
name: room.name
})
return memory getFromServerDelayed(filter, records, cb)
getValue: (_id, collection, records, firstPartValue) ->
getValue: (_id, collection, firstPartValue) -> doc = _.findWhere(records, {_id: _id})
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})
FlowRouter.go FlowRouter.path RocketChat.roomTypes.getRouteLink doc.t, doc FlowRouter.go FlowRouter.path RocketChat.roomTypes.getRouteLink doc.t, doc
spotlight.hide() spotlight.hide()
return ''
return config return config
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
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
Meteor.methods Meteor.methods
spotlight: (text, username) -> spotlight: (text, usernames, type = { users: true, rooms: true }) ->
result = result =
users: [] users: []
rooms: [] rooms: []
...@@ -9,10 +9,11 @@ Meteor.methods ...@@ -9,10 +9,11 @@ Meteor.methods
regex = new RegExp s.trim(s.escapeRegExp(text)), "i" regex = new RegExp s.trim(s.escapeRegExp(text)), "i"
if RocketChat.authz.hasPermission this.userId, 'view-d-room' if type.users is true and 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() 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() result.rooms = RocketChat.models.Rooms.findByNameAndTypeNotContainingUsername(regex, 'c', username, { limit: 5, fields: { t: 1, name: 1 }, sort: { name: 1 } }).fetch()
return result return result
......
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