gancio-upstream/server/federation/resources.js

68 lines
2 KiB
JavaScript
Raw Normal View History

2020-02-04 23:38:03 +01:00
const { event: Event, resource: Resource, ap_user: APUser } = require('../api/models')
2019-12-04 01:18:05 +01:00
const debug = require('debug')('fediverse:resource')
const sanitize = require('sanitize-html')
module.exports = {
2020-02-04 23:38:03 +01:00
// create a resource from AP Note
2019-12-04 01:18:05 +01:00
async create (req, res) {
const body = req.body
// search for related event
2020-02-04 23:38:03 +01:00
let event
// it's an answer
2019-12-04 01:18:05 +01:00
const inReplyTo = body.object.inReplyTo
2020-02-04 23:38:03 +01:00
if (inReplyTo) {
// .. to an event ?
const match = inReplyTo && inReplyTo.match('.*/federation/m/(.*)')
debug('Event reply => ', inReplyTo)
if (match) {
event = await Event.findByPk(Number(match[1]))
} else {
// in reply to another resource...
const resource = await Resource.findOne({ where: { activitypub_id: inReplyTo }, include: [Event] })
event = resource && resource.event
}
2019-12-04 01:18:05 +01:00
}
2020-02-04 23:38:03 +01:00
debug('resource from %s to "%s"', req.body.actor, event && event.title)
// TODO should probably map links here
2019-12-04 01:18:05 +01:00
// clean resource
body.object.content = sanitize(body.object.content, {
2020-02-04 23:38:03 +01:00
nonTextTags: ['style', 'script', 'textarea', 'noscript']
2019-12-04 01:18:05 +01:00
})
await Resource.create({
activitypub_id: body.object.id,
apUserApId: req.body.actor,
data: body.object,
2020-02-04 23:38:03 +01:00
eventId: event && event.id
2019-12-04 01:18:05 +01:00
})
res.sendStatus(201)
},
async remove (req, res) {
2020-02-04 23:37:26 +01:00
const resource = await Resource.findOne({
where: { activitypub_id: req.body.object.id },
include: [{ model: APUser, required: false, attributes: ['ap_id'] }]
})
2019-12-04 01:18:05 +01:00
if (!resource) {
debug('Comment %s not found', req.body.object.id)
return res.status(404).send('Not found')
}
2020-02-04 23:37:26 +01:00
// check if fedi_user that requested resource removal
// is the same that created the resource at first place
debug(res.fedi_user.ap_id, resource.ap_user.ap_id)
if (res.fedi_user.ap_id === resource.ap_user.id) {
await resource.destroy()
debug('Comment %s removed!', req.body.object.id)
res.sendStatus(201)
} else {
res.sendStatus(403)
}
2019-12-04 01:18:05 +01:00
}
}