From 880ab7fddee500c369ce57a7bb9385c69fe52370 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 17 Nov 2022 01:49:34 +0000 Subject: [PATCH] blobs.has git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4051 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/ssb.js | 11 ----------- src/ssb.db.c | 17 +++++++++++++++++ src/ssb.db.h | 1 + src/ssb.rpc.c | 23 +++++++++++++++++++++++ 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/core/ssb.js b/core/ssb.js index b4e95ec0..fc9df420 100644 --- a/core/ssb.js +++ b/core/ssb.js @@ -194,17 +194,6 @@ ssb.addRpc(['blobs', 'createWants'], function(request) { requestMoreBlobs(request); }); -ssb.addRpc(['blobs', 'has'], function(request) { - var found = false; - ssb.sqlStream( - 'SELECT 1 FROM blobs where id = ?1', - [request.args[0]], - function(row) { - found = true; - }); - request.send_json(found); -}); - ssb.addRpc(['tunnel', 'isRoom'], function(request) { request.send_json({"name": "tilde friends tunnel", "membership": false, "features": ["tunnel", "room1"]}); }); diff --git a/src/ssb.db.c b/src/ssb.db.c index b8961fa4..6ab7c4ab 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -366,6 +366,23 @@ bool tf_ssb_db_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_ return result; } +bool tf_ssb_db_blob_has(tf_ssb_t* ssb, const char* id) +{ + bool result = false; + sqlite3_stmt* statement; + const char* query = "SELECT COUNT(*) FROM blobs WHERE id = $1"; + if (sqlite3_prepare(tf_ssb_get_db(ssb), query, -1, &statement, NULL) == SQLITE_OK) + { + if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && + sqlite3_step(statement) == SQLITE_ROW) + { + result = sqlite3_column_int64(statement, 0) != 0; + } + sqlite3_finalize(statement); + } + return result; +} + bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size) { bool result = false; diff --git a/src/ssb.db.h b/src/ssb.db.h index cb5a5484..884cb2a1 100644 --- a/src/ssb.db.h +++ b/src/ssb.db.h @@ -8,6 +8,7 @@ typedef struct _tf_ssb_t tf_ssb_t; void tf_ssb_db_init(tf_ssb_t* ssb); bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, JSValue val, const char* signature, bool sequence_before_author); 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_blob_has(tf_ssb_t* ssb, const char* id); bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size); bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* out_id, size_t out_id_size, bool* out_new); diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index f3871d55..47808b2a 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -75,8 +75,31 @@ static void _tf_ssb_rpc_blobs_get(tf_ssb_connection_t* connection, uint8_t flags NULL); } +static void _tf_ssb_rpc_blobs_has(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_connection_get_context(connection); + JSValue ids = JS_GetPropertyStr(context, args, "args"); + JSValue id = JS_GetPropertyUint32(context, ids, 0); + const char* id_str = JS_ToCString(context, id); + bool has = tf_ssb_db_blob_has(ssb, id_str); + JS_FreeCString(context, id_str); + JS_FreeValue(context, id); + JS_FreeValue(context, ids); + tf_ssb_connection_rpc_send( + connection, + k_ssb_rpc_flag_json | k_ssb_rpc_flag_end_error, + -request_number, + (const uint8_t*)(has ? "true" : "false"), + strlen(has ? "true" : "false"), + NULL, + NULL, + NULL); +} + 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*[]) { "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); }