@@ -92,20 +92,22 @@ module.exports = function (sequelize, DataTypes) {
92
92
return new Promise ( function ( resolve , reject ) {
93
93
// if no content specified then use default note
94
94
if ( ! note . content ) {
95
- var body = null
96
- let filePath = null
97
- if ( ! note . alias ) {
98
- filePath = config . defaultNotePath
99
- } else {
100
- filePath = path . join ( config . docsPath , note . alias + '.md' )
95
+ let filePath = config . defaultNotePath
96
+
97
+ if ( note . alias ) {
98
+ const notePathInDocPath = path . join ( config . docsPath , path . basename ( note . alias ) + '.md' )
99
+ if ( Note . checkFileExist ( notePathInDocPath ) ) {
100
+ filePath = notePathInDocPath
101
+ }
101
102
}
103
+
102
104
if ( Note . checkFileExist ( filePath ) ) {
103
- var fsCreatedTime = moment ( fs . statSync ( filePath ) . ctime )
104
- body = fs . readFileSync ( filePath , 'utf8' )
105
- note . title = Note . parseNoteTitle ( body )
106
- note . content = body
105
+ const noteInFS = readFileSystemNote ( filePath )
106
+ note . title = noteInFS . title
107
+ note . content = noteInFS . content
107
108
if ( filePath !== config . defaultNotePath ) {
108
- note . createdAt = fsCreatedTime
109
+ note . createdAt = noteInFS . lastchangeAt
110
+ note . lastchangeAt = noteInFS . lastchangeAt
109
111
}
110
112
}
111
113
}
@@ -196,6 +198,29 @@ module.exports = function (sequelize, DataTypes) {
196
198
} )
197
199
} )
198
200
}
201
+
202
+ async function syncNote ( noteInFS , note ) {
203
+ const contentLength = noteInFS . content . length
204
+
205
+ let note2 = await note . update ( {
206
+ title : noteInFS . title ,
207
+ content : noteInFS . content ,
208
+ lastchangeAt : noteInFS . lastchangeAt
209
+ } )
210
+ const revision = await sequelize . models . Revision . saveNoteRevisionAsync ( note2 )
211
+ // update authorship on after making revision of docs
212
+ const patch = dmp . patch_fromText ( revision . patch )
213
+ const operations = Note . transformPatchToOperations ( patch , contentLength )
214
+ let authorship = note2 . authorship
215
+ for ( let i = 0 ; i < operations . length ; i ++ ) {
216
+ authorship = Note . updateAuthorshipByOperation ( operations [ i ] , null , authorship )
217
+ }
218
+ note2 = await note . update ( {
219
+ authorship : authorship
220
+ } )
221
+ return note2 . id
222
+ }
223
+
199
224
Note . parseNoteId = function ( noteId , callback ) {
200
225
async . series ( {
201
226
parseNoteIdByAlias : function ( _callback ) {
@@ -204,65 +229,35 @@ module.exports = function (sequelize, DataTypes) {
204
229
where : {
205
230
alias : noteId
206
231
}
207
- } ) . then ( function ( note ) {
208
- if ( note ) {
209
- const filePath = path . join ( config . docsPath , noteId + '.md' )
210
- if ( Note . checkFileExist ( filePath ) ) {
211
- // if doc in filesystem have newer modified time than last change time
212
- // then will update the doc in db
213
- var fsModifiedTime = moment ( fs . statSync ( filePath ) . mtime )
214
- var dbModifiedTime = moment ( note . lastchangeAt || note . createdAt )
215
- var body = fs . readFileSync ( filePath , 'utf8' )
216
- var contentLength = body . length
217
- var title = Note . parseNoteTitle ( body )
218
- if ( fsModifiedTime . isAfter ( dbModifiedTime ) && note . content !== body ) {
219
- note . update ( {
220
- title : title ,
221
- content : body ,
222
- lastchangeAt : fsModifiedTime
223
- } ) . then ( function ( note ) {
224
- sequelize . models . Revision . saveNoteRevision ( note , function ( err , revision ) {
225
- if ( err ) return _callback ( err , null )
226
- // update authorship on after making revision of docs
227
- var patch = dmp . patch_fromText ( revision . patch )
228
- var operations = Note . transformPatchToOperations ( patch , contentLength )
229
- var authorship = note . authorship
230
- for ( let i = 0 ; i < operations . length ; i ++ ) {
231
- authorship = Note . updateAuthorshipByOperation ( operations [ i ] , null , authorship )
232
- }
233
- note . update ( {
234
- authorship : authorship
235
- } ) . then ( function ( note ) {
236
- return callback ( null , note . id )
237
- } ) . catch ( function ( err ) {
238
- return _callback ( err , null )
239
- } )
240
- } )
241
- } ) . catch ( function ( err ) {
242
- return _callback ( err , null )
243
- } )
232
+ } ) . then ( async function ( note ) {
233
+ const filePath = path . join ( config . docsPath , path . basename ( noteId ) + '.md' )
234
+ if ( Note . checkFileExist ( filePath ) ) {
235
+ try {
236
+ if ( note ) {
237
+ // if doc in filesystem have newer modified time than last change time
238
+ // then will update the doc in db
239
+ const noteInFS = readFileSystemNote ( filePath )
240
+ if ( shouldSyncNote ( note , noteInFS ) ) {
241
+ const noteId = await syncNote ( noteInFS , note )
242
+ return callback ( null , noteId )
243
+ }
244
244
} else {
245
+ // create new note with alias, and will sync md file in beforeCreateHook
246
+ const note = await Note . create ( {
247
+ alias : noteId ,
248
+ owner : null ,
249
+ permission : 'locked'
250
+ } )
245
251
return callback ( null , note . id )
246
252
}
247
- } else {
248
- return callback ( null , note . id )
249
- }
250
- } else {
251
- var filePath = path . join ( config . docsPath , noteId + '.md' )
252
- if ( Note . checkFileExist ( filePath ) ) {
253
- Note . create ( {
254
- alias : noteId ,
255
- owner : null ,
256
- permission : 'locked'
257
- } ) . then ( function ( note ) {
258
- return callback ( null , note . id )
259
- } ) . catch ( function ( err ) {
260
- return _callback ( err , null )
261
- } )
262
- } else {
263
- return _callback ( null , null )
253
+ } catch ( err ) {
254
+ return _callback ( err , null )
264
255
}
265
256
}
257
+ if ( ! note ) {
258
+ return _callback ( null , null )
259
+ }
260
+ return callback ( null , note . id )
266
261
} ) . catch ( function ( err ) {
267
262
return _callback ( err , null )
268
263
} )
@@ -589,5 +584,21 @@ module.exports = function (sequelize, DataTypes) {
589
584
return operations
590
585
}
591
586
587
+ function readFileSystemNote ( filePath ) {
588
+ const fsModifiedTime = moment ( fs . statSync ( filePath ) . mtime )
589
+ const content = fs . readFileSync ( filePath , 'utf8' )
590
+
591
+ return {
592
+ lastchangeAt : fsModifiedTime ,
593
+ title : Note . parseNoteTitle ( content ) ,
594
+ content : content
595
+ }
596
+ }
597
+
598
+ function shouldSyncNote ( note , noteInFS ) {
599
+ const dbModifiedTime = moment ( note . lastchangeAt || note . createdAt )
600
+ return noteInFS . lastchangeAt . isAfter ( dbModifiedTime ) && note . content !== noteInFS . content
601
+ }
602
+
592
603
return Note
593
604
}
0 commit comments