hedgedoc/lib/web/note/actions.ts
David Mehren 038db39c0e
Fix missing newlines in actions.ts
Signed-off-by: David Mehren <dmehren1@gmail.com>
2020-04-25 16:04:07 +02:00

92 lines
3 KiB
TypeScript

import { 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: any, 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: any, res: Response, note: Note): void {
const data = {
client_id: config.github.clientID,
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: any, 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)
return
}
} 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)
})
}
}