mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 01:35:18 -04:00
moved response.js to response.ts and readded missing return statements
Signed-off-by: Yannick Bungers <git@innay.de> Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
bb8fb93fee
commit
536c54ff0d
3 changed files with 121 additions and 101 deletions
|
@ -29,6 +29,7 @@ function createDmpWorker (): ChildProcess {
|
||||||
worker.on('message', function (data: Data) {
|
worker.on('message', function (data: Data) {
|
||||||
if (!data || !data.msg || !data.cacheKey) {
|
if (!data || !data.msg || !data.cacheKey) {
|
||||||
logger.error('dmp worker error: not enough data on message')
|
logger.error('dmp worker error: not enough data on message')
|
||||||
|
return
|
||||||
}
|
}
|
||||||
const cacheKey = data.cacheKey
|
const cacheKey = data.cacheKey
|
||||||
switch (data.msg) {
|
switch (data.msg) {
|
||||||
|
@ -150,6 +151,7 @@ export class Revision extends Model<Revision> {
|
||||||
}).then(function (revisions) {
|
}).then(function (revisions) {
|
||||||
if (revisions.length <= 0) {
|
if (revisions.length <= 0) {
|
||||||
errorCallback(null, null)
|
errorCallback(null, null)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
// measure target revision position
|
// measure target revision position
|
||||||
Revision.count({
|
Revision.count({
|
||||||
|
@ -162,6 +164,7 @@ export class Revision extends Model<Revision> {
|
||||||
}).then(function (count) {
|
}).then(function (count) {
|
||||||
if (count <= 0) {
|
if (count <= 0) {
|
||||||
errorCallback(null, null)
|
errorCallback(null, null)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
sendDmpWorker({
|
sendDmpWorker({
|
||||||
msg: 'get revision',
|
msg: 'get revision',
|
||||||
|
@ -180,6 +183,7 @@ export class Revision extends Model<Revision> {
|
||||||
Revision.saveAllNotesRevision(function (err, notes: Note[]) {
|
Revision.saveAllNotesRevision(function (err, notes: Note[]) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err, null)
|
callback(err, null)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if (!notes || notes.length <= 0) {
|
if (!notes || notes.length <= 0) {
|
||||||
callback(null, notes)
|
callback(null, notes)
|
||||||
|
@ -218,6 +222,7 @@ export class Revision extends Model<Revision> {
|
||||||
}).then(function (notes: Note[]) {
|
}).then(function (notes: Note[]) {
|
||||||
if (notes.length <= 0) {
|
if (notes.length <= 0) {
|
||||||
callback(null, notes)
|
callback(null, notes)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
const savedNotes: Note[] = []
|
const savedNotes: Note[] = []
|
||||||
async.each(notes, function (note: Note, _callback) {
|
async.each(notes, function (note: Note, _callback) {
|
||||||
|
@ -241,6 +246,7 @@ export class Revision extends Model<Revision> {
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err, null)
|
callback(err, null)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
// return null when no notes need saving at this moment but have delayed tasks to be done
|
// return null when no notes need saving at this moment but have delayed tasks to be done
|
||||||
const result = ((savedNotes.length === 0) && (notes.length > 0)) ? null : savedNotes
|
const result = ((savedNotes.length === 0) && (notes.length > 0)) ? null : savedNotes
|
||||||
|
@ -285,6 +291,7 @@ export class Revision extends Model<Revision> {
|
||||||
}, function (err, patch) {
|
}, function (err, patch) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('save note revision error', err)
|
logger.error('save note revision error', err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if (!patch) {
|
if (!patch) {
|
||||||
// if patch is empty (means no difference) then just update the latest revision updated time
|
// if patch is empty (means no difference) then just update the latest revision updated time
|
||||||
|
|
|
@ -1,30 +1,29 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
// response
|
|
||||||
// external modules
|
|
||||||
|
|
||||||
// core
|
|
||||||
import { config } from './config'
|
import { config } from './config'
|
||||||
|
import { Note, User } from './models'
|
||||||
|
|
||||||
var fs = require('fs')
|
import fs from 'fs'
|
||||||
var path = require('path')
|
|
||||||
var request = require('request')
|
|
||||||
var logger = require('./logger')
|
|
||||||
var models = require('./models')
|
|
||||||
const noteUtil = require('./web/note/util')
|
|
||||||
const errors = require('./errors')
|
|
||||||
|
|
||||||
// public
|
import {logger} from './logger'
|
||||||
var response = {
|
|
||||||
|
import { NoteUtils } from './web/note/util'
|
||||||
|
|
||||||
|
import errors from './errors'
|
||||||
|
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
|
import request from 'request'
|
||||||
|
export const response = {
|
||||||
showIndex: showIndex,
|
showIndex: showIndex,
|
||||||
githubActions: githubActions,
|
githubActions: githubActions,
|
||||||
gitlabActions: gitlabActions
|
gitlabActions: gitlabActions
|
||||||
}
|
}
|
||||||
|
|
||||||
function showIndex (req, res, next) {
|
function showIndex (req, res, next) {
|
||||||
var authStatus = req.isAuthenticated()
|
const authStatus = req.isAuthenticated()
|
||||||
var deleteToken = ''
|
const deleteToken = ''
|
||||||
|
|
||||||
var data = {
|
const data = {
|
||||||
signin: authStatus,
|
signin: authStatus,
|
||||||
infoMessage: req.flash('info'),
|
infoMessage: req.flash('info'),
|
||||||
errorMessage: req.flash('error'),
|
errorMessage: req.flash('error'),
|
||||||
|
@ -35,11 +34,11 @@ function showIndex (req, res, next) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (authStatus) {
|
if (authStatus) {
|
||||||
models.User.findOne({
|
User.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: req.user.id
|
id: req.user.id
|
||||||
}
|
}
|
||||||
}).then(function (user) {
|
}).then(function (user: User | null) {
|
||||||
if (user) {
|
if (user) {
|
||||||
data.deleteToken = user.deleteToken
|
data.deleteToken = user.deleteToken
|
||||||
res.render('index.ejs', data)
|
res.render('index.ejs', data)
|
||||||
|
@ -50,10 +49,67 @@ function showIndex (req, res, next) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function githubActions (req, res, next) {
|
function githubActionGist (req, res, note: Note): void {
|
||||||
var noteId = req.params.noteId
|
const code = req.query.code
|
||||||
noteUtil.findNote(req, res, function (note) {
|
const state = req.query.state
|
||||||
var action = req.params.action
|
if (!code || !state) {
|
||||||
|
return errors.errorForbidden(res)
|
||||||
|
} else {
|
||||||
|
const data = {
|
||||||
|
client_id: config.github.clientID,
|
||||||
|
client_secret: config.github.clientSecret,
|
||||||
|
code: code,
|
||||||
|
state: state
|
||||||
|
}
|
||||||
|
const authUrl = 'https://github.com/login/oauth/access_token'
|
||||||
|
request({
|
||||||
|
url: authUrl,
|
||||||
|
method: 'POST',
|
||||||
|
json: data
|
||||||
|
}, function (error, httpResponse, body) {
|
||||||
|
if (!error && httpResponse.statusCode === 200) {
|
||||||
|
const accessToken = body.access_token
|
||||||
|
if (accessToken) {
|
||||||
|
const content = note.content
|
||||||
|
const title = Note.decodeTitle(note.title)
|
||||||
|
const filename = title.replace('/', ' ') + '.md'
|
||||||
|
const gist = {
|
||||||
|
files: {}
|
||||||
|
}
|
||||||
|
gist.files[filename] = {
|
||||||
|
content: content
|
||||||
|
}
|
||||||
|
const gistUrl = 'https://api.github.com/gists'
|
||||||
|
request({
|
||||||
|
url: gistUrl,
|
||||||
|
headers: {
|
||||||
|
'User-Agent': 'CodiMD',
|
||||||
|
Authorization: 'token ' + accessToken
|
||||||
|
},
|
||||||
|
method: 'POST',
|
||||||
|
json: gist
|
||||||
|
}, function (error, httpResponse, body) {
|
||||||
|
if (!error && httpResponse.statusCode === 201) {
|
||||||
|
res.setHeader('referer', '')
|
||||||
|
res.redirect(body.html_url)
|
||||||
|
} else {
|
||||||
|
errors.errorForbidden(res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
errors.errorForbidden(res)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
errors.errorForbidden(res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function githubActions (req, res, next): void {
|
||||||
|
const noteId = req.params.noteId
|
||||||
|
NoteUtils.findNoteOrCreate(req, res, function (note: Note) {
|
||||||
|
const action = req.params.action
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'gist':
|
case 'gist':
|
||||||
githubActionGist(req, res, note)
|
githubActionGist(req, res, note)
|
||||||
|
@ -65,87 +121,27 @@ function githubActions (req, res, next) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function githubActionGist (req, res, note) {
|
function gitlabActionProjects (req, res, note): void {
|
||||||
var code = req.query.code
|
|
||||||
var state = req.query.state
|
|
||||||
if (!code || !state) {
|
|
||||||
return errors.errorForbidden(res)
|
|
||||||
} else {
|
|
||||||
var data = {
|
|
||||||
client_id: config.github.clientID,
|
|
||||||
client_secret: config.github.clientSecret,
|
|
||||||
code: code,
|
|
||||||
state: state
|
|
||||||
}
|
|
||||||
var authUrl = 'https://github.com/login/oauth/access_token'
|
|
||||||
request({
|
|
||||||
url: authUrl,
|
|
||||||
method: 'POST',
|
|
||||||
json: data
|
|
||||||
}, function (error, httpResponse, body) {
|
|
||||||
if (!error && httpResponse.statusCode === 200) {
|
|
||||||
var accessToken = body.access_token
|
|
||||||
if (accessToken) {
|
|
||||||
var content = note.content
|
|
||||||
var title = models.Note.decodeTitle(note.title)
|
|
||||||
var filename = title.replace('/', ' ') + '.md'
|
|
||||||
var gist = {
|
|
||||||
'files': {}
|
|
||||||
}
|
|
||||||
gist.files[filename] = {
|
|
||||||
'content': content
|
|
||||||
}
|
|
||||||
var gistUrl = 'https://api.github.com/gists'
|
|
||||||
request({
|
|
||||||
url: gistUrl,
|
|
||||||
headers: {
|
|
||||||
'User-Agent': 'CodiMD',
|
|
||||||
'Authorization': 'token ' + accessToken
|
|
||||||
},
|
|
||||||
method: 'POST',
|
|
||||||
json: gist
|
|
||||||
}, function (error, httpResponse, body) {
|
|
||||||
if (!error && httpResponse.statusCode === 201) {
|
|
||||||
res.setHeader('referer', '')
|
|
||||||
res.redirect(body.html_url)
|
|
||||||
} else {
|
|
||||||
return errors.errorForbidden(res)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
return errors.errorForbidden(res)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return errors.errorForbidden(res)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function gitlabActions (req, res, next) {
|
|
||||||
var noteId = req.params.noteId
|
|
||||||
noteUtil.findNote(req, res, function (note) {
|
|
||||||
var action = req.params.action
|
|
||||||
switch (action) {
|
|
||||||
case 'projects':
|
|
||||||
gitlabActionProjects(req, res, note)
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
res.redirect(config.serverURL + '/' + noteId)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function gitlabActionProjects (req, res, note) {
|
|
||||||
if (req.isAuthenticated()) {
|
if (req.isAuthenticated()) {
|
||||||
models.User.findOne({
|
User.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: req.user.id
|
id: req.user.id
|
||||||
}
|
}
|
||||||
}).then(function (user) {
|
}).then(function (user) {
|
||||||
if (!user) { return errors.errorNotFound(res) }
|
if (!user) {
|
||||||
var ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version }
|
errors.errorNotFound(res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
class GitlabReturn {
|
||||||
|
baseURL;
|
||||||
|
version;
|
||||||
|
accesstoken;
|
||||||
|
profileid;
|
||||||
|
projects;
|
||||||
|
}
|
||||||
|
const ret: GitlabReturn = new GitlabReturn()
|
||||||
|
ret.baseURL = config.gitlab.baseURL
|
||||||
|
ret.version = config.gitlab.version
|
||||||
ret.accesstoken = user.accessToken
|
ret.accesstoken = user.accessToken
|
||||||
ret.profileid = user.profileid
|
ret.profileid = user.profileid
|
||||||
request(
|
request(
|
||||||
|
@ -161,11 +157,24 @@ function gitlabActionProjects (req, res, note) {
|
||||||
)
|
)
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
logger.error('gitlab action projects failed: ' + err)
|
logger.error('gitlab action projects failed: ' + err)
|
||||||
return errors.errorInternalError(res)
|
errors.errorInternalError(res)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
return errors.errorForbidden(res)
|
errors.errorForbidden(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = response
|
function gitlabActions (req, res, next): void {
|
||||||
|
const noteId = req.params.noteId
|
||||||
|
noteUtil.findNote(req, res, function (note) {
|
||||||
|
const action = req.params.action
|
||||||
|
switch (action) {
|
||||||
|
case 'projects':
|
||||||
|
gitlabActionProjects(req, res, note)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
res.redirect(config.serverURL + '/' + noteId)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
|
@ -51,9 +51,11 @@ export function getRevision (req: any, res: Response, note: Note): void {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
errors.errorInternalError(res)
|
errors.errorInternalError(res)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if (!content) {
|
if (!content) {
|
||||||
errors.errorNotFound(res)
|
errors.errorNotFound(res)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
res.set({
|
res.set({
|
||||||
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
||||||
|
@ -66,12 +68,14 @@ export function getRevision (req: any, res: Response, note: Note): void {
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
errors.errorNotFound(res)
|
errors.errorNotFound(res)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Revision.getNoteRevisions(note, function (err, data) {
|
Revision.getNoteRevisions(note, function (err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
errors.errorInternalError(res)
|
errors.errorInternalError(res)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
const out = {
|
const out = {
|
||||||
revision: data
|
revision: data
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue