Expose stored connections to script, and only store connections that were explicitly requested.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4131 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-01-18 00:37:45 +00:00
parent 45a5202456
commit 0f11f497ed
5 changed files with 110 additions and 3 deletions

View File

@ -253,6 +253,28 @@ static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, in
return result;
}
static JSValue _tf_ssb_storedConnections(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_NULL;
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
if (ssb)
{
int count = 0;
tf_ssb_db_stored_connection_t* connections = tf_ssb_db_get_stored_connections(ssb, &count);
result = JS_NewArray(context);
for (int i = 0; i < count; i++)
{
JSValue connection = JS_NewObject(context);
JS_SetPropertyStr(context, connection, "address", JS_NewString(context, connections[i].address));
JS_SetPropertyStr(context, connection, "port", JS_NewInt32(context, connections[i].port));
JS_SetPropertyStr(context, connection, "pubkey", JS_NewString(context, connections[i].pubkey));
JS_SetPropertyUint32(context, result, i, connection);
}
tf_free(connections);
}
return result;
}
static JSValue _tf_ssb_getConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
@ -403,6 +425,32 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar
return JS_UNDEFINED;
}
static JSValue _tf_ssb_forgetStoredConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue args = argv[0];
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
if (ssb)
{
JSValue address = JS_GetPropertyStr(context, args, "address");
JSValue port = JS_GetPropertyStr(context, args, "port");
JSValue pubkey = JS_GetPropertyStr(context, args, "pubkey");
const char* address_str = JS_ToCString(context, address);
int32_t port_int = 0;
JS_ToInt32(context, &port_int, port);
const char* pubkey_str = JS_ToCString(context, pubkey);
if (pubkey_str)
{
tf_ssb_db_forget_stored_connection(ssb, address_str, port_int, pubkey_str);
}
JS_FreeCString(context, pubkey_str);
JS_FreeCString(context, address_str);
JS_FreeValue(context, address);
JS_FreeValue(context, port);
JS_FreeValue(context, pubkey);
}
return JS_UNDEFINED;
}
static void _tf_ssb_cleanup_value(tf_ssb_t* ssb, void* user_data)
{
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
@ -802,8 +850,10 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
JS_SetPropertyStr(context, object, "blobStore", JS_NewCFunction(context, _tf_ssb_blobStore, "blobStore", 2));
JS_SetPropertyStr(context, object, "messageContentGet", JS_NewCFunction(context, _tf_ssb_messageContentGet, "messageContentGet", 1));
JS_SetPropertyStr(context, object, "connections", JS_NewCFunction(context, _tf_ssb_connections, "connections", 0));
JS_SetPropertyStr(context, object, "storedConnections", JS_NewCFunction(context, _tf_ssb_storedConnections, "storedConnections", 0));
JS_SetPropertyStr(context, object, "getConnection", JS_NewCFunction(context, _tf_ssb_getConnection, "getConnection", 1));
JS_SetPropertyStr(context, object, "closeConnection", JS_NewCFunction(context, _tf_ssb_closeConnection, "closeConnection", 1));
JS_SetPropertyStr(context, object, "forgetStoredConnection", JS_NewCFunction(context, _tf_ssb_forgetStoredConnection, "forgetStoredConnection", 1));
JS_SetPropertyStr(context, object, "sqlStream", JS_NewCFunction(context, _tf_ssb_sqlStream, "sqlStream", 3));
JS_SetPropertyStr(context, object, "storeMessage", JS_NewCFunction(context, _tf_ssb_storeMessage, "storeMessage", 1));
JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunction(context, _tf_ssb_getBroadcasts, "getBroadcasts", 0));