Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and delete note api #1559

Merged
merged 11 commits into from
Aug 13, 2020
Prev Previous commit
Next Next commit
Check online users, update authorships, save revisions in update note…
… content API

Signed-off-by: James Tsai <[email protected]>
  • Loading branch information
JamesCamel committed Jul 27, 2020
commit 53526c154a6c9bed6a635266ca9f8d3f3539f55d
31 changes: 28 additions & 3 deletions lib/note/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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({
Yukaii marked this conversation as resolved.
Show resolved Hide resolved
content: req.body.content
content: content,
lastchangeAt: moment(now).format('YYYY-MM-DD HH:mm:ss'),
Yukaii marked this conversation as resolved.
Show resolved Hide resolved
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)
}

Yukaii marked this conversation as resolved.
Show resolved Hide resolved
Revision.saveNoteRevision(note, (err, revision) => {
if (err) return errorInternalError(req, res)
Yukaii marked this conversation as resolved.
Show resolved Hide resolved
if (!revision) return errorNotFound(req, res)
Yukaii marked this conversation as resolved.
Show resolved Hide resolved
})

res.send({
status: 'ok'
})
Expand Down