gancio-upstream/server/api/controller/bot.js

70 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-04-03 00:25:12 +02:00
const fs = require('fs')
const path = require('path')
const moment = require('moment')
2019-06-09 01:53:24 +02:00
const { event: Event, comment: Comment } = require('../models')
const config = require('config')
2019-06-06 23:54:32 +02:00
const Mastodon = require('mastodon-api')
const settingsController = require('./settings')
2019-06-25 01:05:38 +02:00
const get = require('lodash/get')
2019-04-03 00:25:12 +02:00
const botController = {
bot: null,
2019-06-07 17:02:33 +02:00
async initialize() {
2019-06-25 01:05:38 +02:00
const access_token = get(settingsController.secretSettings, 'mastodon_auth.access_token')
const instance = get(settingsController.settings, 'mastodon_instance')
if (!access_token || !instance) return
2019-06-07 17:02:33 +02:00
botController.bot = new Mastodon({
2019-06-25 01:05:38 +02:00
access_token,
api_url: `https://${instance}/api/v1`
})
2019-07-03 23:22:17 +02:00
const listener = botController.bot.stream('/streaming/user')
listener.on('message', botController.message)
listener.on('error', botController.error)
},
2019-07-08 00:06:56 +02:00
async post(event) {
2019-07-03 23:22:17 +02:00
const status = `${event.title} @${event.place.name} ${moment(event.start_datetime*1000).format('ddd, D MMMM HH:mm')} -
2019-06-06 23:54:32 +02:00
${event.description.length > 200 ? event.description.substr(0, 200) + '...' : event.description} - ${event.tags.map(t => '#' + t.tag).join(' ')} ${config.baseurl}/event/${event.id}`
2019-04-03 00:25:12 +02:00
let media
if (event.image_path) {
2019-07-08 00:06:56 +02:00
const file = path.resolve(config.upload_path, event.image_path)
2019-04-03 00:25:12 +02:00
if (fs.statSync(file)) {
2019-07-08 00:06:56 +02:00
media = await botController.bot.post('/media', { file: fs.createReadStream(file) })
2019-04-03 00:25:12 +02:00
}
}
2019-06-26 14:44:21 +02:00
return botController.bot.post('/statuses', { status, media_ids: media ? [media.data.id] : [] })
},
2019-06-06 23:54:32 +02:00
2019-06-07 17:02:33 +02:00
async message(msg) {
2019-06-26 14:44:21 +02:00
const type = msg.event
2019-06-26 17:11:37 +02:00
if (type === 'delete') {
2019-07-03 16:58:24 +02:00
const activitypub_id = String(msg.data)
2019-06-26 17:11:37 +02:00
const event = await Comment.findOne({ where: { activitypub_id } })
if (event) await event.destroy()
return
}
2019-07-03 23:22:17 +02:00
const activitypub_id = String(msg.data.status.in_reply_to_id)
2019-06-26 17:11:37 +02:00
if (!activitypub_id) return
let event = await Event.findOne({ where: { activitypub_id } })
if (!event) {
// check for comment..
2019-06-26 17:11:37 +02:00
const comment = await Comment.findOne( { include: [Event], where: { activitypub_id }})
2019-06-25 01:05:38 +02:00
if (!comment) return
event = comment.event
}
2019-06-26 14:44:21 +02:00
await Comment.create({
2019-07-03 23:22:17 +02:00
activitypub_id: String(msg.data.status.id),
data: msg.data.status,
2019-06-25 01:05:38 +02:00
eventId: event.id
2019-04-30 01:04:24 +02:00
})
},
2019-06-07 17:02:33 +02:00
error(err) {
console.log('error ', err)
}
2019-04-03 00:25:12 +02:00
}
2019-06-26 17:11:37 +02:00
2019-06-25 01:05:38 +02:00
setTimeout(botController.initialize, 5000)
2019-04-03 00:25:12 +02:00
module.exports = botController