Files
gancio/server/federation/comments.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-08-01 15:18:45 +02:00
const { event: Event, comment: Comment } = require('../api/models')
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-09-11 19:12:24 +02:00
// search for related event
2019-08-01 15:18:45 +02:00
const inReplyTo = body.object.inReplyTo
2019-10-28 17:33:20 +01:00
const match = inReplyTo.match('.*/federation/m/(.*)')
2019-09-11 19:12:24 +02:00
if (!match || match.length < 2) {
debug('Comment not found %s', inReplyTo)
2019-09-11 13:12:05 +02:00
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...
2019-09-11 13:12:05 +02:00
const comment = await Comment.findOne({ where: { activitypub_id: inReplyTo }, include: [Event] })
2019-09-11 19:12:24 +02:00
if (!comment) { return res.status(404).send('Not found') }
2019-08-10 15:00:08 +02:00
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,
fedUserApId: req.body.actor,
2019-08-09 14:37:51 +02:00
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-09-11 13:12:05 +02:00
},
2019-08-03 00:48:48 +02:00
2019-09-11 13:12:05 +02:00
async remove (req, res) {
2019-09-11 19:12:24 +02:00
const comment = await Comment.findOne({ where: { activitypub_id: req.body.object.id } })
2019-09-11 13:12:05 +02:00
if (!comment) {
debug('Comment %s not found', req.body.object.id)
return res.status(404).send('Not found')
}
await comment.destroy()
debug('Comment %s removed!', req.body.object.id)
return res.sendStatus(201)
2019-08-01 15:18:45 +02:00
}
}