allow_anon_event, comment via mastodon
This commit is contained in:
@@ -5,15 +5,17 @@ const { event: Event, comment: Comment } = require('../models')
|
||||
const config = require('config')
|
||||
const Mastodon = require('mastodon-api')
|
||||
const settingsController = require('./settings')
|
||||
const get = require('lodash/get')
|
||||
|
||||
const botController = {
|
||||
bot: null,
|
||||
async initialize() {
|
||||
if (!settings.mastodon_auth || !settings.mastodon_auth.access_token) return
|
||||
const mastodon_auth = settings.mastodon_auth
|
||||
const access_token = get(settingsController.secretSettings, 'mastodon_auth.access_token')
|
||||
const instance = get(settingsController.settings, 'mastodon_instance')
|
||||
if (!access_token || !instance) return
|
||||
botController.bot = new Mastodon({
|
||||
access_token: mastodon_auth.access_token,
|
||||
api_url: `https://${mastodon_auth.instance}/api/v1`
|
||||
access_token,
|
||||
api_url: `https://${instance}/api/v1`
|
||||
})
|
||||
const listener = botController.bot.stream('/streaming/direct')
|
||||
listener.on('message', botController.message)
|
||||
@@ -38,10 +40,8 @@ const botController = {
|
||||
// listener.on('error', botController.error)
|
||||
// botController.bots.push({ email: user.email, bot })
|
||||
},
|
||||
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')} -
|
||||
async post(instance, access_token, event) {
|
||||
const status = `${event.title} @${event.place.name} ${moment(event.start_datetime).format('ddd, D MMMM HH:mm')} -
|
||||
${event.description.length > 200 ? event.description.substr(0, 200) + '...' : event.description} - ${event.tags.map(t => '#' + t.tag).join(' ')} ${config.baseurl}/event/${event.id}`
|
||||
|
||||
let media
|
||||
@@ -51,42 +51,30 @@ ${event.description.length > 200 ? event.description.substr(0, 200) + '...' : ev
|
||||
media = await bot.post('media', { file: fs.createReadStream(file) })
|
||||
}
|
||||
}
|
||||
return bot.post('statuses', { status, visibility: 'direct', media_ids: media ? [media.data.id] : [] })
|
||||
return botController.bot.post('/statuses', { status, visibility: 'direct', media_ids: media ? [media.data.id] : [] })
|
||||
},
|
||||
|
||||
// TOFIX: enable message deletion
|
||||
async message(msg) {
|
||||
const replyid = msg.data.in_reply_to_id || msg.data.last_status.in_reply_to_id
|
||||
if (!replyid) return
|
||||
const event = await Event.findOne({ where: { activitypub_id: replyid } })
|
||||
let event = await Event.findOne({ where: { activitypub_id: replyid } })
|
||||
if (!event) {
|
||||
// check for comment..
|
||||
// const comment = await Comment.findOne( {where: { }})
|
||||
return
|
||||
const comment = await Comment.findOne( { include: [Event], where: { activitypub_id: replyid }})
|
||||
if (!comment) return
|
||||
event = comment.event
|
||||
}
|
||||
const comment = await Comment.create({
|
||||
activitypub_id: msg.data.last_status.id,
|
||||
// text: msg.data.last_status.content,
|
||||
data: msg.data
|
||||
// author: msg.data.accounts[0].username
|
||||
data: msg.data,
|
||||
eventId: event.id
|
||||
})
|
||||
event.addComment(comment)
|
||||
// const comment = await Comment.findOne( { where: {activitypub_id: msg.data.in_reply_to}} )
|
||||
// console.log('dentro message ', data)
|
||||
|
||||
// add comment to specified event
|
||||
// let comment
|
||||
// if (!event) {
|
||||
// const comment = await Comment.findOne({where: {activitypub_id: req.body.id}, include: Event})
|
||||
// event = comment.event
|
||||
// }
|
||||
// const comment = new Comment(req.body)
|
||||
// event.addComment(comment)
|
||||
},
|
||||
error(err) {
|
||||
console.log('error ', err)
|
||||
}
|
||||
}
|
||||
|
||||
return setTimeout(botController.initialize, 2000)
|
||||
// botController.initialize()
|
||||
setTimeout(botController.initialize, 5000)
|
||||
module.exports = botController
|
||||
|
||||
@@ -95,7 +95,9 @@ const eventController = {
|
||||
],
|
||||
order: [ [Comment, 'id', 'DESC'], [Tag, 'weigth', 'DESC'] ]
|
||||
})
|
||||
|
||||
if (event) {
|
||||
event.activitypub_id = event.activitypub_id ? String(event.activitypub_id) : null
|
||||
res.json(event)
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
const Mastodon = require('mastodon-api')
|
||||
const { setting: Setting } = require('../models')
|
||||
const config = require('config')
|
||||
|
||||
const settingsController = {
|
||||
settings: null,
|
||||
secretSettings: null,
|
||||
settings: { initialized: false },
|
||||
secretSettings: {},
|
||||
|
||||
// initialize instance settings from db
|
||||
async init (req, res, next) {
|
||||
if (!settingsController.settings) {
|
||||
async initialize () {
|
||||
if (!settingsController.settings.initialized) {
|
||||
const settings = await Setting.findAll()
|
||||
settingsController.settings = {}
|
||||
settingsController.secretSettings = {}
|
||||
settings.forEach( s => settingsController[s.is_secret?'secretSettings':'settings'][s.key] = s.value)
|
||||
settingsController.settings.initialized = true
|
||||
settings.forEach( s => {
|
||||
if (s.is_secret) {
|
||||
settingsController.secretSettings[s.key] = s.value
|
||||
} else {
|
||||
settingsController.settings[s.key] = s.value
|
||||
}
|
||||
})
|
||||
}
|
||||
next()
|
||||
},
|
||||
|
||||
async set(key, value, is_secret=false) {
|
||||
@@ -26,8 +29,6 @@ const settingsController = {
|
||||
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)
|
||||
@@ -43,12 +44,16 @@ const settingsController = {
|
||||
},
|
||||
|
||||
getAllRequest(req, res) {
|
||||
res.json(settingsController.settings)
|
||||
// get public settings and public configuration
|
||||
const settings = {
|
||||
...settingsController.settings,
|
||||
baseurl: config.baseurl
|
||||
}
|
||||
res.json(settings)
|
||||
},
|
||||
|
||||
async getAuthURL(req, res) {
|
||||
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)
|
||||
@@ -72,13 +77,14 @@ const settingsController = {
|
||||
`https://${instance}`, callback)
|
||||
const mastodon_auth = { client_id, client_secret, access_token }
|
||||
await settingsController.set('mastodon_auth', mastodon_auth, true)
|
||||
|
||||
const botController = require('./bot')
|
||||
botController.initialize()
|
||||
res.redirect('/admin')
|
||||
} catch (e) {
|
||||
res.json(e)
|
||||
}
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
setTimeout(settingsController.initialize, 200)
|
||||
module.exports = settingsController
|
||||
|
||||
@@ -7,8 +7,8 @@ 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 settingsController = require('./settings')
|
||||
const notifier = require('../../notifier')
|
||||
|
||||
const userController = {
|
||||
async login(req, res) {
|
||||
@@ -76,7 +76,7 @@ const userController = {
|
||||
|
||||
const eventDetails = {
|
||||
title: body.title,
|
||||
description: body.description.replace(/(<([^>]+)>)/ig, ''),
|
||||
description: body.description ? body.description.replace(/(<([^>]+)>)/ig, '') : '',
|
||||
multidate: body.multidate,
|
||||
start_datetime: body.start_datetime,
|
||||
end_datetime: body.end_datetime,
|
||||
@@ -108,14 +108,19 @@ const userController = {
|
||||
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
|
||||
await event.addTags(tags)
|
||||
}
|
||||
if (req.user) await req.user.addEvent(event)
|
||||
event = await Event.findByPk(event.id, { include: [User, Tag, Place] })
|
||||
if (req.user) {
|
||||
await req.user.addEvent(event)
|
||||
await event.setUser(req.user)
|
||||
}
|
||||
|
||||
// insert notifications
|
||||
const notifications = await eventController.getNotifications(event)
|
||||
await event.setNotifications(notifications)
|
||||
// event = await Event.findByPk(event.id, { include: [Tag, Place] })
|
||||
|
||||
// send response to client
|
||||
res.json(event)
|
||||
|
||||
// send notification (mastodon/email/confirmation)
|
||||
notifier.notifyEvent(event.id)
|
||||
|
||||
return res.json(event)
|
||||
},
|
||||
|
||||
async updateEvent(req, res) {
|
||||
@@ -155,8 +160,8 @@ const userController = {
|
||||
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
|
||||
await event.addTags(tags)
|
||||
}
|
||||
const newEvent = await Event.findByPk(event.id, { include: [User, Tag, Place] })
|
||||
return res.json(newEvent)
|
||||
const newEvent = await Event.findByPk(event.id, { include: [Tag, Place] })
|
||||
res.json(newEvent)
|
||||
},
|
||||
|
||||
async forgotPassword(req, res) {
|
||||
|
||||
Reference in New Issue
Block a user