core: core.globalSettingsDescriptions JS => C.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 31m48s

This commit is contained in:
2025-10-15 19:09:41 -04:00
parent 4bdc7ec616
commit 07605933dc
4 changed files with 107 additions and 9 deletions

View File

@@ -352,15 +352,6 @@ async function getProcessBlob(blobId, key, options) {
} }
}; };
if (process.credentials?.permissions?.administration) { 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) { imports.core.globalSettingsSet = async function (key, value) {
await imports.core.permissionTest('set_global_setting'); await imports.core.permissionTest('set_global_setting');
print('Setting', key, value); print('Setting', key, value);

View File

@@ -555,6 +555,76 @@ static JSValue _tf_ssb_getOwnerIdentities(JSContext* context, JSValueConst this_
return result; 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 typedef struct _settings_get_t
{ {
const char* key; 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, result);
JS_FreeValue(context, work->promise[0]); JS_FreeValue(context, work->promise[0]);
JS_FreeValue(context, work->promise[1]); JS_FreeValue(context, work->promise[1]);
JS_FreeCString(context, work->key);
tf_free(work); 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; JSValue administration = JS_IsObject(permissions) ? JS_GetPropertyStr(context, permissions, "administration") : JS_UNDEFINED;
if (JS_ToBool(context, administration) > 0) 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_SetPropertyStr(context, core, "globalSettingsGet", JS_NewCFunction(context, _tf_ssb_globalSettingsGet, "globalSettingsGet", 1));
} }
JS_FreeValue(context, administration); JS_FreeValue(context, administration);

View File

@@ -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 : ""; 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) static JSValue _util_defaultGlobalSettings(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{ {
JSValue settings = JS_NewObject(context); JSValue settings = JS_NewObject(context);

View File

@@ -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); 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. ** Log documentation for the available settings.
** @param line_prefix Text to prefix each line with." ** @param line_prefix Text to prefix each line with."