mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 01:35:18 -04:00
More Types for realtime.ts
Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
parent
ed9a89efb5
commit
e8a34e7ced
1 changed files with 146 additions and 119 deletions
|
@ -11,6 +11,7 @@ import { History } from './history'
|
||||||
import { logger } from './logger'
|
import { logger } from './logger'
|
||||||
import { Author, Note, Revision, User } from './models'
|
import { Author, Note, Revision, User } from './models'
|
||||||
import { EditorSocketIOServer } from './ot/editor-socketio-server'
|
import { EditorSocketIOServer } from './ot/editor-socketio-server'
|
||||||
|
import { NoteAuthorship } from './models/note'
|
||||||
|
|
||||||
export type SocketWithNoteId = Socket & { noteId: string }
|
export type SocketWithNoteId = Socket & { noteId: string }
|
||||||
|
|
||||||
|
@ -62,7 +63,7 @@ function secure (socket: Socket, next: (err?: Error) => void): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function emitCheck (note): void {
|
function emitCheck (note: NoteSession): void {
|
||||||
const out = {
|
const out = {
|
||||||
title: note.title,
|
title: note.title,
|
||||||
updatetime: note.updatetime,
|
updatetime: note.updatetime,
|
||||||
|
@ -74,14 +75,18 @@ function emitCheck (note): void {
|
||||||
realtime.io.to(note.id).emit('check', out)
|
realtime.io.to(note.id).emit('check', out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class UserSession {
|
class UserSession {
|
||||||
id: string;
|
id?: string
|
||||||
|
address: string
|
||||||
login: any
|
login: any
|
||||||
userid: string
|
userid: string | null
|
||||||
|
'user-agent': any
|
||||||
photo: any
|
photo: any
|
||||||
color: any
|
color: string
|
||||||
cursor: any
|
cursor: any
|
||||||
name: string
|
name: string | null
|
||||||
idle: any
|
idle: any
|
||||||
type: any
|
type: any
|
||||||
}
|
}
|
||||||
|
@ -90,26 +95,24 @@ class NoteSession {
|
||||||
id: string
|
id: string
|
||||||
alias: string
|
alias: string
|
||||||
title: string
|
title: string
|
||||||
owner: UserSession
|
owner: string
|
||||||
ownerprofile: any
|
ownerprofile: any
|
||||||
permission: any
|
permission: any
|
||||||
lastchangeuser: string
|
lastchangeuser: string | null
|
||||||
lastchangeuserprofile: any
|
lastchangeuserprofile: any
|
||||||
socks: SocketWithNoteId[]
|
socks: SocketWithNoteId[]
|
||||||
users: UserSession[]
|
users: Map<string, UserSession>
|
||||||
tempUsers: UserSession[]
|
tempUsers: Map<string, number> // time value
|
||||||
createtime: number
|
createtime: number
|
||||||
updatetime: number
|
updatetime: number
|
||||||
server: any
|
server: any
|
||||||
authors: UserSession[]
|
authors: Map<string, Author>
|
||||||
authorship: UserSession
|
authorship: NoteAuthorship[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// actions
|
// actions
|
||||||
const users: UserSession[] = []
|
const users: Map<string, UserSession> = new Map<string, UserSession>()
|
||||||
const notes: NoteSession[] = []
|
const notes: Map<string, NoteSession> = new Map<string, NoteSession>()
|
||||||
|
|
||||||
let saverSleep = false
|
let saverSleep = false
|
||||||
|
|
||||||
|
@ -147,10 +150,10 @@ function updateNote (note: NoteSession, callback: (err, note) => void): void {
|
||||||
if (!_note) return callback(null, null)
|
if (!_note) return callback(null, null)
|
||||||
// update user note history
|
// update user note history
|
||||||
const tempUsers = Object.assign({}, note.tempUsers)
|
const tempUsers = Object.assign({}, note.tempUsers)
|
||||||
note.tempUsers = []
|
note.tempUsers = new Map<string, number>()
|
||||||
Object.keys(tempUsers).forEach(function (key) {
|
for (const [key, time] of tempUsers) {
|
||||||
updateHistory(key, note, tempUsers[key])
|
updateHistory(key, note, time)
|
||||||
})
|
}
|
||||||
if (note.lastchangeuser) {
|
if (note.lastchangeuser) {
|
||||||
if (_note.lastchangeuserId !== note.lastchangeuser) {
|
if (_note.lastchangeuserId !== note.lastchangeuser) {
|
||||||
User.findOne({
|
User.findOne({
|
||||||
|
@ -180,14 +183,13 @@ function updateNote (note: NoteSession, callback: (err, note) => void): void {
|
||||||
|
|
||||||
// update when the note is dirty
|
// update when the note is dirty
|
||||||
setInterval(function () {
|
setInterval(function () {
|
||||||
async.each(Object.keys(notes), function (key, callback) {
|
for (const [key, note] of notes) {
|
||||||
const note = notes[key]
|
|
||||||
if (note.server.isDirty) {
|
if (note.server.isDirty) {
|
||||||
logger.debug(`updater found dirty note: ${key}`)
|
logger.debug(`updater found dirty note: ${key}`)
|
||||||
note.server.isDirty = false
|
note.server.isDirty = false
|
||||||
updateNote(note, function (err, _note) {
|
updateNote(note, function (err, _note) {
|
||||||
// handle when note already been clean up
|
// handle when note already been clean up
|
||||||
if (!notes[key] || !notes[key].server) return callback(null, null)
|
if (!note || !note.server) return
|
||||||
if (!_note) {
|
if (!_note) {
|
||||||
realtime.io.to(note.id).emit('info', {
|
realtime.io.to(note.id).emit('info', {
|
||||||
code: 404
|
code: 404
|
||||||
|
@ -195,26 +197,22 @@ setInterval(function () {
|
||||||
logger.error('note not found: ', note.id)
|
logger.error('note not found: ', note.id)
|
||||||
}
|
}
|
||||||
if (err || !_note) {
|
if (err || !_note) {
|
||||||
for (let i = 0, l = note.socks.length; i < l; i++) {
|
for (const sock of note.socks) {
|
||||||
const sock = note.socks[i]
|
if (sock) {
|
||||||
if (typeof sock !== 'undefined' && sock) {
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
sock.disconnect()
|
sock.disconnect()
|
||||||
}, 0)
|
}, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return callback(err, null)
|
return logger.error('updater error', err)
|
||||||
}
|
}
|
||||||
note.updatetime = moment(_note.lastchangeAt).valueOf()
|
note.updatetime = moment(_note.lastchangeAt).valueOf()
|
||||||
emitCheck(note)
|
emitCheck(note)
|
||||||
return callback(null, null)
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
return callback(null, null)
|
return
|
||||||
}
|
}
|
||||||
}, function (err) {
|
}
|
||||||
if (err) return logger.error('updater error', err)
|
|
||||||
})
|
|
||||||
}, 1000)
|
}, 1000)
|
||||||
|
|
||||||
// save note revision in interval
|
// save note revision in interval
|
||||||
|
@ -238,12 +236,12 @@ function getStatus (callback): void {
|
||||||
const distinctaddresses: string[] = []
|
const distinctaddresses: string[] = []
|
||||||
const regaddresses: string[] = []
|
const regaddresses: string[] = []
|
||||||
const distinctregaddresses: string[] = []
|
const distinctregaddresses: string[] = []
|
||||||
Object.keys(users).forEach(function (key) {
|
// Object.keys(users).forEach(function (key) {
|
||||||
const user = users[key]
|
for (const user of users.values()) {
|
||||||
if (!user) return
|
if (!user) return
|
||||||
let found = false
|
let found = false
|
||||||
for (let i = 0; i < distinctaddresses.length; i++) {
|
for (const distinctaddress of distinctaddresses) {
|
||||||
if (user.address === distinctaddresses[i]) {
|
if (user.address === distinctaddress) {
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -264,7 +262,7 @@ function getStatus (callback): void {
|
||||||
distinctregaddresses.push(user.address)
|
distinctregaddresses.push(user.address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
User.count().then(function (regcount) {
|
User.count().then(function (regcount) {
|
||||||
// eslint-disable-next-line standard/no-callback-literal
|
// eslint-disable-next-line standard/no-callback-literal
|
||||||
return callback ? callback({
|
return callback ? callback({
|
||||||
|
@ -321,6 +319,8 @@ function buildUserOutData (user): UserSession {
|
||||||
return {
|
return {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
login: user.login,
|
login: user.login,
|
||||||
|
address: '',
|
||||||
|
'user-agent': '',
|
||||||
userid: user.userid,
|
userid: user.userid,
|
||||||
photo: user.photo,
|
photo: user.photo,
|
||||||
color: user.color,
|
color: user.color,
|
||||||
|
@ -333,14 +333,16 @@ function buildUserOutData (user): UserSession {
|
||||||
|
|
||||||
function emitOnlineUsers (socket: SocketWithNoteId): void {
|
function emitOnlineUsers (socket: SocketWithNoteId): void {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
if (!noteId || !notes[noteId]) return
|
if (!noteId) return
|
||||||
|
const note = notes.get(noteId)
|
||||||
|
if (!note) return
|
||||||
const users: UserSession[] = []
|
const users: UserSession[] = []
|
||||||
Object.keys(notes[noteId].users).forEach(function (key) {
|
// Object.keys(note.users).forEach(function (key) {
|
||||||
const user = notes[noteId].users[key]
|
for (const user of note.users.keys()) {
|
||||||
if (user) {
|
if (user) {
|
||||||
users.push(buildUserOutData(user))
|
users.push(buildUserOutData(user))
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
const out = {
|
const out = {
|
||||||
users: users
|
users: users
|
||||||
}
|
}
|
||||||
|
@ -349,16 +351,20 @@ function emitOnlineUsers (socket: SocketWithNoteId): void {
|
||||||
|
|
||||||
function emitUserStatus (socket: SocketWithNoteId): void {
|
function emitUserStatus (socket: SocketWithNoteId): void {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
const user = users[socket.id]
|
if (!noteId) return
|
||||||
if (!noteId || !notes[noteId] || !user) return
|
const note = notes.get(noteId)
|
||||||
|
if (!note) return
|
||||||
|
const user = users.get(socket.id)
|
||||||
|
if (!user) return
|
||||||
const out = buildUserOutData(user)
|
const out = buildUserOutData(user)
|
||||||
socket.broadcast.to(noteId).emit('user status', out)
|
socket.broadcast.to(noteId).emit('user status', out)
|
||||||
}
|
}
|
||||||
|
|
||||||
function emitRefresh (socket: SocketWithNoteId): void {
|
function emitRefresh (socket: SocketWithNoteId): void {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
if (!noteId || !notes[noteId]) return
|
if (!noteId) return
|
||||||
const note = notes[noteId]
|
const note = notes.get(noteId)
|
||||||
|
if (!note) return
|
||||||
const out = {
|
const out = {
|
||||||
title: note.title,
|
title: note.title,
|
||||||
docmaxlength: config.documentMaxLength,
|
docmaxlength: config.documentMaxLength,
|
||||||
|
@ -376,8 +382,8 @@ function emitRefresh (socket: SocketWithNoteId): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDuplicatedInSocketQueue (queue: Socket[], socket: Socket): boolean {
|
function isDuplicatedInSocketQueue (queue: Socket[], socket: Socket): boolean {
|
||||||
for (let i = 0; i < queue.length; i++) {
|
for (const sock of queue) {
|
||||||
if (queue[i] && queue[i].id === socket.id) {
|
if (sock && sock.id === socket.id) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -417,8 +423,8 @@ function failConnection (errorCode: number, errorMessage: string, socket: Socket
|
||||||
}
|
}
|
||||||
|
|
||||||
function interruptConnection (socket: Socket, noteId: string, socketId): void {
|
function interruptConnection (socket: Socket, noteId: string, socketId): void {
|
||||||
if (notes[noteId]) delete notes[noteId]
|
notes.delete(noteId)
|
||||||
if (users[socketId]) delete users[socketId]
|
users.delete(socketId)
|
||||||
if (socket) {
|
if (socket) {
|
||||||
clearSocketQueue(connectionSocketQueue, socket)
|
clearSocketQueue(connectionSocketQueue, socket)
|
||||||
} else {
|
} else {
|
||||||
|
@ -427,7 +433,7 @@ function interruptConnection (socket: Socket, noteId: string, socketId): void {
|
||||||
connectNextSocket()
|
connectNextSocket()
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkViewPermission (req, note): boolean {
|
function checkViewPermission (req, note: NoteSession): boolean {
|
||||||
if (note.permission === 'private') {
|
if (note.permission === 'private') {
|
||||||
return !!(req.user?.logged_in && req.user.id === note.owner)
|
return !!(req.user?.logged_in && req.user.id === note.owner)
|
||||||
} else if (note.permission === 'limited' || note.permission === 'protected') {
|
} else if (note.permission === 'limited' || note.permission === 'protected') {
|
||||||
|
@ -439,21 +445,28 @@ function checkViewPermission (req, note): boolean {
|
||||||
|
|
||||||
function finishConnection (socket: SocketWithNoteId, noteId: string, socketId: string): void {
|
function finishConnection (socket: SocketWithNoteId, noteId: string, socketId: string): void {
|
||||||
// if no valid info provided will drop the client
|
// if no valid info provided will drop the client
|
||||||
if (!socket || !notes[noteId] || !users[socketId]) {
|
if (!socket || !notes.get(noteId) || !users.get(socketId)) {
|
||||||
return interruptConnection(socket, noteId, socketId)
|
return interruptConnection(socket, noteId, socketId)
|
||||||
}
|
}
|
||||||
// check view permission
|
// check view permission
|
||||||
if (!checkViewPermission(socket.request, notes[noteId])) {
|
const note = notes.get(noteId)
|
||||||
|
if (!note) return
|
||||||
|
if (!checkViewPermission(socket.request, note)) {
|
||||||
interruptConnection(socket, noteId, socketId)
|
interruptConnection(socket, noteId, socketId)
|
||||||
return failConnection(403, 'connection forbidden', socket)
|
return failConnection(403, 'connection forbidden', socket)
|
||||||
}
|
}
|
||||||
const note = notes[noteId]
|
const user = users.get(socketId)
|
||||||
const user = users[socketId]
|
if (!user || !user.userid) return
|
||||||
// update user color to author color
|
// update user color to author color
|
||||||
if (note.authors[user.userid]) {
|
const author = note.authors.get(user.userid)
|
||||||
user.color = users[socket.id].color = note.authors[user.userid].color
|
if (author) {
|
||||||
|
const socketIdUser = users.get(socket.id)
|
||||||
|
if (!socketIdUser) return
|
||||||
|
user.color = socketIdUser.color
|
||||||
|
users.set(socket.id, user)
|
||||||
}
|
}
|
||||||
note.users[socket.id] = user
|
|
||||||
|
note.users.set(socket.id, user)
|
||||||
note.socks.push(socket)
|
note.socks.push(socket)
|
||||||
note.server.addClient(socket)
|
note.server.addClient(socket)
|
||||||
note.server.setName(socket, user.name)
|
note.server.setName(socket, user.name)
|
||||||
|
@ -483,8 +496,9 @@ function finishConnection (socket: SocketWithNoteId, noteId: string, socketId: s
|
||||||
|
|
||||||
function ifMayEdit (socket: SocketWithNoteId, originIsOperation: boolean, callback: (mayEdit: boolean) => void): void {
|
function ifMayEdit (socket: SocketWithNoteId, originIsOperation: boolean, callback: (mayEdit: boolean) => void): void {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
if (!noteId || !notes[noteId]) return
|
if (!noteId) return
|
||||||
const note = notes[noteId]
|
const note = notes.get(noteId)
|
||||||
|
if (!note) return
|
||||||
let mayEdit = true
|
let mayEdit = true
|
||||||
switch (note.permission) {
|
switch (note.permission) {
|
||||||
case 'freely':
|
case 'freely':
|
||||||
|
@ -520,15 +534,18 @@ function ifMayEdit (socket: SocketWithNoteId, originIsOperation: boolean, callba
|
||||||
|
|
||||||
function operationCallback (socket: SocketWithNoteId, operation): void {
|
function operationCallback (socket: SocketWithNoteId, operation): void {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
if (!noteId || !notes[noteId]) return
|
if (!noteId) return
|
||||||
const note = notes[noteId]
|
const note = notes.get(noteId)
|
||||||
let userId = null
|
if (!note) return
|
||||||
|
let userId: string | null = null
|
||||||
// save authors
|
// save authors
|
||||||
if (socket.request.user && socket.request.user.logged_in) {
|
if (socket.request.user && socket.request.user.logged_in) {
|
||||||
const user = users[socket.id]
|
const user = users.get(socket.id)
|
||||||
if (!user) return
|
if (!user) return
|
||||||
userId = socket.request.user.id
|
userId = socket.request.user.id
|
||||||
if (!note.authors[userId]) {
|
if (!userId) return
|
||||||
|
const author = note.authors.get(userId)
|
||||||
|
if (!author) {
|
||||||
Author.findOrCreate({
|
Author.findOrCreate({
|
||||||
where: {
|
where: {
|
||||||
noteId: noteId,
|
noteId: noteId,
|
||||||
|
@ -541,18 +558,18 @@ function operationCallback (socket: SocketWithNoteId, operation): void {
|
||||||
}
|
}
|
||||||
}).then(function ([author, _created]) {
|
}).then(function ([author, _created]) {
|
||||||
if (author) {
|
if (author) {
|
||||||
note.authors[author.userId] = {
|
note.authors.set(author.userId, {
|
||||||
userid: author.userId,
|
userid: author.userId,
|
||||||
color: author.color,
|
color: author.color,
|
||||||
photo: user.photo,
|
photo: user.photo,
|
||||||
name: user.name
|
name: user.name
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
logger.error('operation callback failed: ' + err)
|
logger.error('operation callback failed: ' + err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
note.tempUsers[userId] = Date.now()
|
if (userId) note.tempUsers.set(userId, Date.now())
|
||||||
}
|
}
|
||||||
// save authorship - use timer here because it's an O(n) complexity algorithm
|
// save authorship - use timer here because it's an O(n) complexity algorithm
|
||||||
setImmediate(function () {
|
setImmediate(function () {
|
||||||
|
@ -569,7 +586,7 @@ function startConnection (socket: SocketWithNoteId): void {
|
||||||
return failConnection(404, 'note id not found', socket)
|
return failConnection(404, 'note id not found', socket)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!notes[noteId]) {
|
if (!notes.get(noteId)) {
|
||||||
const include = [{
|
const include = [{
|
||||||
model: User,
|
model: User,
|
||||||
as: 'owner'
|
as: 'owner'
|
||||||
|
@ -605,21 +622,27 @@ function startConnection (socket: SocketWithNoteId): void {
|
||||||
const updatetime = note.lastchangeAt
|
const updatetime = note.lastchangeAt
|
||||||
const server = new EditorSocketIOServer(body, [], noteId, ifMayEdit, operationCallback)
|
const server = new EditorSocketIOServer(body, [], noteId, ifMayEdit, operationCallback)
|
||||||
|
|
||||||
const authors = {}
|
const authors = new Map<string, UserSession>()
|
||||||
for (let i = 0; i < note.authors.length; i++) {
|
for (const author of note.authors) {
|
||||||
const author = note.authors[i]
|
|
||||||
const profile = User.getProfile(author.user)
|
const profile = User.getProfile(author.user)
|
||||||
if (profile) {
|
if (profile) {
|
||||||
authors[author.userId] = {
|
authors.set(author.userId, {
|
||||||
|
id: '',
|
||||||
|
address: '',
|
||||||
|
'user-agent': '',
|
||||||
|
login: {},
|
||||||
|
cursor: {},
|
||||||
|
idle: {},
|
||||||
userid: author.userId,
|
userid: author.userId,
|
||||||
color: author.color,
|
color: author.color,
|
||||||
photo: profile.photo,
|
photo: profile.photo,
|
||||||
name: profile.name
|
name: profile.name,
|
||||||
}
|
type: {}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notes[noteId] = {
|
notes.set(noteId, {
|
||||||
id: noteId,
|
id: noteId,
|
||||||
alias: note.alias,
|
alias: note.alias,
|
||||||
title: note.title,
|
title: note.title,
|
||||||
|
@ -629,14 +652,14 @@ function startConnection (socket: SocketWithNoteId): void {
|
||||||
lastchangeuser: lastchangeuser,
|
lastchangeuser: lastchangeuser,
|
||||||
lastchangeuserprofile: lastchangeuserprofile,
|
lastchangeuserprofile: lastchangeuserprofile,
|
||||||
socks: [],
|
socks: [],
|
||||||
users: {},
|
users: new Map<string, UserSession>(),
|
||||||
tempUsers: {},
|
tempUsers: new Map<string, number>(),
|
||||||
createtime: moment(createtime).valueOf(),
|
createtime: moment(createtime).valueOf(),
|
||||||
updatetime: moment(updatetime).valueOf(),
|
updatetime: moment(updatetime).valueOf(),
|
||||||
server: server,
|
server: server,
|
||||||
authors: authors,
|
authors: authors,
|
||||||
authorship: note.authorship
|
authorship: note.authorship
|
||||||
}
|
})
|
||||||
|
|
||||||
return finishConnection(socket, noteId, socket.id)
|
return finishConnection(socket, noteId, socket.id)
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
|
@ -655,17 +678,17 @@ function disconnect (socket: SocketWithNoteId): void {
|
||||||
isDisconnectBusy = true
|
isDisconnectBusy = true
|
||||||
|
|
||||||
logger.debug('SERVER disconnected a client')
|
logger.debug('SERVER disconnected a client')
|
||||||
logger.debug(JSON.stringify(users[socket.id]))
|
logger.debug(JSON.stringify(users.get(socket.id)))
|
||||||
|
|
||||||
if (users[socket.id]) {
|
if (users.get(socket.id)) {
|
||||||
delete users[socket.id]
|
users.delete(socket.id)
|
||||||
}
|
}
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
const note = notes[noteId]
|
const note = notes.get(noteId)
|
||||||
if (note) {
|
if (note) {
|
||||||
// delete user in users
|
// delete user in users
|
||||||
if (note.users[socket.id]) {
|
if (note.users.get(socket.id)) {
|
||||||
delete note.users[socket.id]
|
note.users.delete(socket.id)
|
||||||
}
|
}
|
||||||
// remove sockets in the note socks
|
// remove sockets in the note socks
|
||||||
let index
|
let index
|
||||||
|
@ -676,15 +699,15 @@ function disconnect (socket: SocketWithNoteId): void {
|
||||||
}
|
}
|
||||||
} while (index !== -1)
|
} while (index !== -1)
|
||||||
// remove note in notes if no user inside
|
// remove note in notes if no user inside
|
||||||
if (Object.keys(note.users).length <= 0) {
|
if (note.users.size <= 0) {
|
||||||
if (note.server.isDirty) {
|
if (note.server.isDirty) {
|
||||||
updateNote(note, function (err, _note: Note) {
|
updateNote(note, function (err, _) {
|
||||||
if (err) return logger.error('disconnect note failed: ' + err)
|
if (err) return logger.error('disconnect note failed: ' + err)
|
||||||
// clear server before delete to avoid memory leaks
|
// clear server before delete to avoid memory leaks
|
||||||
note.server.document = ''
|
note.server.document = ''
|
||||||
note.server.operations = []
|
note.server.operations = []
|
||||||
delete note.server
|
delete note.server
|
||||||
delete notes[noteId]
|
notes.delete(noteId)
|
||||||
if (config.debug) {
|
if (config.debug) {
|
||||||
logger.debug(notes)
|
logger.debug(notes)
|
||||||
getStatus(function (data) {
|
getStatus(function (data) {
|
||||||
|
@ -694,7 +717,7 @@ function disconnect (socket: SocketWithNoteId): void {
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
delete note.server
|
delete note.server
|
||||||
delete notes[noteId]
|
notes.delete(noteId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -718,9 +741,9 @@ function disconnect (socket: SocketWithNoteId): void {
|
||||||
|
|
||||||
// clean when user not in any rooms or user not in connected list
|
// clean when user not in any rooms or user not in connected list
|
||||||
setInterval(function () {
|
setInterval(function () {
|
||||||
async.each(Object.keys(users), function (key, callback) {
|
for (const [key, user] of users) {
|
||||||
let socket = realtime.io.sockets.connected[key]
|
let socket = realtime.io.sockets.connected[key]
|
||||||
if ((!socket && users[key]) ||
|
if ((!socket && user) ||
|
||||||
(socket && (!socket.rooms || socket.rooms.length <= 0))) {
|
(socket && (!socket.rooms || socket.rooms.length <= 0))) {
|
||||||
logger.debug(`cleaner found redundant user: ${key}`)
|
logger.debug(`cleaner found redundant user: ${key}`)
|
||||||
if (!socket) {
|
if (!socket) {
|
||||||
|
@ -731,10 +754,7 @@ setInterval(function () {
|
||||||
disconnectSocketQueue.push(socket)
|
disconnectSocketQueue.push(socket)
|
||||||
disconnect(socket)
|
disconnect(socket)
|
||||||
}
|
}
|
||||||
return callback(null, null)
|
}
|
||||||
}, function (err) {
|
|
||||||
if (err) return logger.error('cleaner error', err)
|
|
||||||
})
|
|
||||||
}, 60000)
|
}, 60000)
|
||||||
|
|
||||||
function updateUserData (socket: Socket, user): void {
|
function updateUserData (socket: Socket, user): void {
|
||||||
|
@ -771,16 +791,17 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
// random color
|
// random color
|
||||||
let color = randomcolor()
|
let color = randomcolor()
|
||||||
// make sure color not duplicated or reach max random count
|
// make sure color not duplicated or reach max random count
|
||||||
if (notes[noteId]) {
|
const note = notes.get(noteId)
|
||||||
|
if (note) {
|
||||||
let randomcount = 0
|
let randomcount = 0
|
||||||
const maxrandomcount = 10
|
const maxrandomcount = 10
|
||||||
let found = false
|
let found = false
|
||||||
do {
|
do {
|
||||||
Object.keys(notes[noteId].users).forEach(function (userId) {
|
for (const user of note.users.values()) {
|
||||||
if (notes[noteId].users[userId].color === color) {
|
if (user.color === color) {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
if (found) {
|
if (found) {
|
||||||
color = randomcolor()
|
color = randomcolor()
|
||||||
randomcount++
|
randomcount++
|
||||||
|
@ -788,7 +809,7 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
} while (found && randomcount < maxrandomcount)
|
} while (found && randomcount < maxrandomcount)
|
||||||
}
|
}
|
||||||
// create user data
|
// create user data
|
||||||
users[socket.id] = {
|
users.set(socket.id, {
|
||||||
id: socket.id,
|
id: socket.id,
|
||||||
address: socket.handshake.headers['x-forwarded-for'] || socket.handshake.address,
|
address: socket.handshake.headers['x-forwarded-for'] || socket.handshake.address,
|
||||||
'user-agent': socket.handshake.headers['user-agent'],
|
'user-agent': socket.handshake.headers['user-agent'],
|
||||||
|
@ -798,9 +819,10 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
userid: null,
|
userid: null,
|
||||||
name: null,
|
name: null,
|
||||||
idle: false,
|
idle: false,
|
||||||
type: null
|
type: null,
|
||||||
}
|
photo: {}
|
||||||
updateUserData(socket, users[socket.id])
|
})
|
||||||
|
updateUserData(socket, users.get(socket.id))
|
||||||
|
|
||||||
// start connection
|
// start connection
|
||||||
connectionSocketQueue.push(socket)
|
connectionSocketQueue.push(socket)
|
||||||
|
@ -815,8 +837,8 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
// received user status
|
// received user status
|
||||||
socket.on('user status', function (data) {
|
socket.on('user status', function (data) {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
const user = users[socket.id]
|
const user = users.get(socket.id)
|
||||||
if (!noteId || !notes[noteId] || !user) return
|
if (!noteId || !notes.get(noteId) || !user) return
|
||||||
logger.debug(`SERVER received [${noteId}] user status from [${socket.id}]: ${JSON.stringify(data)}`)
|
logger.debug(`SERVER received [${noteId}] user status from [${socket.id}]: ${JSON.stringify(data)}`)
|
||||||
if (data) {
|
if (data) {
|
||||||
user.idle = data.idle
|
user.idle = data.idle
|
||||||
|
@ -830,8 +852,9 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
// need login to do more actions
|
// need login to do more actions
|
||||||
if (socket.request.user && socket.request.user.logged_in) {
|
if (socket.request.user && socket.request.user.logged_in) {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
if (!noteId || !notes[noteId]) return
|
if (!noteId) return
|
||||||
const note = notes[noteId]
|
const note = notes.get(noteId)
|
||||||
|
if (!note) return
|
||||||
// Only owner can change permission
|
// Only owner can change permission
|
||||||
if (note.owner && note.owner === socket.request.user.id) {
|
if (note.owner && note.owner === socket.request.user.id) {
|
||||||
if (permission === 'freely' && !config.allowAnonymous && !config.allowAnonymousEdits) return
|
if (permission === 'freely' && !config.allowAnonymous && !config.allowAnonymousEdits) return
|
||||||
|
@ -876,8 +899,9 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
// need login to do more actions
|
// need login to do more actions
|
||||||
if (socket.request.user && socket.request.user.logged_in) {
|
if (socket.request.user && socket.request.user.logged_in) {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
if (!noteId || !notes[noteId]) return
|
if (!noteId) return
|
||||||
const note = notes[noteId]
|
const note = notes.get(noteId)
|
||||||
|
if (!note) return
|
||||||
// Only owner can delete note
|
// Only owner can delete note
|
||||||
if (note.owner && note.owner === socket.request.user.id) {
|
if (note.owner && note.owner === socket.request.user.id) {
|
||||||
Note.destroy({
|
Note.destroy({
|
||||||
|
@ -906,8 +930,10 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
socket.on('user changed', function () {
|
socket.on('user changed', function () {
|
||||||
logger.info('user changed')
|
logger.info('user changed')
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
if (!noteId || !notes[noteId]) return
|
if (!noteId) return
|
||||||
const user = notes[noteId].users[socket.id]
|
const note = notes.get(noteId)
|
||||||
|
if (!note) return
|
||||||
|
const user = note.users.get(socket.id)
|
||||||
if (!user) return
|
if (!user) return
|
||||||
updateUserData(socket, user)
|
updateUserData(socket, user)
|
||||||
emitOnlineUsers(socket)
|
emitOnlineUsers(socket)
|
||||||
|
@ -916,14 +942,15 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
// received sync of online users request
|
// received sync of online users request
|
||||||
socket.on('online users', function () {
|
socket.on('online users', function () {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
if (!noteId || !notes[noteId]) return
|
if (!noteId) return
|
||||||
|
const note = notes.get(noteId)
|
||||||
|
if (!note) return
|
||||||
const users: UserSession[] = []
|
const users: UserSession[] = []
|
||||||
Object.keys(notes[noteId].users).forEach(function (key) {
|
for (const user of note.users.values()) {
|
||||||
const user = notes[noteId].users[key]
|
|
||||||
if (user) {
|
if (user) {
|
||||||
users.push(buildUserOutData(user))
|
users.push(buildUserOutData(user))
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
const out = {
|
const out = {
|
||||||
users: users
|
users: users
|
||||||
}
|
}
|
||||||
|
@ -941,8 +968,8 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
// received cursor focus
|
// received cursor focus
|
||||||
socket.on('cursor focus', function (data) {
|
socket.on('cursor focus', function (data) {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
const user = users[socket.id]
|
const user = users.get(socket.id)
|
||||||
if (!noteId || !notes[noteId] || !user) return
|
if (!noteId || !notes.get(noteId) || !user) return
|
||||||
user.cursor = data
|
user.cursor = data
|
||||||
const out = buildUserOutData(user)
|
const out = buildUserOutData(user)
|
||||||
socket.broadcast.to(noteId).emit('cursor focus', out)
|
socket.broadcast.to(noteId).emit('cursor focus', out)
|
||||||
|
@ -951,8 +978,8 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
// received cursor activity
|
// received cursor activity
|
||||||
socket.on('cursor activity', function (data) {
|
socket.on('cursor activity', function (data) {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
const user = users[socket.id]
|
const user = users.get(socket.id)
|
||||||
if (!noteId || !notes[noteId] || !user) return
|
if (!noteId || !notes.get(noteId) || !user) return
|
||||||
user.cursor = data
|
user.cursor = data
|
||||||
const out = buildUserOutData(user)
|
const out = buildUserOutData(user)
|
||||||
socket.broadcast.to(noteId).emit('cursor activity', out)
|
socket.broadcast.to(noteId).emit('cursor activity', out)
|
||||||
|
@ -961,8 +988,8 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
// received cursor blur
|
// received cursor blur
|
||||||
socket.on('cursor blur', function () {
|
socket.on('cursor blur', function () {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
const user = users[socket.id]
|
const user = users.get(socket.id)
|
||||||
if (!noteId || !notes[noteId] || !user) return
|
if (!noteId || !notes.get(noteId) || !user) return
|
||||||
user.cursor = null
|
user.cursor = null
|
||||||
const out = {
|
const out = {
|
||||||
id: socket.id
|
id: socket.id
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue