This commit is contained in:
Fenris Wolf 2024-09-25 15:28:25 +02:00
parent ec775a0178
commit a17b7dd785
8 changed files with 177 additions and 110 deletions

View file

@ -12,15 +12,8 @@
"in_memory": false "in_memory": false
}, },
"authentication": { "authentication": {
"kind": "oidc", "kind": "internal",
"data": { "data": {
"url_authorization": "https://authelia.linke.sx/api/oidc/authorization",
"url_token": "https://authelia.linke.sx/api/oidc/token",
"url_userinfo": "https://authelia.linke.sx/api/oidc/userinfo",
"client_id": "zeitbild",
"client_secret": "cee00b08a818db87e17e703273818e5194f83280e1ef3eae9214ff14675d9e6d",
"backend_url_base": "https://zeitbild.linke.sx",
"label": "linke.sx"
} }
} }
} }

View file

@ -4,7 +4,7 @@ namespace _zeitbild.api
/** /**
*/ */
export function register_calendar_list( export function register_calendar_add(
rest_subject : lib_plankton.rest.type_rest rest_subject : lib_plankton.rest.type_rest
) : void ) : void
{ {
@ -52,65 +52,77 @@ namespace _zeitbild.api
const session : {key : string; value : lib_plankton.session.type_session;} = await session_from_stuff(stuff); 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_id : _zeitbild.type_user_id = await _zeitbild.service.user.identify(session.value.name);
// TODO move logic to calendar service if (stuff.input === null) {
const resource_object : _zeitbild.type_resource_object = ( return Promise.reject(new Error("impossible"));
{ }
"local": { else {
"kind": "local", // TODO move logic to calendar service
"data": { let resource_object : _zeitbild.type_resource_object;
"events": [], switch (stuff.input.resource.kind) {
} case "local": {
resource_object = {
"kind": "local",
"data": {
"event_ids": [],
}
};
break;
}
case "caldav": {
resource_object = {
"kind": "caldav",
"data": {
"url": stuff.input.resource.data.url,
"read_only": stuff.input.resource.data.read_only,
}
};
break;
}
}
const resource_id : _zeitbild.type_resource_id = await _zeitbild.service.resource.add(
resource_object
);
const calendar_object : _zeitbild.type_calendar_object = {
"name": stuff.input.name,
"access": {
"default_level": _zeitbild.value_object.access_level.from_string(stuff.input.access.default_level),
"attributed": lib_plankton.map.hashmap.implementation_map(
lib_plankton.map.hashmap.make(
x => x.toFixed(0),
{
"pairs": (
stuff.input.access.attributed
.map(
(entry) => ({
"key": entry.user_id,
"value": _zeitbild.value_object.access_level.from_string(entry.level),
})
)
.concat(
[
{
"key": user_id,
"value": _zeitbild.enum_access_level.admin,
}
]
)
),
}
)
),
}, },
"caldav": { "resource_id": resource_id
"kind": "caldav", };
"data": { return (
"url": stuff.input.resource.data.url, _zeitbild.service.calendar.add(calendar_object)
"read_only": stuff.input.resource.data.read_only, .then(
} (calendar_id : _zeitbild.type_calendar_id) => Promise.resolve({
}, "status_code": 200,
}[stuff.input.resource.kind] "data": calendar_id,
); })
const resource_id : _zeitbild.type_resource_id = _zeitbild.service.resource.add(
resource_object
);
const calendar_object : _zeitbild.type_calendar_object = {
"name": stuff.input.name,
"access": {
"default_level": _zeitbild.value_object.access_level.from_string(stuff.input.access.default_level),
"attributed": lib_plankton.map.hashmap.make(
x => x.toFixed(0),
{
"pairs": (
stuff.input.access.attributed
.map(
(entry) => ({
"key": entry.user_id,
"value": _zeitbild.value_object.access_level.from_string(entry.level),
})
)
.concat(
[
{
"key": user_id,
"value": _zeitbild.enum_access_level.admin,
}
]
)
),
}
) )
}, );
"resource_id": resource_id }
};
return (
_zeitbild.service.calendar.add(calendar_object)
.then(
(calendar_id : _zeitbild.type_calendar_id) => Promise.resolve({
"status_code": 200,
"data": calendar_id,
})
)
);
} }
} }
); );

View file

