Skip to content

Commit

Permalink
Merge pull request #173 from RocketChat/subscription-manager
Browse files Browse the repository at this point in the history
Subscription manager and meny other improovments
  • Loading branch information
engelgabriel committed Jun 8, 2015
2 parents 1b6f34c + 5537f4c commit 5d01673
Show file tree
Hide file tree
Showing 56 changed files with 912 additions and 1,980 deletions.
3 changes: 1 addition & 2 deletions .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ mizzao:autocomplete
mizzao:timesync
momentjs:moment
monbro:mongodb-mapreduce-aggregation
mrt:mask
mrt:publish-with-relations
mrt:reactive-store
nooitaf:colors
pauli:accounts-linkedin
Expand All @@ -46,6 +44,7 @@ raix:handlebar-helpers
rocketchat:file
rocketchat:lib
rocketchat:me
rocketchat:mentions
simple:highlight.js
tap:i18n
tmeasday:crypto-md5
Expand Down
3 changes: 1 addition & 2 deletions .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ momentjs:moment@2.10.3
monbro:mongodb-mapreduce-aggregation@1.0.1
mongo@1.1.0
mongo-livedata@1.0.8
mrt:mask@0.0.1
mrt:publish-with-relations@0.1.5
mrt:reactive-store@0.0.1
nooitaf:colors@0.0.2
npm-bcrypt@0.7.8_2
Expand All @@ -96,6 +94,7 @@ retry@1.0.3
rocketchat:file@0.0.1
rocketchat:lib@0.0.1
rocketchat:me@0.0.1
rocketchat:mentions@0.0.1
routepolicy@1.0.5
service-configuration@1.0.4
session@1.1.0
Expand Down
30 changes: 15 additions & 15 deletions client/lib/RoomHistoryManager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@

histories = {}

getRoom = (roomId) ->
if not histories[roomId]?
histories[roomId] =
getRoom = (rid) ->
if not histories[rid]?
histories[rid] =
hasMore: ReactiveVar true
isLoading: ReactiveVar false
loaded: 0

return histories[roomId]
return histories[rid]

initRoom = (roomId, from=new Date) ->
room = getRoom roomId
initRoom = (rid, from=new Date) ->
room = getRoom rid

room.from = from

getMore = (roomId, limit=defaultLimit) ->
room = getRoom roomId
getMore = (rid, limit=defaultLimit) ->
room = getRoom rid

if room.hasMore.curValue isnt true or not room.from?
return
Expand All @@ -27,15 +27,15 @@

$('.messages-box .wrapper').data('previous-height', $('.messages-box .wrapper').get(0)?.scrollHeight - $('.messages-box .wrapper').get(0)?.scrollTop)

lastMessage = ChatMessageHistory.findOne({rid: roomId}, {sort: {ts: 1}})
lastMessage ?= ChatMessage.findOne({rid: roomId}, {sort: {ts: 1}})
lastMessage = ChatMessageHistory.findOne({rid: rid}, {sort: {ts: 1}})
lastMessage ?= ChatMessage.findOne({rid: rid}, {sort: {ts: 1}})

if lastMessage?
ts = lastMessage.ts
else
ts = new Date

Meteor.call 'loadHistory', roomId, ts, limit, 0, (err, result) ->
Meteor.call 'loadHistory', rid, ts, limit, 0, (err, result) ->
ChatMessageHistory.insert item for item in result

room.isLoading.set false
Expand All @@ -45,13 +45,13 @@
if result.length < limit
room.hasMore.set false

hasMore = (roomId) ->
room = getRoom roomId
hasMore = (rid) ->
room = getRoom rid

return room.hasMore.get()

isLoading = (roomId) ->
room = getRoom roomId
isLoading = (rid) ->
room = getRoom rid

return room.isLoading.get()

Expand Down
64 changes: 26 additions & 38 deletions client/lib/RoomManager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@
subscription = Meteor.subscribe('subscription')
return subscription

