forked from cory/tildefriends
		
	blobs.has
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4051 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
		
							
								
								
									
										11
									
								
								core/ssb.js
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								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"]}); | ||||
| }); | ||||
|   | ||||
							
								
								
									
										17
									
								
								src/ssb.db.c
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								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; | ||||
|   | ||||
| @@ -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); | ||||
|  | ||||
|   | ||||
| @@ -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); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user