Skip to content

Commit

Permalink
Merge pull request #496 from RocketChat/better-urls
Browse files Browse the repository at this point in the history
Better urls
  • Loading branch information
engelgabriel committed Aug 18, 2015
2 parents 42c3bae + 5150d87 commit 7af937b
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 93 deletions.
100 changes: 59 additions & 41 deletions client/lib/RoomManager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,76 +24,94 @@ Meteor.startup ->
subscription = Meteor.subscribe('subscription')
return subscription

close = (rid) ->
if openedRooms[rid]
if openedRooms[rid].sub?
for sub in openedRooms[rid].sub
close = (typeName) ->
if openedRooms[typeName]
if openedRooms[typeName].sub?
for sub in openedRooms[typeName].sub
sub.stop()

msgStream.removeListener rid
deleteMsgStream.removeListener rid
if openedRooms[typeName].rid?
msgStream.removeListener openedRooms[typeName].rid
deleteMsgStream.removeListener openedRooms[typeName].rid

openedRooms[rid].ready = false
openedRooms[rid].active = false
delete openedRooms[rid].timeout
delete openedRooms[rid].dom
openedRooms[typeName].ready = false
openedRooms[typeName].active = false
delete openedRooms[typeName].timeout
delete openedRooms[typeName].dom

RoomHistoryManager.clear rid

ChatMessage.remove rid: rid
if openedRooms[typeName].rid?
RoomHistoryManager.clear openedRooms[typeName].rid
ChatMessage.remove rid: openedRooms[typeName].rid

computation = Tracker.autorun ->
for rid, record of openedRooms when record.active is true
record.sub = [
Meteor.subscribe 'room', rid
# Meteor.subscribe 'messages', rid
]
for typeName, record of openedRooms when record.active is true
do (typeName, record) ->
record.sub = [
Meteor.subscribe 'room', typeName
# Meteor.subscribe 'messages', typeName
]

record.ready = record.sub[0].ready()
# record.ready = record.sub[0].ready() and record.sub[1].ready()

if record.ready is true
type = typeName.substr(0, 1)
name = typeName.substr(1)

query =
t: type

if type in ['c', 'p']
query.name = name
else if type is 'd'
query.usernames = $all: [Meteor.user().username, name]

record.ready = record.sub[0].ready()
# record.ready = record.sub[0].ready() and record.sub[1].ready()
room = ChatRoom.findOne query, { reactive: false }

Dep.changed()
openedRooms[typeName].rid = room._id

msgStream.on openedRooms[typeName].rid, (msg) ->
ChatMessage.upsert { _id: msg._id }, msg

deleteMsgStream.on openedRooms[typeName].rid, (msg) ->
ChatMessage.remove _id: msg._id

Dep.changed()

setRoomExpireExcept = (except) ->

if openedRooms[except]?.timeout?
clearTimeout openedRooms[except].timeout
delete openedRooms[except].timeout

for rid of openedRooms
if rid isnt except and not openedRooms[rid].timeout?
openedRooms[rid].timeout = setTimeout close, defaultTime, rid
for typeName of openedRooms
if typeName isnt except and not openedRooms[typeName].timeout?
openedRooms[typeName].timeout = setTimeout close, defaultTime, typeName

open = (rid) ->
open = (typeName) ->

if not openedRooms[rid]?
openedRooms[rid] =
if not openedRooms[typeName]?
openedRooms[typeName] =
active: false
ready: false

setRoomExpireExcept rid
setRoomExpireExcept typeName

if subscription.ready()
# if ChatSubscription.findOne { rid: rid }, { reactive: false }
if openedRooms[rid].active isnt true
openedRooms[rid].active = true

msgStream.on rid, (msg) ->
ChatMessage.upsert { _id: msg._id }, msg

deleteMsgStream.on rid, (msg) ->
ChatMessage.remove _id: msg._id
if openedRooms[typeName].active isnt true
openedRooms[typeName].active = true

computation?.invalidate()

return {
ready: ->
Dep.depend()
return openedRooms[rid].ready
return openedRooms[typeName].ready
}

getDomOfRoom = (rid) ->
room = openedRooms[rid]
getDomOfRoom = (typeName, rid) ->
room = openedRooms[typeName]
if not room?
return

Expand All @@ -104,8 +122,8 @@ Meteor.startup ->

return room.dom

existsDomOfRoom = (rid) ->
room = openedRooms[rid]
existsDomOfRoom = (typeName) ->
room = openedRooms[typeName]
return room?.dom?

updateUserStatus = (user, status, utcOffset) ->
Expand Down
109 changes: 71 additions & 38 deletions client/routes/roomRoute.coffee
Original file line number Diff line number Diff line change
@@ -1,49 +1,82 @@
FlowRouter.route '/room/:_id',
name: 'room'
openRoom = (type, name) ->
Session.set 'openedRoom', null

BlazeLayout.render 'main', {center: 'loading'}

Meteor.defer ->
Tracker.autorun (c) ->
if RoomManager.open(type + name).ready() isnt true
return

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?
FlowRouter.go 'home'
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
Meteor.call 'readMessages', room._id if Meteor.userId()?
# KonchatNotification.removeRoomNotification(params._id)

if Meteor.Device.isDesktop()
setTimeout ->
$('.message-form .input-message').focus()
, 100


roomExit = ->
mainNode = document.querySelector('.main-content')
if mainNode?
for child in mainNode.children
if child?
if child.classList.contains('room-container')
child.oldScrollTop = child.querySelector('.messages-box > .wrapper').scrollTop
mainNode.removeChild child


FlowRouter.route '/channel/:name',
name: 'channel'

