allow_anon_event, comment via mastodon

This commit is contained in:
lesion
2019-06-25 01:05:38 +02:00
parent 3c9c85e268
commit b093dae3f3
32 changed files with 259 additions and 219 deletions

View File

@@ -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