use luxon instead of dayjs server side too

This commit is contained in:
lesion
2023-03-28 19:02:08 +02:00
parent f5604a03bc
commit fc52107bd9
12 changed files with 102 additions and 80 deletions

View File

@@ -5,7 +5,7 @@ const fs = require('fs/promises')
const { Op } = require('sequelize')
const linkifyHtml = require('linkify-html')
const Sequelize = require('sequelize')
const dayjs = require('dayjs')
const { DateTime } = require('luxon')
const helpers = require('../../helpers')
const Col = helpers.col
const notifier = require('../../notifier')
@@ -252,7 +252,7 @@ const eventController = {
where: {
parentId: null,
is_visible: false,
start_datetime: { [Op.gt]: dayjs().unix() }
start_datetime: { [Op.gt]: DateTime.local().toUnixInteger() }
},
order: [['start_datetime', 'ASC']],
include: [{ model: Tag, required: false }, Place]
@@ -532,7 +532,7 @@ const eventController = {
* @returns
*/
async _select({
start = dayjs().unix(),
start = DateTime.local().toUnixInteger(),
end,
query,
tags,
@@ -541,7 +541,8 @@ const eventController = {
show_multidate,
limit,
page,
older }) {
older,
reverse }) {
const where = {
// do not include _parent_ recurrent event
@@ -611,7 +612,7 @@ const eventController = {
attributes: {
exclude: ['likes', 'boost', 'userId', 'is_visible', 'createdAt', 'description', 'resources', 'recurrent', 'placeId', 'image_path']
},
order: [['start_datetime', older ? 'DESC' : 'ASC']],
order: [['start_datetime', reverse ? 'DESC' : 'ASC']],
include: [
{
model: Tag,
@@ -640,7 +641,7 @@ const eventController = {
*/
async select(req, res) {
const settings = res.locals.settings
const start = req.query.start || dayjs().unix()
const start = req.query.start || DateTime.local().toUnixInteger()
const end = req.query.end
const query = req.query.query
const tags = req.query.tags
@@ -661,10 +662,12 @@ const eventController = {
},
/**
* Ensure we have the next instance of a recurrent event
* Ensure we have the next occurrence of a recurrent event
*/
async _createRecurrentOccurrence(e, startAt, firstOccurrence) {
async _createRecurrentOccurrence(e, startAt = DateTime.local(), firstOccurrence = true) {
log.debug(`Create recurrent event [${e.id}] ${e.title}"`)
// prepare the new event occurrence copying the parent's properties
const event = {
parentId: e.id,
title: e.title,
@@ -675,49 +678,57 @@ const eventController = {
placeId: e.placeId
}
const recurrent = e.recurrent
const start_date = dayjs.unix(e.start_datetime)
let cursor = start_date > startAt ? start_date : startAt
startAt = cursor
const duration = e.end_datetime ? e.end_datetime-e.start_datetime : 0
const frequency = recurrent.frequency
const type = recurrent.type
const recurrentDetails = e.recurrent
const parentStartDatetime = DateTime.fromSeconds(e.start_datetime)
cursor = cursor.hour(start_date.hour()).minute(start_date.minute()).second(0)
if (!frequency) { return }
// cursor is when start to count
// sets it to
let cursor = parentStartDatetime > startAt ? parentStartDatetime : startAt
startAt = cursor
const duration = e.end_datetime ? e.end_datetime-e.start_datetime : 0
const frequency = recurrentDetails.frequency
const type = recurrentDetails.type
if (!frequency) {
log.warn(`Recurrent event ${e.id} - ${e.title} does not have a frequency specified`)
return
}
cursor = cursor.set({ hour: parentStartDatetime.hour, minute: parentStartDatetime.minute, second: 0 })
// each week or 2
if (frequency[1] === 'w') {
cursor = cursor.day(start_date.day())
if (cursor.isBefore(startAt)) {
cursor = cursor.add(7, 'day')
}
if (frequency[0] === '2' && !firstOccurrence) {
cursor = cursor.add(7, 'day')
cursor = cursor.set({ weekday: parentStartDatetime.weekday }) //day(parentStartDatetime.day())
if (cursor < startAt) {
cursor = cursor.plus({ days: 7 * Number(frequency[0]) })
}
} else if (frequency === '1m') {
if (type === 'ordinal') {
cursor = cursor.date(start_date.date())
cursor = cursor.set({ day: parentStartDatetime.day })
if (cursor.isBefore(startAt)) {
cursor = cursor.add(1, 'month')
if (cursor< startAt) {
cursor = cursor.plus({ months: 1 })
}
} else { // weekday
// get weekday
// get recurrent freq details
cursor = helpers.getWeekdayN(cursor, type, start_date.day())
if (cursor.isBefore(startAt)) {
cursor = cursor.add(4, 'week')
cursor = helpers.getWeekdayN(cursor, type, start_date.day())
cursor = helpers.getWeekdayN(cursor, type, parentStartDatetime.weekday)
if (cursor< startAt) {
cursor = cursor.plus({ months: 1 })
cursor = helpers.getWeekdayN(cursor, type, parentStartDatetime.weekday)
}
}
}
log.debug(cursor)
event.start_datetime = cursor.unix()
event.start_datetime = cursor.toUnixInteger()
event.end_datetime = e.end_datetime ? event.start_datetime + duration : null
try {
const newEvent = await Event.create(event)
return newEvent.addTags(e.tags)
if (e.tags) {
return newEvent.addTags(e.tags)
} else {
return newEvent
}
} catch (e) {
console.error(event)
log.error('[RECURRENT EVENT]', e)
@@ -727,7 +738,7 @@ const eventController = {
/**
* Create instances of recurrent events
*/
async _createRecurrent(start_datetime = dayjs().unix()) {
async _createRecurrent(start_datetime = DateTime.local().toUnixInteger()) {
// select recurrent events and its childs
const events = await Event.findAll({
where: { is_visible: true, recurrent: { [Op.ne]: null } },
@@ -740,9 +751,9 @@ const eventController = {
const creations = events.map(e => {
if (e.child.length) {
if (e.child.find(c => c.is_visible)) return
return eventController._createRecurrentOccurrence(e, dayjs.unix(e.child[0].start_datetime + 1), false)
return eventController._createRecurrentOccurrence(e, DateTime.fromSeconds(e.child[0].start_datetime + 1), false)
}
return eventController._createRecurrentOccurrence(e, dayjs(), true)
return eventController._createRecurrentOccurrence(e, DateTime.local(), true)
})
return Promise.all(creations)