Refactor, variable naming and myNoteList mapping

Signed-off-by: James Tsai <jamesscamel@gmail.com>
This commit is contained in:
James Tsai 2020-07-14 15:38:02 +08:00
parent dcf48e749e
commit a22cf73f60
1 changed files with 18 additions and 22 deletions

View File

@ -8,7 +8,7 @@ const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, e
const { updateHistory } = require('../history') const { updateHistory } = require('../history')
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions') const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
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)
const includes = [] const includes = []
@ -32,7 +32,7 @@ async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
return note return note
} }
async function createNote (userId, noteAlias) { async function createNote(userId, noteAlias) {
if (!config.allowAnonymous && !userId) { if (!config.allowAnonymous && !userId) {
throw new Error('can not create note') throw new Error('can not create note')
} }
@ -50,7 +50,7 @@ async function createNote (userId, noteAlias) {
} }
// controller // controller
async function showNote (req, res) { async function showNote(req, res) {
const noteId = req.params.noteId const noteId = req.params.noteId
const userId = req.user ? req.user.id : null const userId = req.user ? req.user.id : null
@ -78,7 +78,7 @@ async function showNote (req, res) {
return responseCodiMD(res, note) return responseCodiMD(res, note)
} }
function canViewNote (note, isLogin, userId) { function canViewNote(note, isLogin, userId) {
if (note.permission === 'private') { if (note.permission === 'private') {
return note.ownerId === userId return note.ownerId === userId
} }
@ -88,7 +88,7 @@ function canViewNote (note, isLogin, userId) {
return true return true
} }
async function showPublishNote (req, res) { async function showPublishNote(req, res) {
const shortid = req.params.shortid const shortid = req.params.shortid
const note = await getNoteById(shortid, { const note = await getNoteById(shortid, {
@ -141,7 +141,7 @@ async function showPublishNote (req, res) {
res.render('pretty.ejs', data) res.render('pretty.ejs', data)
} }
async function noteActions (req, res) { async function noteActions(req, res) {
const noteId = req.params.noteId const noteId = req.params.noteId
const note = await getNoteById(noteId) const note = await getNoteById(noteId)
@ -189,30 +189,26 @@ async function noteActions (req, res) {
} }
} }
async function getMyNoteList (userid, callback) { async function getMyNoteList(userId, callback) {
const myNotes = await Note.findAll({ const myNotes = await Note.findAll({
where: { where: {
ownerId: userid ownerId: userId
} }
}) })
if (!myNotes) { if (!myNotes) {
return callback(null, null) return callback(null, null)
} }
try { try {
const myNoteList = [] const myNoteList = myNotes.map(note => ({
for (let i = 0; i < myNotes.length; i++) { id: Note.encodeNoteId(note.id),
const note = myNotes[i] text: note.title,
myNoteList[i] = { tags: Note.parseNoteInfo(note.content).tags,
id: Note.encodeNoteId(note.id), createdAt: note.createdAt,
text: note.title, lastchangeAt: note.lastchangeAt,
tags: Note.parseNoteInfo(note.content).tags, shortId: note.shortid
createdAt: note.createdAt, }))
lastchangeAt: note.lastchangeAt,
shortId: note.shortid
}
}
if (config.debug) { if (config.debug) {
logger.info('Parse myNoteList success: ' + userid) logger.info('Parse myNoteList success: ' + userId)
} }
return callback(null, myNoteList) return callback(null, myNoteList)
} catch (err) { } catch (err) {
@ -221,7 +217,7 @@ async function getMyNoteList (userid, callback) {
} }
} }
function listMyNotes (req, res) { function listMyNotes(req, res) {
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
getMyNoteList(req.user.id, (err, myNoteList) => { getMyNoteList(req.user.id, (err, myNoteList) => {
if (err) return errorInternalError(req, res) if (err) return errorInternalError(req, res)