ESLint fixes for Notes

Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
David Mehren 2020-04-25 20:58:38 +02:00
parent acdd627027
commit 5cda02590b
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
5 changed files with 269 additions and 277 deletions

View file

@ -6,7 +6,7 @@ import fs from 'fs'
import { logger } from './logger' import { logger } from './logger'
import { NoteUtils } from './web/note/util' import * as NoteUtils from './web/note/util'
import { errors } from './errors' import { errors } from './errors'

View file

@ -1,140 +1,138 @@
import { NextFunction, Request, Response } from 'express' import { NextFunction, Request, Response } from 'express'
import { NoteUtils } from './util'
import * as ActionController from './actions'
import { errors } from '../../errors'
import { config } from '../../config' import { config } from '../../config'
import { errors } from '../../errors'
import { logger } from '../../logger' import { logger } from '../../logger'
import { User, Note } from '../../models' import { Note, User } from '../../models'
import * as ActionController from './actions'
import * as NoteUtils from './util'
export module NoteController { export function publishNoteActions (req: any, res: Response, next: NextFunction) {
export function publishNoteActions (req: any, res: Response, next: NextFunction) { NoteUtils.findNoteOrCreate(req, res, function (note) {
NoteUtils.findNoteOrCreate(req, res, function (note) { const action = req.params.action
const action = req.params.action switch (action) {
switch (action) { case 'download':
case 'download': exports.downloadMarkdown(req, res, note)
exports.downloadMarkdown(req, res, note) break
break case 'edit':
case 'edit': res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both') break
break default:
default: res.redirect(config.serverURL + '/s/' + note.shortid)
res.redirect(config.serverURL + '/s/' + note.shortid) break
break
}
})
}
export function showPublishNote (req: any, res: Response, next: NextFunction) {
const include = [{
model: User,
as: 'owner'
}, {
model: User,
as: 'lastchangeuser'
}]
NoteUtils.findNoteOrCreate(req, res, function (note) {
// force to use short id
const shortid = req.params.shortid
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
}
note.increment('viewcount').then(function (note) {
if (!note) {
return errors.errorNotFound(res)
}
NoteUtils.getPublishData(req, res, note, (data) => {
res.set({
'Cache-Control': 'private' // only cache by client
})
return res.render('pretty.ejs', data)
})
}).catch(function (err) {
logger.error(err)
return errors.errorInternalError(res)
})
}, include)
}
export function showNote (req: any, res: Response, next: NextFunction) {
NoteUtils.findNoteOrCreate(req, res, function (note) {
// force to use note id
const noteId = req.params.noteId
const id = Note.encodeNoteId(note.id)
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) {
return res.redirect(config.serverURL + '/' + (note.alias || id))
}
const body = note.content
const extracted = Note.extractMeta(body)
const meta = Note.parseMeta(extracted.meta)
let title = Note.decodeTitle(note.title)
title = Note.generateWebTitle(meta.title || title)
const opengraph = Note.parseOpengraph(meta, title)
res.set({
'Cache-Control': 'private', // only cache by client
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
})
return res.render('codimd.ejs', {
title: title,
opengraph: opengraph
})
})
}
export function createFromPOST (req: any, res: Response, next: NextFunction) {
let body = ''
if (req.body && req.body.length > config.documentMaxLength) {
return errors.errorTooLong(res)
} else if (req.body) {
body = req.body
} }
body = body.replace(/[\r]/g, '') })
return NoteUtils.newNote(req, res, body) }
}
export function doAction (req: any, res: Response, next: NextFunction) { export function showPublishNote (req: any, res: Response, next: NextFunction) {
const noteId = req.params.noteId const include = [{
NoteUtils.findNoteOrCreate(req, res, (note) => { model: User,
const action = req.params.action as: 'owner'
// TODO: Don't switch on action, choose action in Router and use separate functions }, {
switch (action) { model: User,
case 'publish': as: 'lastchangeuser'
case 'pretty': // pretty deprecated }]
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid)) NoteUtils.findNoteOrCreate(req, res, function (note) {
break // force to use short id
case 'slide': const shortid = req.params.shortid
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid)) if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
break return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
case 'download': }
exports.downloadMarkdown(req, res, note) note.increment('viewcount').then(function (note) {
break if (!note) {
case 'info': return errors.errorNotFound(res)
ActionController.getInfo(req, res, note)
break
case 'gist':
ActionController.createGist(req, res, note)
break
case 'revision':
ActionController.getRevision(req, res, note)
break
default:
return res.redirect(config.serverURL + '/' + noteId)
} }
NoteUtils.getPublishData(req, res, note, (data) => {
res.set({
'Cache-Control': 'private' // only cache by client
})
return res.render('pretty.ejs', data)
})
}).catch(function (err) {
logger.error(err)
return errors.errorInternalError(res)
}) })
} }, include)
}
export function downloadMarkdown (req: Request, res: Response, note: any) { export function showNote (req: any, res: Response, next: NextFunction) {
NoteUtils.findNoteOrCreate(req, res, function (note) {
// force to use note id
const noteId = req.params.noteId
const id = Note.encodeNoteId(note.id)
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) {
return res.redirect(config.serverURL + '/' + (note.alias || id))
}
const body = note.content const body = note.content
let filename = Note.decodeTitle(note.title) const extracted = Note.extractMeta(body)
filename = encodeURIComponent(filename) const meta = Note.parseMeta(extracted.meta)
let title = Note.decodeTitle(note.title)
title = Note.generateWebTitle(meta.title || title)
const opengraph = Note.parseOpengraph(meta, title)
res.set({ res.set({
'Access-Control-Allow-Origin': '*', // allow CORS as API 'Cache-Control': 'private', // only cache by client
'Access-Control-Allow-Headers': 'Range',
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
'Content-Type': 'text/markdown; charset=UTF-8',
'Cache-Control': 'private',
'Content-disposition': 'attachment; filename=' + filename + '.md',
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
}) })
res.send(body) return res.render('codimd.ejs', {
} title: title,
opengraph: opengraph
})
})
}
export function createFromPOST (req: any, res: Response, next: NextFunction) {
let body = ''
if (req.body && req.body.length > config.documentMaxLength) {
return errors.errorTooLong(res)
} else if (req.body) {
body = req.body
}
body = body.replace(/[\r]/g, '')
return NoteUtils.newNote(req, res, body)
}
export function doAction (req: any, res: Response, next: NextFunction) {
const noteId = req.params.noteId
NoteUtils.findNoteOrCreate(req, res, (note) => {
const action = req.params.action
// TODO: Don't switch on action, choose action in Router and use separate functions
switch (action) {
case 'publish':
case 'pretty': // pretty deprecated
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
break
case 'slide':
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
break
case 'download':
exports.downloadMarkdown(req, res, note)
break
case 'info':
ActionController.getInfo(req, res, note)
break
case 'gist':
ActionController.createGist(req, res, note)
break
case 'revision':
ActionController.getRevision(req, res, note)
break
default:
return res.redirect(config.serverURL + '/' + noteId)
}
})
}
export function downloadMarkdown (req: Request, res: Response, note: any) {
const body = note.content
let filename = Note.decodeTitle(note.title)
filename = encodeURIComponent(filename)
res.set({
'Access-Control-Allow-Origin': '*', // allow CORS as API
'Access-Control-Allow-Headers': 'Range',
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
'Content-Type': 'text/markdown; charset=UTF-8',
'Cache-Control': 'private',
'Content-disposition': 'attachment; filename=' + filename + '.md',
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
})
res.send(body)
} }

