[refactoring] s/comment/resource
This commit is contained in:
@@ -2,29 +2,28 @@ const crypto = require('crypto')
|
||||
const moment = require('moment-timezone')
|
||||
const { Op } = require('sequelize')
|
||||
const lodash = require('lodash')
|
||||
const { event: Event, comment: Comment, tag: Tag, place: Place,
|
||||
user: User, notification: Notification } = require('../models')
|
||||
const { event: Event, resource: Resource, tag: Tag, place: Place, notification: Notification } = require('../models')
|
||||
const Sequelize = require('sequelize')
|
||||
const exportController = require('./export')
|
||||
const debug = require('debug')('controller:event')
|
||||
|
||||
const eventController = {
|
||||
|
||||
/** add a comment to event
|
||||
/** add a resource to event
|
||||
* @todo not used anywhere, should we use with webmention?
|
||||
* @todo should we use this for roply coming from fediverse?
|
||||
*/
|
||||
async addComment (req, res) {
|
||||
// comments could be added to an event or to another comment
|
||||
let event = await Event.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } } })
|
||||
if (!event) {
|
||||
const comment = await Comment.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } }, include: Event })
|
||||
event = comment.event
|
||||
}
|
||||
const comment = new Comment(req.body)
|
||||
event.addComment(comment)
|
||||
res.json(comment)
|
||||
},
|
||||
// async addComment (req, res) {
|
||||
// // comments could be added to an event or to another comment
|
||||
// let event = await Event.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } } })
|
||||
// if (!event) {
|
||||
// const comment = await Resource.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } }, include: Event })
|
||||
// event = comment.event
|
||||
// }
|
||||
// const comment = new Comment(req.body)
|
||||
// event.addComment(comment)
|
||||
// res.json(comment)
|
||||
// },
|
||||
|
||||
async getMeta (req, res) {
|
||||
const places = await Place.findAll({
|
||||
@@ -94,11 +93,10 @@ const eventController = {
|
||||
},
|
||||
include: [
|
||||
{ model: Tag, attributes: ['tag', 'weigth'], through: { attributes: [] } },
|
||||
{ model: User, attributes: ['username'] },
|
||||
{ model: Place, attributes: ['name', 'address'] },
|
||||
{ model: Comment, where: !is_admin && { hidden: false }, required: false }
|
||||
{ model: Resource, where: !is_admin && { hidden: false }, required: false }
|
||||
],
|
||||
order: [ [Comment, 'id', 'DESC'] ]
|
||||
order: [ [Resource, 'id', 'DESC'] ]
|
||||
})
|
||||
|
||||
if (event && (event.is_visible || is_admin)) {
|
||||
@@ -230,12 +228,11 @@ const eventController = {
|
||||
attributes: { exclude: [ 'createdAt', 'updatedAt', 'placeId' ] },
|
||||
order: [[Tag, 'weigth', 'DESC']],
|
||||
include: [
|
||||
{ model: Comment, required: false, attributes: ['id'] },
|
||||
{ model: Resource, required: false, attributes: ['id'] },
|
||||
{ model: Tag, required: false },
|
||||
{ model: Place, required: false, attributes: ['id', 'name', 'address'] }
|
||||
]
|
||||
})
|
||||
|
||||
events = events.map(e => e.get()).map(e => {
|
||||
e.tags = e.tags.map(t => t.tag)
|
||||
return e
|
||||
|
||||
30
server/api/controller/resource.js
Normal file
30
server/api/controller/resource.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const { resource: Resource } = require('../models')
|
||||
|
||||
const resourceController = {
|
||||
async hide (req, res) {
|
||||
const resource_id = req.params.resource_id
|
||||
const hidden = req.body.hidden
|
||||
const resource = await Resource.findByPk(resource_id)
|
||||
await resource.update({ hidden })
|
||||
res.json(resource)
|
||||
},
|
||||
|
||||
async remove (req, res) {
|
||||
const resource_id = req.params.resource_id
|
||||
const resource = await Resource.findByPk(resource_id)
|
||||
await resource.destroy()
|
||||
res.sendStatus(200)
|
||||
},
|
||||
|
||||
async getAll (req, res) {
|
||||
const limit = req.body.limit || 100
|
||||
// const where = {}
|
||||
// if (req.params.instanceId) {
|
||||
// where =
|
||||
//
|
||||
const resources = await Resource.findAll({ limit })
|
||||
res.json(resources)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = resourceController
|
||||
@@ -8,8 +8,9 @@ const eventController = require('./controller/event')
|
||||
const exportController = require('./controller/export')
|
||||
const userController = require('./controller/user')
|
||||
const settingsController = require('./controller/settings')
|
||||
const instancesController = require('./controller/instances')
|
||||
const fedUserController = require('./controller/fed_user')
|
||||
const instanceController = require('./controller/instance')
|
||||
const apUserController = require('./controller/ap_user')
|
||||
const resourceController = require('./controller/resource')
|
||||
|
||||
const storage = require('./storage')
|
||||
const upload = multer({ storage })
|
||||
@@ -84,12 +85,13 @@ api.get('/export/:type', exportController.export)
|
||||
// get events in this range
|
||||
api.get('/event/:month/:year', eventController.getAll)
|
||||
|
||||
api.get('/instances', isAdmin, instancesController.getAll)
|
||||
api.get('/instances/:instance_domain', isAdmin, instancesController.get)
|
||||
api.post('/instances/toggle_block', isAdmin, instancesController.toggleBlock)
|
||||
api.post('/instances/toggle_user_block', isAdmin, fedUserController.toggleBlock)
|
||||
api.post('/comments/:comment_id', isAdmin, fedUserController.hideComment)
|
||||
api.delete('/comments/:comment_id', isAdmin, fedUserController.removeComment)
|
||||
api.get('/instances', isAdmin, instanceController.getAll)
|
||||
api.get('/instances/:instance_domain', isAdmin, instanceController.get)
|
||||
api.post('/instances/toggle_block', isAdmin, instanceController.toggleBlock)
|
||||
api.post('/instances/toggle_user_block', isAdmin, apUserController.toggleBlock)
|
||||
api.put('/resources/:resource_id', isAdmin, resourceController.hide)
|
||||
api.delete('/resources/:resource_id', isAdmin, resourceController.remove)
|
||||
api.get('/resources', isAdmin, resourceController.getAll)
|
||||
|
||||
// Handle 404
|
||||
api.use((req, res) => {
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
'use strict'
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const comment = sequelize.define('comment', {
|
||||
activitypub_id: {
|
||||
type: DataTypes.STRING,
|
||||
index: true,
|
||||
unique: true
|
||||
},
|
||||
hidden: DataTypes.BOOLEAN,
|
||||
fedUserApId: {
|
||||
type: DataTypes.STRING,
|
||||
references: {
|
||||
model: 'fed_users',
|
||||
key: 'ap_id'
|
||||
},
|
||||
onUpdate: 'CASCADE',
|
||||
onDelete: 'CASCADE'
|
||||
},
|
||||
data: DataTypes.JSON
|
||||
}, {})
|
||||
comment.associate = function (models) {
|
||||
comment.belongsTo(models.event)
|
||||
comment.belongsTo(models.fed_users)
|
||||
}
|
||||
return comment
|
||||
}
|
||||
@@ -2,7 +2,7 @@ const config = require('config')
|
||||
const moment = require('moment')
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const event = sequelize.define('event', {
|
||||
const Event = sequelize.define('event', {
|
||||
id: {
|
||||
allowNull: false,
|
||||
type: DataTypes.INTEGER,
|
||||
@@ -29,15 +29,15 @@ module.exports = (sequelize, DataTypes) => {
|
||||
boost: { type: DataTypes.JSON, defaultValue: [] }
|
||||
}, {})
|
||||
|
||||
event.associate = function (models) {
|
||||
event.belongsTo(models.place)
|
||||
event.belongsTo(models.user)
|
||||
event.belongsToMany(models.tag, { through: 'event_tags' })
|
||||
event.belongsToMany(models.notification, { through: 'event_notification' })
|
||||
event.hasMany(models.comment)
|
||||
Event.associate = function (models) {
|
||||
Event.belongsTo(models.place)
|
||||
Event.belongsTo(models.user)
|
||||
Event.belongsToMany(models.tag, { through: 'event_tags' })
|
||||
Event.belongsToMany(models.notification, { through: 'event_notification' })
|
||||
Event.hasMany(models.resource)
|
||||
}
|
||||
|
||||
event.prototype.toAP = function (username, follower = []) {
|
||||
Event.prototype.toNoteAP = function (username, follower = []) {
|
||||
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, ' '))
|
||||
const tag_links = tags.map(t => {
|
||||
return `<a href='/tags/${t}' class='mention hashtag status-link' rel='tag'><span>#${t}</span></a>`
|
||||
@@ -82,5 +82,5 @@ module.exports = (sequelize, DataTypes) => {
|
||||
}
|
||||
}
|
||||
|
||||
return event
|
||||
return Event
|
||||
}
|
||||
|
||||
20
server/api/models/resource.js
Normal file
20
server/api/models/resource.js
Normal file
@@ -0,0 +1,20 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
|
||||
const Resource = sequelize.define('resource', {
|
||||
activitypub_id: {
|
||||
type: DataTypes.STRING,
|
||||
index: true,
|
||||
unique: true
|
||||
},
|
||||
hidden: DataTypes.BOOLEAN,
|
||||
data: DataTypes.JSON
|
||||
}, {})
|
||||
|
||||
Resource.associate = function (models) {
|
||||
// Resource.belongsTo(models.instance)
|
||||
Resource.belongsTo(models.event)
|
||||
Resource.belongsTo(models.ap_user)
|
||||
}
|
||||
|
||||
return Resource
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
25
server/migrations/20191127234917-rename_comment_resource.js
Normal file
25
server/migrations/20191127234917-rename_comment_resource.js
Normal file
@@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
/*
|
||||
Add altering commands here.
|
||||
Return a promise to correctly handle asynchronicity.
|
||||
|
||||
Example:
|
||||
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
|
||||
*/
|
||||
return queryInterface.renameTable('comments', 'resources')
|
||||
},
|
||||
|
||||
down: (queryInterface, Sequelize) => {
|
||||
/*
|
||||
Add reverting commands here.
|
||||
Return a promise to correctly handle asynchronicity.
|
||||
|
||||
Example:
|
||||
return queryInterface.dropTable('users');
|
||||
*/
|
||||
return queryInterface.renameTable('resources', 'comments')
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user