diff --git a/lib/web/note/controller.ts b/lib/web/note/controller.ts index 503511889..b8184d7f5 100644 --- a/lib/web/note/controller.ts +++ b/lib/web/note/controller.ts @@ -1,23 +1,22 @@ import {NextFunction, Response} from "express"; import {NoteUtils} from "./util"; - -import models from "../../models"; - -import noteActions from "./actions"; +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"; export module NoteController { 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; switch (action) { case 'download': exports.downloadMarkdown(req, res, note); break; 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; default: res.redirect(config.serverURL + '/s/' + note.shortid); @@ -28,13 +27,13 @@ export module NoteController { export function showPublishNote(req: any, res: Response, next: NextFunction) { const include = [{ - model: models.User, + model: User, as: 'owner' }, { - model: models.User, + model: User, as: 'lastchangeuser' }]; - NoteUtils.findNote(req, res, function (note) { + 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)) { @@ -58,19 +57,19 @@ export module NoteController { } 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 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)) { return res.redirect(config.serverURL + '/' + (note.alias || id)) } const body = note.content; - const extracted = models.Note.extractMeta(body); - const meta = models.Note.parseMeta(extracted.meta); - let title = models.Note.decodeTitle(note.title); - title = models.Note.generateWebTitle(meta.title || title); - const opengraph = models.Note.parseOpengraph(meta, title); + 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 @@ -95,8 +94,9 @@ export module NoteController { export function doAction(req: any, res: Response, next: NextFunction) { const noteId = req.params.noteId; - NoteUtils.findNote(req, res, function (note) { + 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 @@ -109,13 +109,13 @@ export module NoteController { exports.downloadMarkdown(req, res, note); break; case 'info': - noteActions.getInfo(req, res, note); + ActionController.getInfo(req, res, note); break; case 'gist': - noteActions.createGist(req, res, note); + ActionController.createGist(req, res, note); break; case 'revision': - noteActions.getRevision(req, res, note); + ActionController.getRevision(req, res, note); break; default: return res.redirect(config.serverURL + '/' + noteId) @@ -125,7 +125,7 @@ export module NoteController { export function downloadMarkdown(req: Request, res: Response, note: any) { const body = note.content; - let filename = models.Note.decodeTitle(note.title); + let filename = Note.decodeTitle(note.title); filename = encodeURIComponent(filename); res.set({ 'Access-Control-Allow-Origin': '*', // allow CORS as API diff --git a/lib/web/note/slide.ts b/lib/web/note/slide.ts index 948ac7a5e..52c2d8af2 100644 --- a/lib/web/note/slide.ts +++ b/lib/web/note/slide.ts @@ -1,17 +1,18 @@ -import {NextFunction, Response} from "express"; -import {NoteUtils} from "./util"; -import models from '../../models'; +import { NextFunction, Response } from "express"; +import { NoteUtils } from "./util"; import errors from '../../errors'; import logger from '../../logger'; import config from '../../config'; +import { User } from "../../models/user"; +import { Note } from "../../models/note"; -export module SlideController{ - export function publishSlideActions (req: any, res: Response, next: NextFunction) { - NoteUtils.findNote(req, res, function (note) { +export module SlideController { + export function publishSlideActions(req: any, res: Response, next: NextFunction) { + NoteUtils.findNoteOrCreate(req, res, function (note) { const action = req.params.action 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) } }) } @@ -20,13 +21,13 @@ export module SlideController{ export function showPublishSlide(req: any, res: Response, next: NextFunction) { const include = [{ - model: models.User, + model: User, as: 'owner' }, { - model: models.User, + model: User, as: 'lastchangeuser' }] - NoteUtils.findNote(req, res, function (note) { + 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)) { diff --git a/lib/web/note/util.ts b/lib/web/note/util.ts index ab802bda9..778c82d32 100644 --- a/lib/web/note/util.ts +++ b/lib/web/note/util.ts @@ -1,26 +1,26 @@ -import {Includeable} from "sequelize"; -import {Response} from "express"; +import { Includeable } from "sequelize"; +import { Response } from "express"; import path from "path"; import fs from "fs"; import errors from "../../errors"; import config from "../../config"; import logger from "../../logger"; -import models from "../../models"; +import { Note } from "../../models/note"; +import { User } from "../../models/user"; 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; - models.Note.parseNoteId(id, function (err, _id) { + Note.parseNoteId(id, function (err, _id) { if (err) { logger.error(err); return errors.errorInternalError(res) } - models.Note.findOne({ + Note.findOne({ where: { id: _id - }, - include: include || null + } }).then(function (note) { if (!note) { return newNote(req, res, null) @@ -39,7 +39,7 @@ export module NoteUtils { export function checkViewPermission(req: any, note: any) { 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') { return req.isAuthenticated() } else { @@ -60,12 +60,12 @@ export module NoteUtils { } else if (noteId) { return req.method === 'POST' ? errors.errorForbidden(res) : errors.errorNotFound(res) } - models.Note.create({ + Note.create({ ownerId: owner, alias: req.alias ? req.alias : null, content: body }).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) { logger.error(err); return errors.errorInternalError(res) @@ -74,17 +74,17 @@ export module NoteUtils { export function getPublishData(req: any, res: Response, note: any, callback: (data: any) => void) { const body = note.content; - const extracted = models.Note.extractMeta(body); + const extracted = Note.extractMeta(body); const markdown = extracted.markdown; - const meta = models.Note.parseMeta(extracted.meta); + const meta = Note.parseMeta(extracted.meta); const createtime = note.createdAt; const updatetime = note.lastchangeAt; - let title = models.Note.decodeTitle(note.title); - title = models.Note.generateWebTitle(meta.title || title); - const ogdata = models.Note.parseOpengraph(meta, title); + 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 ? models.Note.generateDescription(markdown) : null), + description: meta.description || (markdown ? Note.generateDescription(markdown) : null), viewcount: note.viewcount, createtime: createtime, updatetime: updatetime, @@ -92,9 +92,9 @@ export module NoteUtils { theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme), meta: JSON.stringify(extracted.meta), 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, - lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null, + lastchangeuserprofile: note.lastchangeuser ? User.getProfile(note.lastchangeuser) : null, robots: meta.robots || false, // default allow robots GA: meta.GA, disqus: meta.disqus,