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

@@ -1,7 +1,7 @@
const { Collection, Filter, Event, Tag, Place } = require('../models/models')
const log = require('../../log')
const dayjs = require('dayjs')
const { DateTime } = require('luxon')
const { col: Col } = require('../../helpers')
const { Op, Sequelize } = require('sequelize')
@@ -21,7 +21,6 @@ const collectionController = {
// return events from collection
async getEvents (req, res) {
const format = req.params.format || 'json'
const name = req.params.name
const collection = await Collection.findOne({ where: { name } })
@@ -33,7 +32,7 @@ const collectionController = {
if (!filters.length) {
return res.json([])
}
const start = dayjs().unix()
const start = DateTime.local().toUnixInteger()
const where = {
// do not include parent recurrent event
recurrent: null,

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)

View File

@@ -2,7 +2,7 @@ const { Event, Place, Tag } = require('../models/models')
const { htmlToText } = require('html-to-text')
const { Op, literal } = require('sequelize')
const moment = require('dayjs')
const { DateTime } = require('luxon')
const ics = require('ics')
const exportController = {
@@ -13,8 +13,13 @@ const exportController = {
const places = req.query.places
const show_recurrent = !!req.query.show_recurrent
const opt = {
zone: res.locals.settings.instance_timezone,
locale: res.locals.settings.instance_locale
}
const where = {}
const yesterday = moment().subtract('1', 'day').unix()
const yesterday = DateTime.local(opt).minus({day: 1}).toUnixInteger()
if (tags && places) {
@@ -69,8 +74,18 @@ const exportController = {
feed (_req, res, events, title = res.locals.settings.title, link = `${res.locals.settings.baseurl}/feed/rss`) {
const settings = res.locals.settings
const opt = {
zone: settings.instance_timezone,
locale: settings.instance_locale
}
function unixFormat (timestamp, format='EEEE d MMMM HH:mm') {
return DateTime.fromSeconds(timestamp, opt).toFormat(format)
}
res.type('application/rss+xml; charset=UTF-8')
res.render('feed/rss.pug', { events, settings, moment, title, link })
res.render('feed/rss.pug', { events, settings, unixFormat, title, link })
},
/**

View File

@@ -88,7 +88,7 @@ const pluginController = {
try {
const plugin = require(pluginFile)
const name = plugin.configuration.name
console.log(`Found plugin '${name}'`)
log.info(`Found plugin '${name}'`)
pluginController.plugins.push(plugin)
if (settingsController.settings['plugin_' + name]) {
const pluginSetting = settingsController.settings['plugin_' + name]