mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-30 10:03:52 -05:00
Many types (and corresponding changes to keek tsc happy) in realtime.ts
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
591096ce8b
commit
9c894633a8
1 changed files with 56 additions and 51 deletions
|
@ -1,25 +1,33 @@
|
||||||
import async from 'async'
|
|
||||||
import Chance from 'chance'
|
import Chance from 'chance'
|
||||||
|
import CodeMirror from 'codemirror'
|
||||||
import cookie from 'cookie'
|
import cookie from 'cookie'
|
||||||
import cookieParser from 'cookie-parser'
|
import cookieParser from 'cookie-parser'
|
||||||
import moment from 'moment'
|
import moment from 'moment'
|
||||||
import randomcolor from 'randomcolor'
|
import randomcolor from 'randomcolor'
|
||||||
import { Socket } from 'socket.io'
|
import SocketIO, { Socket } from 'socket.io'
|
||||||
import { config } from './config'
|
import { config } from './config'
|
||||||
|
|
||||||
import { History } from './history'
|
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 { NoteAuthorship } from './models/note'
|
import { NoteAuthorship } from './models/note'
|
||||||
|
import { PhotoProfile } from './models/user'
|
||||||
|
import { EditorSocketIOServer } from './ot/editor-socketio-server'
|
||||||
|
|
||||||
export type SocketWithNoteId = Socket & { noteId: string }
|
export type SocketWithNoteId = Socket & { noteId: string }
|
||||||
|
|
||||||
const chance = new Chance()
|
const chance = new Chance()
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/no-use-before-define */
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
||||||
const realtime: any = {
|
const realtime: {
|
||||||
io: null,
|
onAuthorizeSuccess: (data, accept) => void;
|
||||||
|
onAuthorizeFail: (data, message, error, accept) => void;
|
||||||
|
io: SocketIO.Server; isReady: () => boolean;
|
||||||
|
connection: (socket: SocketWithNoteId) => void;
|
||||||
|
secure: (socket: SocketIO.Socket, next: (err?: Error) => void) => void;
|
||||||
|
getStatus: (callback) => void; maintenance: boolean;
|
||||||
|
} = {
|
||||||
|
io: SocketIO(),
|
||||||
onAuthorizeSuccess: onAuthorizeSuccess,
|
onAuthorizeSuccess: onAuthorizeSuccess,
|
||||||
onAuthorizeFail: onAuthorizeFail,
|
onAuthorizeFail: onAuthorizeFail,
|
||||||
secure: secure,
|
secure: secure,
|
||||||
|
@ -75,39 +83,37 @@ function emitCheck (note: NoteSession): 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
|
address?: string
|
||||||
login: any
|
login?: boolean
|
||||||
userid: string | null
|
userid: string | null
|
||||||
'user-agent': any
|
'user-agent'?
|
||||||
photo: any
|
photo: string
|
||||||
color: string
|
color: string
|
||||||
cursor: any
|
cursor?: CodeMirror.Position
|
||||||
name: string | null
|
name: string | null
|
||||||
idle: any
|
idle?: boolean
|
||||||
type: any
|
type?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
class NoteSession {
|
class NoteSession {
|
||||||
id: string
|
id: string
|
||||||
alias: string
|
alias: string
|
||||||
title: string
|
title: string
|
||||||
owner: string
|
owner: string
|
||||||
ownerprofile: any
|
ownerprofile: PhotoProfile | null
|
||||||
permission: any
|
permission: string
|
||||||
lastchangeuser: string | null
|
lastchangeuser: string | null
|
||||||
lastchangeuserprofile: any
|
lastchangeuserprofile: PhotoProfile | null
|
||||||
socks: SocketWithNoteId[]
|
socks: SocketWithNoteId[]
|
||||||
users: Map<string, UserSession>
|
users: Map<string, UserSession>
|
||||||
tempUsers: Map<string, number> // time value
|
tempUsers: Map<string, number> // time value
|
||||||
createtime: number
|
createtime: number
|
||||||
updatetime: number
|
updatetime: number
|
||||||
server: any
|
server: EditorSocketIOServer
|
||||||
authors: Map<string, Author>
|
authors: Map<string, UserSession>
|
||||||
authorship: NoteAuthorship[]
|
authorship: NoteAuthorship[]
|
||||||
}
|
}
|
||||||
|
|
||||||
// actions
|
// actions
|
||||||
|
@ -149,7 +155,7 @@ function updateNote (note: NoteSession, callback: (err, note) => void): void {
|
||||||
}).then(function (_note) {
|
}).then(function (_note) {
|
||||||
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 = new Map(note.tempUsers)
|
||||||
note.tempUsers = new Map<string, number>()
|
note.tempUsers = new Map<string, number>()
|
||||||
for (const [key, time] of tempUsers) {
|
for (const [key, time] of tempUsers) {
|
||||||
updateHistory(key, note, time)
|
updateHistory(key, note, time)
|
||||||
|
@ -247,10 +253,14 @@ function getStatus (callback): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!found) {
|
if (!found) {
|
||||||
distinctaddresses.push(user.address)
|
if (user.address != null) {
|
||||||
|
distinctaddresses.push(user.address)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (user.login) {
|
if (user.login) {
|
||||||
regaddresses.push(user.address)
|
if (user.address != null) {
|
||||||
|
regaddresses.push(user.address)
|
||||||
|
}
|
||||||
let found = false
|
let found = false
|
||||||
for (let i = 0; i < distinctregaddresses.length; i++) {
|
for (let i = 0; i < distinctregaddresses.length; i++) {
|
||||||
if (user.address === distinctregaddresses[i]) {
|
if (user.address === distinctregaddresses[i]) {
|
||||||
|
@ -259,7 +269,9 @@ function getStatus (callback): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!found) {
|
if (!found) {
|
||||||
distinctregaddresses.push(user.address)
|
if (user.address != null) {
|
||||||
|
distinctregaddresses.push(user.address)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -556,7 +568,7 @@ function operationCallback (socket: SocketWithNoteId, operation): void {
|
||||||
userId: userId,
|
userId: userId,
|
||||||
color: user.color
|
color: user.color
|
||||||
}
|
}
|
||||||
}).then(function ([author, _created]) {
|
}).then(function ([author, _]) {
|
||||||
if (author) {
|
if (author) {
|
||||||
note.authors.set(author.userId, {
|
note.authors.set(author.userId, {
|
||||||
userid: author.userId,
|
userid: author.userId,
|
||||||
|
@ -627,17 +639,10 @@ function startConnection (socket: SocketWithNoteId): void {
|
||||||
const profile = User.getProfile(author.user)
|
const profile = User.getProfile(author.user)
|
||||||
if (profile) {
|
if (profile) {
|
||||||
authors.set(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: {}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -742,14 +747,14 @@ 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 () {
|
||||||
for (const [key, user] of users) {
|
for (const [key, user] of users) {
|
||||||
let socket = realtime.io.sockets.connected[key]
|
let socket = realtime.io.sockets.connected[key] as SocketWithNoteId
|
||||||
if ((!socket && user) ||
|
if ((!socket && user) ||
|
||||||
(socket && (!socket.rooms || socket.rooms.length <= 0))) {
|
(socket && (!socket.rooms || Object.keys(socket.rooms).length <= 0))) {
|
||||||
logger.debug(`cleaner found redundant user: ${key}`)
|
logger.debug(`cleaner found redundant user: ${key}`)
|
||||||
if (!socket) {
|
if (!socket) {
|
||||||
socket = {
|
socket = {
|
||||||
id: key
|
id: key
|
||||||
}
|
} as SocketWithNoteId
|
||||||
}
|
}
|
||||||
disconnectSocketQueue.push(socket)
|
disconnectSocketQueue.push(socket)
|
||||||
disconnect(socket)
|
disconnect(socket)
|
||||||
|
@ -814,13 +819,13 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
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'],
|
||||||
color: color,
|
color: color,
|
||||||
cursor: null,
|
cursor: undefined,
|
||||||
login: false,
|
login: false,
|
||||||
userid: null,
|
userid: null,
|
||||||
name: null,
|
name: null,
|
||||||
idle: false,
|
idle: false,
|
||||||
type: null,
|
type: '',
|
||||||
photo: {}
|
photo: ''
|
||||||
})
|
})
|
||||||
updateUserData(socket, users.get(socket.id))
|
updateUserData(socket, users.get(socket.id))
|
||||||
|
|
||||||
|
@ -976,7 +981,7 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
})
|
})
|
||||||
|
|
||||||
// received cursor activity
|
// received cursor activity
|
||||||
socket.on('cursor activity', function (data) {
|
socket.on('cursor activity', function (data: CodeMirror.Position) {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
const user = users.get(socket.id)
|
const user = users.get(socket.id)
|
||||||
if (!noteId || !notes.get(noteId) || !user) return
|
if (!noteId || !notes.get(noteId) || !user) return
|
||||||
|
@ -990,7 +995,7 @@ function connection (socket: SocketWithNoteId): void {
|
||||||
const noteId = socket.noteId
|
const noteId = socket.noteId
|
||||||
const user = users.get(socket.id)
|
const user = users.get(socket.id)
|
||||||
if (!noteId || !notes.get(noteId) || !user) return
|
if (!noteId || !notes.get(noteId) || !user) return
|
||||||
user.cursor = null
|
user.cursor = undefined
|
||||||
const out = {
|
const out = {
|
||||||
id: socket.id
|
id: socket.id
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue