@@ -14,12 +14,13 @@ import config from "../config";
1414import { logger } from "../logger" ;
1515import * as history from "../history" ;
1616import { Author , Note , User } from "../models" ;
17+ import { UserProfile } from "../models/baseModel" ;
1718
1819// ot
1920import ot from "ot" ;
2021
2122import { ProcessQueue } from "./processQueue" ;
22- import { RealtimeClientConnection } from "./realtimeClientConnection" ;
23+ import { CursorData , RealtimeClientConnection } from "./realtimeClientConnection" ;
2324import { UpdateDirtyNoteJob } from "./realtimeUpdateDirtyNoteJob" ;
2425import { CleanDanglingUserJob } from "./realtimeCleanDanglingUserJob" ;
2526import { SaveRevisionJob } from "./realtimeSaveRevisionJob" ;
@@ -40,6 +41,42 @@ export interface RealtimeUserData {
4041 idle ?: any
4142 type ?: any
4243}
44+
45+ interface RealtimeAuthorData {
46+ userid : string
47+ color : string
48+ photo : string
49+ name : string
50+ }
51+
52+ export interface RealtimeNoteData {
53+ id : string ,
54+ alias ?: string ,
55+ title ?: string ,
56+ // owner id
57+ owner ?: string ,
58+ ownerprofile ?: UserProfile
59+ permission ?: string
60+ // last change user id
61+ lastchangeuser ?: string
62+ lastchangeuserprofile ?: UserProfile
63+
64+ socks : SocketIO . Socket [ ]
65+ users : Record < string , RealtimeUserData >
66+ //???
67+ tempUsers : any
68+
69+ createtime : number
70+ updatetime : number
71+
72+ // type: ot.EditorSocketIOServer
73+ server : any
74+
75+ authors : Record < string , RealtimeAuthorData >
76+ authorship : string
77+ }
78+
79+
4380const chance = new Chance ( )
4481
4582export let io : SocketIO . Server = null
@@ -98,7 +135,7 @@ export function secure(socket: SocketIO.Socket, next: (err?: Error | null) => vo
98135
99136// TODO: only use in `updateDirtyNote`
100137// TODO: test it
101- export function emitCheck ( note ) {
138+ export function emitCheck ( note : RealtimeNoteData ) : void {
102139 const out = {
103140 title : note . title ,
104141 updatetime : note . updatetime ,
@@ -111,18 +148,18 @@ export function emitCheck(note) {
111148}
112149
113150// actions
114- export const notes = { }
115151export const users : Record < string , RealtimeUserData > = { }
152+ export const notes : Record < string , RealtimeNoteData > = { }
116153
117- export function getNotePool ( ) : any {
154+ export function getNotePool ( ) : Record < string , RealtimeNoteData > {
118155 return notes
119156}
120157
121158export function isNoteExistsInPool ( noteId : string ) : boolean {
122159 return ! ! notes [ noteId ]
123160}
124161
125- export function addNote ( note ) {
162+ export function addNote ( note : RealtimeNoteData ) : boolean {
126163 if ( exports . isNoteExistsInPool ( note . id ) ) return false
127164 notes [ note . id ] = note
128165 return true
@@ -142,7 +179,7 @@ export function deleteAllNoteFromPool(): void {
142179 } )
143180}
144181
145- export function getNoteFromNotePool ( noteId ) {
182+ export function getNoteFromNotePool ( noteId : string ) : RealtimeNoteData | null {
146183 return notes [ noteId ]
147184}
148185
@@ -159,7 +196,7 @@ updateDirtyNoteJob.start()
159196cleanDanglingUserJob . start ( )
160197saveRevisionJob . start ( )
161198
162- export function disconnectSocketOnNote ( note ) {
199+ export function disconnectSocketOnNote ( note : RealtimeNoteData ) : void {
163200 note . socks . forEach ( ( sock ) => {
164201 if ( sock ) {
165202 sock . emit ( 'delete' )
@@ -170,7 +207,7 @@ export function disconnectSocketOnNote(note) {
170207 } )
171208}
172209
173- export function updateNote ( note , callback ) {
210+ export function updateNote ( note : RealtimeNoteData , callback : ( err : Error | null , note : Note ) => void ) : void {
174211 _updateNoteAsync ( note ) . then ( _note => {
175212 callback ( null , _note )
176213 } ) . catch ( ( err ) => {
@@ -179,15 +216,15 @@ export function updateNote(note, callback) {
179216 } )
180217}
181218
182- function findNoteByIdAsync ( id ) {
219+ function findNoteByIdAsync ( id : string ) : Promise < Note > {
183220 return Note . findOne ( {
184221 where : {
185222 id : id
186223 }
187224 } )
188225}
189226
190- function updateHistoryForEveryUserCollaborateNote ( note ) {
227+ function updateHistoryForEveryUserCollaborateNote ( note : RealtimeNoteData ) : void {
191228 // update history to every user in this note
192229 const tempUsers = Object . assign ( { } , note . tempUsers )
193230 note . tempUsers = { }
@@ -197,7 +234,7 @@ function updateHistoryForEveryUserCollaborateNote(note) {
197234 } )
198235}
199236
200- async function getUserProfileByIdAsync ( id ) {
237+ async function getUserProfileByIdAsync ( id : string ) : Promise < UserProfile > {
201238 const user = await User . findOne ( {
202239 where : {
203240 id : id
@@ -237,7 +274,7 @@ function buildNoteUpdateData(note) {
237274 }
238275}
239276
240- async function _updateNoteAsync ( note ) {
277+ async function _updateNoteAsync ( note : RealtimeNoteData ) {
241278 let noteModel = await findNoteByIdAsync ( note . id )
242279 if ( ! noteModel ) return null
243280
@@ -247,7 +284,7 @@ async function _updateNoteAsync(note) {
247284 note . lastchangeuserprofile = await getLastChangeUserProfileAsync (
248285 note . lastchangeuser ,
249286 noteModel . lastchangeuserId ,
250- noteModel . lastchangeuserprofile
287+ note . lastchangeuser
251288 )
252289 } catch ( err ) {
253290 if ( err instanceof UserNotFoundException ) {
@@ -262,8 +299,22 @@ async function _updateNoteAsync(note) {
262299 return noteModel
263300}
264301
302+ interface StatusData {
303+ onlineNotes : number
304+ onlineUsers : number
305+ distinctOnlineUsers : number
306+ notesCount : number
307+ registeredUsers : number
308+ onlineRegisteredUsers : number
309+ distinctOnlineRegisteredUsers : number
310+ isConnectionBusy : boolean
311+ connectionSocketQueueLength : number
312+ isDisconnectBusy : boolean
313+ disconnectSocketQueueLength : number
314+ }
315+
265316// TODO: test it
266- export function getStatus ( ) {
317+ export function getStatus ( ) : Promise < StatusData > {
267318 return Note . count ( )
268319 . then ( function ( notecount : number ) {
269320 const distinctaddresses = [ ]
@@ -402,7 +453,7 @@ export const parseNoteIdFromSocketAsync = async function (socket: SocketIO.Socke
402453export function emitOnlineUsers ( socket : SocketIO . Socket ) : void {
403454 const noteId = socket . noteId
404455 if ( ! noteId || ! notes [ noteId ] ) return
405- const users = [ ]
456+ const users : RealtimeClientUserData [ ] = [ ]
406457 Object . keys ( notes [ noteId ] . users ) . forEach ( function ( key ) {
407458 const user = notes [ noteId ] . users [ key ]
408459 if ( user ) {
@@ -486,10 +537,10 @@ async function fetchFullNoteAsync(noteId: string): Promise<Note> {
486537 } )
487538}
488539
489- function buildAuthorProfilesFromNote ( noteAuthors ) {
490- const authors = { }
540+ function buildAuthorProfilesFromNote ( noteAuthors : Author [ ] ) : Record < string , RealtimeAuthorData > {
541+ const authors : Record < string , RealtimeAuthorData > = { }
491542 noteAuthors . forEach ( ( author ) => {
492- const profile = User . getProfile ( author . user )
543+ const profile = User . getProfile ( author . user as User )
493544 if ( profile ) {
494545 authors [ author . userId ] = {
495546 userid : author . userId ,
@@ -584,7 +635,19 @@ export function queueForDisconnect(socket: SocketIO.Socket): void {
584635 } )
585636}
586637
587- export function buildUserOutData ( user ) {
638+ interface RealtimeClientUserData {
639+ id ?: string
640+ login ?: boolean
641+ userid ?: string
642+ photo ?: string
643+ color ?: string
644+ cursor ?: CursorData
645+ name ?: string
646+ idle ?: boolean
647+ type ?: string
648+ }
649+
650+ export function buildUserOutData ( user : RealtimeUserData ) : RealtimeClientUserData {
588651 const out = {
589652 id : user . id ,
590653 login : user . login ,
0 commit comments