| 1 | package Net::Nostr::Client; | ||
| 2 | |||
| 3 | 3 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 3 | ▶ | use Net::Nostr::_ConstructorArgs (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 3 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use Scalar::Util qw(weaken); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use AnyEvent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 3 | ▶ | use AnyEvent::WebSocket::Client; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 3 | ▶ | use Net::Nostr::Message; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | use Class::Tiny qw( | ||
| 14 | _ws_client | ||
| 15 | _conn | ||
| 16 | _callbacks | ||
| 17 | challenge | ||
| 18 | ); | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $class = shift; | ||
| 22 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 23 | my %allowed = map { $_ => 1 } qw(ssl_no_verify ssl_ca_file); | ||
| 24 | my @unknown = grep { !$allowed{$_} } keys %args; | ||
| 25 | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||
| 26 | my $self = bless {}, $class; | ||
| 27 | $self->_ws_client(AnyEvent::WebSocket::Client->new(%args)); | ||
| 28 | $self->_callbacks({}); | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub connect { | ||
| 33 | my ($self, $url, $cb) = @_; | ||
| 34 | croak "url is required" unless defined $url; | ||
| 35 | croak "callback must be a CODE ref" if defined $cb && ref($cb) ne 'CODE'; | ||
| 36 | |||
| 37 | my $cv = AnyEvent->condvar; | ||
| 38 | $self->_ws_client->connect($url)->cb(sub { | ||
| 39 | my $conn = eval { shift->recv }; | ||
| 40 | if ($@) { | ||
| 41 | $cv->croak("connect failed: $@"); | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | $self->_conn($conn); | ||
| 45 | $self->_setup_handlers; | ||
| 46 | $cv->send; | ||
| 47 | }); | ||
| 48 | |||
| 49 | if ($cb) { | ||
| 50 | $cv->cb(sub { eval { shift->recv }; $cb->($@ || undef) }); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | $cv->recv; | ||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub is_connected { | ||
| 59 | my ($self) = @_; | ||
| 60 | return defined $self->_conn ? 1 : 0; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub disconnect { | ||
| 64 | my ($self) = @_; | ||
| 65 | if ($self->_conn) { | ||
| 66 | $self->_conn->close; | ||
| 67 | $self->_conn(undef); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub publish { | ||
| 72 | my ($self, $event) = @_; | ||
| 73 | croak "not connected" unless $self->is_connected; | ||
| 74 | my $msg = Net::Nostr::Message->new(type => 'EVENT', event => $event); | ||
| 75 | $self->_conn->send($msg->serialize); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub subscribe { | ||
| 79 | my ($self, $sub_id, @filters) = @_; | ||
| 80 | croak "not connected" unless $self->is_connected; | ||
| 81 | my $msg = Net::Nostr::Message->new( | ||
| 82 | type => 'REQ', | ||
| 83 | subscription_id => $sub_id, | ||
| 84 | filters => \@filters, | ||
| 85 | ); | ||
| 86 | $self->_conn->send($msg->serialize); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub count { | ||
| 90 | my ($self, $sub_id, @filters) = @_; | ||
| 91 | croak "not connected" unless $self->is_connected; | ||
| 92 | my $msg = Net::Nostr::Message->new( | ||
| 93 | type => 'COUNT', | ||
| 94 | subscription_id => $sub_id, | ||
| 95 | filters => \@filters, | ||
| 96 | ); | ||
| 97 | $self->_conn->send($msg->serialize); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub close { | ||
| 101 | my ($self, $sub_id) = @_; | ||
| 102 | croak "not connected" unless $self->is_connected; | ||
| 103 | my $msg = Net::Nostr::Message->new(type => 'CLOSE', subscription_id => $sub_id); | ||
| 104 | $self->_conn->send($msg->serialize); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub neg_open { | ||
| 108 | my ($self, $sub_id, $filter, $initial_msg) = @_; | ||
| 109 | croak "not connected" unless $self->is_connected; | ||
| 110 | my $msg = Net::Nostr::Message->new( | ||
| 111 | type => 'NEG-OPEN', | ||
| 112 | subscription_id => $sub_id, | ||
| 113 | filter => $filter, | ||
| 114 | neg_msg => $initial_msg, | ||
| 115 | ); | ||
| 116 | $self->_conn->send($msg->serialize); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub neg_msg { | ||
| 120 | my ($self, $sub_id, $neg_msg) = @_; | ||
| 121 | croak "not connected" unless $self->is_connected; | ||
| 122 | my $msg = Net::Nostr::Message->new( | ||
| 123 | type => 'NEG-MSG', | ||
| 124 | subscription_id => $sub_id, | ||
| 125 | neg_msg => $neg_msg, | ||
| 126 | ); | ||
| 127 | $self->_conn->send($msg->serialize); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub neg_close { | ||
| 131 | my ($self, $sub_id) = @_; | ||
| 132 | croak "not connected" unless $self->is_connected; | ||
| 133 | my $msg = Net::Nostr::Message->new( | ||
| 134 | type => 'NEG-CLOSE', | ||
| 135 | subscription_id => $sub_id, | ||
| 136 | ); | ||
| 137 | $self->_conn->send($msg->serialize); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub authenticate { | ||
| 141 | my ($self, $key, $relay_url) = @_; | ||
| 142 | croak "not connected" unless $self->is_connected; | ||
| 143 | croak "key is required" unless $key; | ||
| 144 | croak "relay_url is required" unless $relay_url; | ||
| 145 | croak "no challenge received" unless defined $self->challenge; | ||
| 146 | |||
| 147 | my $auth_event = $key->create_event( | ||
| 148 | kind => 22242, | ||
| 149 | content => '', | ||
| 150 | tags => [ | ||
| 151 | ['relay', $relay_url], | ||
| 152 | ['challenge', $self->challenge], | ||
| 153 | ], | ||
| 154 | ); | ||
| 155 | my $msg = Net::Nostr::Message->new(type => 'AUTH', event => $auth_event); | ||
| 156 | $self->_conn->send($msg->serialize); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub on { | ||
| 160 | my ($self, $type, $cb) = @_; | ||
| 161 | croak "callback must be a CODE ref" unless defined $cb && ref($cb) eq 'CODE'; | ||
| 162 | $self->_callbacks->{$type} = $cb; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub _emit { | ||
| 166 | my ($self, $type, @args) = @_; | ||
| 167 | my $cb = $self->_callbacks->{$type} or return; | ||
| 168 | eval { $cb->(@args); 1 } or warn "callback '$type' died: $@"; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _setup_handlers { | ||
| 172 | my ($self) = @_; | ||
| 173 | weaken(my $weak_self = $self); | ||
| 174 | $self->_conn->on(each_message => sub { | ||
| 175 | my ($conn, $message) = @_; | ||
| 176 | my $self = $weak_self or return; | ||
| 177 | my $msg = eval { Net::Nostr::Message->parse($message->body) }; | ||
| 178 | return warn "bad message from relay: $@\n" if $@; | ||
| 179 | |||
| 180 | if ($msg->type eq 'EVENT') { | ||
| 181 | $self->_emit('event', $msg->subscription_id, $msg->event); | ||
| 182 | } elsif ($msg->type eq 'OK') { | ||
| 183 | $self->_emit('ok', $msg->event_id, $msg->accepted, $msg->message); | ||
| 184 | } elsif ($msg->type eq 'EOSE') { | ||
| 185 | $self->_emit('eose', $msg->subscription_id); | ||
| 186 | } elsif ($msg->type eq 'NOTICE') { | ||
| 187 | $self->_emit('notice', $msg->message); | ||
| 188 | } elsif ($msg->type eq 'COUNT') { | ||
| 189 | $self->_emit('count', $msg->subscription_id, $msg->count, $msg->approximate); | ||
| 190 | } elsif ($msg->type eq 'CLOSED') { | ||
| 191 | $self->_emit('closed', $msg->subscription_id, $msg->message); | ||
| 192 | } elsif ($msg->type eq 'AUTH') { | ||
| 193 | $self->challenge($msg->challenge); | ||
| 194 | $self->_emit('auth', $msg->challenge); | ||
| 195 | } elsif ($msg->type eq 'NEG-MSG') { | ||
| 196 | $self->_emit('neg_msg', $msg->subscription_id, $msg->neg_msg); | ||
| 197 | } elsif ($msg->type eq 'NEG-ERR') { | ||
| 198 | $self->_emit('neg_err', $msg->subscription_id, $msg->message); | ||
| 199 | } | ||
| 200 | }); | ||
| 201 | |||
| 202 | $self->_conn->on(finish => sub { | ||
| 203 | my $self = $weak_self or return; | ||
| 204 | $self->_conn(undef); | ||
| 205 | }); | ||
| 206 | } | ||
| 207 | |||
| 208 | 1; | ||
| 209 | |||
| 210 | __END__ | ||