gancio/server/federation/resources.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-06-27 02:10:10 +02:00
const Event = require('../api/models/event')
const Resource = require('../api/models/resource')
const APUser = require('../api/models/ap_user')
2022-02-26 21:27:40 +01:00
const settingsController = require('../api/controller/settings')
2020-06-27 02:10:10 +02:00
2021-03-05 14:17:10 +01:00
const log = require('../log')
2020-02-10 00:40:23 +01:00
const helpers = require('../helpers')
2021-09-30 11:06:59 +02:00
const linkifyHtml = require('linkify-html')
2020-02-10 00:40:23 +01:00
2019-12-04 01:18:05 +01:00
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) {
2022-02-26 21:27:40 +01:00
if (!settingsController.settings.enable_resources) {
2021-03-05 14:17:10 +01:00
log.info('Ignore resource as it is disabled in settings')
2020-02-20 23:47:52 +01:00
return
}
2019-12-04 01:18:05 +01:00
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/(.*)')
2021-03-05 14:17:10 +01:00
log.info(`Event reply => ${inReplyTo}`)
2020-02-04 23:38:03 +01:00
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
}
2021-03-05 14:17:10 +01:00
if (!event) {
log.error('This is a direct message. Just ignore it')
log.error(body)
return res.status(404).send('Not found')
}
log.debug(`resource from ${req.body.actor} to "${event.title}"`)
2020-02-04 23:38:03 +01:00
2020-02-20 23:47:52 +01:00
body.object.content = helpers.sanitizeHTML(linkifyHtml(body.object.content))
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 },
2021-06-25 11:36:28 +02:00
include: [{ model: APUser, required: true, attributes: ['ap_id'] }]
2020-02-04 23:37:26 +01:00
})
2019-12-04 01:18:05 +01:00
if (!resource) {
2021-03-05 14:17:10 +01:00
log.info(`Comment ${req.body.object.id} not found`)
2019-12-04 01:18:05 +01:00
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
2022-02-26 21:27:40 +01:00
if (res.locals.fedi_user.ap_id === resource.ap_user.ap_id) {
2020-02-04 23:37:26 +01:00
await resource.destroy()
2021-03-05 14:17:10 +01:00
log.info(`Comment ${req.body.object.id} removed`)
2020-02-04 23:37:26 +01:00
res.sendStatus(201)
} else {
res.sendStatus(403)
}
2019-12-04 01:18:05 +01:00
}
}