Thread.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
« Report.pm ↑ Summary Torrent.pm »
1 package Net::Nostr::Thread;
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  
10 use Class::Tiny qw(
11 root_id
12 root_relay
13 root_pubkey
14 reply_id
15 reply_relay
16 reply_pubkey
17 mentions
18 );
19  
20 my $HEX64 = qr/\A[0-9a-f]{64}\z/;
21  
22 sub new {
23 my $class = shift;
24 my $self = bless { Net::Nostr::_ConstructorArgs::normalize(@_) }, $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 if (defined $self->{root_id}) {
29 croak "root_id must be 64-char lowercase hex" unless $self->{root_id} =~ $HEX64;
30 }
31 if (defined $self->{reply_id}) {
32 croak "reply_id must be 64-char lowercase hex" unless $self->{reply_id} =~ $HEX64;
33 }
34 $self->mentions($self->mentions // []);
35 return $self;
36 }
37  
38 sub reply {
39 my $class = shift;
40 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
41 my $to = $args{to} // croak "reply requires 'to' (event being replied to)";
42 my $pubkey = $args{pubkey} // croak "reply requires 'pubkey'";
43 my $content = $args{content} // croak "reply requires 'content'";
44  
45 croak "kind 1 replies must only reply to kind 1 events (see NIP-22)"
46 unless $to->kind == 1;
47  
48 # Find root from parent's e tags
49 my $root_id;
50 my $root_pubkey;
51 my $root_relay = '';
52 my $parent_thread = $class->from_event($to);
53  
54 if ($parent_thread) {
55 $root_id = $parent_thread->root_id;
56 $root_relay = $parent_thread->root_relay // '';
57 $root_pubkey = $parent_thread->root_pubkey // '';
58 }
59  
60 my $relay_url = $args{relay_url} // '';
61 my @tags;
62  
63 if ($root_id) {
64 # Replying to a reply: root + reply markers, sorted root first
65 push @tags, ['e', $root_id, $root_relay, 'root', $root_pubkey // ''];
66 push @tags, ['e', $to->id, $relay_url, 'reply', $to->pubkey];
67 } else {
68 # Direct reply to root: single "root" marker
69 push @tags, ['e', $to->id, $relay_url, 'root', $to->pubkey];
70 }
71  
72 # Collect p tags: parent's p tags + parent's pubkey, deduped, excluding self
73 my %seen;
74 my @p_tags;
75 my @p_candidates;
76  
77 push @p_candidates, $to->pubkey;
78 for my $tag (@{$to->tags}) {
79 next unless @$tag >= 2 && $tag->[0] eq 'p';
80 push @p_candidates, $tag->[1];
81 }
82  
83 for my $pk (@p_candidates) {
84 next if $pk eq $pubkey; # don't include self
85 next if $seen{$pk}++;
86 push @p_tags, ['p', $pk];
87 }
88  
89 push @tags, @p_tags;
90  
91 # Build the event, passing through extra args
92 delete @args{qw(to relay_url)};
93 return Net::Nostr::Event->new(%args, kind => 1, tags => \@tags);
94 }
95  
96 sub quote {
97 my $class = shift;
98 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
99 my $event = $args{event} // croak "quote requires 'event'";
100 my $pubkey = $args{pubkey} // croak "quote requires 'pubkey'";
101 my $content = $args{content} // croak "quote requires 'content'";
102 my $relay_url = $args{relay_url} // '';
103  
104 my @tags;
105 push @tags, ['q', $event->id, $relay_url, $event->pubkey];
106  
107 # Add author as p tag (unless quoting self)
108 if ($event->pubkey ne $pubkey) {
109 push @tags, ['p', $event->pubkey];
110 }
111  
112 delete @args{qw(event relay_url)};
113 return Net::Nostr::Event->new(%args, kind => 1, tags => \@tags);
114 }
115  
116 sub from_event {
117 my ($class, $event) = @_;
118  
119 my @e_tags = grep { @$_ >= 2 && $_->[0] eq 'e' } @{$event->tags};
120 return undef unless @e_tags;
121  
122 # Check for marked tags
123 my ($root_tag, $reply_tag);
124 for my $tag (@e_tags) {
125 if (@$tag > 3 && defined $tag->[3] && $tag->[3] eq 'root') {
126 $root_tag = $tag;
127 } elsif (@$tag > 3 && defined $tag->[3] && $tag->[3] eq 'reply') {
128 $reply_tag = $tag;
129 }
130 }
131  
132 if ($root_tag) {
133 # Marked e tags (preferred)
134 return $class->new(
135 root_id => $root_tag->[1],
136 root_relay => $root_tag->[2] // '',
137 root_pubkey => (@$root_tag > 4 ? ($root_tag->[4] // '') : ''),
138 ($reply_tag ? (
139 reply_id => $reply_tag->[1],
140 reply_relay => $reply_tag->[2] // '',
141 reply_pubkey => (@$reply_tag > 4 ? ($reply_tag->[4] // '') : ''),
142 ) : ()),
143 );
144 }
145  
146 # Deprecated positional e tags
147 if (@e_tags == 1) {
148 return $class->new(
149 root_id => $e_tags[0][1],
150 root_relay => $e_tags[0][2] // '',
151 );
152 }
153  
154 if (@e_tags == 2) {
155 return $class->new(
156 root_id => $e_tags[0][1],
157 root_relay => $e_tags[0][2] // '',
158 reply_id => $e_tags[1][1],
159 reply_relay => $e_tags[1][2] // '',
160 );
161 }
162  
163 # Many positional: first=root, last=reply, middle=mentions
164 return $class->new(
165 root_id => $e_tags[0][1],
166 root_relay => $e_tags[0][2] // '',
167 reply_id => $e_tags[-1][1],
168 reply_relay => $e_tags[-1][2] // '',
169 mentions => [map { $_->[1] } @e_tags[1 .. $#e_tags - 1]],
170 );
171 }
172  
173 sub is_reply {
174 my ($class, $event) = @_;
175 return defined $class->from_event($event) ? 1 : 0;
176 }
177  
178 1;
179  
180 __END__
« Report.pm ↑ Summary Torrent.pm »