diff --git a/client/routes/roomRoute.coffee b/client/routes/roomRoute.coffee index 152d2ac02f4..96ecb33cede 100644 --- a/client/routes/roomRoute.coffee +++ b/client/routes/roomRoute.coffee @@ -1,88 +1,3 @@ -currentTracker = undefined - -openRoom = (type, name) -> - Session.set 'openedRoom', null - - Meteor.defer -> - currentTracker = Tracker.autorun (c) -> - if RoomManager.open(type + name).ready() isnt true - BlazeLayout.render 'main', {center: 'loading'} - return - - currentTracker = undefined - c.stop() - - query = - t: type - name: name - - if type is 'd' - delete query.name - query.usernames = - $all: [name, Meteor.user()?.username] - - room = ChatRoom.findOne(query) - if not room? - Session.set 'roomNotFound', {type: type, name: name} - BlazeLayout.render 'main', {center: 'roomNotFound'} - return - - mainNode = document.querySelector('.main-content') - if mainNode? - for child in mainNode.children - mainNode.removeChild child if child? - roomDom = RoomManager.getDomOfRoom(type + name, room._id) - mainNode.appendChild roomDom - if roomDom.classList.contains('room-container') - roomDom.querySelector('.messages-box > .wrapper').scrollTop = roomDom.oldScrollTop - - Session.set 'openedRoom', room._id - - Session.set 'editRoomTitle', false - RoomManager.updateMentionsMarksOfRoom type + name - Meteor.setTimeout -> - readMessage.readNow() - , 2000 - # KonchatNotification.removeRoomNotification(params._id) - - if Meteor.Device.isDesktop() - setTimeout -> - $('.message-form .input-message').focus() - , 100 - - RocketChat.TabBar.resetButtons() - RocketChat.TabBar.addButton({ id: 'message-search', title: t('Search'), icon: 'octicon octicon-search', template: 'messageSearch', order: 1 }) - if type is 'd' - RocketChat.TabBar.addButton({ id: 'members-list', title: t('User_Info'), icon: 'octicon octicon-person', template: 'membersList', order: 2 }) - else - RocketChat.TabBar.addButton({ id: 'members-list', title: t('Members_List'), icon: 'octicon octicon-organization', template: 'membersList', order: 2 }) - RocketChat.TabBar.addButton({ id: 'uploaded-files-list', title: t('Room_uploaded_file_list'), icon: 'octicon octicon-file-symlink-directory', template: 'uploadedFilesList', order: 3 }) - - # update user's room subscription - if ChatSubscription.findOne({rid: room._id})?.open is false - Meteor.call 'openRoom', room._id - - RocketChat.callbacks.run 'enter-room' - -roomExit = -> - BlazeLayout.render 'main', {center: 'none'} - - if currentTracker? - currentTracker.stop() - - mainNode = document.querySelector('.main-content') - if mainNode? - for child in mainNode.children - if child? - if child.classList.contains('room-container') - wrapper = child.querySelector('.messages-box > .wrapper') - if wrapper.scrollTop >= wrapper.scrollHeight - wrapper.clientHeight - child.oldScrollTop = 10e10 - else - child.oldScrollTop = wrapper.scrollTop - mainNode.removeChild child - - FlowRouter.route '/channel/:name', name: 'channel' diff --git a/packages/rocketchat-lib/client/lib/openRoom.coffee b/packages/rocketchat-lib/client/lib/openRoom.coffee new file mode 100644 index 00000000000..9176289077e --- /dev/null +++ b/packages/rocketchat-lib/client/lib/openRoom.coffee @@ -0,0 +1,65 @@ +currentTracker = undefined + +@openRoom = (type, name) -> + Session.set 'openedRoom', null + + Meteor.defer -> + currentTracker = Tracker.autorun (c) -> + if RoomManager.open(type + name).ready() isnt true + BlazeLayout.render 'main', {center: 'loading'} + return + + currentTracker = undefined + c.stop() + + query = + t: type + name: name + + if type is 'd' + delete query.name + query.usernames = + $all: [name, Meteor.user()?.username] + + room = ChatRoom.findOne(query) + if not room? + Session.set 'roomNotFound', {type: type, name: name} + BlazeLayout.render 'main', {center: 'roomNotFound'} + return + + mainNode = document.querySelector('.main-content') + if mainNode? + for child in mainNode.children + mainNode.removeChild child if child? + roomDom = RoomManager.getDomOfRoom(type + name, room._id) + mainNode.appendChild roomDom + if roomDom.classList.contains('room-container') + roomDom.querySelector('.messages-box > .wrapper').scrollTop = roomDom.oldScrollTop + + Session.set 'openedRoom', room._id + + Session.set 'editRoomTitle', false + RoomManager.updateMentionsMarksOfRoom type + name + Meteor.setTimeout -> + readMessage.readNow() + , 2000 + # KonchatNotification.removeRoomNotification(params._id) + + if Meteor.Device.isDesktop() + setTimeout -> + $('.message-form .input-message').focus() + , 100 + + RocketChat.TabBar.resetButtons() + RocketChat.TabBar.addButton({ id: 'message-search', title: t('Search'), icon: 'octicon octicon-search', template: 'messageSearch', order: 1 }) + if type is 'd' + RocketChat.TabBar.addButton({ id: 'members-list', title: t('User_Info'), icon: 'octicon octicon-person', template: 'membersList', order: 2 }) + else + RocketChat.TabBar.addButton({ id: 'members-list', title: t('Members_List'), icon: 'octicon octicon-organization', template: 'membersList', order: 2 }) + RocketChat.TabBar.addButton({ id: 'uploaded-files-list', title: t('Room_uploaded_file_list'), icon: 'octicon octicon-file-symlink-directory', template: 'uploadedFilesList', order: 3 }) + + # update user's room subscription + if ChatSubscription.findOne({rid: room._id})?.open is false + Meteor.call 'openRoom', room._id + + RocketChat.callbacks.run 'enter-room' diff --git a/packages/rocketchat-lib/client/lib/roomExit.coffee b/packages/rocketchat-lib/client/lib/roomExit.coffee new file mode 100644 index 00000000000..d8a44ae00a3 --- /dev/null +++ b/packages/rocketchat-lib/client/lib/roomExit.coffee @@ -0,0 +1,17 @@ +@roomExit = -> + BlazeLayout.render 'main', {center: 'none'} + + if currentTracker? + currentTracker.stop() + + mainNode = document.querySelector('.main-content') + if mainNode? + for child in mainNode.children + if child? + if child.classList.contains('room-container') + wrapper = child.querySelector('.messages-box > .wrapper') + if wrapper.scrollTop >= wrapper.scrollHeight - wrapper.clientHeight + child.oldScrollTop = 10e10 + else + child.oldScrollTop = wrapper.scrollTop + mainNode.removeChild child diff --git a/packages/rocketchat-lib/client/lib/roomTypes.coffee b/packages/rocketchat-lib/lib/roomTypes.coffee similarity index 65% rename from packages/rocketchat-lib/client/lib/roomTypes.coffee rename to packages/rocketchat-lib/lib/roomTypes.coffee index af7d6c939f2..69622f99ede 100644 --- a/packages/rocketchat-lib/client/lib/roomTypes.coffee +++ b/packages/rocketchat-lib/lib/roomTypes.coffee @@ -1,6 +1,7 @@ RocketChat.roomTypes = new class rooms = [] routes = {} + publishes = {} ### Sets a route for a room type @param roomType: room type (e.g.: c (for channels), d (for direct channels)) @@ -39,7 +40,29 @@ RocketChat.roomTypes = new class getAllTypes = -> return rooms + ### 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 + ### + addPublish = (roomType, callback) -> + if publishes[roomType]? + throw new Meteor.Error 'route-publish-exists', 'Publish for the given type already exists' + + publishes[roomType] = 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 + addType: addType getTypes: getAllTypes + setRoute: setRoute getRoute: getRoute + + addPublish: addPublish + publish: runPublish diff --git a/packages/rocketchat-lib/package.js b/packages/rocketchat-lib/package.js index e535cdd56ee..ea4c4ff85f5 100644 --- a/packages/rocketchat-lib/package.js +++ b/packages/rocketchat-lib/package.js @@ -21,6 +21,7 @@ Package.onUse(function(api) { // COMMON api.addFiles('lib/core.coffee'); api.addFiles('lib/callbacks.coffee'); + api.addFiles('lib/roomTypes.coffee'); api.addFiles('lib/slashCommand.coffee'); // MODELS SERVER @@ -45,7 +46,8 @@ Package.onUse(function(api) { api.addFiles('settings/lib/settings.coffee'); // CLIENT - api.addFiles('client/lib/roomTypes.coffee', 'client'); + api.addFiles('client/lib/openRoom.coffee', 'client'); + api.addFiles('client/lib/roomExit.coffee', 'client'); api.addFiles('client/Notifications.coffee', 'client'); api.addFiles('client/TabBar.coffee', 'client'); api.addFiles('client/MessageAction.coffee', 'client'); diff --git a/server/publications/room.coffee b/server/publications/room.coffee index a5cdcb4d92c..ef32aecaf2a 100644 --- a/server/publications/room.coffee +++ b/server/publications/room.coffee @@ -10,26 +10,4 @@ Meteor.publish 'room', (typeName) -> type = typeName.substr(0, 1) name = typeName.substr(1) - options = - fields: - name: 1 - t: 1 - cl: 1 - u: 1 - usernames: 1 - - switch type - when 'c' - return RocketChat.models.Rooms.findByTypeAndName 'c', name, options - - when 'p' - user = RocketChat.models.Users.findOneById this.userId, fields: username: 1 - return RocketChat.models.Rooms.findByTypeAndNameContainigUsername 'p', name, user.username, options - - when 'd' - user = RocketChat.models.Users.findOneById this.userId, fields: username: 1 - return RocketChat.models.Rooms.findByTypeContainigUsername 'd', user.username, options - - # Change to validate access manualy - # if not Meteor.call 'canAccessRoom', rid, this.userId - # return this.ready() + return RocketChat.roomTypes.publish.call(this, type, name) diff --git a/server/startup/roomPublishes.coffee b/server/startup/roomPublishes.coffee new file mode 100644 index 00000000000..ae5cdd0225f --- /dev/null +++ b/server/startup/roomPublishes.coffee @@ -0,0 +1,32 @@ +Meteor.startup -> + RocketChat.roomTypes.addPublish 'c', (identifier) -> + options = + fields: + name: 1 + t: 1 + cl: 1 + u: 1 + usernames: 1 + return RocketChat.models.Rooms.findByTypeAndName 'c', identifier, options + + RocketChat.roomTypes.addPublish 'p', (identifier) -> + options = + fields: + name: 1 + t: 1 + cl: 1 + u: 1 + usernames: 1 + 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) -> + options = + fields: + name: 1 + t: 1 + cl: 1 + u: 1 + usernames: 1 + user = RocketChat.models.Users.findOneById this.userId, fields: username: 1 + return RocketChat.models.Rooms.findByTypeContainigUsername 'd', user.username, options