Make storing messages async. Phew.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4355 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-07-20 01:02:50 +00:00
parent 6fcebd7a08
commit fb73fd0afc
7 changed files with 417 additions and 162 deletions

View File

@ -107,9 +107,39 @@ static JSValue _tf_ssb_getAllIdentities(JSContext* context, JSValueConst this_va
return result;
}
typedef struct _append_message_t
{
JSContext* context;
JSValue promise[2];
} append_message_t;
static void _tf_ssb_appendMessage_finish(append_message_t* async, bool success, JSValue result)
{
JSValue error = JS_Call(async->context, success ? async->promise[0] : async->promise[1], JS_UNDEFINED, 1, &result);
tf_util_report_error(async->context, error);
JS_FreeValue(async->context, error);
JS_FreeValue(async->context, async->promise[0]);
JS_FreeValue(async->context, async->promise[1]);
tf_free(async);
}
static void _tf_ssb_appendMessageWithIdentity_callback(const char* id, bool verified, bool is_new, void* user_data)
{
append_message_t* async = user_data;
JSValue result = JS_UNDEFINED;
if (verified)
{
result = is_new ? JS_TRUE : JS_FALSE;
}
_tf_ssb_appendMessage_finish(async, verified, result);
}
static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_UNDEFINED;
append_message_t* async = tf_malloc(sizeof(append_message_t));
*async = (append_message_t) { 0 };
JSValue result = JS_NewPromiseCapability(context, async->promise);
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
if (ssb)
{
@ -118,17 +148,19 @@ static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueCons
uint8_t private_key[crypto_sign_SECRETKEYBYTES];
if (tf_ssb_db_identity_get_private_key(ssb, user, id, private_key, sizeof(private_key)))
{
char message_id[k_id_base64_len] = { 0 };
tf_ssb_append_message_with_keys(ssb, id, private_key, argv[2], message_id, sizeof(message_id));
result = JS_NewString(context, message_id);
tf_ssb_verify_strip_and_store_message(ssb, tf_ssb_sign_message(ssb, id, private_key, argv[2]), _tf_ssb_appendMessageWithIdentity_callback, async);
}
else
{
result = JS_ThrowInternalError(context, "Unable to get private key for user %s with identity %s.", user, id);
_tf_ssb_appendMessage_finish(async, false, JS_ThrowInternalError(context, "Unable to get private key for user %s with identity %s.", user, id));
}
JS_FreeCString(context, id);
JS_FreeCString(context, user);
}
else
{
_tf_ssb_appendMessage_finish(async, false, JS_ThrowInternalError(context, "No SSB instance."));
}
return result;
}
@ -637,18 +669,31 @@ static JSValue _tf_ssb_sqlAsync(JSContext* context, JSValueConst this_val, int a
return result;
}
typedef struct _message_store_t
{
JSContext* context;
JSValue promise[2];
} message_store_t;
void _tf_ssb_message_store_callback(const char* id, bool verified, bool is_new, void* user_data)
{
message_store_t* store = user_data;
JSValue result = JS_Call(store->context, id ? store->promise[0] : store->promise[1], JS_UNDEFINED, 0, NULL);
tf_util_report_error(store->context, result);
JS_FreeValue(store->context, result);
JS_FreeValue(store->context, store->promise[0]);
JS_FreeValue(store->context, store->promise[1]);
tf_free(store);
}
static JSValue _tf_ssb_storeMessage(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
bool is_new = false;
if (tf_ssb_verify_strip_and_store_message(ssb, argv[0], &is_new))
{
return is_new ? JS_TRUE : JS_FALSE;
}
else
{
return JS_UNDEFINED;
}
message_store_t* store = tf_malloc(sizeof(message_store_t));
*store = (message_store_t) { .context = context };
JSValue result = JS_NewPromiseCapability(context, store->promise);
tf_ssb_verify_strip_and_store_message(ssb, argv[0], _tf_ssb_message_store_callback, store);
return result;
}
typedef struct _broadcasts_t