diff --git a/plugins/api.js b/plugins/api.js index 23247b59..8ddec24f 100644 --- a/plugins/api.js +++ b/plugins/api.js @@ -10,7 +10,7 @@ export default ({ $axios }, inject) => { * end_datetime: unix_timestamp * tags: [tag, list], * places: [place_id], - * limit: (default ∞) + * max: (default ∞) * } * */ @@ -22,7 +22,8 @@ export default ({ $axios }, inject) => { end: params.end, places: params.places && params.places.join(','), tags: params.tags && params.tags.join(','), - show_recurrent: !!params.show_recurrent + show_recurrent: !!params.show_recurrent, + max: params.maxs } }) return events.map(e => Object.freeze(e)) diff --git a/server/api/controller/event.js b/server/api/controller/event.js index cf943430..685deb51 100644 --- a/server/api/controller/event.js +++ b/server/api/controller/event.js @@ -22,38 +22,51 @@ const log = require('../../log') const eventController = { - async _getMeta () { + async searchMeta (req, res) { + const search = req.query.search + const places = await Place.findAll({ - order: [[Sequelize.literal('weigth'), 'DESC']], - attributes: { - include: [[Sequelize.fn('count', Sequelize.col('events.placeId')), 'weigth']], - exclude: ['createdAt', 'updatedAt'] + order: [[Sequelize.col('w'), 'DESC']], + where: { + [Op.or]: [ + { name: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), 'LIKE', '%' + search + '%' )}, + { address: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('address')), 'LIKE', '%' + search + '%')}, + ] }, + attributes: [['name', 'label'], 'address', 'id', [Sequelize.cast(Sequelize.fn('COUNT', Sequelize.col('events.placeId')),'INTEGER'), 'w']], include: [{ model: Event, where: { is_visible: true }, required: true, attributes: [] }], - group: ['place.id'] + group: ['place.id'], + raw: true }) const tags = await Tag.findAll({ - order: [[Sequelize.literal('w'), 'DESC']], - attributes: { - include: [[Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'w']] - }, + order: [[Sequelize.col('w'), 'DESC']], + where: { + tag: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('tag')), 'LIKE', '%' + search + '%'), + }, + attributes: [['tag','label'], [Sequelize.cast(Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'INTEGER'), 'w']], include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }], - group: ['tag.tag'] + group: ['tag.tag'], + raw: true }) - return { places, tags } + const ret = places.map(p => { + p.type = 'place' + return p + }).concat(tags.map(t => { + t.type = 'tag' + return t + })).sort( (a, b) => b.w - a.w).slice(0, 10) + + return res.json(ret) }, - async getMeta (req, res) { - res.json(await eventController._getMeta()) - }, async search (req, res) { const search = req.query.search // search for events - const events = Event.findAll({ + const events = await Event.findAll({ logging: console.log, order: [['start_datetime', 'DESC']], attributes: { @@ -65,48 +78,11 @@ const eventController = { parentId: null, title: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('title')), 'LIKE', '%' + search + '%'), }, - limit: 3 + limit: 10 }) - // search for tags - const tags = Tag.findAll({ - order: [[Sequelize.literal('w'), 'DESC']], - attributes: { - include: [[Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'w']], - }, - where: { tag: { [Op.substring]: search } }, - include: [{ model: Event, where: { is_visible: true, recurrent: null, parentId: null }, attributes: [], through: { attributes: [] }, required: true }], - group: ['tag.tag'], - limit: 3 - }) - // let places - // try { - // places = await Place.findAll({ - // logging: console.log, - // where: { name: { [Op.substring]: search } }, - // order: [[Sequelize.literal('weigth'), 'DESC']], - // attributes: { - // include: [[Sequelize.fn('count', Sequelize.col('events.placeId')), 'weigth']], - // // exclude: ['createdAt', 'updatedAt'] - // }, - // include: [{ model: Event, where: { is_visible: true }, through: { attributes: [] }, attributes: [] }], - // limit: 3, - // group: ['place.id'], - // }) - // } catch (e) { - // console.error(e) - // } - - - const [ tmpevents, tmptags] = await Promise.all([events, tags]) - - let ret = tmpevents - .map(e => ({ ...e.get(), type: 'event' })) - .concat(tmptags.map(t => ({ ...t.get(), type: 'tag' }))) - - - return res.json(ret) + return res.json(events) }, async getNotifications (event, action) { @@ -137,12 +113,6 @@ const eventController = { return notifications.filter(notification => match(event, notification.filters)) }, - async updatePlace (req, res) { - const place = await Place.findByPk(req.body.id) - await place.update(req.body) - res.json(place) - }, - async _get(slug) { // retrocompatibility, old events URL does not use slug, use id as fallback const id = Number(slug) || -1 diff --git a/server/api/index.js b/server/api/index.js index 4749295f..7ebd192a 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -80,9 +80,6 @@ if (config.status !== 'READY') { // get all users api.get('/users', isAdmin, userController.getAll) - // update a place (modify address..) - api.put('/place', isAdmin, eventController.updatePlace) - /** * Get events * @category Event @@ -132,7 +129,7 @@ if (config.status !== 'READY') { api.delete('/event/:id', isAuth, eventController.remove) // get tags/places - api.get('/event/meta', eventController.getMeta) + api.get('/event/meta', eventController.searchMeta) // get unconfirmed events api.get('/event/unconfirmed', isAdmin, eventController.getUnconfirmed) @@ -157,11 +154,11 @@ if (config.status !== 'READY') { api.get('/place/:placeName/events', cors, placeController.getEvents) - // api.get('/place', cors, placeController.) + api.get('/place', cors, placeController.get) + api.put('/place', isAdmin, placeController.updatePlace) api.get('/tag', cors, tagController.get) - // - FEDIVERSE INSTANCES, MODERATION, RESOURCES api.get('/instances', isAdmin, instanceController.getAll) api.get('/instances/:instance_domain', isAdmin, instanceController.get) @@ -192,10 +189,10 @@ if (config.status !== 'READY') { api.post('/client', oauthController.createClient) } -api.use((req, res) => res.sendStatus(404)) +api.use((_req, res) => res.sendStatus(404)) // Handle 500 -api.use((error, req, res, next) => { +api.use((error, _req, res, _next) => { log.error('[API ERROR]', error) res.status(500).send('500: Internal Server Error') })