gancio-upstream/server/federation/ego.js

62 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-01-10 18:16:11 +01:00
const { Event } = require('../api/models/models')
2021-09-27 10:42:17 +02:00
const config = require('../config')
2021-03-05 14:17:10 +01:00
const log = require('../log')
2019-08-02 13:43:28 +02:00
module.exports = {
async boost (req, res) {
2024-01-13 10:15:42 +01:00
if (typeof req.body?.object !== 'string') {
log.debug('[FEDI] Igonre Boost for a whole object? %s', JSON.stringify(req.body?.object))
return res.status(404).send('?')
}
2024-01-11 13:00:39 +01:00
const match = req.body?.object?.match(`${config.baseurl}/federation/m/(.*)`)
if (!match || match.length < 2) {
log.debug('[FEDI] Boosted something not local: %s', req.body.object)
return res.status(404).send('Event not found!')
}
2023-12-26 13:04:20 +01:00
log.info(`[FEDI] boost ${match[1]}`)
2019-08-08 16:52:13 +02:00
const event = await Event.findByPk(Number(match[1]))
2024-01-11 13:00:39 +01:00
if (!event) {
log.debug('[FEDI] Boosted event not found: %s', req.body.object)
return res.status(404).send('Event not found!')
}
2019-09-11 19:12:24 +02:00
await event.update({ boost: [...event.boost, req.body.actor] })
2019-08-02 13:43:28 +02:00
res.sendStatus(201)
},
2019-08-08 16:52:13 +02:00
2019-10-27 14:11:57 +01:00
async unboost (req, res) {
2024-01-11 13:00:39 +01:00
const match = req.body?.object?.match(`${config.baseurl}/federation/m/(.*)`)
2019-10-28 17:33:20 +01:00
if (!match || match.length < 2) { return res.status(404).send('Event not found!') }
2021-04-28 12:44:26 +02:00
log.info(`unboost ${match[1]}`)
2019-10-27 14:11:57 +01:00
const event = await Event.findByPk(Number(match[1]))
2019-10-28 17:33:20 +01:00
if (!event) { return res.status(404).send('Event not found!') }
await event.update({ boost: event.boost.filter(actor => actor !== req.body.actor) })
2019-10-27 14:11:57 +01:00
},
2019-08-08 16:52:13 +02:00
async bookmark (req, res) {
const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)
2024-01-23 08:52:28 +01:00
if (!match || match.length < 2) {
log.debug('[FEDI] No match for bookmark: %s', JSON.stringify(req.body))
return res.status(404).send('Event not found!')
}
2019-08-08 16:52:13 +02:00
const event = await Event.findByPk(Number(match[1]))
2021-04-28 12:44:26 +02:00
log.info(`${req.body.actor} bookmark ${event.title} (${event.likes.length})`)
2019-09-11 19:12:24 +02:00
if (!event) { return res.status(404).send('Event not found!') }
// TODO: has to be unique
2019-09-11 19:12:24 +02:00
await event.update({ likes: [...event.likes, req.body.actor] })
2019-08-02 13:43:28 +02:00
res.sendStatus(201)
2019-08-08 16:52:13 +02:00
},
async unbookmark (req, res) {
const body = req.body
const object = body.object
const match = object.object.match(`${config.baseurl}/federation/m/(.*)`)
2019-09-11 19:12:24 +02:00
if (!match || match.length < 2) { return res.status(404).send('Event not found!') }
2019-08-08 16:52:13 +02:00
const event = await Event.findByPk(Number(match[1]))
2021-04-28 12:44:26 +02:00
log.info(`${body.actor} unbookmark ${event.title} (${event.likes.length})`)
2019-09-11 19:12:24 +02:00
if (!event) { return res.status(404).send('Event not found!') }
2019-10-28 17:33:20 +01:00
await event.update({ likes: event.likes.filter(actor => actor !== body.actor) })
2019-08-08 16:52:13 +02:00
res.sendStatus(201)
2019-08-02 13:43:28 +02:00
}
2019-09-11 19:12:24 +02:00
}