action: (params, queryParams) ->
Session.set 'openedRoom', null
openRoom 'c', params.name

BlazeLayout.render 'main', {center: 'loading'}
triggersExit: [roomExit]

Meteor.defer ->
Tracker.autorun (c) ->
if RoomManager.open(params._id).ready() isnt true
return

c.stop()
FlowRouter.route '/group/:name',
name: 'group'

if not ChatRoom.find(params._id).count()
FlowRouter.go 'home'
return
action: (params, queryParams) ->
openRoom 'p', params.name

mainNode = document.querySelector('.main-content')
if mainNode?
for child in mainNode.children
mainNode.removeChild child if child?
room = RoomManager.getDomOfRoom(params._id)
mainNode.appendChild room
if room.classList.contains('room-container')
room.querySelector('.messages-box > .wrapper').scrollTop = room.oldScrollTop
triggersExit: [roomExit]

Session.set 'openedRoom', params._id

Session.set 'editRoomTitle', false
Meteor.call 'readMessages', params._id if Meteor.userId()?
# KonchatNotification.removeRoomNotification(params._id)
FlowRouter.route '/direct/:username',
name: 'direct'

if Meteor.Device.isDesktop()
setTimeout ->
$('.message-form .input-message').focus()
, 100
action: (params, queryParams) ->
openRoom 'd', params.username

triggersExit: [
->
mainNode = document.querySelector('.main-content')
if mainNode?
for child in mainNode.children
if child?
if child.classList.contains('room-container')
child.oldScrollTop = child.querySelector('.messages-box > .wrapper').scrollTop
mainNode.removeChild child
]
triggersExit: [roomExit]
4 changes: 1 addition & 3 deletions client/views/app/room.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,7 @@ Template.room.events
"click .mention-link": (e) ->
channel = $(e.currentTarget).data('channel')
if channel?
channelObj = ChatSubscription.findOne name: channel
if channelObj?
FlowRouter.go 'room', {_id: channelObj.rid}
FlowRouter.go 'channel', {name: channel}
return

Session.set('flexOpened', true)
Expand Down
11 changes: 10 additions & 1 deletion client/views/app/sideNav/chatRoomItem.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Template.chatRoomItem.helpers
when 'p' then return 'icon-lock'

active: ->
if FlowRouter.getParam('_id')? and FlowRouter.getParam('_id') is this.rid
if Session.get('openedRoom') is this.rid
return 'active'

canLeave: ->
Expand All @@ -38,6 +38,15 @@ Template.chatRoomItem.helpers
else
return true

route: ->
return switch this.t
when 'd'
FlowRouter.path('direct', {username: this.name})
when 'p'
FlowRouter.path('group', {name: this.name})
when 'c'
FlowRouter.path('channel', {name: this.name})

Template.chatRoomItem.rendered = ->
if not (FlowRouter.getParam('_id')? and FlowRouter.getParam('_id') is this.data.rid) and not this.data.ls
KonchatNotification.newRoom(this.data.rid)
Expand Down
2 changes: 1 addition & 1 deletion client/views/app/sideNav/chatRoomItem.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template name="chatRoomItem">
<li class="link-room-{{rid}} {{active}} {{#if unread}}has-unread{{/if}} {{#if alert}}has-alert{{/if}}">
<a class="open-room" href="{{ pathFor 'room' _id=rid}}" title="{{name}}">
<a class="open-room" href="{{route}}" title="{{name}}">
{{#if unread}}
<span class="unread">{{unread}}</span>
{{/if}}
Expand Down
2 changes: 1 addition & 1 deletion client/views/app/sideNav/listChannelsFlex.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h4>{{_ "Channels_list"}}</h4>
<ul>
{{#each channel}}
<li>
<a href="{{pathFor "room" _id=_id}}" class="channel-link">
<a href="{{pathFor 'channel' name=name}}" class="channel-link">
<i class="icon-hash"></i>
{{name}}
</a>
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-colors/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ColorsClient
msg = message.html

msg = msg.replace /(?:^|\s|\n)(#[A-Fa-f0-9]{3}([A-Fa-f0-9]{3})?)\b/g, (match, completeColor) ->
return match.replace completeColor, "<div class=\"message-color\"><div class=\"message-color-sample\" style=\"background-color: #{completeColor}\"></div>#{completeColor.toUpperCase()}</div>"
return match.replace completeColor, "<div class=\"message-color\"><div class=\"message-color-sample\" style=\"background-color:#{completeColor}\"></div>#{completeColor.toUpperCase()}</div>"

message.html = msg
return message
Expand Down
34 changes: 27 additions & 7 deletions server/publications/room.coffee
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
Meteor.publish 'room', (rid) ->
Meteor.publish 'room', (typeName) ->
unless this.userId
return this.ready()

console.log '[publish] room ->'.green, 'arguments:', arguments

if typeof rid isnt 'string'
if typeof typeName isnt 'string'
return this.ready()

if not Meteor.call 'canAccessRoom', rid, this.userId
return this.ready()
type = typeName.substr(0, 1)
name = typeName.substr(1)

query = {}

if type in ['c', 'p']
query =
t: type
name: name

if type is 'p'
user = Meteor.users.findOne this.userId, fields: username: 1
query.usernames = user.username

else if type is 'd'
user = Meteor.users.findOne this.userId, fields: username: 1
query =
t: 'd'
usernames:
$all: [user.username, name]

# Change to validate access manualy
# if not Meteor.call 'canAccessRoom', rid, this.userId
# return this.ready()

ChatRoom.find
_id: rid
,
ChatRoom.find query,
fields:
name: 1
t: 1
Expand Down

0 comments on commit 7af937b

Please sign in to comment.