backend/source/api/actions/user_dav_conf.ts

153 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

namespace _zeitbild.api
{
/**
*/
export function register_user_dav_conf(
rest_subject : lib_plankton.rest_http.type_rest
) : void
{
register<
null,
(
null
|
{
address : string;
username : string;
password : (null | string);
setup_hints : Array<
{
label : string;
link : string;
remark : (null | string);
}
>;
}
)
>(
rest_subject,
lib_plankton.http.enum_method.get,
"/user_dav_conf",
{
"description": "gibt die CalDAV-Zugangsdaten eines Nutzers aus",
"output_schema": () => ({
"nullable": true,
"type": "object",
"properties": {
"address": {
"nullable": false,
"type": "string"
},
"username": {
"nullable": false,
"type": "string"
},
"password": {
"nullable": true,
"type": "string"
},
"setup_hints": {
"nullable": false,
"type": "array",
"items": {
"nullable": false,
"type": "object",
"properties": {
"label": {
"nullable": false,
"type": "string"
},
"link": {
"nullable": false,
"type": "string"
},
"remark": {
"nullable": true,
"type": "string",
"default": null,
},
},
"required": [
"label",
"link",
],
"additionalProperties": false
},
"default": []
},
},
"required": [
"address",
"username",
"password",
"setup_hints",
],
"additionalProperties": false
}),
"restriction": restriction_logged_in,
"execution": async (stuff) => {
let result : (
null
|
{
address : string;
username : string;
password : (null | string);
setup_hints : Array<
{
label : string;
link : string;
remark : (null | string);
}
>;
}
) = null;
const raw : (null | any) = _zeitbild.conf.get()["caldav"];
if (raw === null)
{
result = null;
}
else
{
const session : {key : string; value : lib_plankton.session.type_session;} = await session_from_stuff(stuff);
const user_id : _zeitbild.type_user_id = await _zeitbild.service.user.identify(session.value.name);
const user_object : _zeitbild.type_user_object = await _zeitbild.service.user.get(user_id);
const arguments_ : Record<string, string> = Object.fromEntries(
[
{"key": "username", "value": user_object.name},
{"key": "password", "value": user_object.dav_token},
]
.filter(
entry => (entry.value !== null)
)
.map(
entry => ([entry.key, entry.value as string])
)
);
result = {
"address": lib_plankton.string.coin(raw["address"], arguments_),
"username": lib_plankton.string.coin(raw["username"], arguments_),
"password": (
(user_object.dav_token === null)
?
null
:
lib_plankton.string.coin(raw["password"], arguments_)
),
"setup_hints": raw["setup_hints"],
};
}
return Promise.resolve(
{
"status_code": 200,
"data": result,
}
);
}
}
);
}
}