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