OnlineUsersManager.js 5.9 KB

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