OnlineUsersManager.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. define [
  2. "ide/colors/ColorManager"
  3. "libs/md5"
  4. "ide/online-users/controllers/OnlineUsersController"
  5. ], (ColorManager) ->
  6. class OnlineUsersManager
  7. cursorUpdateInterval:500
  8. constructor: (@ide, @$scope) ->
  9. @$scope.onlineUsers = {}
  10. @$scope.onlineUserCursorHighlights = {}
  11. @$scope.onlineUsersArray = []
  12. @$scope.$on "cursor:editor:update", (event, position) =>
  13. @sendCursorPositionUpdate(position)
  14. @$scope.$on "project:joined", () =>
  15. @ide.socket.emit "clientTracking.getConnectedUsers", (error, 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. @$scope.onlineUsers[user.client_id] = {
  23. id: user.client_id
  24. user_id: user.user_id
  25. email: user.email
  26. name: "#{user.first_name} #{user.last_name}"
  27. doc_id: user.cursorData?.doc_id
  28. row: user.cursorData?.row
  29. column: user.cursorData?.column
  30. }
  31. @refreshOnlineUsers()
  32. @ide.socket.on "clientTracking.clientUpdated", (client) =>
  33. if client.id != @ide.socket.socket.sessionid # Check it's not me!
  34. @$scope.$apply () =>
  35. @$scope.onlineUsers[client.id] = client
  36. @refreshOnlineUsers()
  37. @ide.socket.on "clientTracking.clientDisconnected", (client_id) =>
  38. @$scope.$apply () =>
  39. delete @$scope.onlineUsers[client_id]
  40. @refreshOnlineUsers()
  41. @$scope.getHueForUserId = (user_id) =>
  42. ColorManager.getHueForUserId(user_id)
  43. refreshOnlineUsers: () ->
  44. @$scope.onlineUsersArray = []
  45. for client_id, user of @$scope.onlineUsers
  46. if user.doc_id?
  47. user.doc = @ide.fileTreeManager.findEntityById(user.doc_id)
  48. if user.name?.trim().length == 0
  49. user.name = user.email.trim()
  50. user.initial = user.name?[0]
  51. if !user.initial or user.initial == " "
  52. user.initial = "?"
  53. @$scope.onlineUsersArray.push user
  54. @$scope.onlineUserCursorHighlights = {}
  55. for client_id, client of @$scope.onlineUsers
  56. doc_id = client.doc_id
  57. continue if !doc_id? or !client.row? or !client.column?
  58. @$scope.onlineUserCursorHighlights[doc_id] ||= []
  59. @$scope.onlineUserCursorHighlights[doc_id].push {
  60. label: client.name
  61. cursor:
  62. row: client.row
  63. column: client.column
  64. hue: ColorManager.getHueForUserId(client.user_id)
  65. }
  66. if @$scope.onlineUsersArray.length > 0
  67. delete @cursorUpdateTimeout
  68. @cursorUpdateInterval = 500
  69. else
  70. delete @cursorUpdateTimeout
  71. @cursorUpdateInterval = 60 * 1000 * 5
  72. sendCursorPositionUpdate: (position) ->
  73. if position?
  74. @$scope.currentPosition = position # keep track of the latest position
  75. if !@cursorUpdateTimeout?
  76. @cursorUpdateTimeout = setTimeout ()=>
  77. doc_id = @$scope.editor.open_doc_id
  78. # always send the latest position to other clients
  79. @ide.socket.emit "clientTracking.updatePosition", {
  80. row: @$scope.currentPosition?.row
  81. column: @$scope.currentPosition?.column
  82. doc_id: doc_id
  83. }
  84. delete @cursorUpdateTimeout
  85. , @cursorUpdateInterval