expireRoom = (roomId) ->
if openedRooms[roomId]
if openedRooms[roomId].sub?
for sub in openedRooms[roomId].sub
expireRoom = (rid) ->
if openedRooms[rid]
if openedRooms[rid].sub?
for sub in openedRooms[rid].sub
sub.stop()
openedRooms[roomId].ready = false
openedRooms[roomId].active = false
delete openedRooms[roomId].timeout

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

ChatMessageHistory.remove rid: rid

computation = Tracker.autorun ->
for roomId, record of openedRooms when record.active is true
for rid, record of openedRooms when record.active is true
record.sub = [
Meteor.subscribe 'messages', roomId, moment().subtract(2, 'hour').startOf('day').toDate()
Meteor.subscribe 'room', rid
Meteor.subscribe 'messages', rid
]
# @TODO talvez avaliar se todas as subscriptions do array estão 'ready', mas por enquanto, as mensagens são o mais importante
record.ready = record.sub[0].ready()
if record.ready is true and record.historyCalled isnt true
record.historyCalled = true
RoomHistoryManager.initRoom roomId, moment().subtract(2, 'hour').startOf('day').toDate()
Tracker.nonreactive ->
if Session.get('roomData' + roomId)?.msgs > 9 and ChatMessage.find({ rid: roomId }).count() < 10 and ChatMessageHistory.find({ rid: roomId }).count() is 0
RoomHistoryManager.getMore roomId

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

Dep.changed()

Expand All @@ -40,37 +38,27 @@
clearTimeout openedRooms[except].timeout
delete openedRooms[except].timeout

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

open = (roomId) ->
if not openedRooms[roomId]?
openedRooms[roomId] =
open = (rid) ->
if not openedRooms[rid]?
openedRooms[rid] =
active: false
ready: false

if subscription.ready()
if ChatSubscription.findOne { rid: roomId, 'u._id': Meteor.userId() }, { reactive: false }
openedRooms[roomId].active = true
setRoomExpireExcept roomId
# if ChatSubscription.findOne { rid: rid }, { reactive: false }
if openedRooms[rid].active isnt true
openedRooms[rid].active = true
setRoomExpireExcept rid
computation.invalidate()
else
Meteor.call 'canAccessRoom', roomId, (error, result) ->
if result
openedRooms[roomId].active = true
setRoomExpireExcept roomId
computation.invalidate()
else
if error.error is 'without-permission'
toastr.error t('RoomManager.No_permission_to_view_room')

Router.go 'home'

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

open: open
Expand Down
24 changes: 12 additions & 12 deletions client/lib/chatMessages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@
send = (rid, input) ->
if _.trim(input.value) isnt ''
KonchatNotification.removeRoomNotification(rid)
message = input.value
msg = input.value
input.value = ''
stopTyping()
Meteor.call 'sendMessage', {rid: rid, message: message, day: window.day }
Meteor.call 'sendMessage', { rid: rid, msg: msg, day: window.day }

update = (id, input) ->
if _.trim(input.value) isnt ''
message = input.value
msg = input.value
input.value = ''
Meteor.call 'updateMessage', {id: id, message: message }
Meteor.call 'updateMessage', { id: id, msg: msg }

startTyping = (rid, input) ->
unless self.typingTimeout
if Meteor.userId()?
Meteor.call 'typingStatus', { rid: rid }, true

self.typingTimeout = Meteor.setTimeout ->
stopTyping()
, 30000
if _.trim(input.value) isnt ''
unless self.typingTimeout
if Meteor.userId()?
Meteor.call 'typingStatus', rid, true
self.typingTimeout = Meteor.setTimeout ->
stopTyping()
, 30000

stopTyping = ->
self.typingTimeout = null
Expand Down Expand Up @@ -131,4 +131,4 @@
stopEditingLastMessage: stopEditingLastMessage
send: send
init: init
)()
)()
2 changes: 1 addition & 1 deletion client/lib/collections.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Meteor.startup ->
ChatMessageHistory.remove record._id

removed: (record) ->
if not ChatMessageHistory._collection._docs._map[record._id]?
if ChatRoom._collection._docs._map[record.rid]? and not ChatMessageHistory._collection._docs._map[record._id]?
ChatMessageHistory.insert record
4 changes: 2 additions & 2 deletions client/lib/notification.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

