11// history
22// external modules
33import LZString from '@hackmd/lz-string'
4+ import { Request , Response } from "express" ;
45
56import { Note , User } from '../models'
67import { logger } from '../logger'
78import config from '../config'
89import * as response from '../response'
910
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 ) {
1120 User . findOne ( {
1221 where : {
1322 id : userid
@@ -16,11 +25,11 @@ function getHistory(userid, callback) {
1625 if ( ! user ) {
1726 return callback ( null , null )
1827 }
19- let history : any = [ ]
28+ let history : History [ ] = [ ]
2029 if ( user . history ) {
2130 history = JSON . parse ( user . history )
2231 // 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 ++ ) {
2433 // Calculate minimal string length for an UUID that is encoded
2534 // base64 encoded and optimize comparison by using -1
2635 // this should make a lot of LZ-String parsing errors obsolete
@@ -44,12 +53,11 @@ function getHistory(userid, callback) {
4453 }
4554 }
4655 }
47- history = parseHistoryToObject ( history )
4856 }
4957 if ( config . debug ) {
5058 logger . info ( 'read history success: ' + user . id )
5159 }
52- return callback ( null , history )
60+ return callback ( null , parseHistoryToObject ( history ) )
5361 } ) . catch ( function ( err ) {
5462 logger . error ( 'read history failed: ' + err )
5563 return callback ( err , null )
@@ -71,7 +79,7 @@ function setHistory(userid, history, callback) {
7179 } )
7280}
7381
74- export function updateHistory ( userid , noteId , document ?: string , time ?: any ) {
82+ export function updateHistory ( userid : string , noteId : string , document ?: string , time ?: number ) : void {
7583 if ( userid && noteId && typeof document !== 'undefined' ) {
7684 getHistory ( userid , function ( err , history ) {
7785 if ( err || ! history ) return
@@ -84,7 +92,7 @@ export function updateHistory(userid, noteId, document?: string, time?: any) {
8492 noteHistory . text = noteInfo . title
8593 noteHistory . time = time || Date . now ( )
8694 noteHistory . tags = noteInfo . tags
87- setHistory ( userid , history , function ( err , count ) {
95+ setHistory ( userid , history , function ( err ) {
8896 if ( err ) {
8997 logger . log ( err )
9098 }
@@ -102,7 +110,7 @@ function parseHistoryToArray(history) {
102110 return _history
103111}
104112
105- function parseHistoryToObject ( history ) {
113+ function parseHistoryToObject ( history : History [ ] ) : Record < string , History > {
106114 const _history = { }
107115 for ( let i = 0 , l = history . length ; i < l ; i ++ ) {
108116 const item = history [ i ]
@@ -111,7 +119,7 @@ function parseHistoryToObject(history) {
111119 return _history
112120}
113121
114- export function historyGet ( req , res ) {
122+ export function historyGet ( req : Request , res : Response ) {
115123 if ( req . isAuthenticated ( ) ) {
116124 getHistory ( req . user . id , function ( err , history ) {
117125 if ( err ) return response . errorInternalError ( req , res )
@@ -125,7 +133,7 @@ export function historyGet(req, res) {
125133 }
126134}
127135
128- export function historyPost ( req , res ) {
136+ export function historyPost ( req : Request , res : Response ) {
129137 if ( req . isAuthenticated ( ) ) {
130138 const noteId = req . params . noteId
131139 if ( ! noteId ) {
@@ -140,7 +148,7 @@ export function historyPost(req, res) {
140148 return response . errorBadRequest ( req , res )
141149 }
142150 if ( Array . isArray ( history ) ) {
143- setHistory ( req . user . id , history , function ( err , count ) {
151+ setHistory ( req . user . id , history , function ( err ) {
144152 if ( err ) return response . errorInternalError ( req , res )
145153 res . end ( )
146154 } )
@@ -155,7 +163,7 @@ export function historyPost(req, res) {
155163 if ( ! history [ noteId ] ) return response . errorNotFound ( req , res )
156164 if ( req . body . pinned === 'true' || req . body . pinned === 'false' ) {
157165 history [ noteId ] . pinned = ( req . body . pinned === 'true' )
158- setHistory ( req . user . id , history , function ( err , count ) {
166+ setHistory ( req . user . id , history , function ( err ) {
159167 if ( err ) return response . errorInternalError ( req , res )
160168 res . end ( )
161169 } )
@@ -169,11 +177,11 @@ export function historyPost(req, res) {
169177 }
170178}
171179
172- export function historyDelete ( req , res ) {
180+ export function historyDelete ( req : Request , res : Response ) {
173181 if ( req . isAuthenticated ( ) ) {
174182 const noteId = req . params . noteId
175183 if ( ! noteId ) {
176- setHistory ( req . user . id , [ ] , function ( err , count ) {
184+ setHistory ( req . user . id , [ ] , function ( err ) {
177185 if ( err ) return response . errorInternalError ( req , res )
178186 res . end ( )
179187 } )
@@ -182,7 +190,7 @@ export function historyDelete(req, res) {
182190 if ( err ) return response . errorInternalError ( req , res )
183191 if ( ! history ) return response . errorNotFound ( req , res )
184192 delete history [ noteId ]
185- setHistory ( req . user . id , history , function ( err , count ) {
193+ setHistory ( req . user . id , history , function ( err ) {
186194 if ( err ) return response . errorInternalError ( req , res )
187195 res . end ( )
188196 } )
0 commit comments