2021-01-02 13:10:00 -05:00
|
|
|
#include "ssb.qjs.h"
|
|
|
|
|
|
|
|
#include "ssb.h"
|
|
|
|
#include "task.h"
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <uv.h>
|
|
|
|
|
|
|
|
#include "quickjs-libc.h"
|
|
|
|
|
|
|
|
static JSClassID _tf_ssb_classId;
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_whoami(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
char id[512];
|
|
|
|
if (tf_ssb_whoami(ssb, id, sizeof(id))) {
|
|
|
|
return JS_NewString(context, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_getMessage(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
int64_t sequence = 0;
|
|
|
|
JS_ToInt64(context, &sequence, argv[1]);
|
|
|
|
int64_t timestamp = -1;
|
|
|
|
char* contents = NULL;
|
|
|
|
if (tf_ssb_get_message_by_author_and_sequence(ssb, id, sequence, NULL, 0, ×tamp, &contents)) {
|
|
|
|
result = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, result, "timestamp", JS_NewInt64(context, timestamp));
|
|
|
|
JS_SetPropertyStr(context, result, "content", JS_NewString(context, contents));
|
|
|
|
free(contents);
|
|
|
|
}
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
if (tf_ssb_blob_get(ssb, id, &blob, &size)) {
|
|
|
|
result = JS_NewArrayBufferCopy(context, blob, size);
|
|
|
|
free(blob);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
char id[512];
|
|
|
|
if (JS_IsString(argv[0])) {
|
|
|
|
const char* text = JS_ToCStringLen(context, &size, argv[0]);
|
|
|
|
if (tf_ssb_blob_store(ssb, (const uint8_t*)text, size, id, sizeof(id))) {
|
|
|
|
result = JS_NewString(context, id);
|
|
|
|
}
|
|
|
|
JS_FreeCString(context, text);
|
|
|
|
} else if ((blob = tf_try_get_array_buffer(context, &size, argv[0])) != 0) {
|
|
|
|
if (tf_ssb_blob_store(ssb, blob, size, id, sizeof(id))) {
|
|
|
|
result = JS_NewString(context, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-09 18:06:33 -05:00
|
|
|
static JSValue _tf_ssb_messageContentGet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
if (tf_ssb_message_content_get(ssb, id, &blob, &size)) {
|
|
|
|
result = JS_NewArrayBufferCopy(context, blob, size);
|
|
|
|
free(blob);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char** connections = tf_ssb_get_connection_ids(ssb);
|
|
|
|
if (connections) {
|
|
|
|
result = JS_NewArray(context);
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (const char** p = connections; *p; p++, i++) {
|
|
|
|
JS_SetPropertyUint32(context, result, i, JS_NewString(context, *p));
|
|
|
|
}
|
|
|
|
free(connections);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_createHistoryStream(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
tf_ssb_send_createHistoryStream(ssb, id);
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _sqlStream_callback_t
|
|
|
|
{
|
|
|
|
JSContext* context;
|
|
|
|
JSValue callback;
|
|
|
|
} sqlStream_callback_t;
|
|
|
|
|
|
|
|
static void _tf_ssb_sqlStream_callback(JSValue row, void* user_data) {
|
|
|
|
sqlStream_callback_t* info = user_data;
|
|
|
|
JSValue response = JS_Call(info->context, info->callback, JS_UNDEFINED, 1, &row);
|
|
|
|
if (JS_IsException(response)) {
|
|
|
|
printf("Error on SQL callback.\n");
|
|
|
|
js_std_dump_error(info->context);
|
|
|
|
}
|
|
|
|
tf_task_run_jobs(tf_task_get(info->context));
|
|
|
|
JS_FreeValue(info->context, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_sqlStream(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* query = JS_ToCString(context, argv[0]);
|
|
|
|
if (query) {
|
|
|
|
sqlStream_callback_t info = {
|
|
|
|
.context = context,
|
|
|
|
.callback = argv[2],
|
|
|
|
};
|
|
|
|
tf_ssb_visit_query(ssb, query, argv[1], _tf_ssb_sqlStream_callback, &info);
|
|
|
|
JS_FreeCString(context, query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_post(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* post_text = JS_ToCString(context, argv[0]);
|
|
|
|
if (post_text) {
|
|
|
|
tf_ssb_append_post(ssb, post_text);
|
|
|
|
JS_FreeCString(context, post_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_appendMessage(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
tf_ssb_append_message(ssb, argv[0]);
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _broadcasts_t
|
|
|
|
{
|
|
|
|
JSContext* context;
|
|
|
|
JSValue array;
|
|
|
|
int length;
|
|
|
|
} broadcasts_t;
|
|
|
|
|
|
|
|
static void _tf_ssb_broadcasts_visit(const struct sockaddr_in* addr, const uint8_t* pub, void* user_data)
|
|
|
|
{
|
|
|
|
broadcasts_t* broadcasts = user_data;
|
|
|
|
JSValue entry = JS_NewObject(broadcasts->context);
|
|
|
|
char address[256];
|
|
|
|
char pubkey[k_id_base64_len];
|
|
|
|
uv_ip4_name(addr, address, sizeof(address));
|
|
|
|
tf_ssb_id_bin_to_str(pubkey, sizeof(pubkey), pub);
|
|
|
|
JS_SetPropertyStr(broadcasts->context, entry, "address", JS_NewString(broadcasts->context, address));
|
|
|
|
JS_SetPropertyStr(broadcasts->context, entry, "port", JS_NewInt32(broadcasts->context, ntohs(addr->sin_port)));
|
|
|
|
JS_SetPropertyStr(broadcasts->context, entry, "pubkey", JS_NewString(broadcasts->context, pubkey));
|
|
|
|
JS_SetPropertyUint32(broadcasts->context, broadcasts->array, broadcasts->length++, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_getBroadcasts(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_UNDEFINED;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
result = JS_NewArray(context);
|
|
|
|
broadcasts_t broadcasts = {
|
|
|
|
.context = context,
|
|
|
|
.array = result,
|
|
|
|
.length = 0,
|
|
|
|
};
|
|
|
|
tf_ssb_visit_broadcasts(ssb, _tf_ssb_broadcasts_visit, &broadcasts);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue args = argv[0];
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
if (JS_IsString(args)) {
|
|
|
|
const char* address_str = JS_ToCString(context, args);
|
|
|
|
printf("Connecting to %s\n", address_str);
|
|
|
|
tf_ssb_connect_str(ssb, address_str);
|
|
|
|
JS_FreeCString(context, address_str);
|
|
|
|
} else {
|
|
|
|
JSValue address = JS_GetPropertyStr(context, args, "address");
|
|
|
|
JSValue port = JS_GetPropertyStr(context, args, "port");
|
|
|
|
JSValue pubkey = JS_GetPropertyStr(context, args, "pubkey");
|
|
|
|
const char* address_str = JS_ToCString(context, address);
|
|
|
|
int32_t port_int = 0;
|
|
|
|
JS_ToInt32(context, &port_int, port);
|
|
|
|
const char* pubkey_str = JS_ToCString(context, pubkey);
|
|
|
|
uint8_t pubkey_bin[k_id_bin_len];
|
|
|
|
printf("Connecting to %s:%d\n", address_str, port_int);
|
|
|
|
tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str);
|
|
|
|
tf_ssb_connect(ssb, address_str, port_int, pubkey_bin);
|
|
|
|
JS_FreeCString(context, pubkey_str);
|
|
|
|
JS_FreeCString(context, address_str);
|
|
|
|
JS_FreeValue(context, address);
|
|
|
|
JS_FreeValue(context, port);
|
|
|
|
JS_FreeValue(context, pubkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_call_callback(tf_ssb_t* ssb, const char* name, void* user_data)
|
|
|
|
{
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JSValue ssbo = JS_GetPropertyStr(context, global, "ssb");
|
|
|
|
JSValue callback = JS_GetPropertyStr(context, ssbo, name);
|
|
|
|
JSValue args = JS_UNDEFINED;
|
|
|
|
JSValue response = JS_Call(context, callback, JS_UNDEFINED, 0, &args);
|
|
|
|
if (JS_IsException(response)) {
|
|
|
|
printf("Error on callback: %s.\n", name);
|
|
|
|
js_std_dump_error(context);
|
|
|
|
}
|
|
|
|
tf_task_run_jobs(tf_task_get(context));
|
|
|
|
JS_FreeValue(context, response);
|
|
|
|
JS_FreeValue(context, ssbo);
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_broadcasts_changed(tf_ssb_t* ssb, void* user_data)
|
|
|
|
{
|
|
|
|
_tf_ssb_call_callback(ssb, "onBroadcastsChanged", user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_connections_changed(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data)
|
|
|
|
{
|
|
|
|
_tf_ssb_call_callback(ssb, "onConnectionsChanged", user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_ssb_init(JSContext* context, tf_ssb_t* ssb)
|
|
|
|
{
|
|
|
|
JS_NewClassID(&_tf_ssb_classId);
|
|
|
|
JSClassDef def = {
|
|
|
|
.class_name = "ssb",
|
|
|
|
};
|
|
|
|
if (JS_NewClass(JS_GetRuntime(context), _tf_ssb_classId, &def) != 0) {
|
|
|
|
fprintf(stderr, "Failed to register ssb.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_ssb_set_broadcasts_changed_callback(ssb, _tf_ssb_broadcasts_changed, NULL);
|
|
|
|
tf_ssb_add_connections_changed_callback(ssb, _tf_ssb_connections_changed, NULL);
|
|
|
|
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JSValue object = JS_NewObjectClass(context, _tf_ssb_classId);
|
|
|
|
JS_SetPropertyStr(context, global, "ssb", object);
|
|
|
|
JS_SetOpaque(object, ssb);
|
|
|
|
JS_SetPropertyStr(context, object, "whoami", JS_NewCFunction(context, _tf_ssb_whoami, "whoami", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "getMessage", JS_NewCFunction(context, _tf_ssb_getMessage, "getMessage", 2));
|
|
|
|
JS_SetPropertyStr(context, object, "blobGet", JS_NewCFunction(context, _tf_ssb_blobGet, "blobGet", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "blobStore", JS_NewCFunction(context, _tf_ssb_blobStore, "blobStore", 2));
|
2021-01-09 18:06:33 -05:00
|
|
|
JS_SetPropertyStr(context, object, "messageContentGet", JS_NewCFunction(context, _tf_ssb_messageContentGet, "messageContentGet", 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, object, "connections", JS_NewCFunction(context, _tf_ssb_connections, "connections", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "createHistoryStream", JS_NewCFunction(context, _tf_ssb_createHistoryStream, "createHistoryStream", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "sqlStream", JS_NewCFunction(context, _tf_ssb_sqlStream, "sqlStream", 3));
|
|
|
|
JS_SetPropertyStr(context, object, "post", JS_NewCFunction(context, _tf_ssb_post, "post", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "appendMessage", JS_NewCFunction(context, _tf_ssb_appendMessage, "appendMessage", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunction(context, _tf_ssb_getBroadcasts, "getBroadcasts", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "connect", JS_NewCFunction(context, _tf_ssb_connect, "connect", 1));
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
}
|