Skip to content

Commit 53526c1

Browse files
committed
Check online users, update authorships, save revisions in update note content API
Signed-off-by: James Tsai <jamesscamel@gmail.com>
1 parent 96f8f06 commit 53526c1

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

lib/note/index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
const config = require('../config')
44
const logger = require('../logger')
5-
const { Note, User } = require('../models')
5+
const { Note, User, Revision } = require('../models')
66

77
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response')
88
const { updateHistory, historyDelete } = require('../history')
99
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
1010
const realtime = require('../realtime/realtime')
11+
const moment = require('moment')
1112

1213
async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
1314
const id = await Note.parseNoteIdAsync(noteId)
@@ -281,13 +282,37 @@ const updateNote = async (req, res) => {
281282
return errorNotFound(req, res)
282283
}
283284

285+
if (realtime.isNoteExistsInPool(noteId)) {
286+
logger.error('Update note failed: There are online users opening this note.')
287+
return res.status('403').send({ status: 'error', message: 'Update API can only be used when no users is online' })
288+
}
289+
290+
const now = Date.now()
291+
const content = req.body.content
284292
const updated = await note.update({
285-
content: req.body.content
293+
content: content,
294+
lastchangeAt: moment(now).format('YYYY-MM-DD HH:mm:ss'),
295+
authorship: [
296+
[
297+
req.user.id,
298+
0,
299+
content.length,
300+
now,
301+
now
302+
]
303+
]
286304
})
305+
287306
if (!updated) {
288-
logger.error('Update note failed: Write data error.')
307+
logger.error('Update note failed: Write note content error.')
289308
return errorInternalError(req, res)
290309
}
310+
311+
Revision.saveNoteRevision(note, (err, revision) => {
312+
if (err) return errorInternalError(req, res)
313+
if (!revision) return errorNotFound(req, res)
314+
})
315+
291316
res.send({
292317
status: 'ok'
293318
})

0 commit comments

Comments
 (0)