namespace _zeitbild.service.calendar { /** */ export async function list( search_term : (null | string) ) : Promise< Array< { id : _zeitbild.type.calendar_id; preview : { name : string; } } > > { return ( _zeitbild.repository.calendar.list(search_term) .then( x => x.map( (y : any) => ({ "id": y.key, "preview": y.preview, }) ) ) ); } /** */ export async function get( calendar_id : _zeitbild.type.calendar_id ) : Promise<_zeitbild.type.calendar_object> { return _zeitbild.repository.calendar.read(calendar_id); } /** * @todo prevent loops */ export async function gather_events( calendar_ids : Array<_zeitbild.type.calendar_id>, from_pit : _zeitbild.helpers.type_pit, to_pit : _zeitbild.helpers.type_pit ) : Promise< Array< { calendar_id : _zeitbild.type.calendar_id; calendar_name : string; event : _zeitbild.type.event_object; } > > { lib_plankton.log.info( "calendar_gather_events", { "calendar_ids": calendar_ids, } ); let result : Array< { calendar_id : _zeitbild.type.calendar_id; calendar_name : string; event : _zeitbild.type.event_object; } > = []; for await (const calendar_id of calendar_ids) { const calendar_object : _zeitbild.type.calendar_object = await _zeitbild.repository.calendar.read( calendar_id ); if (calendar_object.private) { lib_plankton.log.info( "calendar_gather_events_private_calendar_blocked", { "calendar_id": calendar_id, } ); } else { switch (calendar_object.kind) { case "concrete": { result = ( result .concat( calendar_object.data.events .filter( (event : _zeitbild.type.event_object) => _zeitbild.helpers.pit_is_between( _zeitbild.helpers.pit_from_datetime(event.begin), from_pit, to_pit ) ) .map( (event : _zeitbild.type.event_object) => ({ "calendar_id": calendar_id, "calendar_name": calendar_object.name, "event": event }) ) ) ); break; } case "caldav": { const url : lib_plankton.url.type_url = lib_plankton.url.decode( calendar_object.data.source_url ); const http_request : lib_plankton.http.type_request = { "version": "HTTP/2", "scheme": ((url.scheme === "https") ? "https" : "http"), "host": url.host, "path": (url.path ?? "/"), "query": url.query, "method": lib_plankton.http.enum_method.get, "headers": {}, "body": null, }; // TODO: cache? const http_response : lib_plankton.http.type_response = await lib_plankton.http.call( http_request, { } ); const vcalendar : lib_plankton.ical.type_vcalendar = lib_plankton.ical.ics_decode( http_response.body.toString(), { } ); result = ( result .concat( vcalendar.vevents .map( (vevent : lib_plankton.ical.type_vevent) => ( (vevent.dtstart !== undefined) ? { "name": ( (vevent.summary !== undefined) ? vevent.summary : "???" ), "begin": _zeitbild.helpers.ical_dt_to_own_datetime(vevent.dtstart), "end": ( (vevent.dtend !== undefined) ? _zeitbild.helpers.ical_dt_to_own_datetime(vevent.dtend) : null ), "location": ( (vevent.location !== undefined) ? vevent.location : null ), "description": ( (vevent.description !== undefined) ? vevent.description : null ), } : null ) ) .filter( (event) => (event !== null) ) .filter( (event) => _zeitbild.helpers.pit_is_between( _zeitbild.helpers.pit_from_datetime(event.begin), from_pit, to_pit ) ) .map( (event) => ({ "calendar_id": calendar_id, "calendar_name": calendar_object.name, "event": event, }) ) ) ); break; } } } } return Promise.resolve(result); } }