View file

@ -1,8 +1,8 @@
import { markdownParser } from '../utils'
import { SlideController } from './slide'
import { NoteController } from './controller'
import { Router } from 'express' import { Router } from 'express'
import { markdownParser } from '../utils'
import * as NoteController from './controller'
import * as SlideController from './slide'
const NoteRouter = Router() const NoteRouter = Router()
// get new note // get new note

View file

@ -1,52 +1,48 @@
import { NextFunction, Response } from "express"; import { NextFunction, Response } from 'express'
import { NoteUtils } from "./util"; import { config } from '../../config'
import { errors } from '../../errors' import { errors } from '../../errors'
import { logger } from '../../logger' import { logger } from '../../logger'
import { config } from '../../config' import { Note, User } from '../../models'
import { User } from "../../models/user"; import * as NoteUtils from './util'
import { Note } from "../../models/note";
export function publishSlideActions (req: any, res: Response, next: NextFunction) {
export module SlideController { NoteUtils.findNoteOrCreate(req, res, function (note) {
export function publishSlideActions(req: any, res: Response, next: NextFunction) { const action = req.params.action
NoteUtils.findNoteOrCreate(req, res, function (note) { if (action === 'edit') {
const action = req.params.action res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
if (action === 'edit') { } else {
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both') res.redirect(config.serverURL + '/p/' + note.shortid)
} else { res.redirect(config.serverURL + '/p/' + note.shortid) } }
}) })
} }
export function showPublishSlide (req: any, res: Response, next: NextFunction) {
const include = [{
export function showPublishSlide(req: any, res: Response, next: NextFunction) { model: User,
const include = [{ as: 'owner'
model: User, }, {
as: 'owner' model: User,
}, { as: 'lastchangeuser'
model: User, }]
as: 'lastchangeuser' NoteUtils.findNoteOrCreate(req, res, function (note) {
}] // force to use short id
NoteUtils.findNoteOrCreate(req, res, function (note) { const shortid = req.params.shortid
// force to use short id if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
const shortid = req.params.shortid return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { }
return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid)) note.increment('viewcount').then(function (note) {
} if (!note) {
note.increment('viewcount').then(function (note) { return errors.errorNotFound(res)
if (!note) { }
return errors.errorNotFound(res) NoteUtils.getPublishData(req, res, note, (data) => {
} res.set({
NoteUtils.getPublishData(req, res, note, (data) => { 'Cache-Control': 'private' // only cache by client
res.set({ })
'Cache-Control': 'private' // only cache by client return res.render('slide.ejs', data)
}) })
return res.render('slide.ejs', data) }).catch(function (err) {
}) logger.error(err)
}).catch(function (err) { return errors.errorInternalError(res)
logger.error(err) })
return errors.errorInternalError(res) }, include)
})
}, include)
}
} }

