mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 01:35:18 -04:00
Migrate models to TypeScript
Co-authored-by: David Mehren <dmehren1@gmail.com> Co-authored-by: Yannick Bungers <git@innay.de> Co-authored-by: Philipp Hochkamp <me@phochkamp.de> Co-authored-by: nzbr <mail@nzbr.de> Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
54cd556f2f
commit
1d4107fe90
13 changed files with 565 additions and 539 deletions
97
lib/web/note/actions.ts
Normal file
97
lib/web/note/actions.ts
Normal file
|
@ -0,0 +1,97 @@
|
|||
import {Response} from "express";
|
||||
|
||||
const models = require('../../models')
|
||||
const logger = require('../../logger')
|
||||
const config = require('../../config')
|
||||
const errors = require('../../errors')
|
||||
const shortId = require('shortid')
|
||||
const moment = require('moment')
|
||||
const querystring = require('querystring')
|
||||
|
||||
export module ActionController {
|
||||
|
||||
// TODO: Use correct type for note
|
||||
export function getInfo(req: any, res: Response, note: any) {
|
||||
const body = note.content
|
||||
const extracted = models.Note.extractMeta(body)
|
||||
const markdown = extracted.markdown
|
||||
const meta = models.Note.parseMeta(extracted.meta)
|
||||
const createtime = note.createdAt
|
||||
const updatetime = note.lastchangeAt
|
||||
const title = models.Note.decodeTitle(note.title)
|
||||
const data = {
|
||||
title: meta.title || title,
|
||||
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
||||
viewcount: note.viewcount,
|
||||
createtime: createtime,
|
||||
updatetime: updatetime
|
||||
}
|
||||
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',
|
||||
'Cache-Control': 'private', // only cache by client
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
res.send(data)
|
||||
}
|
||||
|
||||
// TODO: Use correct type for note
|
||||
export function createGist(req: any, res: Response, note: any) {
|
||||
const data = {
|
||||
client_id: config.github.clientID,
|
||||
redirect_uri: config.serverURL + '/auth/github/callback/' + models.Note.encodeNoteId(note.id) + '/gist',
|
||||
scope: 'gist',
|
||||
state: shortId.generate()
|
||||
}
|
||||
const query = querystring.stringify(data)
|
||||
res.redirect('https://github.com/login/oauth/authorize?' + query)
|
||||
}
|
||||
|
||||
// TODO: Use correct type for note
|
||||
export function getRevision(req: any, res: Response, note: any) {
|
||||
const actionId = req.params.actionId
|
||||
if (actionId) {
|
||||
const time = moment(parseInt(actionId))
|
||||
if (time.isValid()) {
|
||||
models.Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
|
||||
if (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
}
|
||||
if (!content) {
|
||||
return errors.errorNotFound(res)
|
||||
}
|
||||
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',
|
||||
'Cache-Control': 'private', // only cache by client
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
res.send(content)
|
||||
})
|
||||
} else {
|
||||
return errors.errorNotFound(res)
|
||||
}
|
||||
} else {
|
||||
models.Revision.getNoteRevisions(note, function (err, data) {
|
||||
if (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
}
|
||||
const out = {
|
||||
revision: data
|
||||
}
|
||||
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',
|
||||
'Cache-Control': 'private', // only cache by client
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
res.send(out)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue