mirror of https://github.com/status-im/codimd.git
refactor: move realtime related code to directory
Signed-off-by: BoHong Li <raccoon@hackmd.io>
This commit is contained in:
parent
68fcf35c70
commit
2135cfcd18
2
app.js
2
app.js
|
@ -66,7 +66,7 @@ io.engine.ws = new (require('ws').Server)({
|
|||
})
|
||||
|
||||
// others
|
||||
var realtime = require('./lib/realtime.js')
|
||||
var realtime = require('./lib/realtime/realtime.js')
|
||||
|
||||
// assign socket io to realtime
|
||||
realtime.io = io
|
||||
|
|
|
@ -12,13 +12,13 @@ const moment = require('moment')
|
|||
const get = require('lodash/get')
|
||||
|
||||
// core
|
||||
const config = require('./config')
|
||||
const logger = require('./logger')
|
||||
const history = require('./web/history')
|
||||
const models = require('./models')
|
||||
const config = require('../config')
|
||||
const logger = require('../logger')
|
||||
const history = require('../web/history')
|
||||
const models = require('../models')
|
||||
|
||||
// ot
|
||||
const ot = require('./ot')
|
||||
const ot = require('../ot')
|
||||
|
||||
const { ProcessQueue } = require('./processQueue')
|
||||
const { RealtimeClientConnection } = require('./realtimeClientConnection')
|
|
@ -1,8 +1,8 @@
|
|||
'use strict'
|
||||
|
||||
const async = require('async')
|
||||
const config = require('./config')
|
||||
const logger = require('./logger')
|
||||
const config = require('../config')
|
||||
const logger = require('../logger')
|
||||
|
||||
/**
|
||||
* clean when user not in any rooms or user not in connected list
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
const get = require('lodash/get')
|
||||
|
||||
const config = require('./config')
|
||||
const models = require('./models')
|
||||
const logger = require('./logger')
|
||||
const config = require('../config')
|
||||
const models = require('../models')
|
||||
const logger = require('../logger')
|
||||
|
||||
class RealtimeClientConnection {
|
||||
constructor (socket) {
|
|
@ -1,7 +1,7 @@
|
|||
'use strict'
|
||||
|
||||
const models = require('./models')
|
||||
const logger = require('./logger')
|
||||
const models = require('../models')
|
||||
const logger = require('../logger')
|
||||
|
||||
/**
|
||||
* clean when user not in any rooms or user not in connected list
|
|
@ -1,7 +1,7 @@
|
|||
'use strict'
|
||||
|
||||
const config = require('./config')
|
||||
const logger = require('./logger')
|
||||
const config = require('../config')
|
||||
const logger = require('../logger')
|
||||
const moment = require('moment')
|
||||
|
||||
class UpdateDirtyNoteJob {
|
|
@ -1,6 +1,6 @@
|
|||
'use strict'
|
||||
|
||||
const realtime = require('../../realtime')
|
||||
const realtime = require('../../realtime/realtime')
|
||||
const config = require('../../config')
|
||||
|
||||
exports.getStatus = async (req, res) => {
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
const assert = require('assert')
|
||||
const sinon = require('sinon')
|
||||
|
||||
const { ProcessQueue } = require('../lib/processQueue')
|
||||
const { ProcessQueue } = require('../lib/realtime/processQueue')
|
||||
|
||||
describe('ProcessQueue', function () {
|
||||
let clock
|
||||
|
|
|
@ -31,13 +31,13 @@ describe('cleanDanglingUser', function () {
|
|||
|
||||
afterEach(() => {
|
||||
clock.restore()
|
||||
removeModuleFromRequireCache('../../lib/realtime')
|
||||
removeModuleFromRequireCache('../../lib/realtime/realtime')
|
||||
mock.stopAll()
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('should call queueForDisconnectSpy when user is dangling', (done) => {
|
||||
const realtime = require('../../lib/realtime')
|
||||
const realtime = require('../../lib/realtime/realtime')
|
||||
const queueForDisconnectSpy = sinon.spy(realtime, 'queueForDisconnect')
|
||||
realtime.io = {
|
||||
to: sinon.stub().callsFake(function () {
|
||||
|
|
|
@ -31,7 +31,7 @@ describe('realtime#connection', function () {
|
|||
mock('../../lib/realtimeCleanDanglingUserJob', realtimeJobStub)
|
||||
mock('../../lib/realtimeSaveRevisionJob', realtimeJobStub)
|
||||
mock('../../lib/ot', require('../testDoubles/otFake'))
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
|
|
|
@ -27,7 +27,7 @@ describe('realtime#update note is dirty timer', function () {
|
|||
}
|
||||
})
|
||||
mock('../../lib/config', {})
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
|
||||
realtime.io = {
|
||||
to: sinon.stub().callsFake(function () {
|
||||
|
@ -39,8 +39,8 @@ describe('realtime#update note is dirty timer', function () {
|
|||
})
|
||||
|
||||
afterEach(() => {
|
||||
removeModuleFromRequireCache('../../lib/realtimeUpdateDirtyNoteJob')
|
||||
removeModuleFromRequireCache('../../lib/realtime')
|
||||
removeModuleFromRequireCache('../../lib/realtime/realtimeUpdateDirtyNoteJob')
|
||||
removeModuleFromRequireCache('../../lib/realtime/realtime')
|
||||
mock.stopAll()
|
||||
clock.restore()
|
||||
})
|
||||
|
|
|
@ -28,7 +28,7 @@ describe('realtime#disconnect', function () {
|
|||
})
|
||||
mock('../../lib/config', {})
|
||||
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
updateNoteStub = sinon.stub(realtime, 'updateNote').callsFake((note, callback) => {
|
||||
callback(null, note)
|
||||
})
|
||||
|
@ -60,7 +60,7 @@ describe('realtime#disconnect', function () {
|
|||
})
|
||||
|
||||
afterEach(() => {
|
||||
removeModuleFromRequireCache('../../lib/realtime')
|
||||
removeModuleFromRequireCache('../../lib/realtime/realtime')
|
||||
mock.stopAll()
|
||||
sinon.restore()
|
||||
})
|
||||
|
|
|
@ -14,14 +14,14 @@ describe('realtime#extractNoteIdFromSocket', function () {
|
|||
})
|
||||
|
||||
afterEach(() => {
|
||||
delete require.cache[require.resolve('../../lib/realtime')]
|
||||
delete require.cache[require.resolve('../../lib/realtime/realtime')]
|
||||
mock.stopAll()
|
||||
})
|
||||
|
||||
describe('urlPath not set', function () {
|
||||
beforeEach(() => {
|
||||
mock('../../lib/config', {})
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
})
|
||||
|
||||
let realtime
|
||||
|
@ -76,7 +76,7 @@ describe('realtime#extractNoteIdFromSocket', function () {
|
|||
mock('../../lib/config', {
|
||||
urlPath: urlPath
|
||||
})
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
const incomingNoteId = 'myNoteId'
|
||||
const incomingSocket = makeMockSocket({
|
||||
referer: `https://localhost:3000/${urlPath}/${incomingNoteId}`
|
||||
|
|
|
@ -81,7 +81,7 @@ describe('realtime#ifMayEdit', function () {
|
|||
client.request.user.id = noteOwnerId
|
||||
}
|
||||
client.noteId = noteId
|
||||
const realtime = require('../../lib/realtime')
|
||||
const realtime = require('../../lib/realtime/realtime')
|
||||
realtime.getNotePool()[noteId] = note
|
||||
const callback = sinon.stub()
|
||||
realtime.ifMayEdit(client, callback)
|
||||
|
@ -98,7 +98,7 @@ describe('realtime#ifMayEdit', function () {
|
|||
client.noteId = noteId
|
||||
const callback = sinon.stub()
|
||||
client.origin = 'operation'
|
||||
const realtime = require('../../lib/realtime')
|
||||
const realtime = require('../../lib/realtime/realtime')
|
||||
realtime.getNotePool()[noteId] = note
|
||||
realtime.ifMayEdit(client, callback)
|
||||
assert(callback.calledOnce)
|
||||
|
@ -116,7 +116,7 @@ describe('realtime#ifMayEdit', function () {
|
|||
client.request.user.id = loggedInUserId
|
||||
const callback = sinon.stub()
|
||||
client.origin = 'operation'
|
||||
const realtime = require('../../lib/realtime')
|
||||
const realtime = require('../../lib/realtime/realtime')
|
||||
realtime.getNotePool()[noteId] = note
|
||||
realtime.ifMayEdit(client, callback)
|
||||
assert(callback.calledOnce)
|
||||
|
|
|
@ -23,12 +23,12 @@ describe('realtime#parseNoteIdFromSocketAsync', function () {
|
|||
})
|
||||
|
||||
afterEach(() => {
|
||||
removeModuleFromRequireCache('../../lib/realtime')
|
||||
removeModuleFromRequireCache('../../lib/realtime/realtime')
|
||||
mock.stopAll()
|
||||
})
|
||||
|
||||
it('should return null when socket not send noteId', async function () {
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
const mockSocket = makeMockSocket()
|
||||
try {
|
||||
const notes = await realtime.parseNoteIdFromSocketAsync(mockSocket)
|
||||
|
@ -49,12 +49,12 @@ describe('realtime#parseNoteIdFromSocketAsync', function () {
|
|||
})
|
||||
})
|
||||
it('should return noteId when noteId exists', async function () {
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
const noteId = '123456'
|
||||
const mockSocket = makeMockSocket(undefined, {
|
||||
noteId: noteId
|
||||
})
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
let parsedNoteId
|
||||
try {
|
||||
parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
|
||||
|
@ -76,12 +76,12 @@ describe('realtime#parseNoteIdFromSocketAsync', function () {
|
|||
})
|
||||
})
|
||||
it('should return null when noteId not exists', async function () {
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
const noteId = '123456'
|
||||
const mockSocket = makeMockSocket(undefined, {
|
||||
noteId: noteId
|
||||
})
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
const parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
|
||||
assert(parsedNoteId === null)
|
||||
})
|
||||
|
@ -99,12 +99,12 @@ describe('realtime#parseNoteIdFromSocketAsync', function () {
|
|||
})
|
||||
})
|
||||
it('should return error when noteId parse error', async function () {
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
const noteId = '123456'
|
||||
const mockSocket = makeMockSocket(undefined, {
|
||||
noteId: noteId
|
||||
})
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
try {
|
||||
await realtime.parseNoteIdFromSocketAsync(mockSocket)
|
||||
} catch (err) {
|
||||
|
|
|
@ -50,7 +50,7 @@ describe('realtime', function () {
|
|||
}
|
||||
})
|
||||
mock('../../lib/config', {})
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
})
|
||||
|
||||
Object.keys(viewPermission).forEach(function (permission) {
|
||||
|
|
|
@ -34,8 +34,8 @@ describe('save revision job', function () {
|
|||
|
||||
afterEach(() => {
|
||||
clock.restore()
|
||||
removeModuleFromRequireCache('../../lib/realtime')
|
||||
removeModuleFromRequireCache('../../lib/realtimeSaveRevisionJob')
|
||||
removeModuleFromRequireCache('../../lib/realtime/realtime')
|
||||
removeModuleFromRequireCache('../../lib/realtime/realtimeSaveRevisionJob')
|
||||
mock.stopAll()
|
||||
sinon.restore()
|
||||
})
|
||||
|
@ -44,7 +44,7 @@ describe('save revision job', function () {
|
|||
mockModels.Revision.saveAllNotesRevision.callsFake((callback) => {
|
||||
callback(null, [])
|
||||
})
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
clock.tick(5 * 60 * 1000)
|
||||
clock.restore()
|
||||
setTimeout(() => {
|
||||
|
@ -58,7 +58,7 @@ describe('save revision job', function () {
|
|||
mockModels.Revision.saveAllNotesRevision.callsFake((callback) => {
|
||||
callback(null, [1])
|
||||
})
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
clock.tick(5 * 60 * 1000)
|
||||
clock.restore()
|
||||
setTimeout(() => {
|
||||
|
|
|
@ -76,7 +76,7 @@ describe('realtime#socket event', function () {
|
|||
mock('../../lib/models', modelsMock)
|
||||
mock('../../lib/config', configMock)
|
||||
mock('../../lib/ot', require('../testDoubles/otFake'))
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
|
||||
// get all socket event handler
|
||||
clientSocket = makeMockSocket(null, {
|
||||
|
@ -122,8 +122,8 @@ describe('realtime#socket event', function () {
|
|||
})
|
||||
|
||||
afterEach(function () {
|
||||
removeModuleFromRequireCache('../../lib/realtime')
|
||||
removeModuleFromRequireCache('../../lib/realtimeClientConnection')
|
||||
removeModuleFromRequireCache('../../lib/realtime/realtime')
|
||||
removeModuleFromRequireCache('../../lib/realtime/realtimeClientConnection')
|
||||
mock.stopAll()
|
||||
sinon.restore()
|
||||
clock.restore()
|
||||
|
|
|
@ -47,7 +47,7 @@ describe('realtime#updateNote', function () {
|
|||
|
||||
it('should save history to each edited user', function (done) {
|
||||
modelsStub.Note.findOne.returns(Promise.resolve({}))
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
const updateHistoryStub = sinon.stub(realtime, 'updateHistory')
|
||||
|
||||
const callback = sinon.stub()
|
||||
|
@ -86,7 +86,7 @@ describe('realtime#updateNote', function () {
|
|||
name: 'User 01'
|
||||
})
|
||||
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
|
||||
realtime.updateNote(note, callback)
|
||||
clock.restore()
|
||||
|
@ -125,7 +125,7 @@ describe('realtime#updateNote', function () {
|
|||
})
|
||||
clock.tick(1000)
|
||||
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
realtime.updateNote(note, callback)
|
||||
setTimeout(() => {
|
||||
assert(note.lastchangeuserprofile.name === 'User 01')
|
||||
|
@ -159,7 +159,7 @@ describe('realtime#updateNote', function () {
|
|||
})
|
||||
clock.tick(1000)
|
||||
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
realtime.updateNote(note, callback)
|
||||
setTimeout(() => {
|
||||
assert(modelsStub.User.findOne.callCount === 0)
|
||||
|
@ -196,7 +196,7 @@ describe('realtime#updateNote', function () {
|
|||
})
|
||||
clock.tick(1000)
|
||||
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
realtime.updateNote(note, callback)
|
||||
setTimeout(() => {
|
||||
assert(modelsStub.User.findOne.callCount === 0)
|
||||
|
@ -233,7 +233,7 @@ describe('realtime#updateNote', function () {
|
|||
})
|
||||
clock.tick(1000)
|
||||
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
realtime.updateNote(note, callback)
|
||||
setTimeout(() => {
|
||||
assert(modelsStub.User.findOne.called)
|
||||
|
@ -272,7 +272,7 @@ describe('realtime#updateNote', function () {
|
|||
})
|
||||
clock.tick(1000)
|
||||
|
||||
realtime = require('../../lib/realtime')
|
||||
realtime = require('../../lib/realtime/realtime')
|
||||
realtime.updateNote(note, callback)
|
||||
setTimeout(() => {
|
||||
assert(note.lastchangeuserprofile.name === 'User 01')
|
||||
|
|
Loading…
Reference in New Issue