From 07605933dc6d048575e59a5993830d9629d9a029 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 15 Oct 2025 19:09:41 -0400 Subject: [PATCH] core: core.globalSettingsDescriptions JS => C. --- core/core.js | 9 ------- src/api.js.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.js.c | 25 ++++++++++++++++++ src/util.js.h | 10 +++++++ 4 files changed, 107 insertions(+), 9 deletions(-) diff --git a/core/core.js b/core/core.js index c7dc2f9c..f3f26b93 100644 --- a/core/core.js +++ b/core/core.js @@ -352,15 +352,6 @@ async function getProcessBlob(blobId, key, options) { } }; if (process.credentials?.permissions?.administration) { - imports.core.globalSettingsDescriptions = async function () { - let settings = Object.assign({}, defaultGlobalSettings()); - for (let [key, value] of Object.entries(await loadSettings())) { - if (settings[key]) { - settings[key].value = value; - } - } - return settings; - }; imports.core.globalSettingsSet = async function (key, value) { await imports.core.permissionTest('set_global_setting'); print('Setting', key, value); diff --git a/src/api.js.c b/src/api.js.c index 94cf90db..b5139c1f 100644 --- a/src/api.js.c +++ b/src/api.js.c @@ -555,6 +555,76 @@ static JSValue _tf_ssb_getOwnerIdentities(JSContext* context, JSValueConst this_ return result; } +typedef struct _settings_descriptions_get_t +{ + const char* settings; + JSContext* context; + JSValue promise[2]; +} settings_descriptions_get_t; + +static void _tf_ssb_get_settings_descriptions_work(tf_ssb_t* ssb, void* user_data) +{ + settings_descriptions_get_t* work = user_data; + work->settings = tf_ssb_db_get_property(ssb, "core", "settings"); +} + +static void _tf_ssb_get_settings_descriptions_after_work(tf_ssb_t* ssb, int status, void* user_data) +{ + settings_descriptions_get_t* work = user_data; + JSContext* context = work->context; + JSValue result = JS_NewObject(context); + JSValue settings = JS_ParseJSON(context, work->settings ? work->settings : "", work->settings ? strlen(work->settings) : 0, NULL); + const char* name; + const char* type; + tf_setting_kind_t kind; + const char* description; + for (int i = 0; tf_util_get_global_setting_by_index(i, &name, &type, &kind, &description); i++) + { + JSValue entry = JS_NewObject(context); + JS_SetPropertyStr(context, entry, "type", JS_NewString(context, type)); + JS_SetPropertyStr(context, entry, "description", JS_NewString(context, description)); + switch (kind) + { + case k_kind_unknown: + break; + case k_kind_bool: + JS_SetPropertyStr(context, entry, "default_value", JS_NewBool(context, tf_util_get_default_global_setting_bool(name))); + break; + case k_kind_int: + JS_SetPropertyStr(context, entry, "default_value", JS_NewInt32(context, tf_util_get_default_global_setting_int(name))); + break; + case k_kind_string: + JS_SetPropertyStr(context, entry, "default_value", JS_NewString(context, tf_util_get_default_global_setting_string(name))); + break; + } + if (JS_IsObject(settings)) + { + JS_SetPropertyStr(context, entry, "value", JS_GetPropertyStr(context, settings, name)); + } + JS_SetPropertyStr(context, result, name, entry); + } + JS_FreeValue(context, settings); + JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result); + tf_util_report_error(context, error); + JS_FreeValue(context, error); + JS_FreeValue(context, result); + JS_FreeValue(context, work->promise[0]); + JS_FreeValue(context, work->promise[1]); + tf_free((void*)work->settings); + tf_free(work); +} + +static JSValue _tf_ssb_globalSettingsDescriptions(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) +{ + tf_task_t* task = tf_task_get(context); + tf_ssb_t* ssb = tf_task_get_ssb(task); + settings_descriptions_get_t* work = tf_malloc(sizeof(settings_descriptions_get_t)); + *work = (settings_descriptions_get_t) { .context = context }; + JSValue result = JS_NewPromiseCapability(context, work->promise); + tf_ssb_run_work(ssb, _tf_ssb_get_settings_descriptions_work, _tf_ssb_get_settings_descriptions_after_work, work); + return result; +} + typedef struct _settings_get_t { const char* key; @@ -627,6 +697,7 @@ static void _tf_ssb_settings_get_after_work(tf_ssb_t* ssb, int status, void* use JS_FreeValue(context, result); JS_FreeValue(context, work->promise[0]); JS_FreeValue(context, work->promise[1]); + JS_FreeCString(context, work->key); tf_free(work); } @@ -673,6 +744,7 @@ static JSValue _tf_api_register_imports(JSContext* context, JSValueConst this_va JSValue administration = JS_IsObject(permissions) ? JS_GetPropertyStr(context, permissions, "administration") : JS_UNDEFINED; if (JS_ToBool(context, administration) > 0) { + JS_SetPropertyStr(context, core, "globalSettingsDescriptions", JS_NewCFunction(context, _tf_ssb_globalSettingsDescriptions, "globalSettingsDescriptions", 0)); JS_SetPropertyStr(context, core, "globalSettingsGet", JS_NewCFunction(context, _tf_ssb_globalSettingsGet, "globalSettingsGet", 1)); } JS_FreeValue(context, administration); diff --git a/src/util.js.c b/src/util.js.c index 67623b90..a81572f8 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -381,6 +381,31 @@ const char* tf_util_get_default_global_setting_string(const char* name) return setting && setting->default_value.string_value ? setting->default_value.string_value : ""; } +bool tf_util_get_global_setting_by_index(int index, const char** out_name, const char** out_type, tf_setting_kind_t* out_kind, const char** out_description) +{ + if (index >= 0 && index < tf_countof(k_settings)) + { + if (out_name) + { + *out_name = k_settings[index].name; + } + if (out_type) + { + *out_type = k_settings[index].type; + } + if (out_kind) + { + *out_kind = k_settings[index].default_value.kind; + } + if (out_description) + { + *out_description = k_settings[index].description; + } + return true; + } + return false; +} + static JSValue _util_defaultGlobalSettings(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue settings = JS_NewObject(context); diff --git a/src/util.js.h b/src/util.js.h index b31f6e9f..5b8fbe32 100644 --- a/src/util.js.h +++ b/src/util.js.h @@ -212,6 +212,16 @@ const char* tf_util_get_default_global_setting_string(const char* name); */ tf_setting_kind_t tf_util_get_global_setting_kind(const char* name); +/** +** Get the index-th global setting. +** @param index The index. +** @param out_name Populated with the setting name. +** @param out_type Populated with the setting type. +** @param out_kind Populated with the setting kind. +** @param out_description Populated with the setting description. +*/ +bool tf_util_get_global_setting_by_index(int index, const char** out_name, const char** out_type, tf_setting_kind_t* out_kind, const char** out_description); + /** ** Log documentation for the available settings. ** @param line_prefix Text to prefix each line with."