clean unused places/tags #107 and unused fields

This commit is contained in:
les
2021-04-09 23:54:17 +02:00
parent 24170f0dfa
commit 5ac73caeac
11 changed files with 86 additions and 46 deletions

View File

@@ -25,23 +25,22 @@ const eventController = {
async _getMeta () { async _getMeta () {
const places = await Place.findAll({ const places = await Place.findAll({
where: { confirmed: true },
order: [[Sequelize.literal('weigth'), 'DESC']], order: [[Sequelize.literal('weigth'), 'DESC']],
attributes: { attributes: {
include: [[Sequelize.fn('count', Sequelize.col('events.placeId')), 'weigth']], include: [[Sequelize.fn('count', Sequelize.col('events.placeId')), 'weigth']],
exclude: ['createdAt', 'updatedAt'] exclude: ['createdAt', 'updatedAt']
}, },
include: [{ model: Event, attributes: [] }], include: [{ model: Event, where: { is_visible: true }, required: true, attributes: [] }],
group: ['place.id'] group: ['place.id']
}) })
const tags = await Tag.findAll({ const tags = await Tag.findAll({
where: { confirmed: true }, order: [[Sequelize.literal('w'), 'DESC']],
raw: true,
order: [['weigth', 'DESC']],
attributes: { 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 } return { places, tags }
@@ -178,14 +177,6 @@ const eventController = {
try { try {
event.is_visible = true 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() await event.save()
res.sendStatus(200) res.sendStatus(200)
@@ -295,8 +286,7 @@ const eventController = {
const [place] = await Place.findOrCreate({ const [place] = await Place.findOrCreate({
where: { name: body.place_name }, where: { name: body.place_name },
defaults: { defaults: {
address: body.place_address, address: body.place_address
confirmed: !!req.user
} }
}) })
@@ -305,9 +295,8 @@ const eventController = {
// create/assign tags // create/assign tags
if (body.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 } } }) 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) await event.addTags(tags)
event.tags = tags event.tags = tags
} }
@@ -456,14 +445,19 @@ const eventController = {
const events = await Event.findAll({ const events = await Event.findAll({
where, where,
attributes: { attributes: {
exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'placeId', 'description', 'resources'] exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'description', 'resources']
// include: [[Sequelize.fn('COUNT', Sequelize.col('activitypub_id')), 'ressources']]
}, },
order: ['start_datetime', [Tag, 'weigth', 'DESC']], order: ['start_datetime', Sequelize.literal('(SELECT COUNT("tagTag") FROM event_tags WHERE "tagTag" = tag) DESC')],
include: [ include: [
{ model: Resource, required: false, attributes: ['id'] }, { 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 => { }).catch(e => {
log.error(e) log.error(e)

View File

@@ -51,6 +51,7 @@ Event.belongsTo(User)
User.hasMany(Event) User.hasMany(Event)
Event.belongsToMany(Tag, { through: 'event_tags' }) Event.belongsToMany(Tag, { through: 'event_tags' })
Tag.belongsToMany(Event, { through: 'event_tags' })
Event.belongsToMany(Notification, { through: EventNotification }) Event.belongsToMany(Notification, { through: EventNotification })
Notification.belongsToMany(Event, { through: EventNotification }) Notification.belongsToMany(Event, { through: EventNotification })

View File

@@ -1,7 +1,6 @@
const { Model, DataTypes } = require('sequelize') const { Model, DataTypes } = require('sequelize')
const sequelize = require('./index') const sequelize = require('./index')
// const Event = require('./event')
class Place extends Model {} class Place extends Model {}
Place.init({ Place.init({
@@ -11,14 +10,7 @@ Place.init({
index: true, index: true,
allowNull: false allowNull: false
}, },
address: DataTypes.STRING, address: DataTypes.STRING
confirmed: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
}
}, { sequelize, modelName: 'place' }) }, { sequelize, modelName: 'place' })
// Place.hasMany(Event)
module.exports = Place module.exports = Place

View File

@@ -1,5 +1,4 @@
const { Model, DataTypes } = require('sequelize') const { Model, DataTypes } = require('sequelize')
// const Event = require('./event')
const sequelize = require('./index') const sequelize = require('./index')
class Tag extends Model {} class Tag extends Model {}
@@ -10,19 +9,7 @@ Tag.init({
allowNull: false, allowNull: false,
index: true, index: true,
primaryKey: true primaryKey: true
},
weigth: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false
},
confirmed: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
} }
}, { sequelize, modelName: 'tag' }) }, { sequelize, modelName: 'tag' })
// Tag.belongsToMany(Event, { through: 'event_tags' })
module.exports = Tag module.exports = Tag

22
server/helpers/place.js Normal file
View 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
View 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) } }
})
}
}

View File

@@ -0,0 +1,6 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('tags', 'confirmed')
}
}

View File

@@ -0,0 +1,6 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('places', 'confirmed')
}
}

View File

@@ -0,0 +1,6 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('tags', 'weigth')
}
}

View File

@@ -1,5 +1,7 @@
const log = require('./log') const log = require('./log')
const eventController = require('./api/controller/event') const eventController = require('./api/controller/event')
const placeHelpers = require('./helpers/place')
const tagHelpers = require('./helpers/tag')
// const notifier = require('./notifier') // const notifier = require('./notifier')
const loopInterval = process.env.NODE_ENV === 'production' ? 15 : 1 const loopInterval = process.env.NODE_ENV === 'production' ? 15 : 1
@@ -42,6 +44,7 @@ class Task {
* - Send AP notifications * - Send AP notifications
* - Create recurrent events * - Create recurrent events
* - Sync AP federation profiles * - Sync AP federation profiles
* - Remove unused tags/places
*/ */
class TaskManager { class TaskManager {

View File

@@ -2,6 +2,7 @@
export default { export default {
theme: { theme: {
dark: true, dark: true,
// theme: { disable: true },
themes: { themes: {
dark: { dark: {
primary: '#FF6E40' primary: '#FF6E40'