List.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
« Key.pm ↑ Summary RelayAdmin.pm »
1 package Net::Nostr::List;
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 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Net::Nostr::Event;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Net::Nostr::Encryption;
11  
12 use Class::Tiny qw(kind identifier title image description _items _private_items);
13  
14 my $JSON = JSON->new->utf8;
15  
16 # Metadata tag names that are not list items
17 my %META_TAGS = map { $_ => 1 } qw(d title image description);
18  
19 sub new {
20 my $class = shift;
21 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
22 my %known; @known{Class::Tiny->get_all_attributes_for($class)} = ();
23 my @unknown = grep { !exists $known{$_} } keys %args;
24 croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
25 croak "new requires 'kind'" unless defined $args{kind};
26 my $self = bless {}, $class;
27 $self->kind($args{kind});
28 $self->identifier($args{identifier} // '');
29 $self->_items([]);
30 $self->_private_items([]);
31 return $self;
32 }
33  
34 sub add {
35 my ($self, @tag) = @_;
36 croak "add requires at least one argument" unless @tag;
37 push @{$self->_items}, \@tag;
38 return $self;
39 }
40  
41 sub add_private {
42 my ($self, @tag) = @_;
43 croak "add_private requires at least one argument" unless @tag;
44 push @{$self->_private_items}, \@tag;
45 return $self;
46 }
47  
48 sub items {
49 my ($self) = @_;
50 return [@{$self->_items}];
51 }
52  
53 sub private_items {
54 my ($self) = @_;
55 return [@{$self->_private_items}];
56 }
57  
58 sub to_event {
59 my $self = shift;
60 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
61 my $pubkey = $args{pubkey} // croak "to_event requires 'pubkey'";
62 my $key = $args{key};
63  
64 my @tags;
65  
66 # For addressable events (sets), add d tag first
67 if ($self->_is_set) {
68 push @tags, ['d', $self->identifier];
69 push @tags, ['title', $self->title] if defined $self->title;
70 push @tags, ['image', $self->image] if defined $self->image;
71 push @tags, ['description', $self->description] if defined $self->description;
72 }
73  
74 # Add public items
75 push @tags, @{$self->_items};
76  
77 # Encrypt private items
78 my $content = '';
79 if (@{$self->_private_items}) {
80 croak "to_event requires 'key' when private items exist" unless $key;
81 my $plaintext = $JSON->encode($self->_private_items);
82 my $conv_key = Net::Nostr::Encryption->get_conversation_key(
83 $key->privkey_hex, $key->pubkey_hex,
84 );
85 $content = Net::Nostr::Encryption->encrypt($plaintext, $conv_key);
86 }
87  
88 delete @args{qw(key)};
89 return Net::Nostr::Event->new(%args, kind => $self->kind, tags => \@tags, content => $content);
90 }
91  
92 sub from_event {
93 my $class = shift;
94 my $event = shift;
95 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
96 my $key = $args{key};
97  
98 my $self = $class->new(kind => $event->kind);
99  
100 for my $tag (@{$event->tags}) {
101 my $name = $tag->[0];
102 if ($name eq 'd') {
103 $self->identifier($tag->[1] // '');
104 } elsif ($name eq 'title') {
105 $self->title($tag->[1]);
106 } elsif ($name eq 'image') {
107 $self->image($tag->[1]);
108 } elsif ($name eq 'description') {
109 $self->description($tag->[1]);
110 } else {
111 push @{$self->_items}, [@$tag];
112 }
113 }
114  
115 # Decrypt private items if key provided and content is non-empty
116 if ($key && defined $event->content && length($event->content) > 0) {
117 croak "NIP-04 encrypted content is not supported (deprecated); re-encrypt with NIP-44"
118 if $event->content =~ /\?iv=/;
119 my $conv_key = Net::Nostr::Encryption->get_conversation_key(
120 $key->privkey_hex, $key->pubkey_hex,
121 );
122 my $plaintext = Net::Nostr::Encryption->decrypt($event->content, $conv_key);
123 my $private = $JSON->decode($plaintext);
124 $self->_private_items($private);
125 }
126  
127 return $self;
128 }
129  
130 sub _is_set {
131 my ($self) = @_;
132 my $k = $self->kind;
133 return ($k >= 30000 && $k < 40000);
134 }
135  
136 1;
137  
138 __END__
« Key.pm ↑ Summary RelayAdmin.pm »