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