2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let EditorRealTimeController
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const RedisWrapper = require('../../infrastructure/RedisWrapper')
|
2019-07-08 09:47:31 -04:00
|
|
|
const rclient = RedisWrapper.client('pubsub')
|
2019-05-29 05:21:06 -04:00
|
|
|
const os = require('os')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
|
|
|
|
const HOST = os.hostname()
|
|
|
|
const RND = crypto.randomBytes(4).toString('hex') // generate a random key for this process
|
|
|
|
let COUNT = 0
|
|
|
|
|
|
|
|
module.exports = EditorRealTimeController = {
|
|
|
|
emitToRoom(room_id, message, ...payload) {
|
|
|
|
// create a unique message id using a counter
|
|
|
|
const message_id = `web:${HOST}:${RND}-${COUNT++}`
|
2019-07-30 03:22:24 -04:00
|
|
|
var channel
|
|
|
|
if (room_id === 'all' || !Settings.publishOnIndividualChannels) {
|
|
|
|
channel = 'editor-events'
|
|
|
|
} else {
|
|
|
|
channel = `editor-events:${room_id}`
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
return rclient.publish(
|
2019-07-30 03:22:24 -04:00
|
|
|
channel,
|
2019-05-29 05:21:06 -04:00
|
|
|
JSON.stringify({
|
|
|
|
room_id,
|
|
|
|
message,
|
|
|
|
payload,
|
|
|
|
_id: message_id
|
|
|
|
})
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
emitToAll(message, ...payload) {
|
|
|
|
return this.emitToRoom('all', message, ...Array.from(payload))
|
|
|
|
}
|
|
|
|
}
|