11//history
22//external modules
33var async = require ( 'async' ) ;
4- var moment = require ( 'moment' ) ;
54
65//core
76var config = require ( "./config.js" ) ;
@@ -14,118 +13,60 @@ var History = {
1413 historyGet : historyGet ,
1514 historyPost : historyPost ,
1615 historyDelete : historyDelete ,
17- isReady : isReady ,
1816 updateHistory : updateHistory
1917} ;
2018
21- var caches = { } ;
22- //update when the history is dirty
23- var updater = setInterval ( function ( ) {
24- var deleted = [ ] ;
25- async . each ( Object . keys ( caches ) , function ( key , callback ) {
26- var cache = caches [ key ] ;
27- if ( cache . isDirty ) {
28- if ( config . debug ) logger . info ( "history updater found dirty history: " + key ) ;
29- var history = parseHistoryToArray ( cache . history ) ;
30- cache . isDirty = false ;
31- finishUpdateHistory ( key , history , function ( err , count ) {
32- if ( err ) return callback ( err , null ) ;
33- if ( ! count ) return callback ( null , null ) ;
34- cache . updateAt = Date . now ( ) ;
35- return callback ( null , null ) ;
36- } ) ;
37- } else {
38- if ( moment ( ) . isAfter ( moment ( cache . updateAt ) . add ( 5 , 'minutes' ) ) ) {
39- deleted . push ( key ) ;
40- }
41- return callback ( null , null ) ;
19+ function getHistory ( userid , callback ) {
20+ models . User . findOne ( {
21+ where : {
22+ id : userid
4223 }
43- } , function ( err ) {
44- if ( err ) return logger . error ( 'history updater error' , err ) ;
24+ } ) . then ( function ( user ) {
25+ if ( ! user )
26+ return callback ( null , null ) ;
27+ var history = { } ;
28+ if ( user . history )
29+ history = parseHistoryToObject ( JSON . parse ( user . history ) ) ;
30+ if ( config . debug )
31+ logger . info ( 'read history success: ' + user . id ) ;
32+ return callback ( null , history ) ;
33+ } ) . catch ( function ( err ) {
34+ logger . error ( 'read history failed: ' + err ) ;
35+ return callback ( err , null ) ;
4536 } ) ;
46- // delete specified caches
47- for ( var i = 0 , l = deleted . length ; i < l ; i ++ ) {
48- caches [ deleted [ i ] ] . history = { } ;
49- delete caches [ deleted [ i ] ] ;
50- }
51- } , 1000 ) ;
37+ }
5238
53- function finishUpdateHistory ( userid , history , callback ) {
39+ function setHistory ( userid , history , callback ) {
5440 models . User . update ( {
55- history : JSON . stringify ( history )
41+ history : JSON . stringify ( parseHistoryToArray ( history ) )
5642 } , {
5743 where : {
5844 id : userid
5945 }
6046 } ) . then ( function ( count ) {
6147 return callback ( null , count ) ;
6248 } ) . catch ( function ( err ) {
49+ logger . error ( 'set history failed: ' + err ) ;
6350 return callback ( err , null ) ;
6451 } ) ;
6552}
6653
67- function isReady ( ) {
68- var dirtyCount = 0 ;
69- async . each ( Object . keys ( caches ) , function ( key , callback ) {
70- if ( caches [ key ] . isDirty ) dirtyCount ++ ;
71- return callback ( null , null ) ;
72- } , function ( err ) {
73- if ( err ) return logger . error ( 'history ready check error' , err ) ;
74- } ) ;
75- return dirtyCount > 0 ? false : true ;
76- }
77-
78- function getHistory ( userid , callback ) {
79- if ( caches [ userid ] ) {
80- return callback ( null , caches [ userid ] . history ) ;
81- } else {
82- models . User . findOne ( {
83- where : {
84- id : userid
85- }
86- } ) . then ( function ( user ) {
87- if ( ! user )
88- return callback ( null , null ) ;
89- var history = [ ] ;
90- if ( user . history )
91- history = JSON . parse ( user . history ) ;
92- if ( config . debug )
93- logger . info ( 'read history success: ' + user . id ) ;
94- setHistory ( userid , history ) ;
95- return callback ( null , history ) ;
96- } ) . catch ( function ( err ) {
97- logger . error ( 'read history failed: ' + err ) ;
98- return callback ( err , null ) ;
99- } ) ;
100- }
101- }
102-
103- function setHistory ( userid , history ) {
104- if ( Array . isArray ( history ) ) history = parseHistoryToObject ( history ) ;
105- if ( ! caches [ userid ] ) {
106- caches [ userid ] = {
107- history : { } ,
108- isDirty : false ,
109- updateAt : Date . now ( )
110- } ;
111- }
112- caches [ userid ] . history = history ;
113- }
114-
115- function updateHistory ( userid , noteId , document ) {
54+ function updateHistory ( userid , noteId , document , time ) {
11655 if ( userid && noteId && typeof document !== 'undefined' ) {
11756 getHistory ( userid , function ( err , history ) {
11857 if ( err || ! history ) return ;
119- if ( ! caches [ userid ] . history [ noteId ] ) {
120- caches [ userid ] . history [ noteId ] = { } ;
58+ if ( ! history [ noteId ] ) {
59+ history [ noteId ] = { } ;
12160 }
122- var noteHistory = caches [ userid ] . history [ noteId ] ;
61+ var noteHistory = history [ noteId ] ;
12362 var noteInfo = models . Note . parseNoteInfo ( document ) ;
12463 noteHistory . id = noteId ;
12564 noteHistory . text = noteInfo . title ;
126- noteHistory . time = moment ( ) . valueOf ( ) ;
65+ noteHistory . time = time || Date . now ( ) ;
12766 noteHistory . tags = noteInfo . tags ;
128- caches [ userid ] . isDirty = true ;
67+ setHistory ( userid , history , function ( err , count ) {
68+ return ;
69+ } ) ;
12970 } ) ;
13071 }
13172}
@@ -175,9 +116,10 @@ function historyPost(req, res) {
175116 return response . errorBadRequest ( res ) ;
176117 }
177118 if ( Array . isArray ( history ) ) {
178- setHistory ( req . user . id , history ) ;
179- caches [ req . user . id ] . isDirty = true ;
180- res . end ( ) ;
119+ setHistory ( req . user . id , history , function ( err , count ) {
120+ if ( err ) return response . errorInternalError ( res ) ;
121+ res . end ( ) ;
122+ } ) ;
181123 } else {
182124 return response . errorBadRequest ( res ) ;
183125 }
@@ -186,11 +128,13 @@ function historyPost(req, res) {
186128 getHistory ( req . user . id , function ( err , history ) {
187129 if ( err ) return response . errorInternalError ( res ) ;
188130 if ( ! history ) return response . errorNotFound ( res ) ;
189- if ( ! caches [ req . user . id ] . history [ noteId ] ) return response . errorNotFound ( res ) ;
131+ if ( ! history [ noteId ] ) return response . errorNotFound ( res ) ;
190132 if ( req . body . pinned === 'true' || req . body . pinned === 'false' ) {
191- caches [ req . user . id ] . history [ noteId ] . pinned = ( req . body . pinned === 'true' ) ;
192- caches [ req . user . id ] . isDirty = true ;
193- res . end ( ) ;
133+ history [ noteId ] . pinned = ( req . body . pinned === 'true' ) ;
134+ setHistory ( req . user . id , history , function ( err , count ) {
135+ if ( err ) return response . errorInternalError ( res ) ;
136+ res . end ( ) ;
137+ } ) ;
194138 } else {
195139 return response . errorBadRequest ( res ) ;
196140 }
@@ -205,16 +149,19 @@ function historyDelete(req, res) {
205149 if ( req . isAuthenticated ( ) ) {
206150 var noteId = req . params . noteId ;
207151 if ( ! noteId ) {
208- setHistory ( req . user . id , [ ] ) ;
209- caches [ req . user . id ] . isDirty = true ;
210- res . end ( ) ;
152+ setHistory ( req . user . id , [ ] , function ( err , count ) {
153+ if ( err ) return response . errorInternalError ( res ) ;
154+ res . end ( ) ;
155+ } ) ;
211156 } else {
212157 getHistory ( req . user . id , function ( err , history ) {
213158 if ( err ) return response . errorInternalError ( res ) ;
214159 if ( ! history ) return response . errorNotFound ( res ) ;
215- delete caches [ req . user . id ] . history [ noteId ] ;
216- caches [ req . user . id ] . isDirty = true ;
217- res . end ( ) ;
160+ delete history [ noteId ] ;
161+ setHistory ( req . user . id , history , function ( err , count ) {
162+ if ( err ) return response . errorInternalError ( res ) ;
163+ res . end ( ) ;
164+ } ) ;
218165 } ) ;
219166 }
220167 } else {
0 commit comments