mirror of https://github.com/status-im/codimd.git
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:
parent
96f8f06b00
commit
53526c154a
|
@ -2,12 +2,13 @@
|
||||||
|
|
||||||
const config = require('../config')
|
const config = require('../config')
|
||||||
const logger = require('../logger')
|
const logger = require('../logger')
|
||||||
const { Note, User } = require('../models')
|
const { Note, User, Revision } = require('../models')
|
||||||
|
|
||||||
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response')
|
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response')
|
||||||
const { updateHistory, historyDelete } = require('../history')
|
const { updateHistory, historyDelete } = require('../history')
|
||||||
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
|
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
|
||||||
const realtime = require('../realtime/realtime')
|
const realtime = require('../realtime/realtime')
|
||||||
|
const moment = require('moment')
|
||||||
|
|
||||||
async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
|
async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
|
||||||
const id = await Note.parseNoteIdAsync(noteId)
|
const id = await Note.parseNoteIdAsync(noteId)
|
||||||
|
@ -281,13 +282,37 @@ const updateNote = async (req, res) => {
|
||||||
return errorNotFound(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({
|
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) {
|
if (!updated) {
|
||||||
logger.error('Update note failed: Write data error.')
|
logger.error('Update note failed: Write note content error.')
|
||||||
return errorInternalError(req, res)
|
return errorInternalError(req, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Revision.saveNoteRevision(note, (err, revision) => {
|
||||||
|
if (err) return errorInternalError(req, res)
|
||||||
|
if (!revision) return errorNotFound(req, res)
|
||||||
|
})
|
||||||
|
|
||||||
res.send({
|
res.send({
|
||||||
status: 'ok'
|
status: 'ok'
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue