- Gets the link based on room type using user's subscription data - Do not loop over subscriptions for rooms without custom linkpull/3946/head
parent
7875e0247e
commit
c95be3ee7e
@ -1,128 +1,60 @@ |
||||
RocketChat.roomTypes = new class |
||||
roomTypesOrder = [] |
||||
roomTypes = {} |
||||
mainOrder = 1 |
||||
|
||||
### Adds a room type to app |
||||
@param identifier An identifier to the room type. If a real room, MUST BE the same of `db.rocketchat_room.t` field, if not, can be null |
||||
@param order Order number of the type |
||||
@param config |
||||
template: template name to render on sideNav |
||||
icon: icon class |
||||
route: |
||||
name: route name |
||||
action: route action function |
||||
### |
||||
add = (identifier, order, config) -> |
||||
unless identifier? |
||||
identifier = Random.id() |
||||
|
||||
if roomTypes[identifier]? |
||||
return false |
||||
|
||||
if not order? |
||||
order = mainOrder + 10 |
||||
mainOrder += 10 |
||||
|
||||
# @TODO validate config options |
||||
roomTypesOrder.push |
||||
identifier: identifier |
||||
order: order |
||||
roomTypes[identifier] = config |
||||
|
||||
if config.route?.path? and config.route?.name? and config.route?.action? |
||||
FlowRouter.route config.route.path, |
||||
name: config.route.name |
||||
action: config.route.action |
||||
triggersExit: [roomExit] |
||||
|
||||
### |
||||
@param roomType: room type (e.g.: c (for channels), d (for direct channels)) |
||||
@param subData: the user's subscription data |
||||
### |
||||
getRouteLink = (roomType, subData) -> |
||||
unless roomTypes[roomType]? |
||||
return false |
||||
|
||||
return FlowRouter.path roomTypes[roomType].route.name, roomTypes[roomType].route.link(subData) |
||||
|
||||
checkCondition = (roomType) -> |
||||
RocketChat.roomTypes = new class roomTypesClient extends roomTypesCommon |
||||
checkCondition: (roomType) -> |
||||
return not roomType.condition? or roomType.condition() |
||||
|
||||
getAllTypes = -> |
||||
getTypes: -> |
||||
orderedTypes = [] |
||||
|
||||
_.sortBy(roomTypesOrder, 'order').forEach (type) -> |
||||
orderedTypes.push roomTypes[type.identifier] |
||||
_.sortBy(@roomTypesOrder, 'order').forEach (type) => |
||||
orderedTypes.push @roomTypes[type.identifier] |
||||
|
||||
return orderedTypes |
||||
|
||||
getIcon = (roomType) -> |
||||
return roomTypes[roomType]?.icon |
||||
getIcon: (roomType) -> |
||||
return @roomTypes[roomType]?.icon |
||||
|
||||
getRoomName = (roomType, roomData) -> |
||||
return roomTypes[roomType]?.roomName roomData |
||||
getRoomName: (roomType, roomData) -> |
||||
return @roomTypes[roomType]?.roomName roomData |
||||
|
||||
getIdentifiers = (except) -> |
||||
getIdentifiers: (except) -> |
||||
except = [].concat except |
||||
list = _.reject roomTypesOrder, (t) -> return except.indexOf(t.identifier) isnt -1 |
||||
list = _.reject @roomTypesOrder, (t) -> return except.indexOf(t.identifier) isnt -1 |
||||
return _.map list, (t) -> return t.identifier |
||||
|
||||
findRoom = (roomType, identifier, user) -> |
||||
return roomTypes[roomType]?.findRoom identifier, user |
||||
findRoom: (roomType, identifier, user) -> |
||||
return @roomTypes[roomType]?.findRoom identifier, user |
||||
|
||||
canSendMessage = (roomId) -> |
||||
canSendMessage: (roomId) -> |
||||
return ChatSubscription.find({ rid: roomId }).count() > 0 |
||||
|
||||
verifyCanSendMessage = (roomId) -> |
||||
verifyCanSendMessage: (roomId) -> |
||||
room = ChatRoom.findOne({ _id: roomId }, { fields: { t: 1 } }) |
||||
return if not room?.t? |
||||
|
||||
roomType = room.t |
||||
|
||||
return roomTypes[roomType]?.canSendMessage roomId if roomTypes[roomType]?.canSendMessage? |
||||
return @roomTypes[roomType]?.canSendMessage roomId if @roomTypes[roomType]?.canSendMessage? |
||||
|
||||
return canSendMessage roomId |
||||
return @canSendMessage roomId |
||||
|
||||
verifyShowJoinLink = (roomId) -> |
||||
verifyShowJoinLink: (roomId) -> |
||||
room = ChatRoom.findOne({ _id: roomId }, { fields: { t: 1 } }) |
||||
return if not room?.t? |
||||
|
||||
roomType = room.t |
||||
|
||||
if not roomTypes[roomType]?.showJoinLink? |
||||
if not @roomTypes[roomType]?.showJoinLink? |
||||
return false |
||||
|
||||
return roomTypes[roomType].showJoinLink roomId |
||||
return @roomTypes[roomType].showJoinLink roomId |
||||
|
||||
getNotSubscribedTpl = (roomId) -> |
||||
getNotSubscribedTpl: (roomId) -> |
||||
room = ChatRoom.findOne({ _id: roomId }, { fields: { t: 1 } }) |
||||
return if not room?.t? |
||||
|
||||
roomType = room.t |
||||
|
||||
if not roomTypes[roomType]?.notSubscribedTpl? |
||||
if not @roomTypes[roomType]?.notSubscribedTpl? |
||||
return false |
||||
|
||||
return roomTypes[roomType].notSubscribedTpl |
||||
|
||||
# addType: addType |
||||
getTypes: getAllTypes |
||||
getIdentifiers: getIdentifiers |
||||
|
||||
findRoom: findRoom |
||||
|
||||
# setIcon: setIcon |
||||
getIcon: getIcon |
||||
getRoomName: getRoomName |
||||
|
||||
# setRoute: setRoute |
||||
getRouteLink: getRouteLink |
||||
|
||||
checkCondition: checkCondition |
||||
|
||||
verifyCanSendMessage: verifyCanSendMessage |
||||
verifyShowJoinLink: verifyShowJoinLink |
||||
getNotSubscribedTpl: getNotSubscribedTpl |
||||
|
||||
add: add |
||||
return @roomTypes[roomType].notSubscribedTpl |
||||
|
||||
@ -0,0 +1,62 @@ |
||||
class @roomTypesCommon |
||||
roomTypes: {} |
||||
roomTypesOrder: [] |
||||
mainOrder: 1 |
||||
|
||||
### Adds a room type to app |
||||
@param identifier An identifier to the room type. If a real room, MUST BE the same of `db.rocketchat_room.t` field, if not, can be null |
||||
@param order Order number of the type |
||||
@param config |
||||
template: template name to render on sideNav |
||||
icon: icon class |
||||
route: |
||||
name: route name |
||||
action: route action function |
||||
### |
||||
add: (identifier, order, config) -> |
||||
unless identifier? |
||||
identifier = Random.id() |
||||
|
||||
if @roomTypes[identifier]? |
||||
return false |
||||
|
||||
if not order? |
||||
order = @mainOrder + 10 |
||||
@mainOrder += 10 |
||||
|
||||
# @TODO validate config options |
||||
@roomTypesOrder.push |
||||
identifier: identifier |
||||
order: order |
||||
@roomTypes[identifier] = config |
||||
|
||||
if config.route?.path? and config.route?.name? and config.route?.action? |
||||
routeConfig = |
||||
name: config.route.name |
||||
action: config.route.action |
||||
|
||||
if Meteor.isClient |
||||
routeConfig.triggersExit = [ roomExit ] |
||||
|
||||
FlowRouter.route config.route.path, routeConfig |
||||
|
||||
hasCustomLink: (roomType) -> |
||||
return @roomTypes[roomType]?.route?.link? |
||||
|
||||
### |
||||
@param roomType: room type (e.g.: c (for channels), d (for direct channels)) |
||||
@param subData: the user's subscription data |
||||
### |
||||
getRouteLink: (roomType, subData) -> |
||||
unless @roomTypes[roomType]? |
||||
return false |
||||
|
||||
routeData = {} |
||||
|
||||
if @roomTypes[roomType]?.route?.link? |
||||
routeData = @roomTypes[roomType].route.link(subData) |
||||
else if subData?.name? |
||||
routeData = { name: subData.name } |
||||
|
||||
return FlowRouter.path @roomTypes[roomType].route.name, routeData |
||||
|
||||
@ -0,0 +1,44 @@ |
||||
/* globals openRoom */ |
||||
|
||||
RocketChat.roomTypes.add('l', 5, { |
||||
template: 'livechat', |
||||
icon: 'icon-chat-empty', |
||||
route: { |
||||
name: 'live', |
||||
path: '/live/:code(\\d+)', |
||||
action(params/*, queryParams*/) { |
||||
openRoom('l', params.code); |
||||
RocketChat.TabBar.showGroup('livechat', 'search'); |
||||
}, |
||||
link(sub) { |
||||
return { |
||||
code: sub.code |
||||
}; |
||||
} |
||||
}, |
||||
|
||||
findRoom(identifier) { |
||||
return ChatRoom.findOne({ code: parseInt(identifier) }); |
||||
}, |
||||
|
||||
roomName(roomData) { |
||||
if (!roomData.name) { |
||||
return roomData.label; |
||||
} else { |
||||
return roomData.name; |
||||
} |
||||
}, |
||||
|
||||
condition() { |
||||
return RocketChat.settings.get('Livechat_enabled') && RocketChat.authz.hasAllPermission('view-l-room'); |
||||
}, |
||||
|
||||
canSendMessage(roomId) { |
||||
let room = ChatRoom.findOne({ _id: roomId }, { fields: { open: 1 } }); |
||||
return room && room.open === true; |
||||
}, |
||||
|
||||
notSubscribedTpl: { |
||||
template: 'livechatNotSubscribed' |
||||
} |
||||
}); |
||||
Loading…
Reference in new issue