commit fff91df87c02e44dfed8f9afa2b097815e9ed741
parent 78cf12171292eda315043ac8e18e0b242a9c5dc4
Author: William Casarin <jb55@jb55.com>
Date: Tue, 25 Jul 2023 19:41:53 -0700
ndb: handle eose and command results
Diffstat:
4 files changed, 56 insertions(+), 10 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -11,3 +11,4 @@ configurator
config.h
libsecp256k1.a
.direnv/
+deps/
diff --git a/nostrdb.c b/nostrdb.c
@@ -655,6 +655,9 @@ int ndb_ws_event_from_json(const char *json, int len, struct ndb_tce *tce,
int tok_len, res;
struct ndb_json_parser parser;
+ tce->subid_len = 0;
+ tce->subid = "";
+
ndb_json_parser_init(&parser, json, len, buf, bufsize);
if ((res = ndb_json_parser_parse(&parser)) < 0)
return res;
@@ -676,10 +679,20 @@ int ndb_ws_event_from_json(const char *json, int len, struct ndb_tce *tce,
if (tok->type != JSMN_STRING)
return 0;
- ev->subid = json + tok->start;
- ev->subid_len = toksize(tok);
+ tce->subid = json + tok->start;
+ tce->subid_len = toksize(tok);
return ndb_parse_json_note(&parser, &ev->note);
+ } else if (tok_len == 4 && !memcmp("EOSE", json + tok->start, 4)) {
+ tce->evtype = NDB_TCE_EOSE;
+
+ tok = &parser.toks[parser.i++];
+ if (tok->type != JSMN_STRING)
+ return 0;
+
+ tce->subid = json + tok->start;
+ tce->subid_len = toksize(tok);
+ return 1;
} else if (tok_len == 2 && !memcmp("OK", json + tok->start, 2)) {
if (parser.num_tokens != 5)
return 0;
@@ -692,8 +705,8 @@ int ndb_ws_event_from_json(const char *json, int len, struct ndb_tce *tce,
if (tok->type != JSMN_STRING)
return 0;
- cr->subid = json + tok->start;
- cr->subid_len = toksize(tok);
+ tce->subid = json + tok->start;
+ tce->subid_len = toksize(tok);
tok = &parser.toks[parser.i++];
if (tok->type != JSMN_PRIMITIVE || toksize(tok) == 0)
diff --git a/nostrdb.h b/nostrdb.h
@@ -25,21 +25,21 @@ struct ndb_str {
struct ndb_event {
struct ndb_note *note;
- const char *subid;
- int subid_len;
};
struct ndb_command_result {
int ok;
const char *msg;
int msglen;
- const char *subid;
- int subid_len;
};
+
// To-client event
struct ndb_tce {
int evtype;
+ const char *subid;
+ int subid_len;
+
union {
struct ndb_event event;
struct ndb_command_result command_result;
diff --git a/test.c b/test.c
@@ -246,6 +246,36 @@ static void test_strings_work_before_finalization() {
assert(!strcmp(ndb_note_str(b->note, &b->note->content).str, "hello"));
}
+static void test_tce_eose() {
+ unsigned char buf[1024];
+ const char json[] = "[\"EOSE\",\"s\"]";
+ struct ndb_tce tce;
+ int ok;
+
+ ok = ndb_ws_event_from_json(json, sizeof(json), &tce, buf, sizeof(buf));
+ assert(ok);
+
+ assert(tce.evtype == NDB_TCE_EOSE);
+ assert(tce.command_result.ok == 1);
+ assert(tce.subid_len == 1);
+ assert(!memcmp(tce.subid, "s", 1));
+}
+
+static void test_tce_command_result() {
+ unsigned char buf[1024];
+ const char json[] = "[\"OK\",\"\",true,\"blocked: ok\"]";
+ struct ndb_tce tce;
+ int ok;
+
+ ok = ndb_ws_event_from_json(json, sizeof(json), &tce, buf, sizeof(buf));
+ assert(ok);
+
+ assert(tce.evtype == NDB_TCE_OK);
+ assert(tce.subid_len == 0);
+ assert(tce.command_result.ok == 1);
+ assert(!memcmp(tce.subid, "", 0));
+}
+
// test to-client event
static void test_tce() {
@@ -261,8 +291,8 @@ static void test_tce() {
assert(ok);
assert(tce.evtype == NDB_TCE_EVENT);
- assert(tce.event.subid_len == 8);
- assert(!memcmp(tce.event.subid, "subid123", 8));
+ assert(tce.subid_len == 8);
+ assert(!memcmp(tce.subid, "subid123", 8));
#undef HEX_ID
#undef HEX_PK
@@ -276,4 +306,6 @@ int main(int argc, const char *argv[]) {
test_parse_contact_list();
test_strings_work_before_finalization();
test_tce();
+ test_tce_command_result();
+ test_tce_eose();
}