Files
gancio/server/federation/comments.js

33 lines
1010 B
JavaScript
Raw Normal View History

2019-08-01 15:18:45 +02:00
const { event: Event, comment: Comment } = require('../api/models')
const config = require('config')
2019-08-09 14:37:51 +02:00
const debug = require('debug')('fediverse:comment')
2019-08-01 15:18:45 +02:00
module.exports = {
2019-08-09 14:37:51 +02:00
async create (req, res) {
2019-08-10 15:00:08 +02:00
const body = req.body
2019-08-01 15:18:45 +02:00
//search for related event
const inReplyTo = body.object.inReplyTo
2019-08-09 14:37:51 +02:00
const match = inReplyTo.match(`${config.baseurl}/federation/m/(.*)`)
if (!match || match.length<2) return res.status(404).send('Event not found!')
2019-08-10 15:00:08 +02:00
let event = await Event.findByPk(Number(match[1]))
2019-08-01 15:18:45 +02:00
2019-08-10 15:00:08 +02:00
debug('comment coming for %s', inReplyTo)
if (!event) {
// in reply to another comment...
const comment = await Comment.findByPk(inReplyTo, { include: [Event] })
if (!comment) return res.status(404).send('Not found')
event = comment.event
}
debug('comment from %s to "%s"', req.body.actor, event.title)
2019-08-01 15:18:45 +02:00
2019-08-09 14:37:51 +02:00
await Comment.create({
activitypub_id: body.object.id,
data: body.object,
eventId: event.id
})
2019-08-01 15:18:45 +02:00
2019-08-09 14:37:51 +02:00
res.sendStatus(201)
2019-08-03 00:48:48 +02:00
2019-08-01 15:18:45 +02:00
}
}