tunnel.isRoom => C.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4089 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-12-30 01:23:44 +00:00
parent 09ddfffa6b
commit 1d13c25ded
2 changed files with 60 additions and 9 deletions

View File

@ -4,7 +4,6 @@ var g_database = new Database('core');
let g_attendants = {}; let g_attendants = {};
const k_use_create_history_stream = false; const k_use_create_history_stream = false;
const k_blobs_concurrent_target = 8; const k_blobs_concurrent_target = 8;
const k_settings = JSON.parse(g_database.get('settings') ?? '{"room": true}');
function following(db, id) { function following(db, id) {
var o = db.get(id + ":following"); var o = db.get(id + ":following");
@ -195,14 +194,6 @@ ssb.addRpc(['blobs', 'createWants'], function(request) {
requestMoreBlobs(request); requestMoreBlobs(request);
}); });
ssb.addRpc(['tunnel', 'isRoom'], function(request) {
if (k_settings.room) {
request.send_json({"name": "tilde friends tunnel", "membership": false, "features": ["tunnel", "room1"]});
} else {
request.send_json(false);
}
});
function notify_attendant_changed(id, type) { function notify_attendant_changed(id, type) {
if (!id) { if (!id) {
print(`notify_attendant_changed called with id=${id}`); print(`notify_attendant_changed called with id=${id}`);

View File

@ -5,6 +5,8 @@
#include "ssb.db.h" #include "ssb.db.h"
#include "util.js.h" #include "util.js.h"
#include <sqlite3.h>
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
@ -206,10 +208,68 @@ static void _tf_ssb_rpc_tunnel_connect(tf_ssb_connection_t* connection, uint8_t
JS_FreeValue(context, arg_array); JS_FreeValue(context, arg_array);
} }
static bool _get_global_setting_bool(tf_ssb_t* ssb, const char* name, bool default_value)
{
bool result = default_value;
JSContext* context = tf_ssb_get_context(ssb);
sqlite3* db = tf_ssb_get_db(ssb);
sqlite3_stmt* statement;
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
JSValue value = JS_ParseJSON(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0), NULL);
JSValue property = JS_GetPropertyStr(context, value, name);
if (JS_IsBool(property))
{
result = JS_ToBool(context, property);
}
JS_FreeValue(context, property);
JS_FreeValue(context, value);
}
sqlite3_finalize(statement);
}
return result;
}
static void _tf_ssb_rpc_tunnel_is_room(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
{
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
JSContext* context = tf_ssb_get_context(ssb);
JSValue response = JS_FALSE;
if (_get_global_setting_bool(ssb, "room", true))
{
response = JS_NewObject(context);
JS_SetPropertyStr(context, response, "name", JS_NewString(context, "tilde friends tunnel"));
JS_SetPropertyStr(context, response, "membership", JS_FALSE);
JSValue features = JS_NewArray(context);
JS_SetPropertyUint32(context, features, 0, JS_NewString(context, "tunnel"));
JS_SetPropertyUint32(context, features, 1, JS_NewString(context, "room1"));
JS_SetPropertyStr(context, response, "features", features);
}
JSValue message_val = JS_JSONStringify(context, response, JS_NULL, JS_NULL);
size_t json_size = 0;
const char* message_str = JS_ToCStringLen(context, &json_size, message_val);
tf_ssb_connection_rpc_send(
connection,
flags | k_ssb_rpc_flag_end_error,
-request_number,
(const uint8_t*)message_str,
json_size,
NULL,
NULL,
NULL);
JS_FreeCString(context, message_str);
JS_FreeValue(context, message_val);
JS_FreeValue(context, response);
}
void tf_ssb_rpc_register(tf_ssb_t* ssb) void tf_ssb_rpc_register(tf_ssb_t* ssb)
{ {
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "gossip", "ping", NULL }, _tf_ssb_rpc_gossip_ping, NULL, NULL); tf_ssb_add_rpc_callback(ssb, (const char*[]) { "gossip", "ping", NULL }, _tf_ssb_rpc_gossip_ping, NULL, NULL);
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "blobs", "get", NULL }, _tf_ssb_rpc_blobs_get, NULL, NULL); tf_ssb_add_rpc_callback(ssb, (const char*[]) { "blobs", "get", NULL }, _tf_ssb_rpc_blobs_get, NULL, NULL);
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "blobs", "has", NULL }, _tf_ssb_rpc_blobs_has, NULL, NULL); tf_ssb_add_rpc_callback(ssb, (const char*[]) { "blobs", "has", NULL }, _tf_ssb_rpc_blobs_has, NULL, NULL);
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "tunnel", "connect", NULL }, _tf_ssb_rpc_tunnel_connect, NULL, NULL); tf_ssb_add_rpc_callback(ssb, (const char*[]) { "tunnel", "connect", NULL }, _tf_ssb_rpc_tunnel_connect, NULL, NULL);
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "tunnel", "isRoom", NULL }, _tf_ssb_rpc_tunnel_is_room, NULL, NULL);
} }