From 2973bfbcebd37058b6c6558a5687a59c64ecc774 Mon Sep 17 00:00:00 2001 From: James Tsai Date: Tue, 30 Jun 2020 18:30:21 +0800 Subject: [PATCH] Add list-my-note API Signed-off-by: James Tsai --- lib/note/index.js | 52 +++++++++++++++++++++++++++++++++++++++++++++-- lib/routes.js | 2 ++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/note/index.js b/lib/note/index.js index b9b7c9e3..a65c63d7 100644 --- a/lib/note/index.js +++ b/lib/note/index.js @@ -2,10 +2,9 @@ const config = require('../config') const logger = require('../logger') - const { Note, User } = require('../models') -const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound } = require('../response') +const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response') const { updateHistory } = require('../history') const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions') @@ -190,6 +189,55 @@ async function noteActions (req, res) { } } +async function getMyNoteList(userid, callback) { + const myNotes = await Note.findAll({ + where: { + ownerId: userid + } + }) + if (!myNotes) { + return callback(null, null) + } + try { + let myNoteList = [] + for (let i = 0; i < myNotes.length; i++) { + const note = myNotes[i] + myNoteList[i] = { + id: Note.encodeNoteId(note.id), + text: note.title, + tags: Note.parseNoteInfo(note.content).tags, + createdAt: note.createdAt, + lastchangeAt: note.lastchangeAt, + shortId: note.shortid + } + } + if (config.debug) { + logger.info('Parse myNoteList success: ' + userid) + } + return callback(null, myNoteList) + } catch (err){ + logger.error('Parse myNoteList failed') + return callback(err, null) + } +} + +function listMyNotes(req, res) { + if (req.isAuthenticated()) { + getMyNoteList(req.user.id, (err, myNoteList) => { + if (err) return errorInternalError(req, res) + if (!myNoteList) return errorNotFound(req, res) + res.send({ + myNotes: myNoteList + }) + }) + } else { + return errorForbidden(req, res) + } +} + exports.showNote = showNote exports.showPublishNote = showPublishNote exports.noteActions = noteActions +exports.listMyNotes = listMyNotes + + diff --git a/lib/routes.js b/lib/routes.js index f4345039..a6f1cc3e 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -70,6 +70,8 @@ appRouter.get('/s/:shortid/:action', response.publishNoteActions) appRouter.get('/p/:shortid', response.showPublishSlide) // publish slide actions appRouter.get('/p/:shortid/:action', response.publishSlideActions) +// gey my note list +appRouter.get('/myNotes', noteController.listMyNotes) // get note by id appRouter.get('/:noteId', wrap(noteController.showNote)) // note actions