core: Move core.globalSettingSet to C.
All checks were successful
Build Tilde Friends / Build-Docs (push) Successful in 2m30s
Build Tilde Friends / Build-All (push) Successful in 10m46s

This commit is contained in:
2025-12-09 13:02:25 -05:00
parent 3c6eeb9cd3
commit f1876a34ec
5 changed files with 73 additions and 18 deletions

View File

@@ -392,17 +392,6 @@ exports.getProcessBlob = async function getProcessBlob(blobId, key, options) {
} }
}; };
if (process.credentials?.permissions?.administration) { 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) { imports.core.deleteUser = async function (user) {
await imports.core.permissionTest('delete_user'); await imports.core.permissionTest('delete_user');
let db = new Database('auth'); let db = new Database('auth');

View File

@@ -27,8 +27,6 @@ commands:
``` ```
```
Usage: out/debug/tildefriends run [options] Usage: out/debug/tildefriends run [options]
Run tildefriends (default). Run tildefriends (default).

View File

@@ -1021,6 +1021,73 @@ static JSValue _tf_ssb_get_blocks(JSContext* context, JSValueConst this_val, int
return result; 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) static JSValue _tf_api_register_imports(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{ {
JSValue imports = argv[0]; 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, "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_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, "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)); JS_SetPropertyStr(context, ssb, "removeBlock", JS_NewCFunctionData(context, _tf_ssb_remove_block, 1, 0, 1, &process));

View File

@@ -2555,7 +2555,7 @@ const char* tf_ssb_db_get_global_setting_string_alloc(sqlite3* db, const char* n
return result; 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); tf_setting_kind_t kind = tf_util_get_global_setting_kind(name);
if (kind == k_kind_unknown) if (kind == k_kind_unknown)

View File

@@ -32,7 +32,7 @@ void tf_ssb_db_init_reader(sqlite3* db);
** @param ssb The SSB instance. ** @param ssb The SSB instance.
** @param id The message identifier. ** @param id The message identifier.
** @param[out] out_blob Populated with the message content. ** @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. ** @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); 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. ** Verify an author's feed.
** @param ssb The SSB instance. ** @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 debug_sequence Message sequence number to debug if non-zero.
** @param fix Fix invalid messages when possible. ** @param fix Fix invalid messages when possible.
** @return true If the feed verified successfully. ** @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. ** Set a global setting from a string representation of its value.
** @param db The database. ** @param db The database.
** @param name The setting name. ** @param name The setting name.
** @param value The settinv value. ** @param value The setting value.
** @return true if the setting was set. ** @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. ** Get the latest profile information for the given identity.