[refactoring] s/comment/resource
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
const { event: Event, comment: Comment } = require('../api/models')
|
||||
const debug = require('debug')('fediverse:comment')
|
||||
|
||||
module.exports = {
|
||||
async create (req, res) {
|
||||
const body = req.body
|
||||
// search for related event
|
||||
const inReplyTo = body.object.inReplyTo
|
||||
const match = inReplyTo.match('.*/federation/m/(.*)')
|
||||
if (!match || match.length < 2) {
|
||||
debug('Comment not found %s', inReplyTo)
|
||||
return res.status(404).send('Event not found!')
|
||||
}
|
||||
let event = await Event.findByPk(Number(match[1]))
|
||||
|
||||
debug('comment coming for %s', inReplyTo)
|
||||
if (!event) {
|
||||
// in reply to another comment...
|
||||
const comment = await Comment.findOne({ where: { activitypub_id: 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)
|
||||
|
||||
await Comment.create({
|
||||
activitypub_id: body.object.id,
|
||||
fedUserApId: req.body.actor,
|
||||
data: body.object,
|
||||
eventId: event.id
|
||||
})
|
||||
|
||||
res.sendStatus(201)
|
||||
},
|
||||
|
||||
async remove (req, res) {
|
||||
const comment = await Comment.findOne({ where: { activitypub_id: req.body.object.id } })
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
const config = require('config')
|
||||
const Helpers = require('./helpers')
|
||||
const { user: User, fed_users: FedUsers } = require('../api/models')
|
||||
const crypto = require('crypto')
|
||||
const debug = require('debug')('federation:follows')
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@ const router = express.Router()
|
||||
const cors = require('cors')
|
||||
const Follows = require('./follows')
|
||||
const Users = require('./users')
|
||||
const { event: Event, user: User, tag: Tag, place: Place } = require('../api/models')
|
||||
const { Event, User, Tag, Place } = require('../api/models')
|
||||
const settingsController = require('../api/controller/settings')
|
||||
const Comments = require('./comments')
|
||||
const Resources = require('./resources')
|
||||
const Helpers = require('./helpers')
|
||||
const Ego = require('./ego')
|
||||
const debug = require('debug')('federation')
|
||||
@@ -65,12 +65,12 @@ router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => {
|
||||
Ego.bookmark(req, res)
|
||||
break
|
||||
case 'Delete':
|
||||
await Comments.remove(req, res)
|
||||
await Resources.remove(req, res)
|
||||
break
|
||||
case 'Create':
|
||||
// this is a reply
|
||||
if (b.object.type === 'Note' && b.object.inReplyTo) {
|
||||
await Comments.create(req, res)
|
||||
await Resources.create(req, res)
|
||||
} else {
|
||||
debug('Create with unsupported Object or not a reply => %s ', b.object.type)
|
||||
}
|
||||
|
||||
52
server/federation/resources.js
Normal file
52
server/federation/resources.js
Normal file
@@ -0,0 +1,52 @@
|
||||
const { event: Event, resource: Resource } = require('../api/models')
|
||||
const debug = require('debug')('fediverse:resource')
|
||||
const sanitize = require('sanitize-html')
|
||||
module.exports = {
|
||||
|
||||
async create (req, res) {
|
||||
const body = req.body
|
||||
|
||||
// search for related event
|
||||
const inReplyTo = body.object.inReplyTo
|
||||
const match = inReplyTo.match('.*/federation/m/(.*)')
|
||||
if (!match || match.length < 2) {
|
||||
debug('Resource not found %s', inReplyTo)
|
||||
return res.status(404).send('Event not found!')
|
||||
}
|
||||
|
||||
let event = await Event.findByPk(Number(match[1]))
|
||||
debug('Resource coming for %s', inReplyTo)
|
||||
if (!event) {
|
||||
// in reply to another resource...
|
||||
const resource = await Resource.findOne({ where: { activitypub_id: inReplyTo }, include: [Event] })
|
||||
if (!resource) { return res.status(404).send('Not found') }
|
||||
event = resource.event
|
||||
}
|
||||
debug('resource from %s to "%s"', req.body.actor, event.title)
|
||||
|
||||
// clean resource
|
||||
body.object.content = sanitize(body.object.content, {
|
||||
nonTextTags: ['span', 'style', 'script', 'textarea', 'noscript']
|
||||
})
|
||||
|
||||
await Resource.create({
|
||||
activitypub_id: body.object.id,
|
||||
apUserApId: req.body.actor,
|
||||
data: body.object,
|
||||
eventId: event.id
|
||||
})
|
||||
|
||||
res.sendStatus(201)
|
||||
},
|
||||
|
||||
async remove (req, res) {
|
||||
const resource = await Resource.findOne({ where: { activitypub_id: req.body.object.id } })
|
||||
if (!resource) {
|
||||
debug('Comment %s not found', req.body.object.id)
|
||||
return res.status(404).send('Not found')
|
||||
}
|
||||
await resource.destroy()
|
||||
debug('Comment %s removed!', req.body.object.id)
|
||||
return res.sendStatus(201)
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
const { user: User, event: Event, place: Place, tag: Tag, fed_users: FedUsers } = require('../api/models')
|
||||
const { User, Event, Place, Tag, APUser } = require('../api/models')
|
||||
const config = require('config')
|
||||
const get = require('lodash/get')
|
||||
const debug = require('debug')('fediverse:user')
|
||||
|
||||
module.exports = {
|
||||
async get (req, res) {
|
||||
get (req, res) {
|
||||
const name = req.params.name
|
||||
if (!name) { return res.status(400).send('Bad request.') }
|
||||
const user = await User.findOne({ where: { username: name } })
|
||||
if (!user) { return res.status(404).send(`No record found for ${name}`) }
|
||||
// const user = await User.findOne({ where: { username: name } })
|
||||
if (name !== req.settings.instance_name) { return res.status(404).send(`No record found for ${name}`) }
|
||||
const ret = {
|
||||
'@context': [
|
||||
'https://www.w3.org/ns/activitystreams',
|
||||
|
||||
Reference in New Issue
Block a user