OnlineUsersManager.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. no-undef,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS206: Consider reworking classes to avoid initClass
  15. * DS207: Consider shorter variations of null checks
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. define([
  19. 'ide/colors/ColorManager',
  20. 'crypto-js/md5',
  21. 'ide/online-users/controllers/OnlineUsersController'
  22. ], function(ColorManager) {
  23. let OnlineUsersManager
  24. return (OnlineUsersManager = (function() {
  25. OnlineUsersManager = class OnlineUsersManager {
  26. static initClass() {
  27. this.prototype.cursorUpdateInterval = 500
  28. }
  29. constructor(ide, $scope) {
  30. this.ide = ide
  31. this.$scope = $scope
  32. this.$scope.onlineUsers = {}
  33. this.$scope.onlineUserCursorHighlights = {}
  34. this.$scope.onlineUsersArray = []
  35. this.$scope.$on('cursor:editor:update', (event, position) => {
  36. return this.sendCursorPositionUpdate(position)
  37. })
  38. this.$scope.$on('project:joined', () => {
  39. return this.ide.socket.emit(
  40. 'clientTracking.getConnectedUsers',
  41. (error, connectedUsers) => {
  42. this.$scope.onlineUsers = {}
  43. for (let user of Array.from(connectedUsers || [])) {
  44. if (user.client_id === this.ide.socket.socket.sessionid) {
  45. // Don't store myself
  46. continue
  47. }
  48. // Store data in the same format returned by clientTracking.clientUpdated
  49. this.$scope.onlineUsers[user.client_id] = {
  50. id: user.client_id,
  51. user_id: user.user_id,
  52. email: user.email,
  53. name: `${user.first_name} ${user.last_name}`,
  54. doc_id:
  55. user.cursorData != null
  56. ? user.cursorData.doc_id
  57. : undefined,
  58. row:
  59. user.cursorData != null ? user.cursorData.row : undefined,
  60. column:
  61. user.cursorData != null ? user.cursorData.column : undefined
  62. }
  63. }
  64. return this.refreshOnlineUsers()
  65. }
  66. )
  67. })
  68. this.ide.socket.on('clientTracking.clientUpdated', client => {
  69. if (client.id !== this.ide.socket.socket.sessionid) {
  70. // Check it's not me!
  71. return this.$scope.$apply(() => {
  72. this.$scope.onlineUsers[client.id] = client
  73. return this.refreshOnlineUsers()
  74. })
  75. }
  76. })
  77. this.ide.socket.on('clientTracking.clientDisconnected', client_id => {
  78. return this.$scope.$apply(() => {
  79. delete this.$scope.onlineUsers[client_id]
  80. return this.refreshOnlineUsers()
  81. })
  82. })
  83. this.$scope.getHueForUserId = user_id => {
  84. return ColorManager.getHueForUserId(user_id)
  85. }
  86. }
  87. refreshOnlineUsers() {
  88. this.$scope.onlineUsersArray = []
  89. for (var client_id in this.$scope.onlineUsers) {
  90. const user = this.$scope.onlineUsers[client_id]
  91. if (user.doc_id != null) {
  92. user.doc = this.ide.fileTreeManager.findEntityById(user.doc_id)
  93. }
  94. // If the user's name is empty use their email as display name
  95. // Otherwise they're probably an anonymous user
  96. if (user.name === null || user.name.trim().length === 0) {
  97. if (user.email) {
  98. user.name = user.email.trim()
  99. } else if (user.user_id === 'anonymous-user') {
  100. user.name = 'Anonymous'
  101. }
  102. }
  103. user.initial = user.name != null ? user.name[0] : undefined
  104. if (!user.initial || user.initial === ' ') {
  105. user.initial = '?'
  106. }
  107. this.$scope.onlineUsersArray.push(user)
  108. }
  109. this.$scope.onlineUserCursorHighlights = {}
  110. for (client_id in this.$scope.onlineUsers) {
  111. const client = this.$scope.onlineUsers[client_id]
  112. const { doc_id } = client
  113. if (doc_id == null || client.row == null || client.column == null) {
  114. continue
  115. }
  116. if (!this.$scope.onlineUserCursorHighlights[doc_id]) {
  117. this.$scope.onlineUserCursorHighlights[doc_id] = []
  118. }
  119. this.$scope.onlineUserCursorHighlights[doc_id].push({
  120. label: client.name,
  121. cursor: {
  122. row: client.row,
  123. column: client.column
  124. },
  125. hue: ColorManager.getHueForUserId(client.user_id)
  126. })
  127. }
  128. if (this.$scope.onlineUsersArray.length > 0) {
  129. delete this.cursorUpdateTimeout
  130. return (this.cursorUpdateInterval = 500)
  131. } else {
  132. delete this.cursorUpdateTimeout
  133. return (this.cursorUpdateInterval = 60 * 1000 * 5)
  134. }
  135. }
  136. sendCursorPositionUpdate(position) {
  137. if (position != null) {
  138. this.$scope.currentPosition = position // keep track of the latest position
  139. }
  140. if (this.cursorUpdateTimeout == null) {
  141. return (this.cursorUpdateTimeout = setTimeout(() => {
  142. const doc_id = this.$scope.editor.open_doc_id
  143. // always send the latest position to other clients
  144. this.ide.socket.emit('clientTracking.updatePosition', {
  145. row:
  146. this.$scope.currentPosition != null
  147. ? this.$scope.currentPosition.row
  148. : undefined,
  149. column:
  150. this.$scope.currentPosition != null
  151. ? this.$scope.currentPosition.column
  152. : undefined,
  153. doc_id
  154. })
  155. return delete this.cursorUpdateTimeout
  156. }, this.cursorUpdateInterval))
  157. }
  158. }
  159. }
  160. OnlineUsersManager.initClass()
  161. return OnlineUsersManager
  162. })())
  163. })