RelayMonitor.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« RelayAccess.pm ↑ Summary Report.pm »
1 package Net::Nostr::RelayMonitor;
2  
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Net::Nostr::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Net::Nostr::Event;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 use Class::Tiny qw(
11 relay_url
12 network
13 relay_type
14 nips
15 requirements
16 topics
17 kinds
18 geohash
19 languages
20 rtt_open
21 rtt_read
22 rtt_write
23 nip11
24 frequency
25 timeouts
26 checks
27 );
28  
29 sub new {
30 my $class = shift;
31 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
32 $args{nips} //= [];
33 $args{requirements} //= [];
34 $args{topics} //= [];
35 $args{kinds} //= [];
36 $args{languages} //= [];
37 $args{timeouts} //= [];
38 $args{checks} //= [];
39 my $self = bless { %args }, $class;
40 my %known; @known{Class::Tiny->get_all_attributes_for($class)} = ();
41 my @unknown = grep { !exists $known{$_} } keys %$self;
42 croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
43 return $self;
44 }
45  
46 sub discovery_event {
47 my $class = shift;
48 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
49  
50 my $relay_url = delete $args{relay_url}
51 // croak "discovery_event requires 'relay_url'";
52 my $nip11 = delete $args{nip11};
53  
54 my @tags;
55 push @tags, ['d', $relay_url];
56  
57 # Single-value optional tags
58 for my $pair (
59 [network => 'n'],
60 [relay_type => 'T'],
61 [geohash => 'g'],
62 [rtt_open => 'rtt-open'],
63 [rtt_read => 'rtt-read'],
64 [rtt_write => 'rtt-write'],
65 ) {
66 my ($key, $tag_name) = @$pair;
67 my $val = delete $args{$key};
68 push @tags, [$tag_name, $val] if defined $val;
69 }
70  
71 # Repeated tags
72 for my $pair (
73 [nips => 'N'],
74 [requirements => 'R'],
75 [topics => 't'],
76 [kinds => 'k'],
77 ) {
78 my ($key, $tag_name) = @$pair;
79 my $vals = delete $args{$key};
80 next unless $vals;
81 for my $v (@$vals) {
82 push @tags, [$tag_name, "$v"];
83 }
84 }
85  
86 # Language tags with ISO-639-1 namespace
87 my $langs = delete $args{languages};
88 if ($langs) {
89 for my $lang (@$langs) {
90 push @tags, ['l', $lang, 'ISO-639-1'];
91 }
92 }
93  
94 return Net::Nostr::Event->new(
95 %args,
96 kind => 30166,
97 content => $nip11 // '',
98 tags => \@tags,
99 );
100 }
101  
102 sub announcement_event {
103 my $class = shift;
104 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
105  
106 my $frequency = delete $args{frequency}
107 // croak "announcement_event requires 'frequency'";
108  
109 my @tags;
110  
111 # Timeout tags
112 my $timeouts = delete $args{timeouts};
113 if ($timeouts) {
114 for my $t (@$timeouts) {
115 if (defined $t->{test}) {
116 push @tags, ['timeout', $t->{test}, $t->{ms}];
117 } else {
118 push @tags, ['timeout', $t->{ms}];
119 }
120 }
121 }
122  
123 push @tags, ['frequency', "$frequency"];
124  
125 # Checks (repeated c tags)
126 my $checks = delete $args{checks};
127 if ($checks) {
128 for my $c (@$checks) {
129 push @tags, ['c', $c];
130 }
131 }
132  
133 # Geohash
134 my $geohash = delete $args{geohash};
135 push @tags, ['g', $geohash] if defined $geohash;
136  
137 return Net::Nostr::Event->new(
138 %args,
139 kind => 10166,
140 content => '',
141 tags => \@tags,
142 );
143 }
144  
145 sub from_event {
146 my ($class, $event) = @_;
147 my $kind = $event->kind;
148  
149 if ($kind == 30166) {
150 return $class->_from_discovery($event);
151 } elsif ($kind == 10166) {
152 return $class->_from_announcement($event);
153 }
154 return undef;
155 }
156  
157 sub _from_discovery {
158 my ($class, $event) = @_;
159  
160 my %attrs;
161 my (@nips, @reqs, @topics, @kinds, @langs);
162  
163 for my $tag (@{$event->tags}) {
164 my $name = $tag->[0];
165 if ($name eq 'd') { $attrs{relay_url} = $tag->[1] }
166 elsif ($name eq 'n') { $attrs{network} = $tag->[1] }
167 elsif ($name eq 'T') { $attrs{relay_type} = $tag->[1] }
168 elsif ($name eq 'g') { $attrs{geohash} = $tag->[1] }
169 elsif ($name eq 'rtt-open') { $attrs{rtt_open} = $tag->[1] }
170 elsif ($name eq 'rtt-read') { $attrs{rtt_read} = $tag->[1] }
171 elsif ($name eq 'rtt-write'){ $attrs{rtt_write} = $tag->[1] }
172 elsif ($name eq 'N') { push @nips, $tag->[1] }
173 elsif ($name eq 'R') { push @reqs, $tag->[1] }
174 elsif ($name eq 't') { push @topics, $tag->[1] }
175 elsif ($name eq 'k') { push @kinds, $tag->[1] }
176 elsif ($name eq 'l') { push @langs, $tag->[1] }
177 }
178  
179 my $content = $event->content;
180 $attrs{nip11} = $content if defined $content && length $content;
181  
182 return $class->new(
183 %attrs,
184 nips => \@nips,
185 requirements => \@reqs,
186 topics => \@topics,
187 kinds => \@kinds,
188 languages => \@langs,
189 );
190 }
191  
192 sub _from_announcement {
193 my ($class, $event) = @_;
194  
195 my %attrs;
196 my (@timeouts, @checks);
197  
198 for my $tag (@{$event->tags}) {
199 my $name = $tag->[0];
200 if ($name eq 'frequency') {
201 $attrs{frequency} = $tag->[1];
202 } elsif ($name eq 'timeout') {
203 if (defined $tag->[2]) {
204 push @timeouts, { test => $tag->[1], ms => $tag->[2] };
205 } else {
206 push @timeouts, { ms => $tag->[1] };
207 }
208 } elsif ($name eq 'c') {
209 push @checks, $tag->[1];
210 } elsif ($name eq 'g') {
211 $attrs{geohash} = $tag->[1];
212 }
213 }
214  
215 return $class->new(
216 %attrs,
217 timeouts => \@timeouts,
218 checks => \@checks,
219 );
220 }
221  
222 sub validate {
223 my ($class, $event) = @_;
224 my $kind = $event->kind;
225  
226 if ($kind == 30166) {
227 my $has_d;
228 for my $tag (@{$event->tags}) {
229 $has_d = 1 if $tag->[0] eq 'd';
230 }
231 croak "discovery event MUST have a 'd' tag set to the relay URL"
232 unless $has_d;
233 return 1;
234 }
235  
236 if ($kind == 10166) {
237 my $has_freq;
238 for my $tag (@{$event->tags}) {
239 $has_freq = 1 if $tag->[0] eq 'frequency';
240 }
241 croak "announcement event MUST have a 'frequency' tag"
242 unless $has_freq;
243 return 1;
244 }
245  
246 croak "event kind must be 30166 (discovery) or 10166 (announcement)";
247 }
248  
249 1;
250  
251 __END__
« RelayAccess.pm ↑ Summary Report.pm »