Session.set('newRoomSound', newRoomSound)

$('.link-room-' + rid).addClass('new-room-highlight')
# $('.link-room-' + rid).addClass('new-room-highlight')

removeRoomNotification: (rid) ->
Tracker.nonreactive ->
Expand All @@ -55,4 +55,4 @@ Tracker.autorun ->
else
$('#chatNewRoomNotification').each ->
this.pause()
this.currentTime = 0
this.currentTime = 0
4 changes: 1 addition & 3 deletions client/lib/sideNav.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
if flexNav.opened then closeFlex() else AccountBox.toggle()

overArrow = ->
console.log "HOVER"
arrow.addClass "hover"

leaveArrow = ->
console.log "OUT"
arrow.removeClass "hover"

arrowBindHover = ->
Expand Down Expand Up @@ -107,4 +105,4 @@
toggleCurrent: toggleCurrent
overArrow: overArrow
leaveArrow: leaveArrow
)()
)()
11 changes: 11 additions & 0 deletions client/methods/hideRoom.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Meteor.methods
hideRoom: (rid) ->
if not Meteor.userId()
throw new Meteor.Error 203, t('general.User_logged_out')

ChatSubscription.update
rid: rid
,
$set:
alert: false
open: false
14 changes: 8 additions & 6 deletions client/methods/leaveRoom.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Meteor.methods
leaveRoom: (roomId) ->
room = ChatRoom.findOne roomId
leaveRoom: (rid) ->
if not Meteor.userId()
throw new Meteor.Error 203, t('general.User_logged_out')

update =
ChatSubscription.remove
rid: rid
'u._id': Meteor.userId()

ChatRoom.update rid,
$pull:
usernames: Meteor.user().username

ChatSubscription.remove { rid: roomId, 'u._id': Meteor.userId() }

ChatRoom.update roomId, update
26 changes: 26 additions & 0 deletions client/methods/saveRoomName.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Meteor.methods
saveRoomName: (rid, name) ->
if not Meteor.userId()
throw new Meteor.Error 203, t('general.User_logged_out')

room = ChatRoom.findOne rid

if room.u._id isnt Meteor.userId() or room.t not in ['c', 'p']
throw new Meteor.Error 403, 'Not allowed'

name = _.slugify name

if name is room.name
return

ChatRoom.update rid,
$set:
name: name

ChatSubscription.update
rid: rid
,
$set:
name: name

return true
31 changes: 14 additions & 17 deletions client/methods/sendMessage.coffee
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
Meteor.methods
sendMessage: (msg) ->
sendMessage: (message) ->
if not Meteor.userId()
throw new Meteor.Error 203, t('general.User_logged_out')

Tracker.nonreactive ->
now = new Date(Date.now() + TimeSync.serverOffset())

msg = RocketChat.callbacks.run 'sendMessage', msg
message.ts = new Date(Date.now() + TimeSync.serverOffset())
message.u =
_id: Meteor.userId()
username: Meteor.user().username
message = RocketChat.callbacks.run 'beforeSaveMessage', message

ChatMessage.upsert { rid: msg.rid, t: 't' },
$set:
ts: now
msg: msg.message
'u.username': Meteor.user().username
ChatMessage.upsert
rid: message.rid
t: 't'
,
$set: message
$unset:
t: 1
expireAt: 1

updateMessage: (msg) ->
Tracker.nonreactive ->
now = new Date(Date.now() + TimeSync.serverOffset())

ChatMessage.update { _id: msg.id, 'u._id': Meteor.userId() },
$set:
ets: now
msg: msg.message
11 changes: 11 additions & 0 deletions client/methods/toogleFavorite.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Meteor.methods
toogleFavorite: (rid, f) ->
if not Meteor.userId()
throw new Meteor.Error 203, t('general.User_logged_out')

ChatSubscription.update
rid: rid
'u._id': Meteor.userId()
,
$set:
f: f
Loading

0 comments on commit 5d01673

Please sign in to comment.