metis/src/calendar.js

184 lines
4.2 KiB
JavaScript
Raw Normal View History

const clouds = {
2022-03-06 00:59:11 +01:00
KLUB_RESEAU: 'klub-reseau',
ELEVES_ENS: 'eleves-ens',
FRAMA_AGENDA: 'frama-agenda'
2022-03-06 00:59:11 +01:00
}
2022-02-20 22:29:15 +01:00
const calendars = {
2022-03-06 00:59:11 +01:00
'5WrcagPPARQ3BD87': {
cloud: clouds.KLUB_RESEAU,
2022-03-06 00:59:11 +01:00
name: 'Club réseau',
color: null,
default_location: "Cave d'hackENS"
2022-02-20 22:29:15 +01:00
},
2022-03-06 00:59:11 +01:00
TFEAKjAgNFQZpNjo: {
cloud: clouds.KLUB_RESEAU,
2022-03-06 00:59:11 +01:00
name: 'hackENS',
color: null,
default_location: "Cave d'hackENS"
},
2022-03-06 00:59:11 +01:00
LLWm8qK9iC5YGrrR: {
cloud: clouds.ELEVES_ENS,
2022-03-06 00:59:11 +01:00
name: 'Délégation Générale',
short_name: 'DG',
color: null
2022-03-06 15:21:46 +01:00
},
w442JdS5AaQ6czrP: {
cloud: clouds.ELEVES_ENS,
name: "Écriv'ENS",
2022-03-06 19:38:26 +01:00
color: null
2022-03-06 21:22:42 +01:00
},
fRtjDkjrZyn6fxd8: {
cloud: clouds.ELEVES_ENS,
2022-03-07 08:33:46 +01:00
name: 'K-Fêt',
2022-03-07 10:59:05 +01:00
color: '#c63b52',
default_location: 'K-Fêt'
},
gsZtZK8c9EmREofn: {
cloud: clouds.ELEVES_ENS,
2022-03-11 22:43:13 +01:00
name: 'Ernestophone',
color: null
},
T5WoHbs4FT5A945Z: {
cloud: clouds.FRAMA_AGENDA,
name: 'CinéClub',
color: null
2022-03-06 00:59:11 +01:00
}
}
2022-02-20 22:29:15 +01:00
const calendarsByName = Object.fromEntries(
2022-03-06 19:38:26 +01:00
Object.entries(calendars).map(([id, { name }]) => [name, id])
)
2022-02-20 22:29:15 +01:00
export const calendarTree = {
2022-03-06 00:59:11 +01:00
'Clubs COF': {
'Club réseau': {},
2022-03-06 15:21:46 +01:00
hackENS: {},
"Écriv'ENS": {},
CinéClub: {},
2022-03-11 22:43:13 +01:00
Ernestophone: {},
'Club inutile': {}
},
2022-03-06 00:59:11 +01:00
COF: {
BDA: {},
2022-03-06 00:59:11 +01:00
AG: {}
},
2022-03-06 00:59:11 +01:00
BDS: {},
2022-03-06 21:22:42 +01:00
'Délégation Générale': {},
'K-Fêt': {}
2022-03-06 00:59:11 +01:00
}
2022-02-20 22:29:15 +01:00
2022-03-06 00:59:11 +01:00
const calendarIds = Object.keys(calendars)
2022-02-20 19:16:33 +01:00
2022-03-06 19:38:26 +01:00
function mkCalendarUrl(id, { cloud }, extra = {}) {
return (
`/cal/${cloud}/${id}/?` +
new URLSearchParams({
...extra,
export: true,
expand: true,
accept: 'jcal'
})
)
2022-02-20 19:16:33 +01:00
}
2022-03-06 19:38:26 +01:00
function fetchCalendar(id, cal, extra = {}) {
return fetch(mkCalendarUrl(id, cal, extra), { credentials: 'omit' })
2022-03-06 00:59:11 +01:00
.then(resp => resp.json())
.catch(err => console.error(err))
2022-02-20 19:16:33 +01:00
}
class Calendar {
2022-03-06 00:59:11 +01:00
constructor(id, calendar) {
const metadata = calendars[id]
this.name = metadata.name
2022-03-06 13:47:05 +01:00
this.short_name = metadata.short_name
2022-03-06 00:59:11 +01:00
this.color = metadata.color || calendar[1][4][3]
2022-03-07 10:59:05 +01:00
this.default_location = metadata.default_location
2022-03-06 00:59:11 +01:00
this.events = calendar[2]
.filter(item => item[0] === 'vevent')
.map(item => this._parse_vevent(item[1]))
2022-02-20 19:16:33 +01:00
}
_parse_vevent(vevent) {
2022-03-06 00:59:11 +01:00
const event = {}
2022-02-20 19:16:33 +01:00
vevent.forEach(elt => {
2022-03-06 00:59:11 +01:00
event[elt[0]] = elt[3]
})
return event
2022-02-20 19:16:33 +01:00
}
}
2022-02-20 22:29:15 +01:00
function fcEventFromjCalEvent(cal) {
return function (evt) {
2022-03-06 00:59:11 +01:00
const start = new Date(evt.dtstart)
const end = new Date(evt.dtend)
2022-02-20 22:29:15 +01:00
const fcEvent = {
2022-03-06 13:47:05 +01:00
title: `${cal.short_name ?? cal.name} : ${evt.summary}`,
start: evt.dtstart,
end: evt.dtend,
2022-02-20 22:29:15 +01:00
color: cal.color,
duration: end - start // in ms
2022-03-06 00:59:11 +01:00
}
2022-03-06 13:47:05 +01:00
fcEvent.calendar = cal.name
fcEvent.short_name = evt.summary
2022-03-07 10:59:05 +01:00
fcEvent.description = evt.description
fcEvent.location = evt.location || cal.default_location
2022-02-20 22:29:15 +01:00
if (evt.status) {
fcEvent.status = evt.status
fcEvent.classNames = [`st-${evt.status.toLowerCase()}`]
}
2022-02-20 22:29:15 +01:00
if (evt.rrule) {
2022-03-06 00:59:11 +01:00
const { freq, byday, interval } = evt.rrule
2022-02-20 22:29:15 +01:00
fcEvent.rrule = {
freq,
byweekday: byday,
2022-03-06 00:59:11 +01:00
dtstart: evt.dtstart
}
2022-02-21 22:13:44 +01:00
if (interval) {
2022-03-06 00:59:11 +01:00
fcEvent.rrule.interval = interval
2022-02-21 22:13:44 +01:00
}
2022-02-20 22:29:15 +01:00
}
2022-03-06 00:59:11 +01:00
return fcEvent
2022-02-20 22:29:15 +01:00
}
}
function mkEventsFromCalendar(id, cal) {
return fetchCalendar(id, cal).then(calendar => {
2022-03-06 00:59:11 +01:00
if (calendar[0] !== 'vcalendar') return
2022-02-20 22:29:15 +01:00
const cal = new Calendar(id, calendar)
return cal.events.map(fcEventFromjCalEvent(cal))
2022-03-06 00:59:11 +01:00
})
2022-02-20 19:16:33 +01:00
}
export function mkSource(name) {
2022-03-06 19:38:26 +01:00
const calendarId = calendarsByName[name]
if (!calendarId) return null
const calendar = calendars[calendarId]
2022-02-20 19:16:33 +01:00
return {
id: name,
...(calendar?.meta || {}),
success: calendarData => {
2022-03-06 19:38:26 +01:00
if (calendarData[0] !== 'vcalendar') return
const cal = new Calendar(calendarId, calendarData)
return cal.events.map(fcEventFromjCalEvent(cal))
},
failure: error => {
2022-03-06 19:38:26 +01:00
console.error(`Fatal error during event source fetching of '${name}': ${error}`)
},
events: (info, successCallback, failureCallback) => {
2022-03-06 19:38:26 +01:00
const { start, end } = info
fetchCalendar(calendarId, calendar, {
start: start.valueOf() / 1000,
end: end.valueOf() / 1000
2022-03-06 19:38:26 +01:00
}).then(successCallback, failureCallback)
}
2022-02-20 19:16:33 +01:00
}
}