|
2 | 2 |
|
3 | 3 | const config = require('../config')
|
4 | 4 | const logger = require('../logger')
|
5 |
| -const { Note, User } = require('../models') |
| 5 | +const { Note, User, Revision } = require('../models') |
6 | 6 |
|
7 | 7 | const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response')
|
8 |
| -const { updateHistory } = require('../history') |
| 8 | +const { updateHistory, historyDelete } = require('../history') |
9 | 9 | const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
|
| 10 | +const realtime = require('../realtime/realtime') |
10 | 11 |
|
11 | 12 | async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
|
12 | 13 | const id = await Note.parseNoteIdAsync(noteId)
|
@@ -232,7 +233,106 @@ function listMyNotes (req, res) {
|
232 | 233 | }
|
233 | 234 | }
|
234 | 235 |
|
| 236 | +const deleteNote = async (req, res) => { |
| 237 | + if (req.isAuthenticated()) { |
| 238 | + const noteId = await Note.parseNoteIdAsync(req.params.noteId) |
| 239 | + try { |
| 240 | + const destroyed = await Note.destroy({ |
| 241 | + where: { |
| 242 | + id: noteId, |
| 243 | + ownerId: req.user.id |
| 244 | + } |
| 245 | + }) |
| 246 | + if (!destroyed) { |
| 247 | + logger.error('Delete note failed: Make sure the noteId and ownerId are correct.') |
| 248 | + return errorNotFound(req, res) |
| 249 | + } |
| 250 | + |
| 251 | + historyDelete(req, res) |
| 252 | + |
| 253 | + if (realtime.isNoteExistsInPool(noteId)) { |
| 254 | + const note = realtime.getNoteFromNotePool(noteId) |
| 255 | + realtime.disconnectSocketOnNote(note) |
| 256 | + } |
| 257 | + |
| 258 | + res.send({ |
| 259 | + status: 'ok' |
| 260 | + }) |
| 261 | + } catch (err) { |
| 262 | + logger.error('Delete note failed: Internal Error.') |
| 263 | + return errorInternalError(req, res) |
| 264 | + } |
| 265 | + } else { |
| 266 | + return errorForbidden(req, res) |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +const updateNote = async (req, res) => { |
| 271 | + if (req.isAuthenticated()) { |
| 272 | + const noteId = await Note.parseNoteIdAsync(req.params.noteId) |
| 273 | + try { |
| 274 | + const note = await Note.findOne({ |
| 275 | + where: { |
| 276 | + id: noteId |
| 277 | + } |
| 278 | + }) |
| 279 | + if (!note) { |
| 280 | + logger.error('Update note failed: Can\'t find the note.') |
| 281 | + return errorNotFound(req, res) |
| 282 | + } |
| 283 | + |
| 284 | + if (realtime.isNoteExistsInPool(noteId)) { |
| 285 | + logger.error('Update note failed: There are online users opening this note.') |
| 286 | + return res.status('403').json({ status: 'error', message: 'Update API can only be used when no users is online' }) |
| 287 | + } |
| 288 | + |
| 289 | + const now = Date.now() |
| 290 | + const content = req.body.content |
| 291 | + const updated = await note.update({ |
| 292 | + title: Note.parseNoteTitle(content), |
| 293 | + content: content, |
| 294 | + lastchangeAt: now, |
| 295 | + authorship: [ |
| 296 | + [ |
| 297 | + req.user.id, |
| 298 | + 0, |
| 299 | + content.length, |
| 300 | + now, |
| 301 | + now |
| 302 | + ] |
| 303 | + ] |
| 304 | + }) |
| 305 | + |
| 306 | + if (!updated) { |
| 307 | + logger.error('Update note failed: Write note content error.') |
| 308 | + return errorInternalError(req, res) |
| 309 | + } |
| 310 | + |
| 311 | + updateHistory(req.user.id, note.id, content) |
| 312 | + |
| 313 | + Revision.saveNoteRevision(note, (err, revision) => { |
| 314 | + if (err) { |
| 315 | + logger.error(err) |
| 316 | + return errorInternalError(req, res) |
| 317 | + } |
| 318 | + if (!revision) return errorNotFound(req, res) |
| 319 | + res.send({ |
| 320 | + status: 'ok' |
| 321 | + }) |
| 322 | + }) |
| 323 | + } catch (err) { |
| 324 | + logger.error(err) |
| 325 | + logger.error('Update note failed: Internal Error.') |
| 326 | + return errorInternalError(req, res) |
| 327 | + } |
| 328 | + } else { |
| 329 | + return errorForbidden(req, res) |
| 330 | + } |
| 331 | +} |
| 332 | + |
235 | 333 | exports.showNote = showNote
|
236 | 334 | exports.showPublishNote = showPublishNote
|
237 | 335 | exports.noteActions = noteActions
|
238 | 336 | exports.listMyNotes = listMyNotes
|
| 337 | +exports.deleteNote = deleteNote |
| 338 | +exports.updateNote = updateNote |
0 commit comments