Skip to content
Snippets Groups Projects
Commit 4048ddb6 authored by Diego Sampaio's avatar Diego Sampaio
Browse files

new roomTypes API

parent 733f7ea4
Branches release-5.3.3
Tags 5.3.3
No related merge requests found
Meteor.startup ->
# RocketChat.roomTypes.addType('starredRooms', roles);
# roles = ['admin', 'moderator', 'user']
RocketChat.roomTypes.add 'c',
template: 'channels'
......
......@@ -20,39 +20,15 @@ RocketChat.roomTypes = new class
roomTypesOrder.push identifier
roomTypes[identifier] = config
### Sets a route for a room type
@param roomType: room type (e.g.: c (for channels), d (for direct channels))
@param routeName: route's name for given type
@param dataCallback: callback for the route data. receives the whole subscription data as parameter
###
setRoute = (roomType, routeName, dataCallback) ->
if routes[roomType]?
throw new Meteor.Error 'route-callback-exists', 'Route callback for the given type already exists'
# dataCallback ?= -> return {}
routes[roomType] =
name: routeName
data: dataCallback or -> return {}
###
@param roomType: room type (e.g.: c (for channels), d (for direct channels))
@param subData: the user's subscription data
###
getRoute = (roomType, subData) ->
unless routes[roomType]?
getRouteLink = (roomType, subData) ->
unless roomTypes[roomType]?
throw new Meteor.Error 'route-doesnt-exists', 'There is no route for the type: ' + roomType
return FlowRouter.path routes[roomType].name, routes[roomType].data(subData)
### add a type of room
@param template: the name of the template to render on sideNav
@param roles[]: a list of roles a user must have to see the template
###
addType = (template, roles = []) ->
rooms.push
template: template
roles: [].concat roles
return FlowRouter.path roomTypes[roomType].route.name, roomTypes[roomType].route.link(subData)
getAllTypes = ->
typesPermitted = []
......@@ -66,38 +42,42 @@ RocketChat.roomTypes = new class
@param roomType: room type (e.g.: c (for channels), d (for direct channels))
@param callback: function that will return the publish's data
###
addPublish = (roomType, callback) ->
if publishes[roomType]?
setPublish = (roomType, callback) ->
if roomTypes[roomType]?.publish?
throw new Meteor.Error 'route-publish-exists', 'Publish for the given type already exists'
publishes[roomType] = callback
unless roomTypes[roomType]?
roomTypesOrder.push roomType
roomTypes[roomType] = {}
roomTypes[roomType].publish = callback
### run the publish for a room type
@param roomType: room type (e.g.: c (for channels), d (for direct channels))
@param identifier: identifier of the room
###
runPublish = (roomType, identifier) ->
return unless publishes[roomType]?
return publishes[roomType].call this, identifier
return unless roomTypes[roomType].publish?
return roomTypes[roomType].publish.call this, identifier
getIcon = (roomType) ->
return icons[roomType]
return roomTypes[roomType]?.icon
###
@param roomType: room type (e.g.: c (for channels), d (for direct channels))
@param iconClass: iconClass to display on sideNav
###
setIcon = (roomType, iconClass) ->
icons[roomType] = iconClass
getIdentifiers = (except) ->
except = [].concat except
return _.reject roomTypesOrder, (t) -> return except.indexOf(t) isnt -1
addType: addType
# addType: addType
getTypes: getAllTypes
getIdentifiers: getIdentifiers
setIcon: setIcon
# setIcon: setIcon
getIcon: getIcon
setRoute: setRoute
getRoute: getRoute
# setRoute: setRoute
getRouteLink: getRouteLink
setPublish: setPublish
runPublish: runPublish
addPublish: addPublish
publish: runPublish
add: add
......@@ -106,6 +106,13 @@ RocketChat.models.Rooms = new class extends RocketChat.models._Base
return @find query, options
findByTypeContainigUsernames: (type, username, options) ->
query =
t: type
usernames: { $all: [].concat(username) }
return @find query, options
findByTypesAndNotUserIdContainingUsername: (types, userId, username, options) ->
query =
t:
......
Meteor.startup(function() {
RocketChat.roomTypes.addPublish('l', function(identifier) {
Meteor.startup(() => {
RocketChat.roomTypes.setPublish('l', (identifier) => {
return RocketChat.models.Rooms.findByTypeAndName('l', identifier, {
fields: {
name: 1,
......
......@@ -33,7 +33,7 @@ Template.chatRoomItem.helpers
return true
route: ->
FlowRouter.path RocketChat.roomTypes.getRoute @t, @
FlowRouter.path RocketChat.roomTypes.getRouteLink @t, @
Template.chatRoomItem.rendered = ->
if not (FlowRouter.getParam('_id')? and FlowRouter.getParam('_id') is this.data.rid) and not this.data.ls
......
......@@ -96,5 +96,5 @@ Template.sideNav.onRendered ->
lastLink = $('.rooms-list h3.history-div').get(0)
RocketChat.roomTypes.getTypes().forEach (roomType) ->
if RocketChat.authz.hasRole(Meteor.userId(), roomType.roles) && Template[roomType.template]?
if Template[roomType.template]?
Blaze.render Template[roomType.template], wrapper, lastLink
......@@ -3,10 +3,7 @@ Template.roomSearch.helpers
return 'icon-at' if this.type is 'u'
if this.type is 'r'
switch this.t
when 'd' then return 'icon-at'
when 'c' then return 'icon-hash'
when 'p' then return 'icon-lock'
return RocketChat.roomTypes.getIcon this.t
userStatus: ->
if this.type is 'u'
......
......@@ -35,10 +35,7 @@ Template.spotlight.events
FlowRouter.go('direct', { username: doc.username })
event.currentTarget.value = ''
else if doc.type is 'r'
if doc.t is 'c'
FlowRouter.go('channel', { name: doc.name })
else if doc.t is 'p'
FlowRouter.go('group', { name: doc.name })
FlowRouter.go FlowRouter.path RocketChat.roomTypes.getRouteLink doc.t, doc
event.currentTarget.value = ''
......
......@@ -10,4 +10,4 @@ Meteor.publish 'room', (typeName) ->
type = typeName.substr(0, 1)
name = typeName.substr(1)
return RocketChat.roomTypes.publish.call(this, type, name)
return RocketChat.roomTypes.runPublish.call(this, type, name)
......@@ -24,7 +24,7 @@ Meteor.publish 'roomSearch', (selector, options, collName) ->
self.removed("autocompleteRecords", id)
if not searchType? or searchType is 'r'
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
subHandleRooms = RocketChat.models.Rooms.findByTypesAndNotUserIdContainingUsername(RocketChat.roomTypes.getIdentifiers('d'), 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)
......
Meteor.startup ->
RocketChat.roomTypes.addPublish 'c', (identifier) ->
RocketChat.roomTypes.setPublish 'c', (identifier) ->
options =
fields:
name: 1
......@@ -9,7 +9,7 @@ Meteor.startup ->
usernames: 1
return RocketChat.models.Rooms.findByTypeAndName 'c', identifier, options
RocketChat.roomTypes.addPublish 'p', (identifier) ->
RocketChat.roomTypes.setPublish 'p', (identifier) ->
options =
fields:
name: 1
......@@ -20,7 +20,7 @@ Meteor.startup ->
user = RocketChat.models.Users.findOneById this.userId, fields: username: 1
return RocketChat.models.Rooms.findByTypeAndNameContainigUsername 'p', identifier, user.username, options
RocketChat.roomTypes.addPublish 'd', (identifier) ->
RocketChat.roomTypes.setPublish 'd', (identifier) ->
options =
fields:
name: 1
......@@ -29,4 +29,4 @@ Meteor.startup ->
u: 1
usernames: 1
user = RocketChat.models.Users.findOneById this.userId, fields: username: 1
return RocketChat.models.Rooms.findByTypeContainigUsername 'd', user.username, options
return RocketChat.models.Rooms.findByTypeContainigUsernames 'd', [user.username, identifier], options
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