ClassifiedListing.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
« Calendar.pm ↑ Summary Deletion.pm »
1 package Net::Nostr::ClassifiedListing;
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 use Net::Nostr::Bech32 qw(encode_naddr);
10  
11 use Class::Tiny qw(
12 identifier
13 title
14 summary
15 published_at
16 location
17 price
18 status
19 images
20 hashtags
21 );
22  
23 sub new {
24 my $class = shift;
25 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
26 $args{hashtags} //= [];
27 $args{images} //= [];
28 my $self = bless { %args }, $class;
29 my %known; @known{Class::Tiny->get_all_attributes_for($class)} = ();
30 my @unknown = grep { !exists $known{$_} } keys %$self;
31 croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
32 return $self;
33 }
34  
35 sub _build_event {
36 my $class = shift;
37 my $kind = shift;
38 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
39  
40 my $pubkey = $args{pubkey} // croak "listing requires 'pubkey'";
41 my $content = $args{content} // croak "listing requires 'content'";
42 my $identifier = $args{identifier} // croak "listing requires 'identifier'";
43  
44 my @tags;
45 push @tags, ['d', $identifier];
46  
47 if (defined $args{title}) {
48 push @tags, ['title', $args{title}];
49 }
50 if (defined $args{summary}) {
51 push @tags, ['summary', $args{summary}];
52 }
53 if (defined $args{published_at}) {
54 push @tags, ['published_at', '' . $args{published_at}];
55 }
56 if (defined $args{location}) {
57 push @tags, ['location', $args{location}];
58 }
59 if (defined $args{price}) {
60 push @tags, ['price', @{$args{price}}];
61 }
62 if (defined $args{status}) {
63 push @tags, ['status', $args{status}];
64 }
65 for my $img (@{$args{images} // []}) {
66 push @tags, ['image', @$img];
67 }
68 for my $ht (@{$args{hashtags} // []}) {
69 push @tags, ['t', $ht];
70 }
71 for my $tag (@{$args{extra_tags} // []}) {
72 push @tags, $tag;
73 }
74  
75 delete @args{qw(identifier title summary published_at location price status images hashtags extra_tags)};
76 return Net::Nostr::Event->new(%args, kind => $kind, tags => \@tags);
77 }
78  
79 sub listing {
80 my $class = shift;
81 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
82 return $class->_build_event(30402, %args);
83 }
84  
85 sub draft {
86 my $class = shift;
87 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
88 return $class->_build_event(30403, %args);
89 }
90  
91 sub from_event {
92 my ($class, $event) = @_;
93 return undef unless $event->kind == 30402 || $event->kind == 30403;
94  
95 my ($identifier, $title, $summary, $published_at, $location, $status);
96 my @price;
97 my @images;
98 my @hashtags;
99  
100 for my $tag (@{$event->tags}) {
101 my $name = $tag->[0];
102 if ($name eq 'd') { $identifier = $tag->[1] // '' }
103 elsif ($name eq 'title') { $title = $tag->[1] }
104 elsif ($name eq 'summary') { $summary = $tag->[1] }
105 elsif ($name eq 'published_at') { $published_at = $tag->[1] }
106 elsif ($name eq 'location') { $location = $tag->[1] }
107 elsif ($name eq 'price') { @price = @{$tag}[1..$#$tag] }
108 elsif ($name eq 'status') { $status = $tag->[1] }
109 elsif ($name eq 'image') { push @images, [@{$tag}[1..$#$tag]] }
110 elsif ($name eq 't') { push @hashtags, $tag->[1] }
111 }
112  
113 return $class->new(
114 identifier => $identifier,
115 title => $title,
116 summary => $summary,
117 published_at => $published_at,
118 location => $location,
119 price => (@price ? \@price : undef),
120 status => $status,
121 images => \@images,
122 hashtags => \@hashtags,
123 );
124 }
125  
126 sub validate {
127 my ($class, $event) = @_;
128  
129 croak "classified listing MUST be kind 30402 or 30403"
130 unless $event->kind == 30402 || $event->kind == 30403;
131  
132 my $has_d = grep { $_->[0] eq 'd' } @{$event->tags};
133 croak "classified listing MUST include a d tag" unless $has_d;
134  
135 return 1;
136 }
137  
138 sub to_naddr {
139 my $class = shift;
140 my $event = shift;
141 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
142 return encode_naddr(
143 identifier => $event->d_tag,
144 pubkey => $event->pubkey,
145 kind => $event->kind,
146 relays => $args{relays} // [],
147 );
148 }
149  
150 1;
151  
152 __END__
« Calendar.pm ↑ Summary Deletion.pm »