2019-03-12 01:16:52 +01:00
|
|
|
// const { User, Event, Comment, Tag } = require('../model')
|
2019-02-26 00:02:42 +01:00
|
|
|
const config = require('../config')
|
|
|
|
const Mastodon = require('mastodon-api')
|
2019-03-12 01:16:52 +01:00
|
|
|
// const Sequelize = require('sequelize')
|
|
|
|
// const Op = Sequelize.Op
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2019-02-26 00:02:42 +01:00
|
|
|
const moment = require('moment')
|
|
|
|
moment.locale('it')
|
2019-03-12 01:16:52 +01:00
|
|
|
|
2019-02-26 00:02:42 +01:00
|
|
|
const botController = {
|
|
|
|
bots: [],
|
2019-03-07 14:59:28 +01:00
|
|
|
// async initialize () {
|
|
|
|
// console.log('initialize bots')
|
|
|
|
// const botUsers = await User.findAll({ where: { mastodon_auth: { [Op.ne]: null } } })
|
|
|
|
// console.log(botUsers)
|
|
|
|
// botController.bots = botUsers.map(user => {
|
|
|
|
// console.log('initialize bot ', user.name)
|
|
|
|
// console.log('.. ', user.mastodon_auth)
|
|
|
|
// const { client_id, client_secret, access_token } = user.mastodon_auth
|
|
|
|
// const bot = new Mastodon({ access_token, api_url: `https://${user.mastodon_instance}/api/v1/` })
|
|
|
|
// const listener = bot.stream('streaming/direct')
|
|
|
|
// listener.on('message', botController.message)
|
|
|
|
// listener.on('error', botController.error)
|
|
|
|
// return { email: user.email, bot }
|
|
|
|
// })
|
|
|
|
// console.log(botController.bots)
|
|
|
|
// },
|
|
|
|
// add (user, token) {
|
|
|
|
// const bot = new Mastodon({ access_token: user.mastodon_auth.access_token, api_url: `https://${user.mastodon_instance}/api/v1/` })
|
|
|
|
// const listener = bot.stream('streaming/direct')
|
|
|
|
// listener.on('message', botController.message)
|
|
|
|
// listener.on('error', botController.error)
|
|
|
|
// botController.bots.push({ email: user.email, bot })
|
|
|
|
// },
|
2019-03-12 01:16:52 +01:00
|
|
|
async post (mastodon_auth, event) {
|
|
|
|
const { access_token, instance } = mastodon_auth
|
|
|
|
const bot = new Mastodon({ access_token, api_url: `https://${instance}/api/v1/` })
|
2019-02-26 00:02:42 +01:00
|
|
|
const status = `${event.title} @ ${event.place.name} ${moment(event.start_datetime).format('ddd, D MMMM HH:mm')} -
|
2019-02-26 01:17:52 +01:00
|
|
|
${event.description} - ${event.tags.map(t => '#' + t.tag).join(' ')} ${config.baseurl}/event/${event.id}`
|
2019-03-12 01:16:52 +01:00
|
|
|
|
|
|
|
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) })
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 20:32:09 +01:00
|
|
|
return bot.post('statuses', { status, visibility: 'public', media_ids: media ? [media.data.id] : [] })
|
2019-02-26 00:02:42 +01:00
|
|
|
}
|
2019-03-07 14:59:28 +01:00
|
|
|
// async message (msg) {
|
|
|
|
// console.log(msg)
|
|
|
|
// console.log(msg.data.accounts)
|
|
|
|
// 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 } })
|
|
|
|
// if (!event) {
|
|
|
|
// check for comment..
|
|
|
|
// const comment = await Comment.findOne( {where: { }})
|
|
|
|
// }
|
|
|
|
// const comment = await Comment.create({activitypub_id: msg.data.last_status.id, text: msg.data.last_status.content, author: msg.data.accounts[0].username })
|
|
|
|
// event.addComment(comment)
|
|
|
|
// console.log(event)
|
|
|
|
// 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)
|
|
|
|
// }
|
2019-02-26 00:02:42 +01:00
|
|
|
}
|
|
|
|
|
2019-03-07 14:59:28 +01:00
|
|
|
// setTimeout(botController.initialize, 2000)
|
2019-02-26 00:02:42 +01:00
|
|
|
module.exports = botController
|