Add list-my-note API

Signed-off-by: James Tsai <jamesscamel@gmail.com>
This commit is contained in:
James Tsai 2020-06-30 18:30:21 +08:00
parent 7b148457a7
commit 2973bfbceb
2 changed files with 52 additions and 2 deletions

View File

@ -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

View File

@ -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