diff --git a/lib/note/index.js b/lib/note/index.js index d020b46f..9ae7a160 100644 --- a/lib/note/index.js +++ b/lib/note/index.js @@ -2,12 +2,13 @@ const config = require('../config') const logger = require('../logger') -const { Note, User } = require('../models') +const { Note, User, Revision } = require('../models') const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response') const { updateHistory, historyDelete } = require('../history') const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions') const realtime = require('../realtime/realtime') +const moment = require('moment') async function getNoteById (noteId, { includeUser } = { includeUser: false }) { const id = await Note.parseNoteIdAsync(noteId) @@ -281,13 +282,37 @@ const updateNote = async (req, res) => { return errorNotFound(req, res) } + if (realtime.isNoteExistsInPool(noteId)) { + logger.error('Update note failed: There are online users opening this note.') + return res.status('403').send({ status: 'error', message: 'Update API can only be used when no users is online' }) + } + + const now = Date.now() + const content = req.body.content const updated = await note.update({ - content: req.body.content + content: content, + lastchangeAt: moment(now).format('YYYY-MM-DD HH:mm:ss'), + authorship: [ + [ + req.user.id, + 0, + content.length, + now, + now + ] + ] }) + if (!updated) { - logger.error('Update note failed: Write data error.') + logger.error('Update note failed: Write note content error.') return errorInternalError(req, res) } + + Revision.saveNoteRevision(note, (err, revision) => { + if (err) return errorInternalError(req, res) + if (!revision) return errorNotFound(req, res) + }) + res.send({ status: 'ok' })