@ -32,10 +32,6 @@ namespace _zeitbild.api
"nullable": false, "nullable": false,
"type": "object", "type": "object",
"properties": { "properties": {
"name": {
"nullable": false,
"type": "string"
},
"name": { "name": {
"nullable": false, "nullable": false,
"type": "string" "type": "string"
@ -81,18 +77,23 @@ namespace _zeitbild.api
}), }),
"restriction": restriction_logged_in, "restriction": restriction_logged_in,
"execution": async (stuff) => { "execution": async (stuff) => {
return ( if (stuff.input === null) {
_zeitbild.service.calendar.event_add( return Promise.reject(new Error("impossible"));
stuff.input.calendar_id, }
stuff.input.event else {
) return (
.then( _zeitbild.service.calendar.event_add(
() => Promise.resolve({ stuff.input.calendar_id,
"status_code": 200, stuff.input.event
"data": null, )
}) .then(
) () => Promise.resolve({
); "status_code": 200,
"data": null,
})
)
);
}
} }
} }
); );

View file

@ -23,7 +23,24 @@ type type_data = {
} }
>; >;
}; };
resource : _zeitbild.type_resource_object; resource : (
{
kind : "local";
data : {
events : Array<
_zeitbild.type_event_object
>
};
}
|
{
kind : "caldav";
data : {
url : string;
read_only : boolean;
};
}
);
} }
>; >;
}; };
@ -63,7 +80,35 @@ async function data_init(
track.user[user_raw.id] = user_id; track.user[user_raw.id] = user_id;
} }
for await (const calendar_raw of data.calendars) { for await (const calendar_raw of data.calendars) {
const resource_object : _zeitbild.type_resource_object = calendar_raw.resource; let resource_object : _zeitbild.type_resource_object;
switch (calendar_raw.resource.kind) {
case "local": {
const event_ids : Array<_zeitbild.type_event_id> = await Promise.all<_zeitbild.type_event_id>(
calendar_raw.resource.data.events
.map(
// TODO do not use repository, but service
(event_raw : _zeitbild.type_event_object) => _zeitbild.repository.local_resource_event.create(event_raw)
)
);
resource_object = {
"kind": "local",
"data": {
"event_ids": event_ids,
}
};
break;
}
case "caldav": {
resource_object = {
"kind": "caldav",
"data": {
"url": calendar_raw.resource.data.url,
"read_only": calendar_raw.resource.data.read_only,
}
};
break;
}
}
const resource_id : _zeitbild.type_resource_id = await _zeitbild.service.resource.add( const resource_id : _zeitbild.type_resource_id = await _zeitbild.service.resource.add(
resource_object resource_object
); );

View file

@ -47,8 +47,7 @@ namespace _zeitbild.repository.local_resource_event
/** /**
*/ */
function encode( function encode(
event : _zeitbild.type_event_object, event : _zeitbild.type_event_object
local_resource_id : int
) : Record<string, any> ) : Record<string, any>
{ {
const encode_datetime : ((datetime : _zeitbild.helpers.type_datetime) => string) = ((datetime) => { const encode_datetime : ((datetime : _zeitbild.helpers.type_datetime) => string) = ((datetime) => {
@ -82,7 +81,6 @@ namespace _zeitbild.repository.local_resource_event
); );
}); });
return { return {
"local_resource_id": local_resource_id,
"name": event.name, "name": event.name,
"begin": encode_datetime(event.begin), "begin": encode_datetime(event.begin),
"end": ( "end": (
@ -152,8 +150,8 @@ namespace _zeitbild.repository.local_resource_event
/** /**
*/ */
function read( export function read(
event_id : _zeitbild.type_local_resource_event_id event_id : _zeitbild.type_event_id
) : Promise<_zeitbild.type_event_object> ) : Promise<_zeitbild.type_event_object>
{ {
return ( return (
@ -167,9 +165,9 @@ namespace _zeitbild.repository.local_resource_event
/** /**
*/ */
function create( export function create(
event_object : _zeitbild.type_event_object event_object : _zeitbild.type_event_object
) : Promise<_zeitbild.type_local_resource_event_id> ) : Promise<_zeitbild.type_event_id>
{ {
return ( return (
Promise.resolve(encode(event_object)) Promise.resolve(encode(event_object))

View file

@ -277,8 +277,10 @@ namespace _zeitbild.repository.resource
} }
); );
for await (const event_id of resource_object.data.event_ids) { for await (const event_id of resource_object.data.event_ids) {
await get_local_resource_event_chest().create( await get_local_resource_event_chest().write(
[local_resource_id, event_id] [local_resource_id, event_id],
{
}
) )
} }
const resource_id : _zeitbild.type_resource_id = await get_resource_core_store().create( const resource_id : _zeitbild.type_resource_id = await get_resource_core_store().create(
@ -307,6 +309,7 @@ namespace _zeitbild.repository.resource
break; break;
} }
default: { default: {
// @ts-ignore
throw (new Error("invalid resource kind: " + resource_object.kind)); throw (new Error("invalid resource kind: " + resource_object.kind));
break; break;
} }
@ -384,6 +387,7 @@ namespace _zeitbild.repository.resource
break; break;
} }
default: { default: {
// @ts-ignore
throw (new Error("invalid resource kind: " + resource_object.kind)); throw (new Error("invalid resource kind: " + resource_object.kind));
break; break;
} }
@ -394,7 +398,7 @@ namespace _zeitbild.repository.resource
/** /**
*/ */
export function local_resource_event_add( export async function local_resource_event_add(
resource_id : _zeitbild.type_resource_id, resource_id : _zeitbild.type_resource_id,
event_id : _zeitbild.type_event_id event_id : _zeitbild.type_event_id
) : Promise<void> ) : Promise<void>
@ -419,7 +423,7 @@ namespace _zeitbild.repository.resource
/** /**
*/ */
export function local_resource_event_delete( export async function local_resource_event_delete(
resource_id : _zeitbild.type_resource_id, resource_id : _zeitbild.type_resource_id,
event_id : _zeitbild.type_event_id event_id : _zeitbild.type_event_id
) : Promise<void> ) : Promise<void>
@ -431,8 +435,7 @@ namespace _zeitbild.repository.resource
else { else {
return ( return (
get_local_resource_event_chest().delete( get_local_resource_event_chest().delete(
[dataset_core["sub_id"], event_id], [dataset_core["sub_id"], event_id]
{}
) )
.then( .then(
() => Promise.resolve(undefined) () => Promise.resolve(undefined)

View file

@ -76,13 +76,16 @@ namespace _zeitbild.service.calendar
event_object : _zeitbild.type_event_object event_object : _zeitbild.type_event_object
) : Promise<void> ) : Promise<void>
{ {
const calendar_object : _zeitbild.type_calendar_object = _zeitbild.repository.calendar.read( const calendar_object : _zeitbild.type_calendar_object = await _zeitbild.repository.calendar.read(
calendar_id calendar_id
); );
return _zeitbild.repository.resource.event_add( const event_id : _zeitbild.type_event_id = await _zeitbild.repository.local_resource_event.create(
calendar_object.resource_id,
event_object event_object
); );
return _zeitbild.repository.resource.local_resource_event_add(
calendar_object.resource_id,
event_id
);
} }
@ -102,16 +105,26 @@ namespace _zeitbild.service.calendar
const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(calendar_object.resource_id); const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(calendar_object.resource_id);
switch (resource_object.kind) { switch (resource_object.kind) {
case "local": { case "local": {
return Promise.resolve( return (
resource_object.data.events Promise.all(
.filter( resource_object.data.event_ids
(event : _zeitbild.type_event_object) => _zeitbild.helpers.pit_is_between( .map(
_zeitbild.helpers.pit_from_datetime(event.begin), (event_id) => _zeitbild.repository.local_resource_event.read(event_id)
from_pit,
to_pit
) )
) )
) .then(
(events) => Promise.resolve(
events
.filter(
(event : _zeitbild.type_event_object) => _zeitbild.helpers.pit_is_between(
_zeitbild.helpers.pit_from_datetime(event.begin),
from_pit,
to_pit
)
)
)
)
);
break; break;
} }
case "caldav": { case "caldav": {

View file

@ -19,7 +19,7 @@ namespace _zeitbild.service.resource
event_object : _zeitbild.type_event_object event_object : _zeitbild.type_event_object
) : Promise<void> ) : Promise<void>
{ {
const resource_object : _zeitbild.type_resource_object = _zeitbild.repository.resource.read( const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
resource_id resource_id
); );
switch (resource_object.kind) { switch (resource_object.kind) {
@ -45,6 +45,7 @@ namespace _zeitbild.service.resource
break; break;
} }
default: { default: {
// @ts-ignore
throw (new Error("unhandled resource kind: " + resource_object.kind)); throw (new Error("unhandled resource kind: " + resource_object.kind));
} }
} }
@ -58,7 +59,7 @@ namespace _zeitbild.service.resource
event_object : _zeitbild.type_event_object event_object : _zeitbild.type_event_object
) : Promise<void> ) : Promise<void>
{ {
const resource_object : _zeitbild.type_resource_object = _zeitbild.repository.resource.read( const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
resource_id resource_id
); );
switch (resource_object.kind) { switch (resource_object.kind) {
@ -84,6 +85,7 @@ namespace _zeitbild.service.resource
break; break;
} }
default: { default: {
// @ts-ignore
throw (new Error("unhandled resource kind: " + resource_object.kind)); throw (new Error("unhandled resource kind: " + resource_object.kind));
} }
} }