Files
gancio/server/api/controller/settings.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-05-30 12:04:14 +02:00
const Mastodon = require('mastodon-api')
2019-06-06 23:54:32 +02:00
const { setting: Setting } = require('../models')
2019-06-07 17:02:33 +02:00
2019-04-03 00:25:12 +02:00
const settingsController = {
2019-06-06 23:54:32 +02:00
2019-06-07 17:02:33 +02:00
async setAdminSetting(key, value) {
2019-06-06 23:54:32 +02:00
await Setting.findOrCreate({ where: { key },
2019-04-03 00:25:12 +02:00
defaults: { value } })
.spread((settings, created) => {
if (!created) return settings.update({ value })
})
},
2019-06-07 17:02:33 +02:00
async getAdminSettings(req, res) {
2019-04-03 00:25:12 +02:00
const settings = await settingsController.settings()
res.json(settings)
},
2019-05-30 12:04:14 +02:00
async getAuthURL(req, res) {
const instance = req.body.instance
const callback = `${process.env.baseurl}/api/settings/oauth`
2019-05-30 12:04:14 +02:00
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`,
2019-06-07 17:02:33 +02:00
'gancio', 'read write', callback)
2019-05-30 12:04:14 +02:00
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret,
`https://${instance}`, 'read write', callback)
await settingsController.setAdminSetting('mastodon_auth', { client_id, client_secret, instance })
res.json(url)
},
async code(req, res) {
const code = req.query.code
let client_id, client_secret, instance
const callback = `${process.env.baseurl}/api/settings/oauth`
2019-05-30 12:04:14 +02:00
const settings = await settingsController.settings()
({ client_id, client_secret, instance } = settings.mastodon_auth)
try {
const token = await Mastodon.getAccessToken(client_id, client_secret, code,
`https://${instance}`, callback)
const mastodon_auth = { client_id, client_secret, access_token: token, instance }
await settingsController.setAdminSetting('mastodon_auth', mastodon_auth)
2019-06-07 17:02:33 +02:00
2019-05-30 12:04:14 +02:00
res.redirect('/admin')
} catch (e) {
res.json(e)
}
},
2019-06-07 17:02:33 +02:00
async settings() {
2019-06-06 23:54:32 +02:00
const settings = await Setting.findAll()
return settings
2019-06-07 17:02:33 +02:00
}
2019-06-06 23:54:32 +02:00
2019-04-03 00:25:12 +02:00
}
module.exports = settingsController