@@ -7,6 +7,7 @@ import randomcolor from "randomcolor";
7
7
import Chance from "chance" ;
8
8
import moment from "moment" ;
9
9
import { get } from "lodash" ;
10
+ import SocketIO from "socket.io" ;
10
11
11
12
// core
12
13
import config from "../config" ;
@@ -25,14 +26,14 @@ import {SaveRevisionJob} from "./realtimeSaveRevisionJob";
25
26
26
27
const chance = new Chance ( )
27
28
28
- export let io = null
29
+ export let io : SocketIO . Server = null
29
30
export let maintenance = true
30
31
31
- export function setSocketIo ( socketIO ) {
32
+ export function setSocketIo ( socketIO : SocketIO . Server ) : void {
32
33
io = socketIO
33
34
}
34
35
35
- export function setMaintenance ( isMaintenance ) {
36
+ export function setMaintenance ( isMaintenance : boolean ) : void {
36
37
maintenance = isMaintenance
37
38
}
38
39
@@ -44,18 +45,18 @@ const cleanDanglingUserJob = new CleanDanglingUserJob(exports)
44
45
export const saveRevisionJob = new SaveRevisionJob ( exports )
45
46
46
47
// TODO: test it
47
- export function onAuthorizeSuccess ( data , accept ) {
48
+ export function onAuthorizeSuccess ( data : Record < null , null > , accept : ( err ?: Error | null , accepted ?: boolean ) => void ) : void {
48
49
accept ( )
49
50
}
50
51
51
52
// TODO: test it
52
- export function onAuthorizeFail ( data , message , error , accept ) {
53
+ export function onAuthorizeFail ( data : Record < null , null > , message : string , error : boolean , accept : ( err ?: Error | null , accepted ?: boolean ) => void ) : void {
53
54
accept ( ) // accept whether authorize or not to allow anonymous usage
54
55
}
55
56
56
57
// TODO: test it
57
58
// secure the origin by the cookie
58
- export function secure ( socket , next ) {
59
+ export function secure ( socket : SocketIO . Socket , next : ( err ?: Error | null ) => void ) : void {
59
60
try {
60
61
const handshakeData = socket . request
61
62
if ( handshakeData . headers . cookie ) {
@@ -101,7 +102,7 @@ export function getNotePool(): any {
101
102
return notes
102
103
}
103
104
104
- export function isNoteExistsInPool ( noteId ) {
105
+ export function isNoteExistsInPool ( noteId : string ) : boolean {
105
106
return ! ! notes [ noteId ]
106
107
}
107
108
@@ -111,15 +112,15 @@ export function addNote(note) {
111
112
return true
112
113
}
113
114
114
- export function getNotePoolSize ( ) {
115
+ export function getNotePoolSize ( ) : number {
115
116
return Object . keys ( notes ) . length
116
117
}
117
118
118
- export function deleteNoteFromPool ( noteId ) {
119
+ export function deleteNoteFromPool ( noteId : string ) : void {
119
120
delete notes [ noteId ]
120
121
}
121
122
122
- export function deleteAllNoteFromPool ( ) {
123
+ export function deleteAllNoteFromPool ( ) : void {
123
124
Object . keys ( notes ) . forEach ( noteId => {
124
125
delete notes [ noteId ]
125
126
} )
@@ -248,7 +249,7 @@ async function _updateNoteAsync(note) {
248
249
// TODO: test it
249
250
export function getStatus ( ) {
250
251
return Note . count ( )
251
- . then ( function ( notecount ) {
252
+ . then ( function ( notecount : number ) {
252
253
const distinctaddresses = [ ]
253
254
const regaddresses = [ ]
254
255
const distinctregaddresses = [ ]
@@ -281,7 +282,7 @@ export function getStatus() {
281
282
} )
282
283
283
284
return User . count ( )
284
- . then ( function ( regcount ) {
285
+ . then ( function ( regcount : number ) {
285
286
return {
286
287
onlineNotes : Object . keys ( notes ) . length ,
287
288
onlineUsers : Object . keys ( users ) . length ,
@@ -307,7 +308,7 @@ export function getStatus() {
307
308
}
308
309
309
310
// TODO: test it
310
- export function isReady ( ) {
311
+ export function isReady ( ) : boolean {
311
312
return io &&
312
313
Object . keys ( notes ) . length === 0 && Object . keys ( users ) . length === 0 &&
313
314
connectProcessQueue . queue . length === 0 && ! connectProcessQueue . lock &&
@@ -329,8 +330,8 @@ function parseUrl(data) {
329
330
return null
330
331
}
331
332
332
- export function extractNoteIdFromSocket ( socket ) {
333
- function extractNoteIdFromReferer ( referer ) {
333
+ export function extractNoteIdFromSocket ( socket : SocketIO . Socket ) : string | null | boolean {
334
+ function extractNoteIdFromReferer ( referer : string ) : string | null | boolean {
334
335
if ( referer ) {
335
336
const hostUrl = parseUrl ( referer )
336
337
if ( ! hostUrl ) {
@@ -362,14 +363,14 @@ export function extractNoteIdFromSocket(socket) {
362
363
return false
363
364
}
364
365
365
- export async function parseNoteIdFromSocketAsync ( socket ) {
366
+ export const parseNoteIdFromSocketAsync = async function ( socket : SocketIO . Socket ) : Promise < string | null > {
366
367
const noteId = extractNoteIdFromSocket ( socket )
367
368
if ( ! noteId ) {
368
369
return null
369
370
}
370
371
371
372
return new Promise ( ( resolve , reject ) => {
372
- Note . parseNoteId ( noteId , function ( err , id ) {
373
+ Note . parseNoteId ( noteId as string , function ( err , id ) {
373
374
if ( err ) {
374
375
reject ( err )
375
376
}
@@ -382,7 +383,7 @@ export async function parseNoteIdFromSocketAsync(socket) {
382
383
}
383
384
384
385
// TODO: test it
385
- export function emitOnlineUsers ( socket ) {
386
+ export function emitOnlineUsers ( socket : SocketIO . Socket ) : void {
386
387
const noteId = socket . noteId
387
388
if ( ! noteId || ! notes [ noteId ] ) return
388
389
const users = [ ]
@@ -399,7 +400,7 @@ export function emitOnlineUsers(socket) {
399
400
}
400
401
401
402
// TODO: test it
402
- export function emitUserStatus ( socket ) {
403
+ export function emitUserStatus ( socket : SocketIO . Socket ) : void {
403
404
const noteId = socket . noteId
404
405
const user = users [ socket . id ]
405
406
if ( ! noteId || ! notes [ noteId ] || ! user ) return
@@ -408,7 +409,7 @@ export function emitUserStatus(socket) {
408
409
}
409
410
410
411
// TODO: test it
411
- export function emitRefresh ( socket ) {
412
+ export function emitRefresh ( socket : SocketIO . Socket ) : void {
412
413
const noteId = socket . noteId
413
414
if ( ! noteId || ! notes [ noteId ] ) return
414
415
const note = notes [ noteId ]
@@ -447,7 +448,7 @@ export function checkViewPermission(req, note) {
447
448
}
448
449
449
450
// TODO: test it
450
- async function fetchFullNoteAsync ( noteId ) {
451
+ async function fetchFullNoteAsync ( noteId : string ) : Promise < Note > {
451
452
return Note . findOne ( {
452
453
where : {
453
454
id : noteId
@@ -513,16 +514,17 @@ function makeNewServerNote(note) {
513
514
}
514
515
515
516
// TODO: test it
516
- export function failConnection ( code , err , socket ) {
517
+ export function failConnection ( code : number , err : string | Error , socket : SocketIO . Socket ) : void {
517
518
logger . error ( err )
518
519
// emit error info
519
520
socket . emit ( 'info' , {
520
521
code : code
521
522
} )
522
- return socket . disconnect ( true )
523
+ socket . disconnect ( true )
524
+ return
523
525
}
524
526
525
- export function queueForDisconnect ( socket ) {
527
+ export function queueForDisconnect ( socket : SocketIO . Socket ) : void {
526
528
disconnectProcessQueue . push ( socket . id , async function ( ) {
527
529
if ( users [ socket . id ] ) {
528
530
delete users [ socket . id ]
@@ -545,7 +547,7 @@ export function queueForDisconnect(socket) {
545
547
// remove note in notes if no user inside
546
548
if ( Object . keys ( note . users ) . length === 0 ) {
547
549
if ( note . server . isDirty ) {
548
- exports . updateNote ( note , function ( err , _note ) {
550
+ exports . updateNote ( note , function ( err ) {
549
551
if ( err ) {
550
552
logger . error ( 'disconnect note failed: ' + err )
551
553
return
@@ -582,7 +584,7 @@ export function buildUserOutData(user) {
582
584
}
583
585
584
586
// TODO: test it
585
- export function updateUserData ( socket , user ) {
587
+ export function updateUserData ( socket : SocketIO . Socket , user ) : void {
586
588
// retrieve user data from passport
587
589
if ( socket . request . user && socket . request . user . logged_in ) {
588
590
const profile = User . getProfile ( socket . request . user )
@@ -597,7 +599,7 @@ export function updateUserData(socket, user) {
597
599
}
598
600
}
599
601
600
- function canEditNote ( notePermission , noteOwnerId , currentUserId ) {
602
+ function canEditNote ( notePermission : string , noteOwnerId : string , currentUserId : string ) : boolean {
601
603
switch ( notePermission ) {
602
604
case 'freely' :
603
605
return true
@@ -613,7 +615,7 @@ function canEditNote(notePermission, noteOwnerId, currentUserId) {
613
615
}
614
616
}
615
617
616
- export function ifMayEdit ( socket , callback ) {
618
+ export function ifMayEdit ( socket : SocketIO . Socket , callback : ( canEdit : boolean ) => void ) : void {
617
619
const note = getNoteFromNotePool ( socket . noteId )
618
620
if ( ! note ) return
619
621
const mayEdit = canEditNote ( note . permission , note . owner , socket . request . user . id )
@@ -630,7 +632,7 @@ export function ifMayEdit(socket, callback) {
630
632
}
631
633
632
634
// TODO: test it
633
- function operationCallback ( socket , operation ) {
635
+ function operationCallback ( socket : SocketIO . Socket , operation : any ) {
634
636
const noteId = socket . noteId
635
637
if ( ! noteId || ! notes [ noteId ] ) return
636
638
const note = notes [ noteId ]
@@ -651,7 +653,7 @@ function operationCallback(socket, operation) {
651
653
userId : userId ,
652
654
color : user . color
653
655
}
654
- } ) . spread ( function ( author , created ) {
656
+ } ) . spread ( function ( author ) {
655
657
if ( author ) {
656
658
note . authors [ author . userId ] = {
657
659
userid : author . userId ,
@@ -674,12 +676,12 @@ function operationCallback(socket, operation) {
674
676
}
675
677
676
678
// TODO: test it
677
- export function updateHistory ( userId , note , time ?: any ) {
679
+ export function updateHistory ( userId : string , note , time ?: number ) : void {
678
680
const noteId = note . alias ? note . alias : Note . encodeNoteId ( note . id )
679
681
if ( note . server ) history . updateHistory ( userId , noteId , note . server . document , time )
680
682
}
681
683
682
- function getUniqueColorPerNote ( noteId , maxAttempt = 10 ) {
684
+ function getUniqueColorPerNote ( noteId : string , maxAttempt = 10 ) : string {
683
685
// random color
684
686
let color = randomcolor ( )
685
687
if ( ! notes [ noteId ] ) return color
@@ -701,7 +703,7 @@ function getUniqueColorPerNote(noteId, maxAttempt = 10) {
701
703
return color
702
704
}
703
705
704
- function queueForConnect ( socket ) {
706
+ function queueForConnect ( socket : SocketIO . Socket ) {
705
707
connectProcessQueue . push ( socket . id , async function ( ) {
706
708
try {
707
709
const noteId = await exports . parseNoteIdFromSocketAsync ( socket ) as string
@@ -800,13 +802,13 @@ function queueForConnect(socket) {
800
802
} )
801
803
}
802
804
803
- export function connection ( socket ) {
805
+ export function connection ( socket : SocketIO . Socket ) : void {
804
806
if ( maintenance ) return
805
807
queueForConnect ( socket )
806
808
}
807
809
808
810
// TODO: test it
809
- export function terminate ( ) {
811
+ export function terminate ( ) : void {
810
812
disconnectProcessQueue . stop ( )
811
813
connectProcessQueue . stop ( )
812
814
updateDirtyNoteJob . stop ( )
0 commit comments