Skip to content

Commit

Permalink
feat: publish event
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteCat6142 committed Aug 16, 2024
1 parent 87068f7 commit 2580d03
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
34 changes: 21 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ int main(int argc, char *argv[])
char *message = "Sample Message";

std::cout << sha256(message, strlen(message)) << std::endl;
/*

NostrRelayLibhv relay;
NostrRelaySimple rx(&relay);
relay.connect("wss://relay-jp.nostr.wirednet.jp/");
NostrSubscription subx;
rx.subscribe([](NostrEvent &ev)
{
if(ev.kind!=1)
return;
NostrEventYYJSON i(&ev);
std::cout << i.encode() << std::endl; }, subx);

if (argc > 1)
{
auto e = sign(argv[1]);
Expand All @@ -74,23 +85,20 @@ int main(int argc, char *argv[])
NostrEventYYJSON i(&e2);
auto y = i.encode();
std::cout << e << std::endl;
std::cout << y << std::endl;
}*/
auto f = rx.publish(e2.id, e);
auto r = f.wait_for(100ms);
if (r==std::future_status::timeout)
std::cout << "timeouted" << std::endl;
bool result = f.get();
std::cout << result << std::endl;
}

auto knostr = R"({"id":"93dc70f965af436095ba1d60d5c66ee235fdf82222e78238022317d73a95565f","pubkey":"6a36c1a62cba047b1cdb93bef316c6617c79816e32b80166c471c30bdb77e526","created_at":1723649902,"kind":1,"tags":[],"content":"test","sig":"8d08e7ec3134288fee4db14842652771f765ecbd7676c442529c9ea9ff4861153919559a7757a77593525774a44ccf15fc2af982b6501ca4f34bf3454ca57f11"})";
auto e3 = NostrEventYYJSON::decode(knostr);
std::cout << NostrEventYYJSON::verify_event(e3) << std::endl;

std::cout << "t"<< std::endl;
NostrRelayLibhv relay;
NostrRelaySimple rx(&relay);
relay.connect("wss://relay-jp.nostr.wirednet.jp/");
NostrSubscription subx;
rx.subscribe([](NostrEvent &ev){
std::cout << "t"<< std::endl;
NostrEventYYJSON i(&ev);
std::cout << i.encode() << std::endl;
},subx);
std::cout << "t" << std::endl;

std::this_thread::sleep_for(20000ms);
/*
Expand Down
2 changes: 1 addition & 1 deletion src/nostr_relay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace cpp_nostr
public:
virtual NostrEventSubId subscribe(NostrEventCallback callback, const NostrSubscription &sub) = 0;
virtual bool unsubscribe(const NostrEventSubId id) = 0;
virtual std::future<bool> publish(const std::string &ev) = 0;
virtual std::future<bool> publish(const std::string &id, const std::string &ev) = 0;
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/nostr_relay_libhv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace cpp_nostr
}

int res = ws->send(str);
return (res==0);
return (res!=-1);
}

bool unsubscribe()
Expand Down
26 changes: 17 additions & 9 deletions src/nostr_relay_simple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <functional>
#include <future>
#include <unordered_map>
#include <utility>

using namespace yyjson;

Expand All @@ -25,7 +26,7 @@ namespace cpp_nostr
private:
NostrRelayInterface *relay;
std::unordered_map<NostrEventSubId, NostrEventCallback> list;
std::unordered_map<std::string, std::promise<bool>> pub_list;
std::unordered_map<std::string, std::promise<bool>*> pub_list;

public:
NostrRelaySimple(NostrRelayInterface *relay_) : relay(relay_)
Expand All @@ -51,15 +52,21 @@ namespace cpp_nostr
NostrEventCallback &fn = this->list.at(sub_id);
++iter;
NostrEvent ev = cast<NostrEvent>(*iter);
if(NostrEventYYJSON::verify_event(ev))
fn(ev);
else
std::cout << "invalid sig" <<std::endl;
}
if(t=="OK")
{
++iter;
std::string id = std::string(*(*iter).as_string());
//std::promise<bool> &pr = pub_list.at(id);
//pr.set_value(true);
//pub_list.erase(id);
std::cout << id <<std::endl;
std::cout << (this->pub_list.size()) <<std::endl;
std::promise<bool> *pr = this->pub_list.at(id);
pr->set_value(true);
pub_list.erase(id);
delete pr;
} });
}
NostrEventSubId subscribe(NostrEventCallback callback, const NostrSubscription &sub) override
Expand All @@ -68,23 +75,24 @@ namespace cpp_nostr

std::string s = NostrRelayUtils::makeSubscribeCommand(sub_id, sub);
relay->send(s);
list.emplace(sub_id, callback);
list.insert_or_assign(sub_id, callback);
return sub_id;
}
bool unsubscribe(const NostrEventSubId sub_id) override
{
relay->send(NostrRelayUtils::makeUnsubscribeCommand(sub_id));
return true;
}
std::future<bool> publish(const std::string &ev)
std::future<bool> publish(const std::string &id, const std::string &ev)
{
std::promise<bool> pr;
std::future<bool> ft = pr.get_future();
std::promise<bool> *pr = new std::promise<bool>();
std::future<bool> ft = pr->get_future();
if (!relay->send(NostrRelayUtils::makePublishCommand(ev)))
{
pr.set_value(false);
pr->set_value(false);
return ft;
}
pub_list.insert_or_assign(std::string(id), pr);
return ft;
}
};
Expand Down

0 comments on commit 2580d03

Please sign in to comment.