Nostr/Community.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Nostr/Channel.pm ↑ Summary Nostr/DVM.pm »
1 package Net::Nostr::Community;
2  
3 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Net::Nostr::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Net::Nostr::Event;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 use Class::Tiny qw(
12 identifier
13 name
14 description
15 image
16 moderators
17 relays
18 communities
19 post_id
20 post_coordinate
21 post_author
22 post_kind
23 );
24  
25 my $JSON = JSON->new->utf8->canonical;
26 my $HEX64 = qr/\A[0-9a-f]{64}\z/;
27  
28 sub new {
29 my $class = shift;
30 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
31 $args{moderators} //= [];
32 $args{relays} //= [];
33 $args{communities} //= [];
34 my $self = bless { %args }, $class;
35 my %known; @known{Class::Tiny->get_all_attributes_for($class)} = ();
36 my @unknown = grep { !exists $known{$_} } keys %$self;
37 croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
38 return $self;
39 }
40  
41 sub community {
42 my $class = shift;
43 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
44  
45 my $pubkey = $args{pubkey} // croak "community requires 'pubkey'";
46 my $identifier = $args{identifier} // croak "community requires 'identifier'";
47  
48 my @tags;
49 push @tags, ['d', $identifier];
50  
51 push @tags, ['name', $args{name}] if defined $args{name};
52 push @tags, ['description', $args{description}] if defined $args{description};
53  
54 if ($args{image}) {
55 my @img = ('image', @{$args{image}});
56 push @tags, \@img;
57 }
58  
59 for my $mod (@{$args{moderators} // []}) {
60 croak "moderator pubkey must be 64-char lowercase hex" unless $mod->{pubkey} =~ $HEX64;
61 push @tags, ['p', $mod->{pubkey}, $mod->{relay} // '', 'moderator'];
62 }
63  
64 for my $r (@{$args{relays} // []}) {
65 my @tag = ('relay', $r->{url});
66 push @tag, $r->{marker} if defined $r->{marker};
67 push @tags, \@tag;
68 }
69  
70 for my $t (@{$args{extra_tags} // []}) {
71 push @tags, $t;
72 }
73  
74 return Net::Nostr::Event->new(
75 kind => 34550,
76 pubkey => $pubkey,
77 content => '',
78 tags => \@tags,
79 (defined $args{created_at} ? (created_at => $args{created_at}) : ()),
80 );
81 }
82  
83 sub post {
84 my $class = shift;
85 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
86  
87 my $pubkey = $args{pubkey} // croak "post requires 'pubkey'";
88 my $content = $args{content} // croak "post requires 'content'";
89 my $cpk = $args{community_pubkey} // croak "post requires 'community_pubkey'";
90 my $cd = $args{community_d} // croak "post requires 'community_d'";
91  
92 croak "community_pubkey must be 64-char lowercase hex" unless $cpk =~ $HEX64;
93  
94 my $coord = "34550:$cpk:$cd";
95 my $relay = $args{relay};
96  
97 my @tags;
98 # Top-level: uppercase and lowercase both point to community
99 if (defined $relay) {
100 push @tags, ['A', $coord, $relay];
101 push @tags, ['a', $coord, $relay];
102 push @tags, ['P', $cpk, $relay];
103 push @tags, ['p', $cpk, $relay];
104 } else {
105 push @tags, ['A', $coord];
106 push @tags, ['a', $coord];
107 push @tags, ['P', $cpk];
108 push @tags, ['p', $cpk];
109 }
110 push @tags, ['K', '34550'];
111 push @tags, ['k', '34550'];
112  
113 return Net::Nostr::Event->new(
114 kind => 1111,
115 pubkey => $pubkey,
116 content => $content,
117 tags => \@tags,
118 (defined $args{created_at} ? (created_at => $args{created_at}) : ()),
119 );
120 }
121  
122 sub reply {
123 my $class = shift;
124 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
125  
126 my $pubkey = $args{pubkey} // croak "reply requires 'pubkey'";
127 my $content = $args{content} // croak "reply requires 'content'";
128 my $cpk = $args{community_pubkey} // croak "reply requires 'community_pubkey'";
129 my $cd = $args{community_d} // croak "reply requires 'community_d'";
130 my $pid = $args{parent_id} // croak "reply requires 'parent_id'";
131 my $ppk = $args{parent_pubkey} // croak "reply requires 'parent_pubkey'";
132 my $pkind = $args{parent_kind} // croak "reply requires 'parent_kind'";
133  
134 croak "community_pubkey must be 64-char lowercase hex" unless $cpk =~ $HEX64;
135 croak "parent_pubkey must be 64-char lowercase hex" unless $ppk =~ $HEX64;
136  
137 my $coord = "34550:$cpk:$cd";
138 my $relay = $args{relay};
139  
140 my @tags;
141 # Uppercase tags: community definition (root scope)
142 if (defined $relay) {
143 push @tags, ['A', $coord, $relay];
144 push @tags, ['P', $cpk, $relay];
145 } else {
146 push @tags, ['A', $coord];
147 push @tags, ['P', $cpk];
148 }
149 push @tags, ['K', '34550'];
150  
151 # Lowercase tags: parent post/reply
152 if (defined $relay) {
153 push @tags, ['e', $pid, $relay];
154 push @tags, ['p', $ppk, $relay];
155 } else {
156 push @tags, ['e', $pid];
157 push @tags, ['p', $ppk];
158 }
159 push @tags, ['k', $pkind];
160  
161 return Net::Nostr::Event->new(
162 kind => 1111,
163 pubkey => $pubkey,
164 content => $content,
165 tags => \@tags,
166 (defined $args{created_at} ? (created_at => $args{created_at}) : ()),
167 );
168 }
169  
170 sub approval {
171 my $class = shift;
172 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
173  
174 my $pubkey = $args{pubkey} // croak "approval requires 'pubkey'";
175 my $post = $args{post} // croak "approval requires 'post'";
176  
177 my @tags;
178  
179 # Community a tags
180 if ($args{communities}) {
181 for my $c (@{$args{communities}}) {
182 croak "community pubkey must be 64-char lowercase hex" unless $c->{pubkey} =~ $HEX64;
183 my $coord = "34550:$c->{pubkey}:$c->{d}";
184 my @tag = ('a', $coord);
185 push @tag, $c->{relay} if defined $c->{relay};
186 push @tags, \@tag;
187 }
188 } else {
189 my $cpk = $args{community_pubkey} // croak "approval requires 'community_pubkey'";
190 my $cd = $args{community_d} // croak "approval requires 'community_d'";
191 croak "community_pubkey must be 64-char lowercase hex" unless $cpk =~ $HEX64;
192 my $coord = "34550:$cpk:$cd";
193 my @tag = ('a', $coord);
194 push @tag, $args{relay} if defined $args{relay};
195 push @tags, \@tag;
196 }
197  
198 # Post reference: e tag, a tag, or both
199 my $via = $args{approve_via} // 'e';
200  
201 if ($via eq 'e' || $via eq 'both') {
202 my @e = ('e', $post->id);
203 push @e, $args{relay} if defined $args{relay};
204 push @tags, \@e;
205 }
206 if (($via eq 'a' || $via eq 'both') && $post->is_addressable) {
207 my $post_coord = $post->kind . ':' . $post->pubkey . ':' . $post->d_tag;
208 my @a = ('a', $post_coord);
209 push @a, $args{relay} if defined $args{relay};
210 push @tags, \@a;
211 }
212  
213 # Post author p tag
214 my @p = ('p', $post->pubkey);
215 push @p, $args{relay} if defined $args{relay};
216 push @tags, \@p;
217  
218 # Post kind k tag
219 push @tags, ['k', '' . $post->kind];
220  
221 # Content SHOULD be JSON-stringified post
222 my $content = $JSON->encode($post->to_hash);
223  
224 return Net::Nostr::Event->new(
225 kind => 4550,
226 pubkey => $pubkey,
227 content => $content,
228 tags => \@tags,
229 (defined $args{created_at} ? (created_at => $args{created_at}) : ()),
230 );
231 }
232  
233 sub from_event {
234 my ($class, $event) = @_;
235  
236 if ($event->kind == 34550) {
237 return $class->_parse_community($event);
238 } elsif ($event->kind == 4550) {
239 return $class->_parse_approval($event);
240 }
241  
242 return undef;
243 }
244  
245 sub _parse_community {
246 my ($class, $event) = @_;
247  
248 my ($identifier, $name, $description, @image, @moderators, @relays);
249  
250 for my $tag (@{$event->tags}) {
251 my $t = $tag->[0];
252 if ($t eq 'd') {
253 $identifier = $tag->[1];
254 } elsif ($t eq 'name') {
255 $name = $tag->[1];
256 } elsif ($t eq 'description') {
257 $description = $tag->[1];
258 } elsif ($t eq 'image') {
259 @image = @{$tag}[1 .. $#$tag];
260 } elsif ($t eq 'p' && defined $tag->[3] && $tag->[3] eq 'moderator') {
261 my %mod = (pubkey => $tag->[1]);
262 $mod{relay} = $tag->[2] if defined $tag->[2] && $tag->[2] ne '';
263 push @moderators, \%mod;
264 } elsif ($t eq 'relay') {
265 my %r = (url => $tag->[1]);
266 $r{marker} = $tag->[2] if defined $tag->[2];
267 push @relays, \%r;
268 }
269 }
270  
271 return $class->new(
272 identifier => $identifier,
273 name => $name,
274 description => $description,
275 image => @image ? \@image : undef,
276 moderators => \@moderators,
277 relays => \@relays,
278 );
279 }
280  
281 sub _parse_approval {
282 my ($class, $event) = @_;
283  
284 my (@communities, $post_id, $post_coord, $post_author, $post_kind);
285  
286 for my $tag (@{$event->tags}) {
287 my $t = $tag->[0];
288 if ($t eq 'a' && defined $tag->[1] && $tag->[1] =~ /^34550:/) {
289 push @communities, $tag->[1];
290 } elsif ($t eq 'e') {
291 $post_id = $tag->[1];
292 } elsif ($t eq 'a' && defined $tag->[1] && $tag->[1] !~ /^34550:/) {
293 $post_coord = $tag->[1];
294 } elsif ($t eq 'p') {
295 $post_author //= $tag->[1];
296 } elsif ($t eq 'k') {
297 $post_kind = $tag->[1];
298 }
299 }
300  
301 return $class->new(
302 communities => \@communities,
303 post_id => $post_id,
304 post_coordinate => $post_coord,
305 post_author => $post_author,
306 post_kind => $post_kind,
307 );
308 }
309  
310 sub validate {
311 my ($class, $event) = @_;
312  
313 croak "community event MUST be kind 34550 or 4550"
314 unless $event->kind == 34550 || $event->kind == 4550;
315  
316 if ($event->kind == 34550) {
317 my $has_d = grep { $_->[0] eq 'd' } @{$event->tags};
318 croak "community MUST include a d tag" unless $has_d;
319 }
320  
321 if ($event->kind == 4550) {
322 my $has_community = grep {
323 $_->[0] eq 'a' && defined $_->[1] && $_->[1] =~ /^34550:/
324 } @{$event->tags};
325 croak "approval MUST include a community a tag" unless $has_community;
326  
327 my $has_post = grep {
328 $_->[0] eq 'e' || ($_->[0] eq 'a' && defined $_->[1] && $_->[1] !~ /^34550:/)
329 } @{$event->tags};
330 croak "approval MUST include an e or a tag for the post" unless $has_post;
331  
332 my $has_p = grep { $_->[0] eq 'p' } @{$event->tags};
333 croak "approval MUST include a p tag for the post author" unless $has_p;
334 }
335  
336 return 1;
337 }
338  
339 sub community_filter {
340 my $class = shift;
341 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
342 my %filter = (kinds => [34550]);
343 $filter{'#d'} = $args{identifiers} if $args{identifiers};
344 $filter{authors} = $args{authors} if $args{authors};
345 return \%filter;
346 }
347  
348 sub approval_filter {
349 my $class = shift;
350 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
351 my %filter = (kinds => [4550]);
352 $filter{'#a'} = [$args{community}] if $args{community};
353 $filter{authors} = $args{authors} if $args{authors};
354 return \%filter;
355 }
356  
357 sub legacy_post_filter {
358 my $class = shift;
359 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
360 return {
361 kinds => [1],
362 '#a' => [$args{community}],
363 };
364 }
365  
366 1;
367  
368 __END__
« Nostr/Channel.pm ↑ Summary Nostr/DVM.pm »