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 { 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 includes = []
@ -32,7 +32,7 @@ async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
return note
}
async function createNote (userId, noteAlias) {
async function createNote(userId, noteAlias) {
if (!config.allowAnonymous && !userId) {
throw new Error('can not create note')
}
@ -50,7 +50,7 @@ async function createNote (userId, noteAlias) {
}
// controller
async function showNote (req, res) {
async function showNote(req, res) {
const noteId = req.params.noteId
const userId = req.user ? req.user.id : null
@ -78,7 +78,7 @@ async function showNote (req, res) {
return responseCodiMD(res, note)
}
function canViewNote (note, isLogin, userId) {
function canViewNote(note, isLogin, userId) {
if (note.permission === 'private') {
return note.ownerId === userId
}
@ -88,7 +88,7 @@ function canViewNote (note, isLogin, userId) {
return true
}
async function showPublishNote (req, res) {
async function showPublishNote(req, res) {
const shortid = req.params.shortid
const note = await getNoteById(shortid, {
@ -141,7 +141,7 @@ async function showPublishNote (req, res) {
res.render('pretty.ejs', data)
}
async function noteActions (req, res) {
async function noteActions(req, res) {
const noteId = req.params.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({
where: {
ownerId: userid
ownerId: userId
}
})
if (!myNotes) {
return callback(null, null)
}
try {
const 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
}
}
const myNoteList = myNotes.map(note => ({
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)
logger.info('Parse myNoteList success: ' + userId)
}
return callback(null, myNoteList)
} catch (err) {
@ -221,7 +217,7 @@ async function getMyNoteList (userid, callback) {
}
}
function listMyNotes (req, res) {
function listMyNotes(req, res) {
if (req.isAuthenticated()) {
getMyNoteList(req.user.id, (err, myNoteList) => {
if (err) return errorInternalError(req, res)