overleaf/services/web/public/coffee/ide/online-users/OnlineUsersManager.coffee

105 lines
3.1 KiB
CoffeeScript
Raw Normal View History

2014-07-08 07:02:26 -04:00
define [
"ide/colors/ColorManager"
2014-07-08 07:02:26 -04:00
"libs/md5"
2014-07-17 10:25:22 -04:00
"ide/online-users/controllers/OnlineUsersController"
], (ColorManager) ->
2014-07-08 07:02:26 -04:00
class OnlineUsersManager
cursorUpdateInterval:500
2014-07-08 07:02:26 -04:00
constructor: (@ide, @$scope) ->
@$scope.onlineUsers = {}
@$scope.onlineUserCursorHighlights = {}
2014-07-17 10:25:22 -04:00
@$scope.onlineUsersArray = []
2014-07-08 07:02:26 -04:00
@$scope.$on "cursor:editor:update", (event, position) =>
@sendCursorPositionUpdate(position)
2014-07-17 10:25:22 -04:00
@$scope.$on "project:joined", () =>
2015-02-05 11:37:37 -05:00
@ide.socket.emit "clientTracking.getConnectedUsers", (error, connectedUsers) =>
@$scope.onlineUsers = {}
for user in connectedUsers or []
if user.client_id == @ide.socket.socket.sessionid
# Don't store myself
continue
# Store data in the same format returned by clientTracking.clientUpdated
2015-02-05 11:37:37 -05:00
@$scope.onlineUsers[user.client_id] = {
id: user.client_id
user_id: user.user_id
email: user.email
name: "#{user.first_name} #{user.last_name}"
doc_id: user.cursorData?.doc_id
row: user.cursorData?.row
column: user.cursorData?.column
}
@refreshOnlineUsers()
2014-07-08 07:02:26 -04:00
@ide.socket.on "clientTracking.clientUpdated", (client) =>
if client.id != @ide.socket.socket.sessionid # Check it's not me!
@$scope.$apply () =>
@$scope.onlineUsers[client.id] = client
2014-07-17 10:25:22 -04:00
@refreshOnlineUsers()
2014-07-08 07:02:26 -04:00
@ide.socket.on "clientTracking.clientDisconnected", (client_id) =>
@$scope.$apply () =>
delete @$scope.onlineUsers[client_id]
2014-07-17 10:25:22 -04:00
@refreshOnlineUsers()
@$scope.getHueForUserId = (user_id) =>
ColorManager.getHueForUserId(user_id)
2014-07-08 07:02:26 -04:00
2014-07-17 10:25:22 -04:00
refreshOnlineUsers: () ->
@$scope.onlineUsersArray = []
for client_id, user of @$scope.onlineUsers
if user.doc_id?
user.doc = @ide.fileTreeManager.findEntityById(user.doc_id)
2014-12-09 06:11:06 -05:00
if user.name?.trim().length == 0
user.name = user.email.trim()
user.initial = user.name?[0]
if !user.initial or user.initial == " "
user.initial = "?"
2014-12-09 06:11:06 -05:00
2014-07-17 10:25:22 -04:00
@$scope.onlineUsersArray.push user
2014-07-08 07:02:26 -04:00
@$scope.onlineUserCursorHighlights = {}
for client_id, client of @$scope.onlineUsers
doc_id = client.doc_id
2014-07-17 10:25:22 -04:00
continue if !doc_id? or !client.row? or !client.column?
2014-07-08 07:02:26 -04:00
@$scope.onlineUserCursorHighlights[doc_id] ||= []
@$scope.onlineUserCursorHighlights[doc_id].push {
label: client.name
cursor:
row: client.row
column: client.column
hue: ColorManager.getHueForUserId(client.user_id)
2014-07-08 07:02:26 -04:00
}
if @$scope.onlineUsersArray.length > 0
delete @cursorUpdateTimeout
@cursorUpdateInterval = 500
else
delete @cursorUpdateTimeout
@cursorUpdateInterval = 60 * 1000 * 5
sendCursorPositionUpdate: (position) ->
if position?
@$scope.currentPosition = position # keep track of the latest position
2014-07-08 07:02:26 -04:00
if !@cursorUpdateTimeout?
@cursorUpdateTimeout = setTimeout ()=>
doc_id = @$scope.editor.open_doc_id
# always send the latest position to other clients
2014-07-08 07:02:26 -04:00
@ide.socket.emit "clientTracking.updatePosition", {
row: @$scope.currentPosition?.row
column: @$scope.currentPosition?.column
2014-07-08 07:02:26 -04:00
doc_id: doc_id
}
delete @cursorUpdateTimeout
, @cursorUpdateInterval
2014-07-08 07:02:26 -04:00