From f1876a34ec969a4bfa4bd68715035e8287d8b498 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Tue, 9 Dec 2025 13:02:25 -0500 Subject: [PATCH] core: Move core.globalSettingSet to C. --- core/core.js | 11 --------- docs/usage.md | 2 -- src/api.js.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/ssb.db.c | 2 +- src/ssb.db.h | 8 +++--- 5 files changed, 73 insertions(+), 18 deletions(-) diff --git a/core/core.js b/core/core.js index a4a272c5..ec584644 100644 --- a/core/core.js +++ b/core/core.js @@ -392,17 +392,6 @@ exports.getProcessBlob = async function getProcessBlob(blobId, key, options) { } }; if (process.credentials?.permissions?.administration) { - imports.core.globalSettingsSet = async function (key, value) { - await imports.core.permissionTest( - 'set_global_setting', - `Set ${JSON.stringify(key)} to ${JSON.stringify(value)}.` - ); - print('Setting', key, value); - let settings = await loadSettings(); - settings[key] = value; - await new Database('core').set('settings', JSON.stringify(settings)); - print('Done.'); - }; imports.core.deleteUser = async function (user) { await imports.core.permissionTest('delete_user'); let db = new Database('auth'); diff --git a/docs/usage.md b/docs/usage.md index 5543ca6e..ba112239 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -27,8 +27,6 @@ commands: ``` -``` - Usage: out/debug/tildefriends run [options] Run tildefriends (default). diff --git a/src/api.js.c b/src/api.js.c index 0fd32153..a51ec352 100644 --- a/src/api.js.c +++ b/src/api.js.c @@ -1021,6 +1021,73 @@ static JSValue _tf_ssb_get_blocks(JSContext* context, JSValueConst this_val, int return result; } +typedef struct _global_setting_set_t +{ + const char* key; + const char* value; + JSValue promise[2]; + bool done; + JSValue result; +} global_setting_set_t; + +static void _tf_ssb_globalSettingsSet_work(tf_ssb_t* ssb, void* user_data) +{ + global_setting_set_t* work = user_data; + tf_printf("SET [%s]=[%s]\n", work->key, work->value); + sqlite3* db = tf_ssb_acquire_db_writer(ssb); + work->done = tf_ssb_db_set_global_setting_from_string(db, work->key, work->value); + tf_ssb_release_db_writer(ssb, db); +} + +static void _tf_ssb_globalSettingsSet_after_work(tf_ssb_t* ssb, int status, void* user_data) +{ + global_setting_set_t* work = user_data; + JSContext* context = tf_ssb_get_context(ssb); + JSValue error = JS_Call(context, work->done ? work->promise[0] : work->promise[1], JS_UNDEFINED, 1, &work->result); + JS_FreeValue(context, work->result); + tf_util_report_error(context, error); + JS_FreeValue(context, error); + JS_FreeValue(context, work->promise[0]); + JS_FreeValue(context, work->promise[1]); + JS_FreeCString(context, work->key); + JS_FreeCString(context, work->value); + tf_free(work); +} + +static void _tf_ssb_globalSettingsSet_permission_callback(JSContext* context, bool granted, JSValue value, void* user_data) +{ + global_setting_set_t* work = user_data; + tf_task_t* task = tf_task_get(context); + tf_ssb_t* ssb = tf_task_get_ssb(task); + work->result = value; + if (granted) + { + tf_ssb_run_work(ssb, _tf_ssb_globalSettingsSet_work, _tf_ssb_globalSettingsSet_after_work, work); + } + else + { + _tf_ssb_globalSettingsSet_after_work(ssb, -1, work); + } +} + +static JSValue _tf_ssb_globalSettingsSet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data) +{ + const char* key = JS_ToCString(context, argv[0]); + const char* value = JS_ToCString(context, argv[1]); + + global_setting_set_t* work = tf_malloc(sizeof(global_setting_set_t)); + *work = (global_setting_set_t) { + .key = key, + .value = value, + }; + JSValue result = JS_NewPromiseCapability(context, work->promise); + + char description[256] = ""; + snprintf(description, sizeof(description), "Set %s to %s.", key, value); + _tf_ssb_permission_test(context, data[0], "set_global_setting", description, _tf_ssb_globalSettingsSet_permission_callback, work); + return result; +} + static JSValue _tf_api_register_imports(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue imports = argv[0]; @@ -1056,6 +1123,7 @@ static JSValue _tf_api_register_imports(JSContext* context, JSValueConst this_va { 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, "globalSettingsSet", JS_NewCFunctionData(context, _tf_ssb_globalSettingsSet, 2, 0, 1, &process)); JS_SetPropertyStr(context, ssb, "addBlock", JS_NewCFunctionData(context, _tf_ssb_add_block, 1, 0, 1, &process)); JS_SetPropertyStr(context, ssb, "removeBlock", JS_NewCFunctionData(context, _tf_ssb_remove_block, 1, 0, 1, &process)); diff --git a/src/ssb.db.c b/src/ssb.db.c index 7f30578a..835655e3 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -2555,7 +2555,7 @@ const char* tf_ssb_db_get_global_setting_string_alloc(sqlite3* db, const char* n return result; } -bool tf_ssb_db_set_global_setting_from_string(sqlite3* db, const char* name, char* value) +bool tf_ssb_db_set_global_setting_from_string(sqlite3* db, const char* name, const char* value) { tf_setting_kind_t kind = tf_util_get_global_setting_kind(name); if (kind == k_kind_unknown) diff --git a/src/ssb.db.h b/src/ssb.db.h index 6f87c440..31f3401f 100644 --- a/src/ssb.db.h +++ b/src/ssb.db.h @@ -32,7 +32,7 @@ void tf_ssb_db_init_reader(sqlite3* db); ** @param ssb The SSB instance. ** @param id The message identifier. ** @param[out] out_blob Populated with the message content. -** @param[out] out_size POpulated with the size of the message content. +** @param[out] out_size Populated with the size of the message content. ** @return true If the message content was found and retrieved. */ bool tf_ssb_db_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size); @@ -454,7 +454,7 @@ const char* tf_ssb_db_resolve_index(sqlite3* db, const char* host); /** ** Verify an author's feed. ** @param ssb The SSB instance. -** @param id The author'd identity. +** @param id The author's identity. ** @param debug_sequence Message sequence number to debug if non-zero. ** @param fix Fix invalid messages when possible. ** @return true If the feed verified successfully. @@ -511,10 +511,10 @@ const char* tf_ssb_db_get_global_setting_string_alloc(sqlite3* db, const char* n ** Set a global setting from a string representation of its value. ** @param db The database. ** @param name The setting name. -** @param value The settinv value. +** @param value The setting value. ** @return true if the setting was set. */ -bool tf_ssb_db_set_global_setting_from_string(sqlite3* db, const char* name, char* value); +bool tf_ssb_db_set_global_setting_from_string(sqlite3* db, const char* name, const char* value); /** ** Get the latest profile information for the given identity.