EditorRealTimeController.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* eslint-disable
  2. max-len,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let EditorRealTimeController
  14. const Settings = require('@overleaf/settings')
  15. const Metrics = require('@overleaf/metrics')
  16. const RedisWrapper = require('../../infrastructure/RedisWrapper')
  17. const rclient = RedisWrapper.client('pubsub')
  18. const os = require('os')
  19. const crypto = require('crypto')
  20. const HOST = os.hostname()
  21. const RND = crypto.randomBytes(4).toString('hex') // generate a random key for this process
  22. let COUNT = 0
  23. module.exports = EditorRealTimeController = {
  24. emitToRoom(roomId, message, ...payload) {
  25. // create a unique message id using a counter
  26. const messageId = `web:${HOST}:${RND}-${COUNT++}`
  27. let channel
  28. if (roomId === 'all' || !Settings.publishOnIndividualChannels) {
  29. channel = 'editor-events'
  30. } else {
  31. channel = `editor-events:${roomId}`
  32. }
  33. const blob = JSON.stringify({
  34. room_id: roomId,
  35. message,
  36. payload,
  37. _id: messageId,
  38. })
  39. Metrics.summary('redis.publish.editor-events', blob.length, {
  40. status: message,
  41. })
  42. return rclient.publish(channel, blob)
  43. },
  44. emitToAll(message, ...payload) {
  45. return this.emitToRoom('all', message, ...Array.from(payload))
  46. },
  47. }