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) {
|
||||
|
||||
@@ -18,7 +18,7 @@ const api = express.Router()
|
||||
api.use(cookieParser())
|
||||
api.use(bodyParser.urlencoded({ extended: false }))
|
||||
api.use(bodyParser.json())
|
||||
api.use(settingsController.init)
|
||||
// api.use(settingsController.init)
|
||||
|
||||
const jwt = expressJwt({
|
||||
secret: config.secret,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const comment = sequelize.define('comment', {
|
||||
activitypub_id: DataTypes.BIGINT,
|
||||
activitypub_id: DataTypes.STRING(18),
|
||||
data: DataTypes.JSON
|
||||
}, {})
|
||||
comment.associate = function (models) {
|
||||
|
||||
@@ -16,9 +16,9 @@ module.exports = (sequelize, DataTypes) => {
|
||||
image_path: DataTypes.STRING,
|
||||
is_visible: DataTypes.BOOLEAN,
|
||||
activitypub_id: {
|
||||
type: DataTypes.BIGINT,
|
||||
type: DataTypes.STRING(18),
|
||||
index: true
|
||||
}
|
||||
},
|
||||
}, {})
|
||||
event.associate = function (models) {
|
||||
event.belongsTo(models.place)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const eventNotification = sequelize.define('eventNotification', {
|
||||
const event_notification = sequelize.define('event_notification', {
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
values: ['new', 'sent', 'error'],
|
||||
@@ -10,5 +10,5 @@ module.exports = (sequelize, DataTypes) => {
|
||||
}
|
||||
}, {})
|
||||
|
||||
return eventNotification
|
||||
return event_notification
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
#!/usr/bin/env node
|
||||
process.env.NODE_ENV = "production"
|
||||
const path = require('path')
|
||||
process.chdir(path.resolve(__dirname, '..'))
|
||||
|
||||
const arg = require('arg')
|
||||
const inquirer = require('inquirer')
|
||||
const package = require('../package.json')
|
||||
const consola = require('consola')
|
||||
const firstrun = require('./firstrun')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const sequelize = require('sequelize')
|
||||
|
||||
@@ -138,8 +141,8 @@ async function cli(args) {
|
||||
consola.info(`Cool! You're going to setup gancio on this machine.`)
|
||||
const config = await setupQuestionnaire()
|
||||
await firstrun.setup(config, options.config)
|
||||
consola.info(`This is your configuration, run "gancio --install" or edit ${options.config} to modify it: `)
|
||||
consola.info(JSON.stringify(config, null, 2))
|
||||
consola.info(`You can edit '${options.config}' to modify your configuration. `)
|
||||
consola.info(`- Run "gancio --config ${options.config}"`)
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
|
||||
@@ -34,10 +34,17 @@ module.exports = {
|
||||
is_active: true
|
||||
})
|
||||
|
||||
// set default settings
|
||||
consola.info('Set default settings')
|
||||
const settings = require('./api/controller/settings')
|
||||
settings.set('enable_registration', true)
|
||||
settings.set('allow_anon_event', true)
|
||||
settings.set('allow_mastodon_association', true)
|
||||
await settings.set('allow_registration', true)
|
||||
await settings.set('allow_anon_event', true)
|
||||
|
||||
// add default notification
|
||||
consola.info('Add default notification')
|
||||
// send confirmed event to mastodon
|
||||
await db.notification.create({ type: 'mastodon', filters: { is_visible: true } })
|
||||
// await notification.create({ type: 'mastodon', filters: { is_visible: true } })
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,18 @@
|
||||
const mail = require('./api/mail')
|
||||
const bot = require('./api/controller/bot')
|
||||
const settingsController = require('./api/controller/settings')
|
||||
const eventController = require()
|
||||
const config = require('config')
|
||||
const eventController = require('./api/controller/event')
|
||||
const get = require('lodash/get')
|
||||
|
||||
const { event: Event, notification: Notification, eventNotification: EventNotification,
|
||||
const { event: Event, notification: Notification, event_notification: EventNotification,
|
||||
user: User, place: Place, tag: Tag } = require('./api/models')
|
||||
|
||||
const notifier = {
|
||||
async sendNotification(notification, event, eventNotification) {
|
||||
async sendNotification(notification, event) {
|
||||
console.error('dentro sendNotification ', settingsController.settings, notification.type)
|
||||
const access_token = get(settingsController.secretSettings, 'mastodon_auth.access_token')
|
||||
const instance = get(settingsController.settings, 'mastodon_instance')
|
||||
const promises = []
|
||||
switch (notification.type) {
|
||||
case 'mail':
|
||||
@@ -19,26 +23,39 @@ const notifier = {
|
||||
return mail.send(admin_emails, 'event', { event, to_confirm: true, notification })
|
||||
case 'mastodon':
|
||||
// instance publish
|
||||
if (settings.mastodon_auth.instance && settings.mastodon_auth.access_token) {
|
||||
const b = bot.post(settings.mastodon_auth, event).then(b => {
|
||||
if (instance && access_token) {
|
||||
const b = bot.post(instance, access_token, event).then(b => {
|
||||
console.error(b)
|
||||
event.activitypub_id = b.data.id
|
||||
event.activitypub_ids.push(b.data.id)
|
||||
return event.save()
|
||||
}).catch(e => {
|
||||
console.error("ERRORE !! ", e)
|
||||
})
|
||||
promises.push(b)
|
||||
}
|
||||
}
|
||||
return Promise.all(promises)
|
||||
},
|
||||
async notifyEvent(event) {
|
||||
async notifyEvent(eventId) {
|
||||
const event = await Event.findByPk(eventId, {
|
||||
include: [ Tag, Place, User ]
|
||||
})
|
||||
|
||||
// insert notifications
|
||||
const notifications = await eventController.getNotifications(event)
|
||||
await event.setNotifications(notifications)
|
||||
const a = await event.setNotifications(notifications)
|
||||
|
||||
const promises = notifications.map(async e => {
|
||||
const eventNotifications = await EventNotification.findAll({
|
||||
where: {
|
||||
notificationId: notifications.map(n=>n.id),
|
||||
status: 'new'
|
||||
}
|
||||
})
|
||||
|
||||
const promises = eventNotifications.map(async e => {
|
||||
const notification = await Notification.findByPk(e.notificationId)
|
||||
try {
|
||||
await sendNotification(notification, event, e)
|
||||
await notifier.sendNotification(notification, event)
|
||||
e.status = 'sent'
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
@@ -79,4 +96,5 @@ const notifier = {
|
||||
|
||||
// startLoop(26000)
|
||||
|
||||
module.exports = notifier
|
||||
module.exports = notifier
|
||||
// export default notifier
|
||||
Reference in New Issue
Block a user