diff --git a/server/api/controller/event.js b/server/api/controller/event.js index 481828ab..823b5ef3 100644 --- a/server/api/controller/event.js +++ b/server/api/controller/event.js @@ -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) diff --git a/server/api/models/event.js b/server/api/models/event.js index a59b23b3..2f04b142 100644 --- a/server/api/models/event.js +++ b/server/api/models/event.js @@ -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 }) diff --git a/server/api/models/place.js b/server/api/models/place.js index 8db4e845..fac19112 100644 --- a/server/api/models/place.js +++ b/server/api/models/place.js @@ -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 diff --git a/server/api/models/tag.js b/server/api/models/tag.js index 7a47e7af..ec292354 100644 --- a/server/api/models/tag.js +++ b/server/api/models/tag.js @@ -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 diff --git a/server/helpers/place.js b/server/helpers/place.js new file mode 100644 index 00000000..ff87797a --- /dev/null +++ b/server/helpers/place.js @@ -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 } } + }) + } +} diff --git a/server/helpers/tag.js b/server/helpers/tag.js new file mode 100644 index 00000000..63f31627 --- /dev/null +++ b/server/helpers/tag.js @@ -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) } } + }) + } +} diff --git a/server/migrations/20210409212349-remove_tag_confirmation.js b/server/migrations/20210409212349-remove_tag_confirmation.js new file mode 100644 index 00000000..ab01586e --- /dev/null +++ b/server/migrations/20210409212349-remove_tag_confirmation.js @@ -0,0 +1,6 @@ + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.removeColumn('tags', 'confirmed') + } +} diff --git a/server/migrations/20210409212803-remove_place_confirmation.js b/server/migrations/20210409212803-remove_place_confirmation.js new file mode 100644 index 00000000..4775b923 --- /dev/null +++ b/server/migrations/20210409212803-remove_place_confirmation.js @@ -0,0 +1,6 @@ + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.removeColumn('places', 'confirmed') + } +} diff --git a/server/migrations/20210409212842-remove_tag_weigth.js b/server/migrations/20210409212842-remove_tag_weigth.js new file mode 100644 index 00000000..9c9e7b2d --- /dev/null +++ b/server/migrations/20210409212842-remove_tag_weigth.js @@ -0,0 +1,6 @@ + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.removeColumn('tags', 'weigth') + } +} diff --git a/server/taskManager.js b/server/taskManager.js index 1fc40627..18a583c8 100644 --- a/server/taskManager.js +++ b/server/taskManager.js @@ -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 { diff --git a/vuetify.options.js b/vuetify.options.js index 983751c7..474ac8bf 100644 --- a/vuetify.options.js +++ b/vuetify.options.js @@ -2,6 +2,7 @@ export default { theme: { dark: true, + // theme: { disable: true }, themes: { dark: { primary: '#FF6E40'