backend/source/api/actions/caldav_probe.ts
Fenris Wolf 54e27f2ad9 [int]
2024-11-14 19:57:13 +01:00

126 lines
2.8 KiB
TypeScript

namespace _zeitbild.api
{
/**
*/
export function register_caldav_probe(
rest_subject : lib_plankton.rest_caldav.type_rest
) : void
{
register<
null,
string
>(
rest_subject,
lib_plankton.caldav.enum_method.propfind,
"/caldav",
{
"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",
}),
"response_body_mimetype": "text/xml; charset=utf-8",
"response_body_encode": output => Buffer.from(output),
// "restriction": restriction_basic_auth,
"restriction": restriction_none,
/**
* @todo examine body
*/
"execution": async (stuff) => {
const user_id : (null | type_user_id) = await _zeitbild.api.web_auth(
stuff.headers["Authorization"]
??
stuff.headers["authorization"]
??
null
);
if (user_id === null) {
return Promise.resolve(
{
"status_code": 401,
"data": "",
"extra_headers": {
"WWW-Authenticate": "Basic realm=Restricted",
}
}
);
}
else {
return (
_zeitbild.service.calendar.overview(user_id)
.then(
(data_raw) => Promise.resolve(
data_raw
.map(
(entry) => ({
"id": entry.id,
"name": entry.name,
"access_level": _zeitbild.value_object.access_level.to_string(entry.access_level),
})
)
)
)
.then(
(data) => Promise.resolve({
"status_code": 207,
"data": (
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+
lib_plankton.webdav.data_multistatus_encode(
{
"responses": [
{
"href": "/caldav/events",
"body": {
"propstats": data.map(
(entry) => ({
"prop": [
{"name": "displayname", "value": entry.name},
// {"name": "cs:getctag", "value": "47"}, // TODO correct value
// {"name": "current-user-privilege-set", "value": ""},
],
"status": "HTTP/2.0 200 OK",
"description": entry.access_level,
})
),
},
"description": null,
}
],
"description": null,
}
)
),
})
)
);
}
}
}
);
}
}