backend/source/helpers.ts

217 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-09-11 17:24:20 +02:00
/**
*/
2024-09-12 00:03:29 +02:00
namespace _zeitbild.helpers
2024-09-11 17:24:20 +02:00
{
/**
* @todo timezone
*/
function ical_datetime_to_own_datetime(
ical_datetime : lib_plankton.ical.type_datetime
2024-09-26 16:47:38 +02:00
) : lib_plankton.pit.type_datetime
2024-09-11 17:24:20 +02:00
{
return {
"timezone_shift": 0,
"date": {
"year": ical_datetime.date.year,
"month": ical_datetime.date.month,
"day": ical_datetime.date.day,
},
"time": (
(ical_datetime.time === null)
?
null
:
{
"hour": ical_datetime.time.hour,
"minute": ical_datetime.time.minute,
"second": ical_datetime.time.second,
}
)
};
}
2024-09-11 17:24:20 +02:00
/**
* @todo timezone
*/
export function ical_dt_to_own_datetime(
ical_dt: lib_plankton.ical.type_dt
2024-09-26 16:47:38 +02:00
) : lib_plankton.pit.type_datetime
2024-09-11 17:24:20 +02:00
{
return {
"timezone_shift": 0,
"date": ical_dt.value.date,
"time": (
(ical_dt.value.time === null)
?
null
:
{
"hour": ical_dt.value.time.hour,
"minute": ical_dt.value.time.minute,
"second": ical_dt.value.time.second,
}
)
};
}
2024-09-12 16:35:57 +02:00
/**
* @todo timezone
*/
export function ical_dt_from_own_datetime(
datetime : lib_plankton.pit.type_datetime
) : lib_plankton.ical.type_dt
{
return {
"tzid": "Europe/Berlin",
"value": {
"date": {
"year": datetime.date.year,
"month": datetime.date.month,
"day": datetime.date.day,
},
"time": (
(datetime.time === null)
?
null
:
{
"utc": true,
"hour": datetime.time.hour,
"minute": datetime.time.minute,
"second": datetime.time.second,
}
)
},
};
}
/**
*/
export function ical_vevent_from_own_event(
event_object : _zeitbild.type_event_object,
uid : string
) : lib_plankton.ical.type_vevent
{
return {
"uid": uid,
"dtstamp": ical_dt_from_own_datetime(event_object.begin).value,
"dtstart": ical_dt_from_own_datetime(event_object.begin),
"dtend": (
(event_object.end === null)
?
undefined
:
ical_dt_from_own_datetime(event_object.end)
),
"location": (event_object.location ?? undefined),
"summary": event_object.name,
"url": (event_object.link ?? undefined),
"description": (event_object.description ?? undefined),
};
}
/**
* @todo assign better uids
*/
export function ical_vcalendar_from_own_event_list(
events : Array<_zeitbild.type_event_object>
) : lib_plankton.ical.type_vcalendar
{
const pit_now : lib_plankton.pit.type_pit = lib_plankton.pit.now();
const datetime_now : lib_plankton.pit.type_datetime = lib_plankton.pit.to_datetime(pit_now);
const stamp : string = lib_plankton.string.coin(
"{{year}}{{month}}{{day}}",
{
"year": datetime_now.date.year.toFixed(0).padStart(4, "0"),
"month": datetime_now.date.month.toFixed(0).padStart(2, "0"),
"day": datetime_now.date.day.toFixed(0).padStart(2, "0"),
}
);
return {
"version": "2.0",
"prodid": "",
"vevents": (
events
.map<lib_plankton.ical.type_vevent>(
(entry, index) => ical_vevent_from_own_event(
entry,
lib_plankton.string.coin(
"zeitbild_{{stamp}}_{{index}}",
{
"stamp": stamp,
"index": index.toFixed(0)
}
)
),
)
),
"method": "PUBLISH",
"vtimezone": {
"tzid": "Europe/Berlin",
},
};
}
2024-09-12 16:35:57 +02:00
/**
*/
export async function template_coin(
name : string,
data : Record<string, string>
) : Promise<string>
{
2024-12-09 21:40:07 +01:00
const content : string = await lib_plankton.cache.get<string>(
_zeitbild.cache_templates,
name,
null,
() => (
lib_plankton.file.read(
lib_plankton.string.coin(
"templates/{{name}}.html.tpl",
{
"name": name,
}
2024-09-12 16:35:57 +02:00
)
)
2024-12-09 21:40:07 +01:00
.then(
x => Promise.resolve<string>(x.toString())
)
)
);
2024-09-12 16:35:57 +02:00
return Promise.resolve<string>(
lib_plankton.string.coin(
content,
data
)
);
}
/**
* @todo outsource
*/
export async function promise_row<type_result>(
members : Array<
() => Promise<type_result>
>
) : Promise<
Array<
type_result
>
>
{
let results : Array<type_result> = [];
for await (const member of members) {
results.push(await member());
}
return Promise.resolve<Array<type_result>>(results);
}
2024-09-11 17:24:20 +02:00
}