Move ssb.appendMessageWithIdentity's DB work off the main thread.
This commit is contained in:
67
src/ssb.js.c
67
src/ssb.js.c
@ -663,8 +663,15 @@ static JSValue _tf_ssb_getIdentityInfo(JSContext* context, JSValueConst this_val
|
||||
|
||||
typedef struct _append_message_t
|
||||
{
|
||||
char id[k_id_base64_len];
|
||||
uint8_t private_key[crypto_sign_SECRETKEYBYTES];
|
||||
bool got_private_key;
|
||||
char previous_id[512];
|
||||
int64_t previous_sequence;
|
||||
JSContext* context;
|
||||
JSValue promise[2];
|
||||
JSValue message;
|
||||
char user[];
|
||||
} append_message_t;
|
||||
|
||||
static void _tf_ssb_appendMessage_finish(append_message_t* async, bool success, JSValue result)
|
||||
@ -672,6 +679,7 @@ static void _tf_ssb_appendMessage_finish(append_message_t* async, bool success,
|
||||
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->message);
|
||||
JS_FreeValue(async->context, async->promise[0]);
|
||||
JS_FreeValue(async->context, async->promise[1]);
|
||||
tf_free(async);
|
||||
@ -688,35 +696,50 @@ static void _tf_ssb_appendMessageWithIdentity_callback(const char* id, bool veri
|
||||
_tf_ssb_appendMessage_finish(async, verified, result);
|
||||
}
|
||||
|
||||
static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
static void _tf_ssb_append_message_with_identity_get_key_work(tf_ssb_t* ssb, void* user_data)
|
||||
{
|
||||
append_message_t* async = tf_malloc(sizeof(append_message_t));
|
||||
*async = (append_message_t) { .context = context };
|
||||
JSValue result = JS_NewPromiseCapability(context, async->promise);
|
||||
append_message_t* work = user_data;
|
||||
work->got_private_key = tf_ssb_db_identity_get_private_key(ssb, work->user, work->id, work->private_key, sizeof(work->private_key));
|
||||
tf_ssb_db_get_latest_message_by_author(ssb, work->id, &work->previous_sequence, work->previous_id, sizeof(work->previous_id));
|
||||
}
|
||||
|
||||
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
||||
if (ssb)
|
||||
static void _tf_ssb_append_message_with_identity_get_key_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
||||
{
|
||||
append_message_t* work = user_data;
|
||||
if (work->got_private_key)
|
||||
{
|
||||
const char* user = JS_ToCString(context, argv[0]);
|
||||
const char* id = JS_ToCString(context, argv[1]);
|
||||
uint8_t private_key[crypto_sign_SECRETKEYBYTES];
|
||||
if (tf_ssb_db_identity_get_private_key(ssb, user, id, private_key, sizeof(private_key)))
|
||||
{
|
||||
JSValue signed_message = tf_ssb_sign_message(ssb, id, private_key, argv[2]);
|
||||
tf_ssb_verify_strip_and_store_message(ssb, signed_message, _tf_ssb_appendMessageWithIdentity_callback, async);
|
||||
JS_FreeValue(context, signed_message);
|
||||
}
|
||||
else
|
||||
{
|
||||
_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);
|
||||
JSValue signed_message = tf_ssb_sign_message(ssb, work->id, work->private_key, work->message, work->previous_id, work->previous_sequence);
|
||||
tf_ssb_verify_strip_and_store_message(ssb, signed_message, _tf_ssb_appendMessageWithIdentity_callback, work);
|
||||
JS_FreeValue(work->context, signed_message);
|
||||
}
|
||||
else
|
||||
{
|
||||
_tf_ssb_appendMessage_finish(async, false, JS_ThrowInternalError(context, "No SSB instance."));
|
||||
_tf_ssb_appendMessage_finish(work, false, JS_ThrowInternalError(work->context, "Unable to get private key for user %s with identity %s.", work->user, work->id));
|
||||
}
|
||||
}
|
||||
|
||||
static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
||||
if (!ssb)
|
||||
{
|
||||
return JS_ThrowInternalError(context, "No SSB instance.");
|
||||
}
|
||||
|
||||
size_t user_length = 0;
|
||||
const char* user = JS_ToCStringLen(context, &user_length, argv[0]);
|
||||
const char* id = JS_ToCString(context, argv[1]);
|
||||
|
||||
append_message_t* work = tf_malloc(sizeof(append_message_t) + user_length + 1);
|
||||
*work = (append_message_t) { .context = context, .message = JS_DupValue(context, argv[2]) };
|
||||
memcpy(work->user, user, user_length + 1);
|
||||
snprintf(work->id, sizeof(work->id), "%s", id);
|
||||
|
||||
JS_FreeCString(context, id);
|
||||
JS_FreeCString(context, user);
|
||||
|
||||
JSValue result = JS_NewPromiseCapability(context, work->promise);
|
||||
tf_ssb_run_work(ssb, _tf_ssb_append_message_with_identity_get_key_work, _tf_ssb_append_message_with_identity_get_key_after_work, work);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user