better config / install from cli / allow_registration

This commit is contained in:
lesion
2019-06-21 23:52:18 +02:00
parent 4c3c7ee324
commit cf81a73f2f
38 changed files with 530 additions and 272 deletions

View File

@@ -2,15 +2,13 @@ const fs = require('fs')
const path = require('path')
const moment = require('moment')
const { event: Event, comment: Comment } = require('../models')
const config = require('../../config')
const config = require('config')
const Mastodon = require('mastodon-api')
const settingsController = require('./settings')
moment.locale(process.env.locale)
const botController = {
bot: null,
async initialize() {
const settings = await settingsController.settings()
if (!settings.mastodon_auth || !settings.mastodon_auth.access_token) return
const mastodon_auth = settings.mastodon_auth
botController.bot = new Mastodon({

View File

@@ -169,17 +169,19 @@ const eventController = {
async getAll(req, res) {
// this is due how v-calendar shows dates
const start = moment().year(req.params.year).month(req.params.month)
.startOf('month').startOf('isoWeek')
let end = moment().year(req.params.year).month(req.params.month).endOf('month')
.startOf('month').startOf('isoWeek').unix()
let end = moment().utc().year(req.params.year).month(req.params.month).endOf('month')
const shownDays = end.diff(start, 'days')
if (shownDays <= 34) end = end.add(1, 'week')
end = end.endOf('isoWeek')
end = end.endOf('isoWeek').unix()
const events = await Event.findAll({
where: {
is_visible: true,
[Op.and]: [
{ start_datetime: { [Op.gte]: start } },
{ start_datetime: { [Op.lte]: end } }
Sequelize.literal(`start_datetime >= ${start}`),
Sequelize.literal(`start_datetime <= ${end}`)
// { start_datetime: { [Op.gte]: start } },
// { start_datetime: { [Op.lte]: end } }
]
},
order: [

View File

@@ -1,47 +1,77 @@
const Mastodon = require('mastodon-api')
const { setting: Setting } = require('../models')
const config = require('config')
const settingsController = {
settings: null,
secretSettings: null,
async setAdminSetting(key, value) {
await Setting.findOrCreate({ where: { key },
defaults: { value } })
.spread((settings, created) => {
if (!created) return settings.update({ value })
})
// initialize instance settings from db
async init (req, res, next) {
if (!settingsController.settings) {
const settings = await Setting.findAll()
settingsController.settings = {}
settingsController.secretSettings = {}
settings.forEach( s => settingsController[s.is_secret?'secretSettings':'settings'][s.key] = s.value)
}
next()
},
async getAdminSettings(req, res) {
const settings = await settingsController.settings()
res.json(settings)
async set(key, value, is_secret=false) {
try {
await Setting.findOrCreate({
where: { key },
defaults: { value, is_secret }
}).spread((settings, created) => {
if (!created) return settings.update({ value, is_secret })
})
settingsController[is_secret?'secretSettings':'settings'][key]=value
console.error('settings ', settingsController.settings)
console.error('settings controller ', settingsController.secretSettings)
return true
} catch(e) {
console.error(e)
return false
}
},
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) {
res.json(settingsController.settings)
},
async getAuthURL(req, res) {
const instance = req.body.instance
const callback = `${process.env.baseurl}/api/settings/oauth`
const instance = req.body.instance
console.error('DENTRO GET AUTH URL ', instance)
const callback = `${config.baseurl}/api/settings/oauth`
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`,
'gancio', 'read write', callback)
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret,
`https://${instance}`, 'read write', callback)
await settingsController.setAdminSetting('mastodon_auth', { client_id, client_secret, instance })
await settingsController.set('mastodon_instance', instance )
await settingsController.set('mastodon_auth', { client_id, client_secret }, true)
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`
const settings = await settingsController.settings()
({ client_id, client_secret, instance } = settings.mastodon_auth)
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
try {
const token = await Mastodon.getAccessToken(client_id, client_secret, code,
const access_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)
const mastodon_auth = { client_id, client_secret, access_token }
await settingsController.set('mastodon_auth', mastodon_auth, true)
res.redirect('/admin')
} catch (e) {
@@ -49,11 +79,6 @@ const settingsController = {
}
},
async settings() {
const settings = await Setting.findAll()
return settings
}
}
module.exports = settingsController

View File

@@ -4,10 +4,11 @@ const crypto = require('crypto')
const jwt = require('jsonwebtoken')
const { Op } = require('sequelize')
const jsonwebtoken = require('jsonwebtoken')
const config = require('config')
const mail = require('../mail')
const { user: User, event: Event, tag: Tag, place: Place } = require('../models')
const eventController = require('./event')
const config = require('../../config')
const settingsController = require('./settings')
const userController = {
async login(req, res) {
@@ -219,6 +220,7 @@ const userController = {
async register(req, res) {
if (!settingsController.settings.allow_registration) return res.sendStatus(404)
const n_users = await User.count()
try {
// the first registered user will be an active admin