OnlineUsersManager.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. define [
  2. "libs/md5"
  3. "ide/online-users/controllers/OnlineUsersController"
  4. ], () ->
  5. class OnlineUsersManager
  6. constructor: (@ide, @$scope) ->
  7. @$scope.onlineUsers = {}
  8. @$scope.onlineUserCursorHighlights = {}
  9. @$scope.onlineUsersArray = []
  10. @$scope.$on "cursor:editor:update", (event, position) =>
  11. @sendCursorPositionUpdate(position)
  12. @$scope.$on "project:joined", () =>
  13. ide.$http
  14. .get "/project/#{@ide.$scope.project._id}/connected_users"
  15. .success (connectedUsers) =>
  16. @$scope.onlineUsers = {}
  17. for user in connectedUsers or []
  18. if user.client_id == @ide.socket.socket.sessionid
  19. # Don't store myself
  20. continue
  21. # Store data in the same format returned by clientTracking.clientUpdated
  22. if user.first_name?.length == 0 and user.last_name.length == 0
  23. name = user.email
  24. else
  25. name = "#{user.first_name} #{user.last_name}"
  26. @$scope.onlineUsers[user.client_id] = {
  27. id: user.client_id
  28. user_id: user.user_id
  29. email: user.email
  30. name: name
  31. doc_id: user.cursorData?.doc_id
  32. row: user.cursorData?.row
  33. column: user.cursorData?.column
  34. }
  35. @refreshOnlineUsers()
  36. @ide.socket.on "clientTracking.clientUpdated", (client) =>
  37. if client.id != @ide.socket.socket.sessionid # Check it's not me!
  38. @$scope.$apply () =>
  39. @$scope.onlineUsers[client.id] = client
  40. @refreshOnlineUsers()
  41. @ide.socket.on "clientTracking.clientDisconnected", (client_id) =>
  42. @$scope.$apply () =>
  43. delete @$scope.onlineUsers[client_id]
  44. @refreshOnlineUsers()
  45. @$scope.getHueForUserId = (user_id) =>
  46. @getHueForUserId(user_id)
  47. refreshOnlineUsers: () ->
  48. @$scope.onlineUsersArray = []
  49. for client_id, user of @$scope.onlineUsers
  50. if user.doc_id?
  51. user.doc = @ide.fileTreeManager.findEntityById(user.doc_id)
  52. @$scope.onlineUsersArray.push user
  53. @$scope.onlineUserCursorHighlights = {}
  54. for client_id, client of @$scope.onlineUsers
  55. doc_id = client.doc_id
  56. continue if !doc_id? or !client.row? or !client.column?
  57. @$scope.onlineUserCursorHighlights[doc_id] ||= []
  58. @$scope.onlineUserCursorHighlights[doc_id].push {
  59. label: client.name
  60. cursor:
  61. row: client.row
  62. column: client.column
  63. hue: @getHueForUserId(client.user_id)
  64. }
  65. UPDATE_INTERVAL: 500
  66. sendCursorPositionUpdate: (position) ->
  67. if !@cursorUpdateTimeout?
  68. @cursorUpdateTimeout = setTimeout ()=>
  69. doc_id = @$scope.editor.open_doc_id
  70. @ide.socket.emit "clientTracking.updatePosition", {
  71. row: position.row
  72. column: position.column
  73. doc_id: doc_id
  74. }
  75. delete @cursorUpdateTimeout
  76. , @UPDATE_INTERVAL
  77. OWN_HUE: 200 # We will always appear as this color to ourselves
  78. ANONYMOUS_HUE: 100
  79. getHueForUserId: (user_id) ->
  80. if !user_id? or user_id == "anonymous-user"
  81. return @ANONYMOUS_HUE
  82. if window.user.id == user_id
  83. return @OWN_HUE
  84. hash = CryptoJS.MD5(user_id)
  85. hue = parseInt(hash.toString().slice(0,8), 16) % 320
  86. # Avoid 20 degrees either side of the personal hue
  87. if hue > @OWNER_HUE - 20
  88. hue = hue + 40
  89. return hue