1
1
// history
2
2
// external modules
3
3
import LZString from '@hackmd/lz-string'
4
+ import { Request , Response } from "express" ;
4
5
5
6
import { Note , User } from '../models'
6
7
import { logger } from '../logger'
7
8
import config from '../config'
8
9
import * as response from '../response'
9
10
10
- function getHistory ( userid , callback ) {
11
+ interface History {
12
+ id ?: string
13
+ text ?: string
14
+ time ?: Date | number
15
+ tags ?: string [ ]
16
+ pinned ?: boolean
17
+ }
18
+
19
+ function getHistory ( userid : string , callback : ( err : Error | null , history : Record < string , History > ) => void ) {
11
20
User . findOne ( {
12
21
where : {
13
22
id : userid
@@ -16,11 +25,11 @@ function getHistory(userid, callback) {
16
25
if ( ! user ) {
17
26
return callback ( null , null )
18
27
}
19
- let history : any = [ ]
28
+ let history : History [ ] = [ ]
20
29
if ( user . history ) {
21
30
history = JSON . parse ( user . history )
22
31
// migrate LZString encoded note id to base64url encoded note id
23
- for ( let i = 0 , l = ( history as [ ] ) . length ; i < l ; i ++ ) {
32
+ for ( let i = 0 , l = history . length ; i < l ; i ++ ) {
24
33
// Calculate minimal string length for an UUID that is encoded
25
34
// base64 encoded and optimize comparison by using -1
26
35
// this should make a lot of LZ-String parsing errors obsolete
@@ -44,12 +53,11 @@ function getHistory(userid, callback) {
44
53
}
45
54
}
46
55
}
47
- history = parseHistoryToObject ( history )
48
56
}
49
57
if ( config . debug ) {
50
58
logger . info ( 'read history success: ' + user . id )
51
59
}
52
- return callback ( null , history )
60
+ return callback ( null , parseHistoryToObject ( history ) )
53
61
} ) . catch ( function ( err ) {
54
62
logger . error ( 'read history failed: ' + err )
55
63
return callback ( err , null )
@@ -71,7 +79,7 @@ function setHistory(userid, history, callback) {
71
79
} )
72
80
}
73
81
74
- export function updateHistory ( userid , noteId , document ?: string , time ?: any ) {
82
+ export function updateHistory ( userid : string , noteId : string , document ?: string , time ?: number ) : void {
75
83
if ( userid && noteId && typeof document !== 'undefined' ) {
76
84
getHistory ( userid , function ( err , history ) {
77
85
if ( err || ! history ) return
@@ -84,7 +92,7 @@ export function updateHistory(userid, noteId, document?: string, time?: any) {
84
92
noteHistory . text = noteInfo . title
85
93
noteHistory . time = time || Date . now ( )
86
94
noteHistory . tags = noteInfo . tags
87
- setHistory ( userid , history , function ( err , count ) {
95
+ setHistory ( userid , history , function ( err ) {
88
96
if ( err ) {
89
97
logger . log ( err )
90
98
}
@@ -102,7 +110,7 @@ function parseHistoryToArray(history) {
102
110
return _history
103
111
}
104
112
105
- function parseHistoryToObject ( history ) {
113
+ function parseHistoryToObject ( history : History [ ] ) : Record < string , History > {
106
114
const _history = { }
107
115
for ( let i = 0 , l = history . length ; i < l ; i ++ ) {
108
116
const item = history [ i ]
@@ -111,7 +119,7 @@ function parseHistoryToObject(history) {
111
119
return _history
112
120
}
113
121
114
- export function historyGet ( req , res ) {
122
+ export function historyGet ( req : Request , res : Response ) {
115
123
if ( req . isAuthenticated ( ) ) {
116
124
getHistory ( req . user . id , function ( err , history ) {
117
125
if ( err ) return response . errorInternalError ( req , res )
@@ -125,7 +133,7 @@ export function historyGet(req, res) {
125
133
}
126
134
}
127
135
128
- export function historyPost ( req , res ) {
136
+ export function historyPost ( req : Request , res : Response ) {
129
137
if ( req . isAuthenticated ( ) ) {
130
138
const noteId = req . params . noteId
131
139
if ( ! noteId ) {
@@ -140,7 +148,7 @@ export function historyPost(req, res) {
140
148
return response . errorBadRequest ( req , res )
141
149
}
142
150
if ( Array . isArray ( history ) ) {
143
- setHistory ( req . user . id , history , function ( err , count ) {
151
+ setHistory ( req . user . id , history , function ( err ) {
144
152
if ( err ) return response . errorInternalError ( req , res )
145
153
res . end ( )
146
154
} )
@@ -155,7 +163,7 @@ export function historyPost(req, res) {
155
163
if ( ! history [ noteId ] ) return response . errorNotFound ( req , res )
156
164
if ( req . body . pinned === 'true' || req . body . pinned === 'false' ) {
157
165
history [ noteId ] . pinned = ( req . body . pinned === 'true' )
158
- setHistory ( req . user . id , history , function ( err , count ) {
166
+ setHistory ( req . user . id , history , function ( err ) {
159
167
if ( err ) return response . errorInternalError ( req , res )
160
168
res . end ( )
161
169
} )
@@ -169,11 +177,11 @@ export function historyPost(req, res) {
169
177
}
170
178
}
171
179
172
- export function historyDelete ( req , res ) {
180
+ export function historyDelete ( req : Request , res : Response ) {
173
181
if ( req . isAuthenticated ( ) ) {
174
182
const noteId = req . params . noteId
175
183
if ( ! noteId ) {
176
- setHistory ( req . user . id , [ ] , function ( err , count ) {
184
+ setHistory ( req . user . id , [ ] , function ( err ) {
177
185
if ( err ) return response . errorInternalError ( req , res )
178
186
res . end ( )
179
187
} )
@@ -182,7 +190,7 @@ export function historyDelete(req, res) {
182
190
if ( err ) return response . errorInternalError ( req , res )
183
191
if ( ! history ) return response . errorNotFound ( req , res )
184
192
delete history [ noteId ]
185
- setHistory ( req . user . id , history , function ( err , count ) {
193
+ setHistory ( req . user . id , history , function ( err ) {
186
194
if ( err ) return response . errorInternalError ( req , res )
187
195
res . end ( )
188
196
} )
0 commit comments