/** */ namespace _dali.backend { /** */ type type_conf = { scheme : string; host : string; port : int; path : string; }; /** */ type type_request = { method : lib_plankton.http.enum_method; action : string; input : (null | any); }; /** */ type type_event_object = { name : string; begin : { timezone_shift: int; date: { year: int; month: int; day: int; }; time: ( null | { hour: int; minute: int; second: int; } ); }; end : ( null | { timezone_shift: int; date: { year: int; month: int; day: int; }; time: ( null | { hour: int; minute: int; second: int; } ); } ); location : ( null | string ); link : ( null | string ); description : ( null | string ); }; /** */ var _conf : type_conf; /** */ var _data_chest : ( null | lib_plankton.storage.type_chest ) = null; /** */ var _cache : ( null | lib_plankton.cache.type_subject ); /** */ var _queue : { items : Array< { request : type_request; resolve : ((result : any) => void); reject : ((reason : any) => void); } >; busy : boolean; }; /** */ export async function initialize( conf : type_conf ) : Promise { _conf = conf; _data_chest = lib_plankton.storage.localstorage.implementation_chest( { "corner": "zeitbild", } ); _cache = lib_plankton.cache.make( /* lib_plankton.storage.memory.implementation_chest( { } ) */ ); _queue = { "items": [], "busy": false, }; return Promise.resolve(undefined); } /** */ async function get_session_key( ) : Promise<(null | string)> { try { return (await _data_chest.read("session_key")); } catch (error) { return null; } } /** */ async function call_real( request : type_request ) : Promise { const with_body : boolean = ( [ lib_plankton.http.enum_method.post, lib_plankton.http.enum_method.put, lib_plankton.http.enum_method.patch, ].includes(request.method) ); const session_key : (null | string) = await get_session_key(); const http_request : lib_plankton.http.type_request = { "version": "HTTP/2", "scheme": ( (_conf.scheme === "http") ? "http" : "https" ), "host": lib_plankton.string.coin( "{{host}}:{{port}}", { "host": _conf.host, "port": _conf.port.toFixed(0), } ), "path": lib_plankton.string.coin( "{{base}}{{action}}", { "base": _conf.path, "action": request.action, } ), "method": request.method, "query": ( (with_body || (request.input === null)) ? null : ("?" + lib_plankton.www_form.encode(request.input)) ), "headers": Object.assign( {}, ( (! with_body) ? {} : {"Content-Type": "application/json"} ), ( (session_key === null) ? {} : {"X-Session-Key": session_key} ) ), "body": ( ((! with_body) || (request.input === null)) ? null : /*Buffer.from*/(lib_plankton.json.encode(request.input)) ), }; const http_response : lib_plankton.http.type_response = await lib_plankton.http.call(http_request); if ( ! ( (http_response.status_code >= 200) && (http_response.status_code < 300) ) ) { return Promise.reject(http_response.body.toString()); } else { const output : any = lib_plankton.json.decode(http_response.body.toString()); return Promise.resolve(output); } } /** */ async function process( ) : Promise { if (_queue.busy) { // do nothing } else { _queue.busy = true; while (_queue.items.length > 0) { const entry = _queue.items.shift(); let successful : boolean; let reason : any; let result : any; try { result = await call_real(entry.request); successful = true; } catch (error) { reason = error; successful = false; } if (successful) { entry.resolve(result); } else { entry.reject(reason); } } _queue.busy = false; // process(); } } /** */ async function call( method : lib_plankton.http.enum_method, action : string, input : (null | any) ) : Promise { const request : type_request = { "method": method, "action": action, "input": input, }; const promise : Promise = new Promise( (resolve, reject) => { _queue.items.push( { "request": request, "resolve": resolve, "reject": reject, } ); } ); process(); return promise; } /** */ export function status( ) : Promise< { logged_in : boolean; name : (null | string); } > { return call( lib_plankton.http.enum_method.get, "/session/status", null ) } /** */ export async function session_prepare( input : any ) : Promise<{kind : string; data : any;}> { return call( lib_plankton.http.enum_method.post, "/session/prepare", input ); } /** */ export function set_session_key( session_key : string ) : Promise { return ( _data_chest.write("session_key", session_key) .then(() => Promise.resolve(undefined)) ); } /** */ export async function session_begin( name : string, password : string ) : Promise { const session_key : string = await call( lib_plankton.http.enum_method.post, "/session/begin", { "name": name, "password": password, } ); await _data_chest.write("session_key", session_key); return Promise.resolve(undefined); } /** */ export async function session_end( ) : Promise { await call( lib_plankton.http.enum_method.delete, "/session/end", null ); await _data_chest.delete("session_key"); return Promise.resolve(undefined); } /** */ export function user_list( ) : Promise< Array< { id : int; name : string; } > > { return call( lib_plankton.http.enum_method.get, "/users", null ); } /** */ export function user_dav_conf( ) : Promise< ( null | { address : string; username : string; password : string; setup_hints : Array< { label : string; link : string; remark : (null | string); } >; } ) > { return call( lib_plankton.http.enum_method.get, "/user_dav_conf", null ); } /** */ export function user_dav_token( ) : Promise { return call( lib_plankton.http.enum_method.patch, "/user_dav_token", null ); } /** */ export async function calendar_list( ) : Promise< Array< { id : int; name : string; hue : float; access_level : string; } > > { return call( lib_plankton.http.enum_method.get, "/calendar", null ); } /** */ export async function calendar_get( calendar_id : int ) : Promise< { name : string; hue : float; access : { public : boolean; default_level : string; attributed : Array< { user_id : int; level : string; } > }; resource_id : int; } > { return call( lib_plankton.http.enum_method.get, lib_plankton.string.coin( "/calendar/{{calendar_id}}", { "calendar_id": calendar_id.toFixed(0), } ), null ); } /** */ export async function calendar_add( data : { name : string; access : { public : boolean; default_level : string; attributed : Array< { user_id : int; level : string; } >; }; resource : ( { kind : "local"; data : { }; } | { kind : "ics_feed"; data : { url : string; from_fucked_up_wordpress : boolean; }; } ); hue : float; } ) : Promise< int > { return call( lib_plankton.http.enum_method.post, lib_plankton.string.coin( "/calendar", { } ), data ); } /** */ export async function calendar_change( id : int, data : { name : string; hue : float; access : { public : boolean; default_level : string; attributed : Array< { user_id : int; level : string; } >; }; } ) : Promise< void > { return call( lib_plankton.http.enum_method.put, lib_plankton.string.coin( "/calendar/{{id}}", { "id": id.toFixed(0), } ), data ); } /** */ export async function calendar_remove( id : int ) : Promise< void > { return call( lib_plankton.http.enum_method.delete, lib_plankton.string.coin( "/calendar/{{id}}", { "id": id.toFixed(0), } ), null ); } /** */ export async function calendar_event_get( calendar_id : int, event_id : int ) : Promise< type_event_object > { return call( lib_plankton.http.enum_method.get, lib_plankton.string.coin( "/calendar/{{calendar_id}}/event/{{event_id}}", { "calendar_id": calendar_id.toFixed(0), "event_id": event_id.toFixed(0), } ), null ); } /** */ export async function calendar_event_add( calendar_id : int, event_data : type_event_object ) : Promise< { local_resource_event_id : (null | int); hash : string; } > { return call( lib_plankton.http.enum_method.post, lib_plankton.string.coin( "/calendar/{{calendar_id}}/event", { "calendar_id": calendar_id.toFixed(0), } ), event_data ); } /** * @todo Möglichkeit den Kalender zu ändern */ export async function calendar_event_change( calendar_id : int, event_id : int, event_object : type_event_object ) : Promise { return call( lib_plankton.http.enum_method.put, lib_plankton.string.coin( "/calendar/{{calendar_id}}/event/{{event_id}}", { "calendar_id": calendar_id.toFixed(0), "event_id": event_id.toFixed(0), } ), event_object ); } /** */ export async function calendar_event_remove( calendar_id : int, event_id : int ) : Promise { return call( lib_plankton.http.enum_method.delete, lib_plankton.string.coin( "/calendar/{{calendar_id}}/event/{{event_id}}", { "calendar_id": calendar_id.toFixed(0), "event_id": event_id.toFixed(0), } ), null ); } /** * @todo prevent loops */ export async function events( from_timestamp : int, to_timestamp : int, { "calendar_ids": calendar_ids = null, } : { calendar_ids ?: (null | Array); } = { } ) : Promise< Array< { hash : string; calendar_id : int; calendar_name : string; hue : float; access_level : string; event_id : (null | int); event_object : type_event_object; } > > { return call( lib_plankton.http.enum_method.get, "/events", Object.assign( { "from": from_timestamp, "to": to_timestamp, }, ( (calendar_ids === null) ? {} : {"calendar_ids": calendar_ids.join(",")} ) ) ); } }