2017-03-14 01:02:43 -04:00
|
|
|
'use strict'
|
2017-03-08 05:45:51 -05:00
|
|
|
// response
|
|
|
|
// external modules
|
|
|
|
var fs = require('fs')
|
2018-05-22 19:14:52 -04:00
|
|
|
var path = require('path')
|
2017-03-08 05:45:51 -05:00
|
|
|
var request = require('request')
|
|
|
|
// core
|
2017-04-12 12:20:28 -04:00
|
|
|
var config = require('./config')
|
|
|
|
var logger = require('./logger')
|
2017-03-08 05:45:51 -05:00
|
|
|
var models = require('./models')
|
2019-10-27 08:51:53 -04:00
|
|
|
const noteUtil = require('./web/note/util')
|
|
|
|
const errors = require('./errors')
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// public
|
2015-05-04 03:53:29 -04:00
|
|
|
var response = {
|
2017-03-08 05:45:51 -05:00
|
|
|
showIndex: showIndex,
|
|
|
|
githubActions: githubActions,
|
|
|
|
gitlabActions: gitlabActions
|
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function showIndex (req, res, next) {
|
2018-05-25 12:19:31 -04:00
|
|
|
var authStatus = req.isAuthenticated()
|
|
|
|
var deleteToken = ''
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
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) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function githubActions (req, res, next) {
|
|
|
|
var noteId = req.params.noteId
|
2019-10-27 08:51:53 -04:00
|
|
|
noteUtil.findNote(req, res, function (note) {
|
2017-03-08 05:45:51 -05:00
|
|
|
var action = req.params.action
|
|
|
|
switch (action) {
|
|
|
|
case 'gist':
|
|
|
|
githubActionGist(req, res, note)
|
|
|
|
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-01-31 16:42:26 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function githubActionGist (req, res, note) {
|
|
|
|
var code = req.query.code
|
|
|
|
var state = req.query.state
|
|
|
|
if (!code || !state) {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
} 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: {
|
2018-06-24 08:13:38 -04:00
|
|
|
'User-Agent': 'CodiMD',
|
2017-03-08 05:45:51 -05:00
|
|
|
'Authorization': 'token ' + accessToken
|
|
|
|
},
|
|
|
|
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 {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
return 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
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function gitlabActions (req, res, next) {
|
|
|
|
var noteId = req.params.noteId
|
2019-10-27 08:51:53 -04:00
|
|
|
noteUtil.findNote(req, res, function (note) {
|
2017-03-08 05:45:51 -05:00
|
|
|
var action = req.params.action
|
|
|
|
switch (action) {
|
|
|
|
case 'projects':
|
|
|
|
gitlabActionProjects(req, res, note)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function gitlabActionProjects (req, res, note) {
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
2019-10-27 08:51:53 -04:00
|
|
|
if (!user) { return errors.errorNotFound(res) }
|
2018-07-30 09:47:09 -04:00
|
|
|
var ret = { baseURL: config.gitlab.baseURL, 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)
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorInternalError(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorForbidden(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2016-05-16 06:16:45 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
module.exports = response
|