Nostr/Channel.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/Blossom.pm ↑ Summary Nostr/Community.pm »
1 package Net::Nostr::Channel;
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 1 ▶use Net::Nostr::Event;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 my $JSON = JSON->new->utf8;
12 my $HEX64 = qr/\A[0-9a-f]{64}\z/;
13  
14 sub create {
15 my $class = shift;
16 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
17 my $pubkey = $args{pubkey} // croak "create requires 'pubkey'";
18 my $name = $args{name} // croak "create requires 'name'";
19  
20 my %meta = (name => $name);
21 $meta{about} = $args{about} if defined $args{about};
22 $meta{picture} = $args{picture} if defined $args{picture};
23 $meta{relays} = $args{relays} if defined $args{relays};
24  
25 # Clients MAY add additional metadata fields
26 if ($args{metadata}) {
27 %meta = (%meta, %{$args{metadata}});
28 }
29  
30 delete @args{qw(name about picture relays metadata)};
31 return Net::Nostr::Event->new(%args, kind => 40, content => $JSON->encode(\%meta), tags => []);
32 }
33  
34 sub set_metadata {
35 my $class = shift;
36 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
37 my $pubkey = $args{pubkey} // croak "set_metadata requires 'pubkey'";
38 my $channel_id = $args{channel_id} // croak "set_metadata requires 'channel_id'";
39 croak "channel_id must be 64-char lowercase hex" unless defined $channel_id && $channel_id =~ $HEX64;
40  
41 my $relay_url = $args{relay_url} // '';
42 my $categories = $args{categories};
43  
44 my %meta;
45 $meta{name} = $args{name} if defined $args{name};
46 $meta{about} = $args{about} if defined $args{about};
47 $meta{picture} = $args{picture} if defined $args{picture};
48 $meta{relays} = $args{relays} if defined $args{relays};
49  
50 # Clients MAY add additional metadata fields
51 if ($args{metadata}) {
52 %meta = (%meta, %{$args{metadata}});
53 }
54  
55 my @tags;
56 push @tags, ['e', $channel_id, $relay_url, 'root'];
57 if ($categories) {
58 push @tags, ['t', $_] for @$categories;
59 }
60  
61 delete @args{qw(channel_id relay_url name about picture relays categories metadata)};
62 return Net::Nostr::Event->new(%args, kind => 41, content => $JSON->encode(\%meta), tags => \@tags);
63 }
64  
65 sub message {
66 my $class = shift;
67 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
68 my $pubkey = $args{pubkey} // croak "message requires 'pubkey'";
69 my $channel_id = $args{channel_id} // croak "message requires 'channel_id'";
70 croak "channel_id must be 64-char lowercase hex" unless defined $channel_id && $channel_id =~ $HEX64;
71 my $content = $args{content} // croak "message requires 'content'";
72  
73 my $relay_url = $args{relay_url} // '';
74  
75 my @tags;
76 push @tags, ['e', $channel_id, $relay_url, 'root'];
77  
78 delete @args{qw(channel_id relay_url)};
79 return Net::Nostr::Event->new(%args, kind => 42, tags => \@tags);
80 }
81  
82 sub reply {
83 my $class = shift;
84 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
85 my $pubkey = $args{pubkey} // croak "reply requires 'pubkey'";
86 my $channel_id = $args{channel_id} // croak "reply requires 'channel_id'";
87 croak "channel_id must be 64-char lowercase hex" unless defined $channel_id && $channel_id =~ $HEX64;
88 my $to = $args{to} // croak "reply requires 'to'";
89 my $content = $args{content} // croak "reply requires 'content'";
90  
91 my $relay_url = $args{relay_url} // '';
92  
93 my @tags;
94 push @tags, ['e', $channel_id, $relay_url, 'root'];
95 push @tags, ['e', $to->id, $relay_url, 'reply'];
96  
97 # Add p tag for parent author unless replying to self
98 if ($to->pubkey ne $pubkey) {
99 push @tags, ['p', $to->pubkey, $relay_url];
100 }
101  
102 delete @args{qw(channel_id relay_url to)};
103 return Net::Nostr::Event->new(%args, kind => 42, tags => \@tags);
104 }
105  
106 sub hide_message {
107 my $class = shift;
108 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
109 my $pubkey = $args{pubkey} // croak "hide_message requires 'pubkey'";
110 my $message_id = $args{message_id} // croak "hide_message requires 'message_id'";
111 croak "message_id must be 64-char lowercase hex" unless defined $message_id && $message_id =~ $HEX64;
112  
113 my $content = '';
114 if (defined $args{reason}) {
115 $content = $JSON->encode({ reason => $args{reason} });
116 }
117  
118 my @tags = (['e', $message_id]);
119  
120 delete @args{qw(message_id reason)};
121 return Net::Nostr::Event->new(%args, kind => 43, content => $content, tags => \@tags);
122 }
123  
124 sub mute_user {
125 my $class = shift;
126 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
127 my $pubkey = $args{pubkey} // croak "mute_user requires 'pubkey'";
128 my $user_pubkey = $args{user_pubkey} // croak "mute_user requires 'user_pubkey'";
129 croak "user_pubkey must be 64-char lowercase hex" unless defined $user_pubkey && $user_pubkey =~ $HEX64;
130  
131 my $content = '';
132 if (defined $args{reason}) {
133 $content = $JSON->encode({ reason => $args{reason} });
134 }
135  
136 my @tags = (['p', $user_pubkey]);
137  
138 delete @args{qw(user_pubkey reason)};
139 return Net::Nostr::Event->new(%args, kind => 44, content => $content, tags => \@tags);
140 }
141  
142 sub metadata_from_event {
143 my ($class, $event) = @_;
144 croak "event must be kind 40 or 41" unless $event->kind == 40 || $event->kind == 41;
145 return $JSON->decode($event->content);
146 }
147  
148 sub channel_id {
149 my ($class, $event) = @_;
150 for my $tag (@{$event->tags}) {
151 return $tag->[1] if $tag->[0] eq 'e' && defined $tag->[3] && $tag->[3] eq 'root';
152 }
153 return undef;
154 }
155  
156 sub hide_from_event {
157 my ($class, $event) = @_;
158 croak "event must be kind 43" unless $event->kind == 43;
159  
160 my $message_id;
161 for my $tag (@{$event->tags}) {
162 if ($tag->[0] eq 'e') {
163 $message_id = $tag->[1];
164 last;
165 }
166 }
167  
168 my $reason;
169 if (defined $event->content && length($event->content) > 0) {
170 my $parsed = $JSON->decode($event->content);
171 $reason = $parsed->{reason};
172 }
173  
174 return { message_id => $message_id, reason => $reason };
175 }
176  
177 sub mute_from_event {
178 my ($class, $event) = @_;
179 croak "event must be kind 44" unless $event->kind == 44;
180  
181 my $pubkey;
182 for my $tag (@{$event->tags}) {
183 if ($tag->[0] eq 'p') {
184 $pubkey = $tag->[1];
185 last;
186 }
187 }
188  
189 my $reason;
190 if (defined $event->content && length($event->content) > 0) {
191 my $parsed = $JSON->decode($event->content);
192 $reason = $parsed->{reason};
193 }
194  
195 return { pubkey => $pubkey, reason => $reason };
196 }
197  
198 1;
199  
200 __END__
« Nostr/Blossom.pm ↑ Summary Nostr/Community.pm »