start a better events selection

This commit is contained in:
les
2020-10-17 00:41:21 +02:00
parent b80eb3f6e3
commit 3f892f7f4a
18 changed files with 199 additions and 266 deletions

View File

@@ -417,17 +417,30 @@ const eventController = {
}
},
async _select (start = moment().unix(), limit = 100) {
async _select ({ start, end, tags, places}) {
const where = {
// confirmed event only
recurrent: null,
is_visible: true,
start_datetime: { [Op.gt]: start }
start_datetime: { [Op.gt]: start },
}
if (end) {
where['end_datetime'] = { [Op.lt]: end }
}
if (places) {
where.placeId = places.split(',')
}
let where_tags = {}
if (tags) {
where_tags = { where: { tag: tags.split(',') } }
}
const events = await Event.findAll({
where,
limit,
attributes: {
exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'placeId']
// include: [[Sequelize.fn('COUNT', Sequelize.col('activitypub_id')), 'ressources']]
@@ -435,7 +448,7 @@ const eventController = {
order: ['start_datetime', [Tag, 'weigth', 'DESC']],
include: [
{ model: Resource, required: false, attributes: ['id'] },
{ model: Tag, attributes: ['tag'], required: false, through: { attributes: [] } },
{ model: Tag, attributes: ['tag'], required: tags ? true : false, ...where_tags, through: { attributes: [] } },
{ model: Place, required: false, attributes: ['id', 'name', 'address'] }
]
})
@@ -451,9 +464,14 @@ const eventController = {
* Select events based on params
*/
async select (req, res) {
const start = req.query.start || moment().unix()
const limit = req.query.limit || 100
res.json(await eventController._select(start, limit))
const start = req.query.start
const end = req.query.end
const tags = req.query.tags
const places = req.query.places
res.json(await eventController._select({
start, end, places, tags
}))
},
/**

View File

@@ -125,7 +125,6 @@ const settingsController = {
.resize(400)
.png({ quality: 90 })
.toFile(baseImgPath + '.png', async (err, info) => {
console.error(err)
const image = await readFile(baseImgPath + '.png')
const favicon = await toIco([image], { sizes: [64], resize: true })
writeFile(baseImgPath + '.ico', favicon)

View File

@@ -121,7 +121,7 @@ api.get('/export/:type', cors, exportController.export)
// get events in this range
// api.get('/event/:month/:year', cors, eventController.getAll)
api.get('/event', cors, eventController.select)
api.get('/events', cors, eventController.select)
api.get('/instances', isAdmin, instanceController.getAll)
api.get('/instances/:instance_domain', isAdmin, instanceController.get)

View File

@@ -115,7 +115,6 @@ module.exports = {
Microformats.get({ html: response.data, filter: ['h-event'] }, (err, data) => {
if (!data.items.length || !data.items[0].properties) return res.sendStatus(404)
const event = data.items[0].properties
console.error(event)
return res.json({
title: get(event, 'name[0]', ''),
description: get(event, 'content[0]', ''),

View File

@@ -66,8 +66,8 @@ app.use((error, req, res, next) => {
// first nuxt component is ./pages/index.vue (with ./layouts/default.vue)
// prefill current events, tags, places and announcements (used in every path)
app.use(async (req, res, next) => {
const start_datetime = getUnixTime(startOfWeek(startOfMonth(new Date())))
req.events = await eventController._select(start_datetime, 100)
// const start_datetime = getUnixTime(startOfWeek(startOfMonth(new Date())))
// req.events = await eventController._select(start_datetime, 100)
req.meta = await eventController._getMeta()
req.announcements = await announceController._getVisible()
next()