@@ -14,12 +14,13 @@ import config from "../config";
14
14
import { logger } from "../logger" ;
15
15
import * as history from "../history" ;
16
16
import { Author , Note , User } from "../models" ;
17
+ import { UserProfile } from "../models/baseModel" ;
17
18
18
19
// ot
19
20
import ot from "ot" ;
20
21
21
22
import { ProcessQueue } from "./processQueue" ;
22
- import { RealtimeClientConnection } from "./realtimeClientConnection" ;
23
+ import { CursorData , RealtimeClientConnection } from "./realtimeClientConnection" ;
23
24
import { UpdateDirtyNoteJob } from "./realtimeUpdateDirtyNoteJob" ;
24
25
import { CleanDanglingUserJob } from "./realtimeCleanDanglingUserJob" ;
25
26
import { SaveRevisionJob } from "./realtimeSaveRevisionJob" ;
@@ -40,6 +41,42 @@ export interface RealtimeUserData {
40
41
idle ?: any
41
42
type ?: any
42
43
}
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
+
43
80
const chance = new Chance ( )
44
81
45
82
export let io : SocketIO . Server = null
@@ -98,7 +135,7 @@ export function secure(socket: SocketIO.Socket, next: (err?: Error | null) => vo
98
135
99
136
// TODO: only use in `updateDirtyNote`
100
137
// TODO: test it
101
- export function emitCheck ( note ) {
138
+ export function emitCheck ( note : RealtimeNoteData ) : void {
102
139
const out = {
103
140
title : note . title ,
104
141
updatetime : note . updatetime ,
@@ -111,18 +148,18 @@ export function emitCheck(note) {
111
148
}
112
149
113
150
// actions
114
- export const notes = { }
115
151
export const users : Record < string , RealtimeUserData > = { }
152
+ export const notes : Record < string , RealtimeNoteData > = { }
116
153
117
- export function getNotePool ( ) : any {
154
+ export function getNotePool ( ) : Record < string , RealtimeNoteData > {
118
155
return notes
119
156
}
120
157
121
158
export function isNoteExistsInPool ( noteId : string ) : boolean {
122
159
return ! ! notes [ noteId ]
123
160
}
124
161
125
- export function addNote ( note ) {
162
+ export function addNote ( note : RealtimeNoteData ) : boolean {
126
163
if ( exports . isNoteExistsInPool ( note . id ) ) return false
127
164
notes [ note . id ] = note
128
165
return true
@@ -142,7 +179,7 @@ export function deleteAllNoteFromPool(): void {
142
179
} )
143
180
}
144
181
145
- export function getNoteFromNotePool ( noteId ) {
182
+ export function getNoteFromNotePool ( noteId : string ) : RealtimeNoteData | null {
146
183
return notes [ noteId ]
147
184
}
148
185
@@ -159,7 +196,7 @@ updateDirtyNoteJob.start()
159
196
cleanDanglingUserJob . start ( )
160
197
saveRevisionJob . start ( )
161
198
162
- export function disconnectSocketOnNote ( note ) {
199
+ export function disconnectSocketOnNote ( note : RealtimeNoteData ) : void {
163
200
note . socks . forEach ( ( sock ) => {
164
201
if ( sock ) {
165
202
sock . emit ( 'delete' )
@@ -170,7 +207,7 @@ export function disconnectSocketOnNote(note) {
170
207
} )
171
208
}
172
209
173
- export function updateNote ( note , callback ) {
210
+ export function updateNote ( note : RealtimeNoteData , callback : ( err : Error | null , note : Note ) => void ) : void {
174
211
_updateNoteAsync ( note ) . then ( _note => {
175
212
callback ( null , _note )
176
213
} ) . catch ( ( err ) => {
@@ -179,15 +216,15 @@ export function updateNote(note, callback) {
179
216
} )
180
217
}
181
218
182
- function findNoteByIdAsync ( id ) {
219
+ function findNoteByIdAsync ( id : string ) : Promise < Note > {
183
220
return Note . findOne ( {
184
221
where : {
185
222
id : id
186
223
}
187
224
} )
188
225
}
189
226
190
- function updateHistoryForEveryUserCollaborateNote ( note ) {
227
+ function updateHistoryForEveryUserCollaborateNote ( note : RealtimeNoteData ) : void {
191
228
// update history to every user in this note
192
229
const tempUsers = Object . assign ( { } , note . tempUsers )
193
230
note . tempUsers = { }
@@ -197,7 +234,7 @@ function updateHistoryForEveryUserCollaborateNote(note) {
197
234
} )
198
235
}
199
236
200
- async function getUserProfileByIdAsync ( id ) {
237
+ async function getUserProfileByIdAsync ( id : string ) : Promise < UserProfile > {
201
238
const user = await User . findOne ( {
202
239
where : {
203
240
id : id
@@ -237,7 +274,7 @@ function buildNoteUpdateData(note) {
237
274
}
238
275
}
239
276
240
- async function _updateNoteAsync ( note ) {
277
+ async function _updateNoteAsync ( note : RealtimeNoteData ) {
241
278
let noteModel = await findNoteByIdAsync ( note . id )
242
279
if ( ! noteModel ) return null
243
280
@@ -247,7 +284,7 @@ async function _updateNoteAsync(note) {
247
284
note . lastchangeuserprofile = await getLastChangeUserProfileAsync (
248
285
note . lastchangeuser ,
249
286
noteModel . lastchangeuserId ,
250
- noteModel . lastchangeuserprofile
287
+ note . lastchangeuser
251
288
)
252
289
} catch ( err ) {
253
290
if ( err instanceof UserNotFoundException ) {
@@ -262,8 +299,22 @@ async function _updateNoteAsync(note) {
262
299
return noteModel
263
300
}
264
301
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
+
265
316
// TODO: test it
266
- export function getStatus ( ) {
317
+ export function getStatus ( ) : Promise < StatusData > {
267
318
return Note . count ( )
268
319
. then ( function ( notecount : number ) {
269
320
const distinctaddresses = [ ]
@@ -402,7 +453,7 @@ export const parseNoteIdFromSocketAsync = async function (socket: SocketIO.Socke
402
453
export function emitOnlineUsers ( socket : SocketIO . Socket ) : void {
403
454
const noteId = socket . noteId
404
455
if ( ! noteId || ! notes [ noteId ] ) return
405
- const users = [ ]
456
+ const users : RealtimeClientUserData [ ] = [ ]
406
457
Object . keys ( notes [ noteId ] . users ) . forEach ( function ( key ) {
407
458
const user = notes [ noteId ] . users [ key ]
408
459
if ( user ) {
@@ -486,10 +537,10 @@ async function fetchFullNoteAsync(noteId: string): Promise<Note> {
486
537
} )
487
538
}
488
539
489
- function buildAuthorProfilesFromNote ( noteAuthors ) {
490
- const authors = { }
540
+ function buildAuthorProfilesFromNote ( noteAuthors : Author [ ] ) : Record < string , RealtimeAuthorData > {
541
+ const authors : Record < string , RealtimeAuthorData > = { }
491
542
noteAuthors . forEach ( ( author ) => {
492
- const profile = User . getProfile ( author . user )
543
+ const profile = User . getProfile ( author . user as User )
493
544
if ( profile ) {
494
545
authors [ author . userId ] = {
495
546
userid : author . userId ,
@@ -584,7 +635,19 @@ export function queueForDisconnect(socket: SocketIO.Socket): void {
584
635
} )
585
636
}
586
637
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 {
588
651
const out = {
589
652
id : user . id ,
590
653
login : user . login ,
0 commit comments