createHistoryStream JS -> C.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4110 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-01-08 17:45:15 +00:00
parent 6ff33191bb
commit 53e4f4341c
6 changed files with 216 additions and 52 deletions

View File

@ -188,6 +188,13 @@ typedef struct _tf_ssb_t
int broadcasts_changed_count;
} tf_ssb_t;
typedef struct _tf_ssb_connection_message_request_t
{
char author[k_id_base64_len];
int32_t request_number;
bool keys;
} tf_ssb_connection_message_request_t;
typedef struct _tf_ssb_connection_t
{
tf_ssb_t* ssb;
@ -241,6 +248,9 @@ typedef struct _tf_ssb_connection_t
tf_ssb_request_t* requests;
int requests_count;
const char* destroy_reason;
tf_ssb_connection_message_request_t* message_requests;
int message_requests_count;
} tf_ssb_connection_t;
static JSClassID _connection_class_id;
@ -499,6 +509,34 @@ void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t requ
connection->ssb->request_count++;
}
static int _message_request_compare(const void* a, const void* b)
{
const char* author = a;
const tf_ssb_connection_message_request_t* rb = b;
return strcmp(author, rb->author);
}
void tf_ssb_connection_add_new_message_request(tf_ssb_connection_t* connection, const char* author, int32_t request_number, bool keys)
{
int index = tf_util_insert_index(author, connection->message_requests, connection->message_requests_count, sizeof(tf_ssb_connection_message_request_t), _message_request_compare);
if (index < connection->message_requests_count && strcmp(author, connection->message_requests[index].author) == 0)
{
return;
}
connection->message_requests = tf_resize_vec(connection->message_requests, sizeof(tf_ssb_connection_message_request_t) * (connection->message_requests_count + 1));
if (connection->message_requests_count - index)
{
memmove(connection->message_requests + index + 1, connection->message_requests + index, sizeof(tf_ssb_connection_message_request_t) * (connection->message_requests_count - index));
}
connection->message_requests[index] = (tf_ssb_connection_message_request_t)
{
.request_number = request_number,
.keys = keys,
};
snprintf(connection->message_requests[index].author, sizeof(connection->message_requests[index].author), "%s", author);
connection->message_requests_count++;
}
static void _tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, int32_t request_number)
{
tf_ssb_request_t* request = bsearch(&request_number, connection->requests, connection->requests_count, sizeof(tf_ssb_request_t), _request_compare);
@ -518,7 +556,6 @@ static void _tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, i
void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const uint8_t* message, size_t size, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data)
{
printf("SEND %p\n", connection);
if (!connection)
{
return;
@ -1475,12 +1512,10 @@ void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message)
void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* reason)
{
printf("DESTROY %p\n", connection);
tf_ssb_t* ssb = connection->ssb;
if (!connection->destroy_reason)
{
connection->destroy_reason = reason;
printf("destroying connection %p obj=%p: %s\n", connection, JS_VALUE_GET_PTR(connection->object), reason);
}
while (connection->requests)
{
@ -1558,6 +1593,10 @@ void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* rea
!connection->tcp.data &&
!connection->connect.data)
{
tf_free(connection->message_requests);
connection->message_requests = NULL;
connection->message_requests_count = 0;
tf_free(connection);
}
}
@ -2907,6 +2946,43 @@ void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id)
next = node->next;
node->callback(ssb, id, node->user_data);
}
JSContext* context = ssb->context;
JSValue message_keys = tf_ssb_db_get_message_by_id(ssb, id, true);
JSValue message = JS_GetPropertyStr(context, message_keys, "value");
if (!JS_IsUndefined(message))
{
JSValue author = JS_GetPropertyStr(context, message, "author");
const char* author_string = JS_ToCString(context, author);
for (tf_ssb_connection_t* connection = ssb->connections; connection; connection = connection->next)
{
tf_ssb_connection_message_request_t* message_request =
bsearch(
author_string,
connection->message_requests,
connection->message_requests_count,
sizeof(tf_ssb_connection_message_request_t),
_message_request_compare);
if (message_request)
{
tf_ssb_connection_rpc_send_json(
connection,
k_ssb_rpc_flag_stream,
message_request->request_number,
message_request->keys ? message_keys : message,
NULL,
NULL,
NULL);
}
}
JS_FreeCString(context, author_string);
JS_FreeValue(context, author);
}
JS_FreeValue(context, message);
JS_FreeValue(context, message_keys);
}
void tf_ssb_add_blob_want_added_callback(tf_ssb_t* ssb, void (*callback)(tf_ssb_t* ssb, const char* id, void* user_data), void (*cleanup)(tf_ssb_t* ssb, void* user_data), void* user_data)