View file

@ -1,113 +1,111 @@
import { Includeable } from 'sequelize'
import { Response } from 'express' import { Response } from 'express'
import fs from 'fs'
import path from 'path' import path from 'path'
import fs from 'fs' import { Includeable } from 'sequelize'
import { errors } from '../../errors'
import { config } from '../../config' import { config } from '../../config'
import { errors } from '../../errors'
import { logger } from '../../logger' import { logger } from '../../logger'
import { Note , User } from '../../models' import { Note, User } from '../../models'
export module NoteUtils { export function findNoteOrCreate (req, res, callback: (note: any) => void, include?: Includeable[]) {
export function findNoteOrCreate(req, res, callback: (note: any) => void, include?: Includeable[]) { const id = req.params.noteId || req.params.shortid
const id = req.params.noteId || req.params.shortid Note.parseNoteId(id, function (err, _id) {
Note.parseNoteId(id, function (err, _id) { if (err) {
if (err) { logger.error(err)
logger.error(err) return errors.errorInternalError(res)
return errors.errorInternalError(res) }
Note.findOne({
where: {
id: _id
} }
Note.findOne({
where: {
id: _id
}
}).then(function (note) {
if (!note) {
return newNote(req, res, "")
}
if (!checkViewPermission(req, note)) {
return errors.errorForbidden(res)
} else {
return callback(note)
}
}).catch(function (err) {
logger.error(err)
return errors.errorInternalError(res)
})
})
}
export function checkViewPermission (req: any, note: any) {
if (note.permission === 'private') {
return req.isAuthenticated() && note.ownerId === req.user.id
} else if (note.permission === 'limited' || note.permission === 'protected') {
return req.isAuthenticated()
} else {
return true
}
}
export function newNote (req: any, res: Response, body: string | null) {
let owner = null
const noteId = req.params.noteId ? req.params.noteId : null
if (req.isAuthenticated()) {
owner = req.user.id
} else if (!config.allowAnonymous) {
return errors.errorForbidden(res)
}
if (config.allowFreeURL && noteId && !config.forbiddenNoteIDs.includes(noteId)) {
req.alias = noteId
} else if (noteId) {
return req.method === 'POST' ? errors.errorForbidden(res) : errors.errorNotFound(res)
}
Note.create({
ownerId: owner,
alias: req.alias ? req.alias : null,
content: body
}).then(function (note) { }).then(function (note) {
return res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id))) if (!note) {
return newNote(req, res, "")
}
if (!checkViewPermission(req, note)) {
return errors.errorForbidden(res)
} else {
return callback(note)
}
}).catch(function (err) { }).catch(function (err) {
logger.error(err) logger.error(err)
return errors.errorInternalError(res) return errors.errorInternalError(res)
}) })
} })
}
export function getPublishData (req: any, res: Response, note: any, callback: (data: any) => void) { export function checkViewPermission (req: any, note: any) {
const body = note.content if (note.permission === 'private') {
const extracted = Note.extractMeta(body) return req.isAuthenticated() && note.ownerId === req.user.id
const markdown = extracted.markdown } else if (note.permission === 'limited' || note.permission === 'protected') {
const meta = Note.parseMeta(extracted.meta) return req.isAuthenticated()
const createtime = note.createdAt } else {
const updatetime = note.lastchangeAt return true
let title = Note.decodeTitle(note.title)
title = Note.generateWebTitle(meta.title || title)
const ogdata = Note.parseOpengraph(meta, title)
const data = {
title: title,
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
viewcount: note.viewcount,
createtime: createtime,
updatetime: updatetime,
body: markdown,
theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme),
meta: JSON.stringify(extracted.meta),
owner: note.owner ? note.owner.id : null,
ownerprofile: note.owner ? User.getProfile(note.owner) : null,
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
lastchangeuserprofile: note.lastchangeuser ? User.getProfile(note.lastchangeuser) : null,
robots: meta.robots || false, // default allow robots
GA: meta.GA,
disqus: meta.disqus,
cspNonce: res.locals.nonce,
dnt: req.headers.dnt,
opengraph: ogdata
}
callback(data)
}
function isRevealTheme (theme: string) {
if (fs.existsSync(path.join(__dirname, '..', '..', '..', 'public', 'build', 'reveal.js', 'css', 'theme', theme + '.css'))) {
return theme
}
return undefined
} }
} }
export function newNote (req: any, res: Response, body: string | null) {
let owner = null
const noteId = req.params.noteId ? req.params.noteId : null
if (req.isAuthenticated()) {
owner = req.user.id
} else if (!config.allowAnonymous) {
return errors.errorForbidden(res)
}
if (config.allowFreeURL && noteId && !config.forbiddenNoteIDs.includes(noteId)) {
req.alias = noteId
} else if (noteId) {
return req.method === 'POST' ? errors.errorForbidden(res) : errors.errorNotFound(res)
}
Note.create({
ownerId: owner,
alias: req.alias ? req.alias : null,
content: body
}).then(function (note) {
return res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)))
}).catch(function (err) {
logger.error(err)
return errors.errorInternalError(res)
})
}
export function getPublishData (req: any, res: Response, note: any, callback: (data: any) => void) {
const body = note.content
const extracted = Note.extractMeta(body)
const markdown = extracted.markdown
const meta = Note.parseMeta(extracted.meta)
const createtime = note.createdAt
const updatetime = note.lastchangeAt
let title = Note.decodeTitle(note.title)
title = Note.generateWebTitle(meta.title || title)
const ogdata = Note.parseOpengraph(meta, title)
const data = {
title: title,
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
viewcount: note.viewcount,
createtime: createtime,
updatetime: updatetime,
body: markdown,
theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme),
meta: JSON.stringify(extracted.meta),
owner: note.owner ? note.owner.id : null,
ownerprofile: note.owner ? User.getProfile(note.owner) : null,
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
lastchangeuserprofile: note.lastchangeuser ? User.getProfile(note.lastchangeuser) : null,
robots: meta.robots || false, // default allow robots
GA: meta.GA,
disqus: meta.disqus,
cspNonce: res.locals.nonce,
dnt: req.headers.dnt,
opengraph: ogdata
}
callback(data)
}
function isRevealTheme (theme: string) {
if (fs.existsSync(path.join(__dirname, '..', '..', '..', 'public', 'build', 'reveal.js', 'css', 'theme', theme + '.css'))) {
return theme
}
return undefined
}