mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 01:35:18 -04:00
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:
parent
f9193822a7
commit
bb8297dca3
3 changed files with 122 additions and 131 deletions
|
@ -114,7 +114,7 @@ export class Revision extends Model<Revision> {
|
||||||
@BelongsTo(() => Note, { foreignKey: 'noteId', constraints: false, onDelete: 'CASCADE', hooks: true })
|
@BelongsTo(() => Note, { foreignKey: 'noteId', constraints: false, onDelete: 'CASCADE', hooks: true })
|
||||||
note: Note;
|
note: Note;
|
||||||
|
|
||||||
getNoteRevisions (note: Note, callback): void {
|
static getNoteRevisions (note: Note, callback): void {
|
||||||
Revision.findAll({
|
Revision.findAll({
|
||||||
where: {
|
where: {
|
||||||
noteId: note.id
|
noteId: note.id
|
||||||
|
@ -138,7 +138,7 @@ export class Revision extends Model<Revision> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
getPatchedNoteRevisionByTime (note: Note, time, errorCallback): void {
|
static getPatchedNoteRevisionByTime (note: Note, time, errorCallback): void {
|
||||||
// find all revisions to prepare for all possible calculation
|
// find all revisions to prepare for all possible calculation
|
||||||
Revision.findAll({
|
Revision.findAll({
|
||||||
where: {
|
where: {
|
||||||
|
|
|
@ -1,30 +1,25 @@
|
||||||
import {Response} from "express";
|
import { Response } from 'express'
|
||||||
|
|
||||||
const models = require('../../models')
|
import { Note, Revision } from '../../models'
|
||||||
const logger = require('../../logger')
|
import logger from '../../logger'
|
||||||
const config = require('../../config')
|
import config from '../../config'
|
||||||
const errors = require('../../errors')
|
import errors from '../../errors'
|
||||||
const shortId = require('shortid')
|
import shortId from 'shortid'
|
||||||
const moment = require('moment')
|
import moment from 'moment'
|
||||||
const querystring = require('querystring')
|
import querystring from 'querystring'
|
||||||
|
|
||||||
export module ActionController {
|
export function getInfo (req: any, res: Response, note: Note): void {
|
||||||
|
|
||||||
// TODO: Use correct type for note
|
|
||||||
export function getInfo(req: any, res: Response, note: any) {
|
|
||||||
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 title = Note.decodeTitle(note.title)
|
||||||
const updatetime = note.lastchangeAt
|
|
||||||
const title = models.Note.decodeTitle(note.title)
|
|
||||||
const data = {
|
const data = {
|
||||||
title: meta.title || title,
|
title: meta.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: note.createdAt,
|
||||||
updatetime: updatetime
|
updatetime: note.lastchangeAt
|
||||||
}
|
}
|
||||||
res.set({
|
res.set({
|
||||||
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
||||||
|
@ -36,11 +31,10 @@ export module ActionController {
|
||||||
res.send(data)
|
res.send(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Use correct type for note
|
export function createGist (req: any, res: Response, note: Note): void {
|
||||||
export function createGist(req: any, res: Response, note: any) {
|
|
||||||
const data = {
|
const data = {
|
||||||
client_id: config.github.clientID,
|
client_id: config.github.clientID,
|
||||||
redirect_uri: config.serverURL + '/auth/github/callback/' + models.Note.encodeNoteId(note.id) + '/gist',
|
redirect_uri: config.serverURL + '/auth/github/callback/' + Note.encodeNoteId(note.id) + '/gist',
|
||||||
scope: 'gist',
|
scope: 'gist',
|
||||||
state: shortId.generate()
|
state: shortId.generate()
|
||||||
}
|
}
|
||||||
|
@ -48,19 +42,18 @@ export module ActionController {
|
||||||
res.redirect('https://github.com/login/oauth/authorize?' + query)
|
res.redirect('https://github.com/login/oauth/authorize?' + query)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Use correct type for note
|
export function getRevision (req: any, res: Response, note: Note): void {
|
||||||
export function getRevision(req: any, res: Response, note: any) {
|
|
||||||
const actionId = req.params.actionId
|
const actionId = req.params.actionId
|
||||||
if (actionId) {
|
if (actionId) {
|
||||||
const time = moment(parseInt(actionId))
|
const time = moment(parseInt(actionId))
|
||||||
if (time.isValid()) {
|
if (time.isValid()) {
|
||||||
models.Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
|
Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
return errors.errorInternalError(res)
|
errors.errorInternalError(res)
|
||||||
}
|
}
|
||||||
if (!content) {
|
if (!content) {
|
||||||
return errors.errorNotFound(res)
|
errors.errorNotFound(res)
|
||||||
}
|
}
|
||||||
res.set({
|
res.set({
|
||||||
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
||||||
|
@ -72,13 +65,13 @@ export module ActionController {
|
||||||
res.send(content)
|
res.send(content)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
return errors.errorNotFound(res)
|
errors.errorNotFound(res)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
models.Revision.getNoteRevisions(note, function (err, data) {
|
Revision.getNoteRevisions(note, function (err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
return errors.errorInternalError(res)
|
errors.errorInternalError(res)
|
||||||
}
|
}
|
||||||
const out = {
|
const out = {
|
||||||
revision: data
|
revision: data
|
||||||
|
@ -94,4 +87,3 @@ export module ActionController {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -1,25 +1,24 @@
|
||||||
import {NextFunction, Response} from "express";
|
import { NextFunction, Response } from 'express'
|
||||||
import {NoteUtils} from "./util";
|
import { NoteUtils } from './util'
|
||||||
import { ActionController } from "./actions";
|
import * as ActionController 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 { User, Note } from '../../models'
|
||||||
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.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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -32,10 +31,10 @@ export module NoteController {
|
||||||
}, {
|
}, {
|
||||||
model: User,
|
model: User,
|
||||||
as: 'lastchangeuser'
|
as: 'lastchangeuser'
|
||||||
}];
|
}]
|
||||||
NoteUtils.findNoteOrCreate(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)) {
|
||||||
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||||
}
|
}
|
||||||
|
@ -46,11 +45,11 @@ export module NoteController {
|
||||||
NoteUtils.getPublishData(req, res, note, (data) => {
|
NoteUtils.getPublishData(req, res, note, (data) => {
|
||||||
res.set({
|
res.set({
|
||||||
'Cache-Control': 'private' // only cache by client
|
'Cache-Control': 'private' // only cache by client
|
||||||
});
|
})
|
||||||
return res.render('pretty.ejs', data)
|
return res.render('pretty.ejs', data)
|
||||||
})
|
})
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
logger.error(err);
|
logger.error(err)
|
||||||
return errors.errorInternalError(res)
|
return errors.errorInternalError(res)
|
||||||
})
|
})
|
||||||
}, include)
|
}, include)
|
||||||
|
@ -59,21 +58,21 @@ export module NoteController {
|
||||||
export function showNote (req: any, res: Response, next: NextFunction) {
|
export function showNote (req: any, res: Response, next: NextFunction) {
|
||||||
NoteUtils.findNoteOrCreate(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 = 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 = Note.extractMeta(body);
|
const extracted = Note.extractMeta(body)
|
||||||
const meta = Note.parseMeta(extracted.meta);
|
const meta = Note.parseMeta(extracted.meta)
|
||||||
let title = Note.decodeTitle(note.title);
|
let title = Note.decodeTitle(note.title)
|
||||||
title = Note.generateWebTitle(meta.title || title);
|
title = Note.generateWebTitle(meta.title || title)
|
||||||
const opengraph = 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
|
||||||
});
|
})
|
||||||
return res.render('codimd.ejs', {
|
return res.render('codimd.ejs', {
|
||||||
title: title,
|
title: title,
|
||||||
opengraph: opengraph
|
opengraph: opengraph
|
||||||
|
@ -82,41 +81,41 @@ export module NoteController {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createFromPOST (req: any, res: Response, next: NextFunction) {
|
export function createFromPOST (req: any, res: Response, next: NextFunction) {
|
||||||
let body = '';
|
let body = ''
|
||||||
if (req.body && req.body.length > config.documentMaxLength) {
|
if (req.body && req.body.length > config.documentMaxLength) {
|
||||||
return errors.errorTooLong(res)
|
return errors.errorTooLong(res)
|
||||||
} else if (req.body) {
|
} else if (req.body) {
|
||||||
body = req.body
|
body = req.body
|
||||||
}
|
}
|
||||||
body = body.replace(/[\r]/g, '');
|
body = body.replace(/[\r]/g, '')
|
||||||
return NoteUtils.newNote(req, res, body)
|
return NoteUtils.newNote(req, res, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
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.findNoteOrCreate(req, res, (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
|
// 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
|
||||||
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid));
|
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||||
break;
|
break
|
||||||
case 'slide':
|
case 'slide':
|
||||||
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid));
|
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
|
||||||
break;
|
break
|
||||||
case 'download':
|
case 'download':
|
||||||
exports.downloadMarkdown(req, res, note);
|
exports.downloadMarkdown(req, res, note)
|
||||||
break;
|
break
|
||||||
case 'info':
|
case 'info':
|
||||||
ActionController.getInfo(req, res, note);
|
ActionController.getInfo(req, res, note)
|
||||||
break;
|
break
|
||||||
case 'gist':
|
case 'gist':
|
||||||
ActionController.createGist(req, res, note);
|
ActionController.createGist(req, res, note)
|
||||||
break;
|
break
|
||||||
case 'revision':
|
case 'revision':
|
||||||
ActionController.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)
|
||||||
}
|
}
|
||||||
|
@ -124,9 +123,9 @@ 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 = 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
|
||||||
'Access-Control-Allow-Headers': 'Range',
|
'Access-Control-Allow-Headers': 'Range',
|
||||||
|
@ -135,7 +134,7 @@ export module NoteController {
|
||||||
'Cache-Control': 'private',
|
'Cache-Control': 'private',
|
||||||
'Content-disposition': 'attachment; filename=' + filename + '.md',
|
'Content-disposition': 'attachment; filename=' + filename + '.md',
|
||||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||||
});
|
})
|
||||||
res.send(body)
|
res.send(body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue