backend/source/api/actions/caldav_get.ts

237 lines
5.4 KiB
TypeScript
Raw Normal View History

namespace _zeitbild.api
{
/**
*/
2024-10-30 07:20:13 +01:00
export function register_caldav_get(
rest_subject : lib_plankton.rest_caldav.type_rest
) : void
{
register<
null,
2024-11-29 08:44:05 +01:00
/*
(
2024-11-28 23:08:24 +01:00
null
|
string
2024-11-28 23:08:24 +01:00
|
lib_plankton.ical.type_vcalendar
)
2024-11-29 08:44:05 +01:00
*/
(
null
|
lib_plankton.xml.type_node_data
)
>(
rest_subject,
2024-10-30 07:20:13 +01:00
lib_plankton.caldav.enum_method.report,
2024-11-28 23:08:24 +01:00
"/caldav/project/:id",
{
2024-10-30 07:20:13 +01:00
"description": "trägt Veranstaltungen aus verschiedenen Kalendern zusammen im ical-Format",
"query_parameters": () => ([
{
"name": "from",
"required": false,
"description": "UNIX timestamp",
},
{
"name": "to",
"required": false,
"description": "UNIX timestamp",
},
{
"name": "calendar_ids",
"required": false,
"description": "comma separated",
},
{
"name": "auth",
"required": true,
"description": "",
},
]),
"output_schema": () => ({
"nullable": false,
"type": "string",
}),
2024-11-29 08:44:05 +01:00
/*
"response_body_mimetype": "text/calendar",
2024-11-28 23:08:24 +01:00
"response_body_encode": (output) => (
(output === null)
?
2024-11-28 23:08:24 +01:00
null
:
2024-11-28 23:08:24 +01:00
Buffer.from(
(typeof(output) === "string")
?
output
:
lib_plankton.ical.ics_encode(output)
)
2024-11-29 08:44:05 +01:00
),
*/
"response_body_mimetype": "text/xml",
"response_body_encode": output => Promise.resolve<Buffer>(
Buffer.from(
lib_plankton.xml.get_node_logic(output).compile(0)
)
),
"restriction": restriction_none,
2024-11-28 23:08:24 +01:00
/**
* @todo use stuff.path_parameters["id"]
*/
"execution": async (stuff) => {
2024-11-28 23:08:24 +01:00
const user_id : (null | type_user_id) = await _zeitbild.api.web_auth(
stuff.headers["Authorization"]
??
stuff.headers["authorization"]
??
null
);
2024-11-28 23:08:24 +01:00
if (user_id === null) {
return Promise.resolve(
{
2024-11-28 23:08:24 +01:00
"status_code": 401,
"data": null,
"extra_headers": {
"WWW-Authenticate": "Basic realm=Restricted",
}
}
);
}
else {
2024-11-28 23:08:24 +01:00
const from : lib_plankton.pit.type_pit = (
("from" in stuff.query_parameters)
?
parseInt(stuff.query_parameters["from"])
:
lib_plankton.pit.shift_week(
lib_plankton.pit.now(),
-2
)
);
const to : lib_plankton.pit.type_pit = (
("to" in stuff.query_parameters)
?
parseInt(stuff.query_parameters["to"])
:
lib_plankton.pit.shift_week(
lib_plankton.pit.now(),
+6
)
);
const calendar_ids_wanted : (null | Array<_zeitbild.type_calendar_id>) = (
(
("calendar_ids" in stuff.query_parameters)
&&
(stuff.query_parameters["calendar_ids"] !== null)
)
?
lib_plankton.call.convey(
stuff.query_parameters["calendar_ids"],
[
(x : string) => x.split(","),
(x : Array<string>) => x.map(parseInt),
(x : Array<int>) => x.filter(y => (! isNaN(y)))
]
)
:
null
);
return (
_zeitbild.service.calendar.gather_events(
calendar_ids_wanted,
from,
to,
user_id
)
.then(
(data) => Promise.resolve(
{
"status_code": 200,
2024-11-29 08:44:05 +01:00
/*
"data": _zeitbild.helpers.ical_vcalendar_from_own_event_list(
data.map(entry => entry.event_object)
)
2024-11-29 08:44:05 +01:00
*/
"data": lib_plankton.webdav.data_multistatus_encode_xml(
{
"responses": (
/**
* @todo find proper solution for null values
*/
data
.filter(
entry => (entry.event_id !== null)
)
.map(
entry => ({
"href": lib_plankton.string.coin(
"/caldav/project/{{calendar_id}}/{{event_id}}",
{
"calendar_id": stuff.path_parameters["id"],
"event_id": (entry.event_id ?? 0).toFixed(0),
}
),
"body": {
"propstats": [
{
"prop": [
{
"name": "C:calendar-data",
"value": {
"kind": "primitive",
"data": lib_plankton.ical.ics_encode(
2024-12-06 15:52:22 +01:00
/*
2024-11-29 08:44:05 +01:00
{
"version": "2.0",
"prodid": "zeitbild",
"vevents": [],
"method": "publish",
}
2024-12-06 15:52:22 +01:00
*/
_zeitbild.helpers.ical_vcalendar_from_own_event_list(
data
.map(
entry => entry.event_object
)
)
)
2024-11-29 08:44:05 +01:00
}
},
],
"status": "HTTP/1.1 200 OK",
"description": null,
}
]
},
"description": null
})
)
),
"description": null
}
)
}
)
)
.catch(
(reason) => Promise.resolve(
{
"status_code": 403,
2024-11-29 08:44:05 +01:00
"data": /*String(reason)*/null,
}
)
)
);
}
}
}
);
}
}