2020-04-11 10:08:40 -04:00
|
|
|
'use strict'
|
2020-04-12 07:11:06 -04:00
|
|
|
import { config } from './config'
|
2020-04-12 08:26:32 -04:00
|
|
|
import { Note, User } from './models'
|
|
|
|
|
|
|
|
import fs from 'fs'
|
|
|
|
|
2020-04-12 09:40:35 -04:00
|
|
|
import { logger } from './logger'
|
2020-04-12 08:26:32 -04:00
|
|
|
|
|
|
|
import { NoteUtils } from './web/note/util'
|
|
|
|
|
2020-04-12 11:39:00 -04:00
|
|
|
import { errors } from './errors'
|
2020-04-12 07:11:06 -04:00
|
|
|
|
2020-04-12 08:26:32 -04:00
|
|
|
import path from 'path'
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2020-04-12 08:26:32 -04:00
|
|
|
import request from 'request'
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2020-04-12 09:40:35 -04:00
|
|
|
function showIndex (req, res, _): void {
|
2020-04-12 08:26:32 -04:00
|
|
|
const authStatus = req.isAuthenticated()
|
|
|
|
const deleteToken = ''
|
2018-05-25 12:19:31 -04:00
|
|
|
|
2020-04-12 08:26:32 -04:00
|
|
|
const data = {
|
2018-05-25 12:19:31 -04:00
|
|
|
signin: authStatus,
|
2017-03-08 05:45:51 -05:00
|
|
|
infoMessage: req.flash('info'),
|
2018-05-22 19:14:52 -04:00
|
|
|
errorMessage: req.flash('error'),
|
2019-08-26 08:55:41 -04:00
|
|
|
imprint: fs.existsSync(path.join(config.docsPath, 'imprint.md')),
|
2018-05-22 19:14:52 -04:00
|
|
|
privacyStatement: fs.existsSync(path.join(config.docsPath, 'privacy.md')),
|
2018-05-25 12:19:31 -04:00
|
|
|
termsOfUse: fs.existsSync(path.join(config.docsPath, 'terms-of-use.md')),
|
|
|
|
deleteToken: deleteToken
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authStatus) {
|
2020-04-12 08:26:32 -04:00
|
|
|
User.findOne({
|
2018-05-25 12:19:31 -04:00
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
2020-04-12 08:26:32 -04:00
|
|
|
}).then(function (user: User | null) {
|
2018-05-25 12:19:31 -04:00
|
|
|
if (user) {
|
|
|
|
data.deleteToken = user.deleteToken
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('index.ejs', data)
|
2018-05-25 12:19:31 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('index.ejs', data)
|
2018-05-25 12:19:31 -04:00
|
|
|
}
|
2015-09-22 00:06:13 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 08:26:32 -04:00
|
|
|
function githubActionGist (req, res, note: Note): void {
|
|
|
|
const code = req.query.code
|
|
|
|
const state = req.query.state
|
2017-03-08 05:45:51 -05:00
|
|
|
if (!code || !state) {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
} else {
|
2020-04-12 09:40:35 -04:00
|
|
|
// This is the way the github api works, therefore we can't change it to camelcase
|
2020-04-12 08:26:32 -04:00
|
|
|
const data = {
|
2020-04-12 09:40:35 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
2017-03-08 05:45:51 -05:00
|
|
|
client_id: config.github.clientID,
|
2020-04-12 09:40:35 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
2017-03-08 05:45:51 -05:00
|
|
|
client_secret: config.github.clientSecret,
|
|
|
|
code: code,
|
|
|
|
state: state
|
|
|
|
}
|
2020-04-12 08:26:32 -04:00
|
|
|
const authUrl = 'https://github.com/login/oauth/access_token'
|
2017-03-08 05:45:51 -05:00
|
|
|
request({
|
|
|
|
url: authUrl,
|
|
|
|
method: 'POST',
|
|
|
|
json: data
|
|
|
|
}, function (error, httpResponse, body) {
|
|
|
|
if (!error && httpResponse.statusCode === 200) {
|
2020-04-12 08:26:32 -04:00
|
|
|
const accessToken = body.access_token
|
2017-03-08 05:45:51 -05:00
|
|
|
if (accessToken) {
|
2020-04-12 08:26:32 -04:00
|
|
|
const content = note.content
|
|
|
|
const title = Note.decodeTitle(note.title)
|
|
|
|
const filename = title.replace('/', ' ') + '.md'
|
|
|
|
const gist = {
|
|
|
|
files: {}
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
gist.files[filename] = {
|
2020-04-12 08:26:32 -04:00
|
|
|
content: content
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2020-04-12 08:26:32 -04:00
|
|
|
const gistUrl = 'https://api.github.com/gists'
|
2017-03-08 05:45:51 -05:00
|
|
|
request({
|
|
|
|
url: gistUrl,
|
|
|
|
headers: {
|
2018-06-24 08:13:38 -04:00
|
|
|
'User-Agent': 'CodiMD',
|
2020-04-12 08:26:32 -04:00
|
|
|
Authorization: 'token ' + accessToken
|
2017-03-08 05:45:51 -05:00
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
json: gist
|
|
|
|
}, function (error, httpResponse, body) {
|
|
|
|
if (!error && httpResponse.statusCode === 201) {
|
|
|
|
res.setHeader('referer', '')
|
|
|
|
res.redirect(body.html_url)
|
2016-04-20 06:03:55 -04:00
|
|
|
} else {
|
2020-04-12 08:26:32 -04:00
|
|
|
errors.errorForbidden(res)
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
} else {
|
2020-04-12 08:26:32 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-12 08:26:32 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-01-31 16:42:26 -05:00
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2020-04-12 09:40:35 -04:00
|
|
|
function githubActions (req, res, _): void {
|
2020-04-12 08:26:32 -04:00
|
|
|
const noteId = req.params.noteId
|
|
|
|
NoteUtils.findNoteOrCreate(req, res, function (note: Note) {
|
|
|
|
const action = req.params.action
|
2017-03-08 05:45:51 -05:00
|
|
|
switch (action) {
|
2020-04-12 08:26:32 -04:00
|
|
|
case 'gist':
|
|
|
|
githubActionGist(req, res, note)
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
default:
|
2018-03-07 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/' + noteId)
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-05-16 06:16:45 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 09:40:35 -04:00
|
|
|
function gitlabActionProjects (req, res, _): void {
|
2017-03-08 05:45:51 -05:00
|
|
|
if (req.isAuthenticated()) {
|
2020-04-12 08:26:32 -04:00
|
|
|
User.findOne({
|
2017-03-08 05:45:51 -05:00
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
2020-04-12 08:26:32 -04:00
|
|
|
if (!user) {
|
|
|
|
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
|
2017-03-08 05:45:51 -05:00
|
|
|
ret.accesstoken = user.accessToken
|
|
|
|
ret.profileid = user.profileid
|
|
|
|
request(
|
2019-05-30 18:27:56 -04:00
|
|
|
config.gitlab.baseURL + '/api/' + config.gitlab.version + '/projects?membership=yes&per_page=100&access_token=' + user.accessToken,
|
|
|
|
function (error, httpResponse, body) {
|
|
|
|
if (!error && httpResponse.statusCode === 200) {
|
|
|
|
ret.projects = JSON.parse(body)
|
|
|
|
return res.send(ret)
|
|
|
|
} else {
|
|
|
|
return res.send(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2017-03-08 05:45:51 -05:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('gitlab action projects failed: ' + err)
|
2020-04-12 08:26:32 -04:00
|
|
|
errors.errorInternalError(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
} else {
|
2020-04-12 08:26:32 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2016-05-16 06:16:45 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 09:40:35 -04:00
|
|
|
function gitlabActions (req, res, _): void {
|
2020-04-12 08:26:32 -04:00
|
|
|
const noteId = req.params.noteId
|
2020-04-12 09:40:35 -04:00
|
|
|
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
2020-04-12 08:26:32 -04:00
|
|
|
const action = req.params.action
|
|
|
|
switch (action) {
|
|
|
|
case 'projects':
|
|
|
|
gitlabActionProjects(req, res, note)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
res.redirect(config.serverURL + '/' + noteId)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-12 09:40:35 -04:00
|
|
|
|
|
|
|
export const response = {
|
|
|
|
showIndex: showIndex,
|
|
|
|
githubActions: githubActions,
|
|
|
|
gitlabActions: gitlabActions
|
|
|
|
}
|