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

93 lines
3.0 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')
const config = require('config')
2019-04-03 00:25:12 +02:00
const settingsController = {
2019-06-25 01:05:38 +02:00
settings: { initialized: false },
secretSettings: {},
2019-06-06 23:54:32 +02:00
// initialize instance settings from db
2019-06-25 01:05:38 +02:00
async initialize () {
if (!settingsController.settings.initialized) {
const settings = await Setting.findAll()
2019-06-25 01:05:38 +02:00
settingsController.settings.initialized = true
settings.forEach( s => {
if (s.is_secret) {
settingsController.secretSettings[s.key] = s.value
} else {
settingsController.settings[s.key] = s.value
}
})
}
},
async set(key, value, is_secret=false) {
try {
await Setting.findOrCreate({
where: { key },
defaults: { value, is_secret }
2019-07-13 01:02:11 +02:00
}).spread((setting, created) => {
if (!created) return setting.update({ value, is_secret })
2019-04-03 00:25:12 +02:00
})
settingsController[is_secret?'secretSettings':'settings'][key]=value
return true
} catch(e) {
console.error(e)
return false
}
2019-04-03 00:25:12 +02:00
},
async setRequest(req, res) {
const { key, value, is_secret } = req.body
const ret = await settingsController.set(key, value, is_secret)
if (ret) res.sendStatus(200)
else res.sendStatus(400)
},
getAllRequest(req, res) {
2019-06-25 01:05:38 +02:00
// get public settings and public configuration
const settings = {
...settingsController.settings,
2019-07-08 00:06:56 +02:00
baseurl: config.baseurl,
title: config.title,
description: config.description
2019-06-25 01:05:38 +02:00
}
res.json(settings)
2019-04-03 00:25:12 +02:00
},
2019-05-30 12:04:14 +02:00
async getAuthURL(req, res) {
const instance = req.body.instance
const callback = `${config.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.set('mastodon_instance', instance )
await settingsController.set('mastodon_auth', { client_id, client_secret }, true)
2019-05-30 12:04:14 +02:00
res.json(url)
},
async code(req, res) {
const code = req.query.code
const callback = `${config.baseurl}/api/settings/oauth`
const client_id = settingsController.secretSettings.mastodon_auth.client_id
const client_secret = settingsController.secretSettings.mastodon_auth.client_secret
const instance = settingsController.settings.mastodon_instance
2019-05-30 12:04:14 +02:00
try {
const access_token = await Mastodon.getAccessToken(client_id, client_secret, code,
2019-05-30 12:04:14 +02:00
`https://${instance}`, callback)
const mastodon_auth = { client_id, client_secret, access_token }
await settingsController.set('mastodon_auth', mastodon_auth, true)
2019-06-26 14:44:21 +02:00
const botController = require('./bot')
2019-06-25 01:05:38 +02:00
botController.initialize()
2019-05-30 12:04:14 +02:00
res.redirect('/admin')
} catch (e) {
res.json(e)
}
},
2019-04-03 00:25:12 +02:00
}
2019-06-25 01:05:38 +02:00
setTimeout(settingsController.initialize, 200)
2019-04-03 00:25:12 +02:00
module.exports = settingsController