Skip to content

Commit 20b10b7

Browse files
committed
refactor: beforeCreate and parseNoteId
Signed-off-by: Raccoon <[email protected]>
1 parent d3bbdfc commit 20b10b7

File tree

2 files changed

+78
-64
lines changed

2 files changed

+78
-64
lines changed

lib/models/note.js

Lines changed: 76 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,22 @@ module.exports = function (sequelize, DataTypes) {
9393
// if no content specified then use default note
9494
if (!note.content) {
9595
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+
}
101103
}
104+
102105
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
107109
if (filePath !== config.defaultNotePath) {
108-
note.createdAt = fsCreatedTime
110+
note.createdAt = noteInFS.lastchangeAt
111+
note.lastchangeAt = noteInFS.lastchangeAt
109112
}
110113
}
111114
}
@@ -196,6 +199,29 @@ module.exports = function (sequelize, DataTypes) {
196199
})
197200
})
198201
}
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+
199225
Note.parseNoteId = function (noteId, callback) {
200226
async.series({
201227
parseNoteIdByAlias: function (_callback) {
@@ -204,65 +230,35 @@ module.exports = function (sequelize, DataTypes) {
204230
where: {
205231
alias: noteId
206232
}
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+
}
244245
} 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+
})
245252
return callback(null, note.id)
246253
}
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)
264256
}
265257
}
258+
if (!note) {
259+
return callback(null, null)
260+
}
261+
return callback(null, note.id)
266262
}).catch(function (err) {
267263
return _callback(err, null)
268264
})
@@ -589,5 +585,21 @@ module.exports = function (sequelize, DataTypes) {
589585
return operations
590586
}
591587

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+
592604
return Note
593605
}

lib/models/revision.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var moment = require('moment')
66
var childProcess = require('child_process')
77
var shortId = require('shortid')
88
var path = require('path')
9+
var util = require('util')
910

1011
var Op = Sequelize.Op
1112

@@ -296,6 +297,7 @@ module.exports = function (sequelize, DataTypes) {
296297
return callback(err, null)
297298
})
298299
}
300+
Revision.saveNoteRevisionAsync = util.promisify(Revision.saveNoteRevision)
299301
Revision.finishSaveNoteRevision = function (note, revision, callback) {
300302
note.update({
301303
savedAt: revision.updatedAt

0 commit comments

Comments
 (0)