Added Types to actions.js and reformat

Signed-off-by: Yannick Bungers <git@innay.de>
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
Yannick Bungers 2020-04-11 16:08:40 +02:00 committed by David Mehren
parent f9193822a7
commit bb8297dca3
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
3 changed files with 122 additions and 131 deletions

View file

@ -1,41 +1,40 @@
import {NextFunction, Response} from "express";
import {NoteUtils} from "./util";
import { ActionController } from "./actions";
import errors from "../../errors";
import config from "../../config";
import logger from "../../logger";
import { User } from "../../models/user";
import { Note } from "../../models/note";
import { NextFunction, Response } from 'express'
import { NoteUtils } from './util'
import * as ActionController from './actions'
import errors from '../../errors'
import config from '../../config'
import logger from '../../logger'
import { User, Note } from '../../models'
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) {
const action = req.params.action;
const action = req.params.action
switch (action) {
case 'download':
exports.downloadMarkdown(req, res, note);
break;
exports.downloadMarkdown(req, res, note)
break
case 'edit':
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both');
break;
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
break
default:
res.redirect(config.serverURL + '/s/' + note.shortid);
res.redirect(config.serverURL + '/s/' + note.shortid)
break
}
})
}
export function showPublishNote(req: any, res: Response, next: NextFunction) {
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;
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))
}
@ -46,34 +45,34 @@ export module NoteController {
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);
logger.error(err)
return errors.errorInternalError(res)
})
}, include)
}
export function showNote(req: any, res: Response, next: NextFunction) {
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);
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);
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
@ -81,52 +80,52 @@ export module NoteController {
})
}
export function createFromPOST(req: any, res: Response, next: NextFunction) {
let body = '';
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, '');
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;
export function doAction (req: any, res: Response, next: NextFunction) {
const noteId = req.params.noteId
NoteUtils.findNoteOrCreate(req, res, (note) => {
const action = req.params.action;
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;
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
break
case 'slide':
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid));
break;
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
break
case 'download':
exports.downloadMarkdown(req, res, note);
break;
exports.downloadMarkdown(req, res, note)
break
case 'info':
ActionController.getInfo(req, res, note);
break;
ActionController.getInfo(req, res, note)
break
case 'gist':
ActionController.createGist(req, res, note);
break;
ActionController.createGist(req, res, note)
break
case 'revision':
ActionController.getRevision(req, res, note);
break;
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);
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',
@ -135,7 +134,7 @@ export module NoteController {
'Cache-Control': 'private',
'Content-disposition': 'attachment; filename=' + filename + '.md',
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
});
})
res.send(body)
}
}