Various refactors to use the new models

Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
David Mehren 2020-04-11 00:05:18 +02:00
parent 4e02f46cee
commit 77e336dfda
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
3 changed files with 51 additions and 50 deletions

View file

@ -1,23 +1,22 @@
import {NextFunction, Response} from "express"; import {NextFunction, Response} from "express";
import {NoteUtils} from "./util"; import {NoteUtils} from "./util";
import { ActionController } from "./actions";
import models from "../../models";
import noteActions from "./actions";
import errors from "../../errors"; import errors from "../../errors";
import config from "../../config"; import config from "../../config";
import logger from "../../logger"; import logger from "../../logger";
import { User } from "../../models/user";
import { Note } from "../../models/note";
export module NoteController { export module NoteController {
export function publishNoteActions(req: any, res: Response, next: NextFunction) { export function publishNoteActions(req: any, res: Response, next: NextFunction) {
NoteUtils.findNote(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 : models.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);
@ -28,13 +27,13 @@ export module NoteController {
export function showPublishNote(req: any, res: Response, next: NextFunction) { export function showPublishNote(req: any, res: Response, next: NextFunction) {
const include = [{ const include = [{
model: models.User, model: User,
as: 'owner' as: 'owner'
}, { }, {
model: models.User, model: User,
as: 'lastchangeuser' as: 'lastchangeuser'
}]; }];
NoteUtils.findNote(req, res, function (note) { NoteUtils.findNoteOrCreate(req, res, function (note) {
// force to use short id // 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)) { if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
@ -58,19 +57,19 @@ export module NoteController {
} }
export function showNote(req: any, res: Response, next: NextFunction) { export function showNote(req: any, res: Response, next: NextFunction) {
NoteUtils.findNote(req, res, function (note) { NoteUtils.findNoteOrCreate(req, res, function (note) {
// force to use note id // force to use note id
const noteId = req.params.noteId; const noteId = req.params.noteId;
const id = models.Note.encodeNoteId(note.id); const id = Note.encodeNoteId(note.id);
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) { if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) {
return res.redirect(config.serverURL + '/' + (note.alias || id)) return res.redirect(config.serverURL + '/' + (note.alias || id))
} }
const body = note.content; const body = note.content;
const extracted = models.Note.extractMeta(body); const extracted = Note.extractMeta(body);
const meta = models.Note.parseMeta(extracted.meta); const meta = Note.parseMeta(extracted.meta);
let title = models.Note.decodeTitle(note.title); let title = Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(meta.title || title); title = Note.generateWebTitle(meta.title || title);
const opengraph = models.Note.parseOpengraph(meta, title); const opengraph = Note.parseOpengraph(meta, title);
res.set({ res.set({
'Cache-Control': 'private', // only cache by client 'Cache-Control': 'private', // only cache by client
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
@ -95,8 +94,9 @@ export module NoteController {
export function doAction(req: any, res: Response, next: NextFunction) { export function doAction(req: any, res: Response, next: NextFunction) {
const noteId = req.params.noteId; const noteId = req.params.noteId;
NoteUtils.findNote(req, res, function (note) { 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) { switch (action) {
case 'publish': case 'publish':
case 'pretty': // pretty deprecated case 'pretty': // pretty deprecated
@ -109,13 +109,13 @@ export module NoteController {
exports.downloadMarkdown(req, res, note); exports.downloadMarkdown(req, res, note);
break; break;
case 'info': case 'info':
noteActions.getInfo(req, res, note); ActionController.getInfo(req, res, note);
break; break;
case 'gist': case 'gist':
noteActions.createGist(req, res, note); ActionController.createGist(req, res, note);
break; break;
case 'revision': case 'revision':
noteActions.getRevision(req, res, note); ActionController.getRevision(req, res, note);
break; break;
default: default:
return res.redirect(config.serverURL + '/' + noteId) return res.redirect(config.serverURL + '/' + noteId)
@ -125,7 +125,7 @@ export module NoteController {
export function downloadMarkdown(req: Request, res: Response, note: any) { export function downloadMarkdown(req: Request, res: Response, note: any) {
const body = note.content; const body = note.content;
let filename = models.Note.decodeTitle(note.title); let filename = Note.decodeTitle(note.title);
filename = encodeURIComponent(filename); filename = encodeURIComponent(filename);
res.set({ res.set({
'Access-Control-Allow-Origin': '*', // allow CORS as API 'Access-Control-Allow-Origin': '*', // allow CORS as API

View file

@ -1,17 +1,18 @@
import {NextFunction, Response} from "express"; import { NextFunction, Response } from "express";
import {NoteUtils} from "./util"; import { NoteUtils } from "./util";
import models from '../../models';
import errors from '../../errors'; import errors from '../../errors';
import logger from '../../logger'; import logger from '../../logger';
import config from '../../config'; import config from '../../config';
import { User } from "../../models/user";
import { Note } from "../../models/note";
export module SlideController{ export module SlideController {
export function publishSlideActions (req: any, res: Response, next: NextFunction) { export function publishSlideActions(req: any, res: Response, next: NextFunction) {
NoteUtils.findNote(req, res, function (note) { NoteUtils.findNoteOrCreate(req, res, function (note) {
const action = req.params.action const action = req.params.action
if (action === 'edit') { if (action === 'edit') {
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)) + '?both') res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
} else { res.redirect(config.serverURL + '/p/' + note.shortid) } } else { res.redirect(config.serverURL + '/p/' + note.shortid) }
}) })
} }
@ -20,13 +21,13 @@ export module SlideController{
export function showPublishSlide(req: any, res: Response, next: NextFunction) { export function showPublishSlide(req: any, res: Response, next: NextFunction) {
const include = [{ const include = [{
model: models.User, model: User,
as: 'owner' as: 'owner'
}, { }, {
model: models.User, model: User,
as: 'lastchangeuser' as: 'lastchangeuser'
}] }]
NoteUtils.findNote(req, res, function (note) { NoteUtils.findNoteOrCreate(req, res, function (note) {
// force to use short id // 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)) { if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {

View file

@ -1,26 +1,26 @@
import {Includeable} from "sequelize"; import { Includeable } from "sequelize";
import {Response} from "express"; import { Response } from "express";
import path from "path"; import path from "path";
import fs from "fs"; import fs from "fs";
import errors from "../../errors"; import errors from "../../errors";
import config from "../../config"; import config from "../../config";
import logger from "../../logger"; import logger from "../../logger";
import models from "../../models"; import { Note } from "../../models/note";
import { User } from "../../models/user";
export module NoteUtils { export module NoteUtils {
export function findNote(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;
models.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)
} }
models.Note.findOne({ Note.findOne({
where: { where: {
id: _id id: _id
}, }
include: include || null
}).then(function (note) { }).then(function (note) {
if (!note) { if (!note) {
return newNote(req, res, null) return newNote(req, res, null)
@ -39,7 +39,7 @@ export module NoteUtils {
export function checkViewPermission(req: any, note: any) { export function checkViewPermission(req: any, note: any) {
if (note.permission === 'private') { if (note.permission === 'private') {
return !(!req.isAuthenticated() || note.ownerId !== req.user.id) return req.isAuthenticated() && note.ownerId === req.user.id
} else if (note.permission === 'limited' || note.permission === 'protected') { } else if (note.permission === 'limited' || note.permission === 'protected') {
return req.isAuthenticated() return req.isAuthenticated()
} else { } else {
@ -60,12 +60,12 @@ export module NoteUtils {
} else if (noteId) { } else if (noteId) {
return req.method === 'POST' ? errors.errorForbidden(res) : errors.errorNotFound(res) return req.method === 'POST' ? errors.errorForbidden(res) : errors.errorNotFound(res)
} }
models.Note.create({ Note.create({
ownerId: owner, ownerId: owner,
alias: req.alias ? req.alias : null, alias: req.alias ? req.alias : null,
content: body content: body
}).then(function (note) { }).then(function (note) {
return res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id))) return res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)))
}).catch(function (err) { }).catch(function (err) {
logger.error(err); logger.error(err);
return errors.errorInternalError(res) return errors.errorInternalError(res)
@ -74,17 +74,17 @@ export module NoteUtils {
export function getPublishData(req: any, res: Response, note: any, callback: (data: any) => void) { export function getPublishData(req: any, res: Response, note: any, callback: (data: any) => void) {
const body = note.content; const body = note.content;
const extracted = models.Note.extractMeta(body); const extracted = Note.extractMeta(body);
const markdown = extracted.markdown; const markdown = extracted.markdown;
const meta = models.Note.parseMeta(extracted.meta); const meta = Note.parseMeta(extracted.meta);
const createtime = note.createdAt; const createtime = note.createdAt;
const updatetime = note.lastchangeAt; const updatetime = note.lastchangeAt;
let title = models.Note.decodeTitle(note.title); let title = Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(meta.title || title); title = Note.generateWebTitle(meta.title || title);
const ogdata = models.Note.parseOpengraph(meta, title); const ogdata = Note.parseOpengraph(meta, title);
const data = { const data = {
title: title, title: title,
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null), description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
viewcount: note.viewcount, viewcount: note.viewcount,
createtime: createtime, createtime: createtime,
updatetime: updatetime, updatetime: updatetime,
@ -92,9 +92,9 @@ export module NoteUtils {
theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme), theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme),
meta: JSON.stringify(extracted.meta), meta: JSON.stringify(extracted.meta),
owner: note.owner ? note.owner.id : null, owner: note.owner ? note.owner.id : null,
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null, ownerprofile: note.owner ? User.getProfile(note.owner) : null,
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null, lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null, lastchangeuserprofile: note.lastchangeuser ? User.getProfile(note.lastchangeuser) : null,
robots: meta.robots || false, // default allow robots robots: meta.robots || false, // default allow robots
GA: meta.GA, GA: meta.GA,
disqus: meta.disqus, disqus: meta.disqus,