clean unused places/tags #107 and unused fields
This commit is contained in:
@@ -25,23 +25,22 @@ const eventController = {
|
||||
|
||||
async _getMeta () {
|
||||
const places = await Place.findAll({
|
||||
where: { confirmed: true },
|
||||
order: [[Sequelize.literal('weigth'), 'DESC']],
|
||||
attributes: {
|
||||
include: [[Sequelize.fn('count', Sequelize.col('events.placeId')), 'weigth']],
|
||||
exclude: ['createdAt', 'updatedAt']
|
||||
},
|
||||
include: [{ model: Event, attributes: [] }],
|
||||
include: [{ model: Event, where: { is_visible: true }, required: true, attributes: [] }],
|
||||
group: ['place.id']
|
||||
})
|
||||
|
||||
const tags = await Tag.findAll({
|
||||
where: { confirmed: true },
|
||||
raw: true,
|
||||
order: [['weigth', 'DESC']],
|
||||
order: [[Sequelize.literal('w'), 'DESC']],
|
||||
attributes: {
|
||||
exclude: ['createdAt', 'updatedAt']
|
||||
}
|
||||
include: [[Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'w']]
|
||||
},
|
||||
include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }],
|
||||
group: ['tag.tag']
|
||||
})
|
||||
|
||||
return { places, tags }
|
||||
@@ -178,14 +177,6 @@ const eventController = {
|
||||
try {
|
||||
event.is_visible = true
|
||||
|
||||
// confirm tag & place if needed
|
||||
if (!event.place.confirmed) {
|
||||
await event.place.update({ confirmed: true })
|
||||
}
|
||||
|
||||
await Tag.update({ confirmed: true },
|
||||
{ where: { confirmed: false, tag: { [Op.in]: event.tags.map(t => t.tag) } } })
|
||||
|
||||
await event.save()
|
||||
|
||||
res.sendStatus(200)
|
||||
@@ -295,8 +286,7 @@ const eventController = {
|
||||
const [place] = await Place.findOrCreate({
|
||||
where: { name: body.place_name },
|
||||
defaults: {
|
||||
address: body.place_address,
|
||||
confirmed: !!req.user
|
||||
address: body.place_address
|
||||
}
|
||||
})
|
||||
|
||||
@@ -305,9 +295,8 @@ const eventController = {
|
||||
|
||||
// create/assign tags
|
||||
if (body.tags) {
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t, confirmed: !!req.user })), { ignoreDuplicates: true })
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
|
||||
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
|
||||
await Promise.all(tags.map(t => t.update({ weigth: Number(t.weigth) + 1, confirmed: true })))
|
||||
await event.addTags(tags)
|
||||
event.tags = tags
|
||||
}
|
||||
@@ -456,14 +445,19 @@ const eventController = {
|
||||
const events = await Event.findAll({
|
||||
where,
|
||||
attributes: {
|
||||
exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'placeId', 'description', 'resources']
|
||||
// include: [[Sequelize.fn('COUNT', Sequelize.col('activitypub_id')), 'ressources']]
|
||||
exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'description', 'resources']
|
||||
},
|
||||
order: ['start_datetime', [Tag, 'weigth', 'DESC']],
|
||||
order: ['start_datetime', Sequelize.literal('(SELECT COUNT("tagTag") FROM event_tags WHERE "tagTag" = tag) DESC')],
|
||||
include: [
|
||||
{ model: Resource, required: false, attributes: ['id'] },
|
||||
{ model: Tag, attributes: ['tag'], required: !!tags, ...where_tags, through: { attributes: [] } },
|
||||
{ model: Place, required: false, attributes: ['id', 'name', 'address'] }
|
||||
{
|
||||
model: Tag,
|
||||
attributes: ['tag'],
|
||||
required: !!tags,
|
||||
...where_tags,
|
||||
through: { attributes: [] }
|
||||
},
|
||||
{ model: Place, required: true, attributes: ['id', 'name', 'address'] }
|
||||
]
|
||||
}).catch(e => {
|
||||
log.error(e)
|
||||
|
||||
@@ -51,6 +51,7 @@ Event.belongsTo(User)
|
||||
User.hasMany(Event)
|
||||
|
||||
Event.belongsToMany(Tag, { through: 'event_tags' })
|
||||
Tag.belongsToMany(Event, { through: 'event_tags' })
|
||||
|
||||
Event.belongsToMany(Notification, { through: EventNotification })
|
||||
Notification.belongsToMany(Event, { through: EventNotification })
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
const sequelize = require('./index')
|
||||
|
||||
// const Event = require('./event')
|
||||
class Place extends Model {}
|
||||
|
||||
Place.init({
|
||||
@@ -11,14 +10,7 @@ Place.init({
|
||||
index: true,
|
||||
allowNull: false
|
||||
},
|
||||
address: DataTypes.STRING,
|
||||
confirmed: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: true,
|
||||
allowNull: false
|
||||
}
|
||||
address: DataTypes.STRING
|
||||
}, { sequelize, modelName: 'place' })
|
||||
|
||||
// Place.hasMany(Event)
|
||||
|
||||
module.exports = Place
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
// const Event = require('./event')
|
||||
const sequelize = require('./index')
|
||||
|
||||
class Tag extends Model {}
|
||||
@@ -10,19 +9,7 @@ Tag.init({
|
||||
allowNull: false,
|
||||
index: true,
|
||||
primaryKey: true
|
||||
},
|
||||
weigth: {
|
||||
type: DataTypes.INTEGER,
|
||||
defaultValue: 0,
|
||||
allowNull: false
|
||||
},
|
||||
confirmed: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: true,
|
||||
allowNull: false
|
||||
}
|
||||
}, { sequelize, modelName: 'tag' })
|
||||
|
||||
// Tag.belongsToMany(Event, { through: 'event_tags' })
|
||||
|
||||
module.exports = Tag
|
||||
|
||||
22
server/helpers/place.js
Normal file
22
server/helpers/place.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const Place = require('../api/models/place')
|
||||
const Event = require('../api/models/event')
|
||||
const Sequelize = require('sequelize')
|
||||
const log = require('../log')
|
||||
|
||||
module.exports = {
|
||||
// remove places not related to any events
|
||||
async _cleanUnused () {
|
||||
const places = await Place.findAll({
|
||||
include: [{ model: Event, as: 'events', required: false, attributes: [] }],
|
||||
group: ['place.id'],
|
||||
having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col('events.id')), '=', 0)
|
||||
})
|
||||
if (!places.length) { return }
|
||||
log.debug(`Remove ${places.length} unrelated places`)
|
||||
|
||||
const ids = places.map(p => p.id)
|
||||
await Place.destroy({
|
||||
where: { id: { [Sequelize.Op.in]: ids } }
|
||||
})
|
||||
}
|
||||
}
|
||||
22
server/helpers/tag.js
Normal file
22
server/helpers/tag.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const Tag = require('../api/models/tag')
|
||||
const Event = require('../api/models/event')
|
||||
const Sequelize = require('sequelize')
|
||||
const log = require('../log')
|
||||
|
||||
module.exports = {
|
||||
// remove tags not related to any events
|
||||
async _cleanUnused () {
|
||||
const tags = await Tag.findAll({
|
||||
include: [{ model: Event, as: 'events', required: false, attributes: [], through: { attributes: [] } }],
|
||||
group: ['tag.tag'],
|
||||
having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col('events.id')), '=', 0)
|
||||
})
|
||||
|
||||
if (!tags.length) { return }
|
||||
log.info(`Remove ${tags.length} unrelated tags`)
|
||||
|
||||
await Tag.destroy({
|
||||
where: { tag: { [Sequelize.Op.in]: tags.map(p => p.tag) } }
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
return queryInterface.removeColumn('tags', 'confirmed')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
return queryInterface.removeColumn('places', 'confirmed')
|
||||
}
|
||||
}
|
||||
6
server/migrations/20210409212842-remove_tag_weigth.js
Normal file
6
server/migrations/20210409212842-remove_tag_weigth.js
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
return queryInterface.removeColumn('tags', 'weigth')
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
const log = require('./log')
|
||||
const eventController = require('./api/controller/event')
|
||||
const placeHelpers = require('./helpers/place')
|
||||
const tagHelpers = require('./helpers/tag')
|
||||
// const notifier = require('./notifier')
|
||||
|
||||
const loopInterval = process.env.NODE_ENV === 'production' ? 15 : 1
|
||||
@@ -42,6 +44,7 @@ class Task {
|
||||
* - Send AP notifications
|
||||
* - Create recurrent events
|
||||
* - Sync AP federation profiles
|
||||
* - Remove unused tags/places
|
||||
*/
|
||||
|
||||
class TaskManager {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
export default {
|
||||
theme: {
|
||||
dark: true,
|
||||
// theme: { disable: true },
|
||||
themes: {
|
||||
dark: {
|
||||
primary: '#FF6E40'
|
||||
|
||||
Reference in New Issue
Block a user