[refactoring] s/comment/resource

This commit is contained in:
les
2019-12-04 01:18:05 +01:00
parent bb14fbed83
commit c43fe095a5
17 changed files with 214 additions and 165 deletions

View File

@@ -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

View 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

View File

@@ -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) => {

View File

@@ -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
}

View File

@@ -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
}

View 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
}