Check online users, update authorships, save revisions in update note content API

Signed-off-by: James Tsai <jamesscamel@gmail.com>
This commit is contained in:
James Tsai 2020-07-27 17:59:51 +08:00
parent 96f8f06b00
commit 53526c154a
1 changed files with 28 additions and 3 deletions

View File

@ -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'
})