22// external modules
33import LZString from '@hackmd/lz-string'
44
5- import * as models from '../models'
5+ import { Note , User } from '../models'
66import { logger } from '../logger'
77import config from '../config'
88import * as response from '../response'
99
10- function getHistory ( userid , callback ) {
11- models . User . findOne ( {
10+ function getHistory ( userid , callback ) {
11+ User . findOne ( {
1212 where : {
1313 id : userid
1414 }
@@ -32,8 +32,8 @@ function getHistory (userid, callback) {
3232 }
3333 try {
3434 const id = LZString . decompressFromBase64 ( history [ i ] . id )
35- if ( id && models . Note . checkNoteIdValid ( id ) ) {
36- history [ i ] . id = models . Note . encodeNoteId ( id )
35+ if ( id && Note . checkNoteIdValid ( id ) ) {
36+ history [ i ] . id = Note . encodeNoteId ( id )
3737 }
3838 } catch ( err ) {
3939 // most error here comes from LZString, ignore
@@ -56,8 +56,8 @@ function getHistory (userid, callback) {
5656 } )
5757}
5858
59- function setHistory ( userid , history , callback ) {
60- models . User . update ( {
59+ function setHistory ( userid , history , callback ) {
60+ User . update ( {
6161 history : JSON . stringify ( parseHistoryToArray ( history ) )
6262 } , {
6363 where : {
@@ -71,15 +71,15 @@ function setHistory (userid, history, callback) {
7171 } )
7272}
7373
74- export function updateHistory ( userid , noteId , document ?: string , time ?: any ) {
74+ export function updateHistory ( userid , noteId , document ?: string , time ?: any ) {
7575 if ( userid && noteId && typeof document !== 'undefined' ) {
7676 getHistory ( userid , function ( err , history ) {
7777 if ( err || ! history ) return
7878 if ( ! history [ noteId ] ) {
7979 history [ noteId ] = { }
8080 }
8181 const noteHistory = history [ noteId ]
82- const noteInfo = models . Note . parseNoteInfo ( document )
82+ const noteInfo = Note . parseNoteInfo ( document )
8383 noteHistory . id = noteId
8484 noteHistory . text = noteInfo . title
8585 noteHistory . time = time || Date . now ( )
@@ -93,7 +93,7 @@ export function updateHistory (userid, noteId, document?: string, time?: any) {
9393 }
9494}
9595
96- function parseHistoryToArray ( history ) {
96+ function parseHistoryToArray ( history ) {
9797 const _history = [ ]
9898 Object . keys ( history ) . forEach ( function ( key ) {
9999 const item = history [ key ]
@@ -102,7 +102,7 @@ function parseHistoryToArray (history) {
102102 return _history
103103}
104104
105- function parseHistoryToObject ( history ) {
105+ function parseHistoryToObject ( history ) {
106106 const _history = { }
107107 for ( let i = 0 , l = history . length ; i < l ; i ++ ) {
108108 const item = history [ i ]
@@ -111,7 +111,7 @@ function parseHistoryToObject (history) {
111111 return _history
112112}
113113
114- export function historyGet ( req , res ) {
114+ export function historyGet ( req , res ) {
115115 if ( req . isAuthenticated ( ) ) {
116116 getHistory ( req . user . id , function ( err , history ) {
117117 if ( err ) return response . errorInternalError ( req , res )
@@ -125,12 +125,14 @@ export function historyGet (req, res) {
125125 }
126126}
127127
128- export function historyPost ( req , res ) {
128+ export function historyPost ( req , res ) {
129129 if ( req . isAuthenticated ( ) ) {
130130 const noteId = req . params . noteId
131131 if ( ! noteId ) {
132132 if ( typeof req . body . history === 'undefined' ) return response . errorBadRequest ( req , res )
133- if ( config . debug ) { logger . info ( 'SERVER received history from [' + req . user . id + ']: ' + req . body . history ) }
133+ if ( config . debug ) {
134+ logger . info ( 'SERVER received history from [' + req . user . id + ']: ' + req . body . history )
135+ }
134136 let history = null
135137 try {
136138 history = JSON . parse ( req . body . history )
@@ -167,7 +169,7 @@ export function historyPost (req, res) {
167169 }
168170}
169171
170- export function historyDelete ( req , res ) {
172+ export function historyDelete ( req , res ) {
171173 if ( req . isAuthenticated ( ) ) {
172174 const noteId = req . params . noteId
173175 if ( ! noteId ) {
0 commit comments