hedgedoc/old_src/lib/web/note/actions.ts

96 lines
3.2 KiB
TypeScript
Raw Normal View History

import { Request, Response } from 'express'
import { Note, Revision } from '../../models'
import { logger } from '../../logger'
import { config } from '../../config'
import { errors } from '../../errors'
import shortId from 'shortid'
import moment from 'moment'
import querystring from 'querystring'
export function getInfo (req: Request, res: Response, note: Note): void {
const body = note.content
const extracted = Note.extractMeta(body)
const markdown = extracted.markdown
const meta = Note.parseMeta(extracted.meta)
const title = Note.decodeTitle(note.title)
const data = {
title: meta.title || title,
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
viewcount: note.viewcount,
createtime: note.createdAt,
updatetime: note.lastchangeAt
}
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)
}
export function createGist (req: Request, res: Response, note: Note): void {
const data = {
// eslint-disable-next-line @typescript-eslint/camelcase
client_id: config.github.clientID,
// eslint-disable-next-line @typescript-eslint/camelcase
redirect_uri: config.serverURL + '/auth/github/callback/' + Note.encodeNoteId(note.id) + '/gist',
scope: 'gist',
state: shortId.generate()
}
const query = querystring.stringify(data)
res.redirect('https://github.com/login/oauth/authorize?' + query)
}
export function getRevision (req: Request, res: Response, note: Note): void {
const actionId = req.params.actionId
if (actionId) {
const time = moment(parseInt(actionId))
if (time.isValid()) {
Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
if (err) {
logger.error(err)
errors.errorInternalError(res)
return
}
if (!content) {
errors.errorNotFound(res)
return
}
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 {
errors.errorNotFound(res)
}
} else {
Revision.getNoteRevisions(note, function (err, data) {
if (err) {
logger.error(err)
errors.errorInternalError(res)
return
}
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)
})
}
}