.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
const { Settings } = require('../model')
|
||||
const { SHARED_CONF } = require('../../../config')
|
||||
const Mastodon = require('mastodon-api')
|
||||
const { setting: Setting } = require('../models')
|
||||
const config = require('../../config').SHARED_CONF
|
||||
|
||||
const settingsController = {
|
||||
|
||||
async setAdminSetting (key, value) {
|
||||
await Settings.findOrCreate({ where: { key },
|
||||
await Setting.findOrCreate({ where: { key },
|
||||
defaults: { value } })
|
||||
.spread((settings, created) => {
|
||||
if (!created) return settings.update({ value })
|
||||
@@ -16,11 +17,15 @@ const settingsController = {
|
||||
res.json(settings)
|
||||
},
|
||||
|
||||
async getConfig (req, res) {
|
||||
res.json(config)
|
||||
},
|
||||
|
||||
async getAuthURL(req, res) {
|
||||
const instance = req.body.instance
|
||||
const callback = `${SHARED_CONF.baseurl}/api/settings/oauth`
|
||||
const callback = `${config.baseurl}/api/settings/oauth`
|
||||
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`,
|
||||
SHARED_CONF.title, 'read write', callback)
|
||||
config.title, 'read write', callback)
|
||||
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret,
|
||||
`https://${instance}`, 'read write', callback)
|
||||
|
||||
@@ -31,19 +36,16 @@ const settingsController = {
|
||||
async code(req, res) {
|
||||
const code = req.query.code
|
||||
let client_id, client_secret, instance
|
||||
const callback = `${SHARED_CONF.baseurl}/api/settings/oauth`
|
||||
console.error('sono dentro CODEEEEEEEEEE', code)
|
||||
const callback = `${config.baseurl}/api/settings/oauth`
|
||||
|
||||
const settings = await settingsController.settings()
|
||||
|
||||
console.log(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 }
|
||||
console.error(mastodon_auth)
|
||||
await settingsController.setAdminSetting('mastodon_auth', mastodon_auth)
|
||||
|
||||
res.redirect('/admin')
|
||||
@@ -53,13 +55,11 @@ const settingsController = {
|
||||
},
|
||||
|
||||
async settings () {
|
||||
const settings = await Settings.findAll()
|
||||
const map = {}
|
||||
settings.forEach(setting => {
|
||||
map[setting.key] = setting.value
|
||||
})
|
||||
return map
|
||||
}
|
||||
console.error('ma sono dentro settings ?!?!')
|
||||
const settings = await Setting.findAll()
|
||||
return settings
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
module.exports = settingsController
|
||||
|
||||
Reference in New Issue
Block a user