diff --git a/packages/rocketchat-lib/client/lib/roomTypes.coffee b/packages/rocketchat-lib/client/lib/roomTypes.coffee index 96a2bbde7b4..09bce518392 100644 --- a/packages/rocketchat-lib/client/lib/roomTypes.coffee +++ b/packages/rocketchat-lib/client/lib/roomTypes.coffee @@ -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 diff --git a/packages/rocketchat-lib/lib/roomTypesCommon.coffee b/packages/rocketchat-lib/lib/roomTypesCommon.coffee new file mode 100644 index 00000000000..f18efd87c77 --- /dev/null +++ b/packages/rocketchat-lib/lib/roomTypesCommon.coffee @@ -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 + diff --git a/packages/rocketchat-lib/package.js b/packages/rocketchat-lib/package.js index 78a6e29df65..2f6212696b1 100644 --- a/packages/rocketchat-lib/package.js +++ b/packages/rocketchat-lib/package.js @@ -34,7 +34,7 @@ Package.onUse(function(api) { api.use('rocketchat:logger'); api.use('templating', 'client'); - api.use('kadira:flow-router', 'client'); + api.use('kadira:flow-router'); api.addFiles('lib/core.coffee'); @@ -48,6 +48,7 @@ Package.onUse(function(api) { api.addFiles('lib/fileUploadRestrictions.js'); api.addFiles('lib/placeholders.js'); api.addFiles('lib/promises.coffee'); + api.addFiles('lib/roomTypesCommon.coffee'); api.addFiles('lib/slashCommand.coffee'); api.addFiles('lib/Message.coffee'); api.addFiles('lib/MessageTypes.coffee'); @@ -138,6 +139,8 @@ Package.onUse(function(api) { api.addFiles('client/models/_Base.coffee', 'client'); api.addFiles('client/models/Uploads.coffee', 'client'); + api.addFiles('startup/defaultRoomTypes.coffee'); + // VERSION api.addFiles('rocketchat.info'); diff --git a/packages/rocketchat-lib/server/lib/roomTypes.coffee b/packages/rocketchat-lib/server/lib/roomTypes.coffee index eb2b751d10a..6a43a125aa5 100644 --- a/packages/rocketchat-lib/server/lib/roomTypes.coffee +++ b/packages/rocketchat-lib/server/lib/roomTypes.coffee @@ -1,26 +1,23 @@ -RocketChat.roomTypes = new class - roomTypes = {} - +RocketChat.roomTypes = new class roomTypesServer extends roomTypesCommon ### add a publish for a room type @param roomType: room type (e.g.: c (for channels), d (for direct channels)) @param callback: function that will return the publish's data ### - setPublish = (roomType, callback) -> - if roomTypes[roomType]?.publish? + setPublish: (roomType, callback) -> + if @roomTypes[roomType]?.publish? throw new Meteor.Error 'route-publish-exists', 'Publish for the given type already exists' - unless roomTypes[roomType]? - roomTypes[roomType] = {} + unless @roomTypes[roomType]? + @roomTypes[roomType] = {} - roomTypes[roomType].publish = callback + @roomTypes[roomType].publish = callback ### run the publish for a room type + @param scope: Meteor publish scope @param roomType: room type (e.g.: c (for channels), d (for direct channels)) @param identifier: identifier of the room ### - runPublish = (roomType, identifier) -> - return unless roomTypes[roomType].publish? - return roomTypes[roomType].publish.call this, identifier + runPublish: (scope, roomType, identifier) -> + return unless @roomTypes[roomType]?.publish? + return @roomTypes[roomType].publish.call scope, identifier - setPublish: setPublish - runPublish: runPublish diff --git a/packages/rocketchat-lib/server/lib/sendEmailOnMessage.js b/packages/rocketchat-lib/server/lib/sendEmailOnMessage.js index 91ca179673d..c88c6e682fd 100644 --- a/packages/rocketchat-lib/server/lib/sendEmailOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendEmailOnMessage.js @@ -27,8 +27,9 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { }); } - var getMessageLink = () => { - var path = directMessage ? `direct/${ room.username }` : `channel/${ room.name }`; + var getMessageLink = (room, sub) => { + var roomPath = RocketChat.roomTypes.getRouteLink(room.t, sub); + var path = Meteor.absoluteUrl(roomPath ? roomPath.replace(/^\//, '') : ''); var style = [ 'color: #fff;', 'padding: .5em;', @@ -41,24 +42,23 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { 'margin-bottom: 8px;' ].join(' '); var message = TAPi18n.__('Offline_Link_Message'); - return `${ message }`; + return `${ message }`; }; var divisorMessage = '