@@ -7,6 +7,7 @@ import randomcolor from "randomcolor";
77import Chance from "chance" ;
88import moment from "moment" ;
99import { get } from "lodash" ;
10+ import SocketIO from "socket.io" ;
1011
1112// core
1213import config from "../config" ;
@@ -25,14 +26,14 @@ import {SaveRevisionJob} from "./realtimeSaveRevisionJob";
2526
2627const chance = new Chance ( )
2728
28- export let io = null
29+ export let io : SocketIO . Server = null
2930export let maintenance = true
3031
31- export function setSocketIo ( socketIO ) {
32+ export function setSocketIo ( socketIO : SocketIO . Server ) : void {
3233 io = socketIO
3334}
3435
35- export function setMaintenance ( isMaintenance ) {
36+ export function setMaintenance ( isMaintenance : boolean ) : void {
3637 maintenance = isMaintenance
3738}
3839
@@ -44,18 +45,18 @@ const cleanDanglingUserJob = new CleanDanglingUserJob(exports)
4445export const saveRevisionJob = new SaveRevisionJob ( exports )
4546
4647// 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 {
4849 accept ( )
4950}
5051
5152// 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 {
5354 accept ( ) // accept whether authorize or not to allow anonymous usage
5455}
5556
5657// TODO: test it
5758// 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 {
5960 try {
6061 const handshakeData = socket . request
6162 if ( handshakeData . headers . cookie ) {
@@ -101,7 +102,7 @@ export function getNotePool(): any {
101102 return notes
102103}
103104
104- export function isNoteExistsInPool ( noteId ) {
105+ export function isNoteExistsInPool ( noteId : string ) : boolean {
105106 return ! ! notes [ noteId ]
106107}
107108
@@ -111,15 +112,15 @@ export function addNote(note) {
111112 return true
112113}
113114
114- export function getNotePoolSize ( ) {
115+ export function getNotePoolSize ( ) : number {
115116 return Object . keys ( notes ) . length
116117}
117118
118- export function deleteNoteFromPool ( noteId ) {
119+ export function deleteNoteFromPool ( noteId : string ) : void {
119120 delete notes [ noteId ]
120121}
121122
122- export function deleteAllNoteFromPool ( ) {
123+ export function deleteAllNoteFromPool ( ) : void {
123124 Object . keys ( notes ) . forEach ( noteId => {
124125 delete notes [ noteId ]
125126 } )
@@ -248,7 +249,7 @@ async function _updateNoteAsync(note) {
248249// TODO: test it
249250export function getStatus ( ) {
250251 return Note . count ( )
251- . then ( function ( notecount ) {
252+ . then ( function ( notecount : number ) {
252253 const distinctaddresses = [ ]
253254 const regaddresses = [ ]
254255 const distinctregaddresses = [ ]
@@ -281,7 +282,7 @@ export function getStatus() {
281282 } )
282283
283284 return User . count ( )
284- . then ( function ( regcount ) {
285+ . then ( function ( regcount : number ) {
285286 return {
286287 onlineNotes : Object . keys ( notes ) . length ,
287288 onlineUsers : Object . keys ( users ) . length ,
@@ -307,7 +308,7 @@ export function getStatus() {
307308}
308309
309310// TODO: test it
310- export function isReady ( ) {
311+ export function isReady ( ) : boolean {
311312 return io &&
312313 Object . keys ( notes ) . length === 0 && Object . keys ( users ) . length === 0 &&
313314 connectProcessQueue . queue . length === 0 && ! connectProcessQueue . lock &&
@@ -329,8 +330,8 @@ function parseUrl(data) {
329330 return null
330331}
331332
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 {
334335 if ( referer ) {
335336 const hostUrl = parseUrl ( referer )
336337 if ( ! hostUrl ) {
@@ -362,14 +363,14 @@ export function extractNoteIdFromSocket(socket) {
362363 return false
363364}
364365
365- export async function parseNoteIdFromSocketAsync ( socket ) {
366+ export const parseNoteIdFromSocketAsync = async function ( socket : SocketIO . Socket ) : Promise < string | null > {
366367 const noteId = extractNoteIdFromSocket ( socket )
367368 if ( ! noteId ) {
368369 return null
369370 }
370371
371372 return new Promise ( ( resolve , reject ) => {
372- Note . parseNoteId ( noteId , function ( err , id ) {
373+ Note . parseNoteId ( noteId as string , function ( err , id ) {
373374 if ( err ) {
374375 reject ( err )
375376 }
@@ -382,7 +383,7 @@ export async function parseNoteIdFromSocketAsync(socket) {
382383}
383384
384385// TODO: test it
385- export function emitOnlineUsers ( socket ) {
386+ export function emitOnlineUsers ( socket : SocketIO . Socket ) : void {
386387 const noteId = socket . noteId
387388 if ( ! noteId || ! notes [ noteId ] ) return
388389 const users = [ ]
@@ -399,7 +400,7 @@ export function emitOnlineUsers(socket) {
399400}
400401
401402// TODO: test it
402- export function emitUserStatus ( socket ) {
403+ export function emitUserStatus ( socket : SocketIO . Socket ) : void {
403404 const noteId = socket . noteId
404405 const user = users [ socket . id ]
405406 if ( ! noteId || ! notes [ noteId ] || ! user ) return
@@ -408,7 +409,7 @@ export function emitUserStatus(socket) {
408409}
409410
410411// TODO: test it
411- export function emitRefresh ( socket ) {
412+ export function emitRefresh ( socket : SocketIO . Socket ) : void {
412413 const noteId = socket . noteId
413414 if ( ! noteId || ! notes [ noteId ] ) return
414415 const note = notes [ noteId ]
@@ -447,7 +448,7 @@ export function checkViewPermission(req, note) {
447448}
448449
449450// TODO: test it
450- async function fetchFullNoteAsync ( noteId ) {
451+ async function fetchFullNoteAsync ( noteId : string ) : Promise < Note > {
451452 return Note . findOne ( {
452453 where : {
453454 id : noteId
@@ -513,16 +514,17 @@ function makeNewServerNote(note) {
513514}
514515
515516// TODO: test it
516- export function failConnection ( code , err , socket ) {
517+ export function failConnection ( code : number , err : string | Error , socket : SocketIO . Socket ) : void {
517518 logger . error ( err )
518519 // emit error info
519520 socket . emit ( 'info' , {
520521 code : code
521522 } )
522- return socket . disconnect ( true )
523+ socket . disconnect ( true )
524+ return
523525}
524526
525- export function queueForDisconnect ( socket ) {
527+ export function queueForDisconnect ( socket : SocketIO . Socket ) : void {
526528 disconnectProcessQueue . push ( socket . id , async function ( ) {
527529 if ( users [ socket . id ] ) {
528530 delete users [ socket . id ]
@@ -545,7 +547,7 @@ export function queueForDisconnect(socket) {
545547 // remove note in notes if no user inside
546548 if ( Object . keys ( note . users ) . length === 0 ) {
547549 if ( note . server . isDirty ) {
548- exports . updateNote ( note , function ( err , _note ) {
550+ exports . updateNote ( note , function ( err ) {
549551 if ( err ) {
550552 logger . error ( 'disconnect note failed: ' + err )
551553 return
@@ -582,7 +584,7 @@ export function buildUserOutData(user) {
582584}
583585
584586// TODO: test it
585- export function updateUserData ( socket , user ) {
587+ export function updateUserData ( socket : SocketIO . Socket , user ) : void {
586588 // retrieve user data from passport
587589 if ( socket . request . user && socket . request . user . logged_in ) {
588590 const profile = User . getProfile ( socket . request . user )
@@ -597,7 +599,7 @@ export function updateUserData(socket, user) {
597599 }
598600}
599601
600- function canEditNote ( notePermission , noteOwnerId , currentUserId ) {
602+ function canEditNote ( notePermission : string , noteOwnerId : string , currentUserId : string ) : boolean {
601603 switch ( notePermission ) {
602604 case 'freely' :
603605 return true
@@ -613,7 +615,7 @@ function canEditNote(notePermission, noteOwnerId, currentUserId) {
613615 }
614616}
615617
616- export function ifMayEdit ( socket , callback ) {
618+ export function ifMayEdit ( socket : SocketIO . Socket , callback : ( canEdit : boolean ) => void ) : void {
617619 const note = getNoteFromNotePool ( socket . noteId )
618620 if ( ! note ) return
619621 const mayEdit = canEditNote ( note . permission , note . owner , socket . request . user . id )
@@ -630,7 +632,7 @@ export function ifMayEdit(socket, callback) {
630632}
631633
632634// TODO: test it
633- function operationCallback ( socket , operation ) {
635+ function operationCallback ( socket : SocketIO . Socket , operation : any ) {
634636 const noteId = socket . noteId
635637 if ( ! noteId || ! notes [ noteId ] ) return
636638 const note = notes [ noteId ]
@@ -651,7 +653,7 @@ function operationCallback(socket, operation) {
651653 userId : userId ,
652654 color : user . color
653655 }
654- } ) . spread ( function ( author , created ) {
656+ } ) . spread ( function ( author ) {
655657 if ( author ) {
656658 note . authors [ author . userId ] = {
657659 userid : author . userId ,
@@ -674,12 +676,12 @@ function operationCallback(socket, operation) {
674676}
675677
676678// TODO: test it
677- export function updateHistory ( userId , note , time ?: any ) {
679+ export function updateHistory ( userId : string , note , time ?: number ) : void {
678680 const noteId = note . alias ? note . alias : Note . encodeNoteId ( note . id )
679681 if ( note . server ) history . updateHistory ( userId , noteId , note . server . document , time )
680682}
681683
682- function getUniqueColorPerNote ( noteId , maxAttempt = 10 ) {
684+ function getUniqueColorPerNote ( noteId : string , maxAttempt = 10 ) : string {
683685 // random color
684686 let color = randomcolor ( )
685687 if ( ! notes [ noteId ] ) return color
@@ -701,7 +703,7 @@ function getUniqueColorPerNote(noteId, maxAttempt = 10) {
701703 return color
702704}
703705
704- function queueForConnect ( socket ) {
706+ function queueForConnect ( socket : SocketIO . Socket ) {
705707 connectProcessQueue . push ( socket . id , async function ( ) {
706708 try {
707709 const noteId = await exports . parseNoteIdFromSocketAsync ( socket ) as string
@@ -800,13 +802,13 @@ function queueForConnect(socket) {
800802 } )
801803}
802804
803- export function connection ( socket ) {
805+ export function connection ( socket : SocketIO . Socket ) : void {
804806 if ( maintenance ) return
805807 queueForConnect ( socket )
806808}
807809
808810// TODO: test it
809- export function terminate ( ) {
811+ export function terminate ( ) : void {
810812 disconnectProcessQueue . stop ( )
811813 connectProcessQueue . stop ( )
812814 updateDirtyNoteJob . stop ( )
0 commit comments