keep going with recurrent event

This commit is contained in:
lesion
2019-07-13 01:02:11 +02:00
parent c929c314a9
commit 03b563971b
15 changed files with 109 additions and 72 deletions

View File

@@ -22,7 +22,7 @@ const botController = {
listener.on('error', botController.error)
},
async post(event) {
const status = `${event.title} @${event.place.name} ${moment(event.start_datetime*1000).format('ddd, D MMMM HH:mm')} -
const status = `${event.title} @${event.place.name} ${moment(event.start_datetime).format('ddd, D MMMM HH:mm')} -
${event.description.length > 200 ? event.description.substr(0, 200) + '...' : event.description} - ${event.tags.map(t => '#' + t.tag).join(' ')} ${config.baseurl}/event/${event.id}`
let media

View File

@@ -194,53 +194,55 @@ const eventController = {
let events = await Event.findAll({
where: {
// return only confirmed events
is_visible: true,
[Op.and]: [
Sequelize.literal(`start_datetime >= ${start.unix()}`),
Sequelize.literal(`start_datetime <= ${end.unix()}`)
[Op.or]: [
// return all recurrent events
{recurrent: { [Op.ne]: null }},
// and events in specified range
{ start_datetime: { [Op.between]: [start.unix(), end.unix()] }}
]
},
order: [
['start_datetime', 'ASC'],
[Tag, 'weigth', 'DESC']
],
attributes: { exclude: ['createdAt', 'updatedAt', 'placeId' ] },
order: [[Tag, 'weigth', 'DESC']],
include: [
{ model: Tag, required: false, attributes: ['tag', 'weigth'] },
{ model: Tag, required: false },
{ model: Place, required: false, attributes: ['id', 'name', 'address'] }
]
})
events = events.map(e => {
events = events.map(e => e.get()).map(e => {
e.start_datetime = e.start_datetime*1000
e.end_datetime = e.end_datetime*1000
e.tags = e.tags.map(t => t.tag)
return e
})
// build singular events from a recurrent pattern from today due to
// specified parameters
function createEventsFromRecurrent(e, dueTo=null, maxEvents=20) {
// build singular events from a recurrent pattern
function createEventsFromRecurrent(e, dueTo=null, maxEvents=8) {
const events = []
const cursor = moment()
const recurrent = JSON.parse(e.recurrent)
if (!recurrent.frequency) return false
const cursor = moment(start)
const start_date = moment(e.start_datetime)
const frequency = e.recurrent.frequency
const days = e.recurrent.days
const ordinal = e.recurrent.ordinal
const frequency = recurrent.frequency
const days = [start_date.day()]
const ordinal = recurrent.ordinal
// EACH WEEK
if (frequency === '1w') {
while(true) {
const found = days.indexOf(cursor.day())
if (found) break
if (found!==-1) break
cursor.add(1, 'day')
}
e.start_datetime = cursor.set('hour', e.start_datetime.hour()).set('minute', e.start_datetime.minutes())
cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
while (true) {
if ((dueTo && cursor.isAfter(dueTo)) || events.length>maxEvents) break
e.start_datetime = cursor.unix()
events.push(e)
cursors.add(1, 'week')
e.start_datetime = cursor.unix()*1000
events.push( Object.assign({}, e) )
cursor.add(1, 'week')
}
}
@@ -249,10 +251,15 @@ const eventController = {
return events
}
const normalEvents = events.filter(e => !e.recurrent)
const recurrentEvents = events.filter(e => e.recurrent).map(createEventsFromRecurrent)
let allEvents = events.filter(e => !e.recurrent)
events.filter(e => e.recurrent).forEach(e => {
const events = createEventsFromRecurrent(e, end)
if (events)
allEvents = allEvents.concat(events)
})
res.json(normalEvents.concat(recurrentEvents))
// allEvents.sort((a,b) => a.start_datetime-b.start_datetime)
res.json(allEvents.sort((a,b) => a.start_datetime-b.start_datetime))
}
}

View File

@@ -25,8 +25,8 @@ const settingsController = {
await Setting.findOrCreate({
where: { key },
defaults: { value, is_secret }
}).spread((settings, created) => {
if (!created) return settings.update({ value, is_secret })
}).spread((setting, created) => {
if (!created) return setting.update({ value, is_secret })
})
settingsController[is_secret?'secretSettings':'settings'][key]=value
return true

View File

@@ -82,7 +82,8 @@ const userController = {
start_datetime: body.start_datetime,
end_datetime: body.end_datetime,
// publish this event if authenticated
recurrent: body.recurrent,
// publish this event only if authenticated
is_visible: !!req.user
}