admin settings fix #12, fix #13 image in mastodon

This commit is contained in:
lesion
2019-03-12 01:16:52 +01:00
parent 9702f93cf9
commit c5989fa8b4
23 changed files with 232 additions and 85 deletions

View File

@@ -1,10 +1,13 @@
const { User, Event, Comment, Tag } = require('../model')
// const { User, Event, Comment, Tag } = require('../model')
const config = require('../config')
const Mastodon = require('mastodon-api')
const Sequelize = require('sequelize')
const Op = Sequelize.Op
// const Sequelize = require('sequelize')
// const Op = Sequelize.Op
const fs = require('fs')
const path = require('path')
const moment = require('moment')
moment.locale('it')
const botController = {
bots: [],
// async initialize () {
@@ -30,12 +33,20 @@ const botController = {
// listener.on('error', botController.error)
// botController.bots.push({ email: user.email, bot })
// },
post (user, event) {
const { client_id, client_secret, access_token } = user.mastodon_auth
const bot = new Mastodon({ access_token, api_url: `https://${user.mastodon_instance}/api/v1/` })
async post (mastodon_auth, event) {
const { access_token, instance } = mastodon_auth
const bot = new Mastodon({ access_token, api_url: `https://${instance}/api/v1/` })
const status = `${event.title} @ ${event.place.name} ${moment(event.start_datetime).format('ddd, D MMMM HH:mm')} -
${event.description} - ${event.tags.map(t => '#' + t.tag).join(' ')} ${config.baseurl}/event/${event.id}`
return bot.post('/statuses', { status, visibility: 'private' })
let media
if (event.image_path) {
const file = path.join(__dirname, '..', '..', event.image_path)
if (fs.statSync(file)) {
media = await bot.post('media', { file: fs.createReadStream(file) })
}
}
return bot.post('statuses', { status, visibility: 'direct', media_ids: media ? [media.data.id] : [] })
}
// async message (msg) {
// console.log(msg)

View File

@@ -19,7 +19,6 @@ const eventController = {
},
async getMeta (req, res) {
console.log('GET META')
const places = await Place.findAll()
const tags = await Tag.findAll()
res.json({ tags, places })
@@ -28,6 +27,7 @@ const eventController = {
async getNotifications (event) {
function match (event, filters) {
// matches if no filter specified
if (!filters) return true
if (!filters.tags.length && !filters.places.length) return true
if (filters.tags.length) {
const m = lodash.intersection(event.tags.map(t => t.tag), filters.tags)
@@ -47,7 +47,6 @@ const eventController = {
async updateTag (req, res) {
const tag = await Tag.findByPk(req.body.tag)
console.log(tag)
if (tag) {
res.json(await tag.update(req.body))
} else {

View File

@@ -5,6 +5,7 @@ const moment = require('moment')
const ics = require('ics')
const exportController = {
async export (req, res) {
console.log('type ', req.params.type)
const type = req.params.type
@@ -34,6 +35,7 @@ const exportController = {
return exportController.ics(res, events)
}
},
async feed (res, events) {
res.type('application/rss+xml; charset=UTF-8')
res.render('feed/rss.pug', { events, config, moment })

View File

@@ -0,0 +1,27 @@
const { Settings } = require('../model')
const settingsController = {
async setAdminSetting (key, value) {
await Settings.findOrCreate({ where: { key },
defaults: { value } })
.spread((settings, created) => {
if (!created) return settings.update({ value })
})
},
async getAdminSettings (req, res) {
const settings = await settingsController.settings()
res.json(settings)
},
async settings () {
const settings = await Settings.findAll()
const map = {}
settings.forEach(setting => {
map[setting.key] = setting.value
})
return map
}
}
module.exports = settingsController

View File

@@ -2,7 +2,8 @@ const jwt = require('jsonwebtoken')
const Mastodon = require('mastodon-api')
const User = require('../models/user')
const { Event, Tag, Place, Notification } = require('../models/event')
const { Event, Tag, Place } = require('../models/event')
const settingsController = require('./settings')
const eventController = require('./event')
const config = require('../config')
const mail = require('../mail')
@@ -57,7 +58,7 @@ const userController = {
async addEvent (req, res) {
const body = req.body
// remove description tag and create anchor tag
// remove description tag and create anchor tags
const description = body.description
.replace(/(<([^>]+)>)/ig, '')
.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1">$1</a>')
@@ -68,28 +69,27 @@ const userController = {
multidate: body.multidate,
start_datetime: body.start_datetime,
end_datetime: body.end_datetime,
is_visible: req.user ? true : false
is_visible: !!req.user
}
if (req.file) {
eventDetails.image_path = req.file.path
}
let event = await Event.create(eventDetails)
// create place
let place
try {
place = await Place.findOrCreate({ where: { name: body.place_name },
defaults: { address: body.place_address } })
.spread((place, created) => place)
await event.setPlace(place)
} catch (e) {
console.log(e)
console.error(e)
}
let event = await Event.create(eventDetails)
await event.setPlace(place)
// create/assign tags
console.log(body.tags)
if (body.tags) {
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
@@ -98,22 +98,11 @@ const userController = {
if (req.user) await req.user.addEvent(event)
event = await Event.findByPk(event.id, { include: [User, Tag, Place] })
// check if bot exists
if (req.user && req.user.mastodon_auth) {
const post = await bot.post(req.user, event)
event.activitypub_id = post.id
event.save()
}
if (req.user) {
// insert notifications
const notifications = await eventController.getNotifications(event)
await event.setNotifications(notifications)
} else {
const notification = await Notification.create({ type: 'admin_email' })
await event.setNotification(notification)
}
return res.json(event)
},
@@ -139,7 +128,6 @@ const userController = {
}
await event.setPlace(place)
await event.setTags([])
console.log(body.tags)
if (body.tags) {
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
const tags = await Tag.findAll({ where: { tag: { [Op.eq]: body.tags } } })
@@ -157,26 +145,47 @@ const userController = {
async getAuthURL (req, res) {
const instance = req.body.instance
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`, 'eventi', 'read write', `${config.baseurl}/settings`)
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret, `https://${instance}`, 'read write', `${config.baseurl}/settings`)
console.log(req.user)
req.user.mastodon_instance = instance
req.user.mastodon_auth = { client_id, client_secret }
await req.user.save()
const is_admin = req.body.admin && req.user.is_admin
const callback = `${config.baseurl}/${is_admin ? 'admin/oauth' : 'settings'}`
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`,
config.title, 'read write', callback)
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret,
`https://${instance}`, 'read write', callback)
if (is_admin) {
await settingsController.setAdminSetting('mastodon_auth', { client_id, client_secret, instance })
} else {
req.user.mastodon_auth = { client_id, client_secret, instance }
await req.user.save()
}
res.json(url)
},
async code (req, res) {
const code = req.body.code
const { client_id, client_secret } = req.user.mastodon_auth
const instance = req.user.mastodon_instance
const { code, is_admin } = req.body
let client_id, client_secret, instance
const callback = `${config.baseurl}/${is_admin ? 'admin/oauth' : 'settings'}`
if (is_admin) {
const settings = await settingsController.settings();
({ client_id, client_secret, instance } = settings.mastodon_auth)
} else {
({ client_id, client_secret, instance } = req.user.mastodon_auth)
}
try {
const token = await Mastodon.getAccessToken(client_id, client_secret, code, `https://${instance}`, `${config.baseurl}/settings`)
const mastodon_auth = { client_id, client_secret, access_token: token }
req.user.mastodon_auth = mastodon_auth
await req.user.save()
await bot.add(req.user, token)
res.json(req.user)
const token = await Mastodon.getAccessToken(client_id, client_secret, code,
`https://${instance}`, callback)
const mastodon_auth = { client_id, client_secret, access_token: token, instance }
if (is_admin) {
await settingsController.setAdminSetting('mastodon_auth', mastodon_auth)
res.json(instance)
} else {
req.user.mastodon_auth = mastodon_auth
await req.user.save()
// await bot.add(req.user, token)
res.json(req.user)
}
} catch (e) {
res.json(e)
}
@@ -204,13 +213,18 @@ const userController = {
},
async register (req, res) {
const n_users = await User.count()
try {
req.body.is_active = false
if (n_users === 0) {
// admin will be the first registered user
req.body.is_active = req.body.is_admin = true
} else {
req.body.is_active = false
}
const user = await User.create(req.body)
try {
mail.send(user.email, 'register', { user })
} catch (e) {
console.log(e)
return res.status(400).json(e)
}
const payload = { email: user.email }