EditorRealTimeController.js 1.5 KB

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