Mention.pm

filter: Statementstmt 85.4% 47 / 55 Branchbran 65.6% 21 / 32 Conditioncond 61.9% 13 / 21 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 73.3% 99 / 135 CC 25 SCAR 19.7
CC
25
Cov
75%
replace_mentions
14.2
mention_event
7.8
extract_mentions
6.1
CRAP 7.1
« Negentropy.pm ↑ Summary KeyEncrypt.pm »
1 package Net::Nostr::Mention;
2  
3 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Net::Nostr::Bech32 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 encode_npub encode_note
8 encode_nprofile encode_nevent encode_naddr
9 decode_bech32_entity
10 2 );
11 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our @EXPORT_OK = qw(
14 extract_mentions
15 replace_mentions
16 mention_pubkey
17 mention_event
18 mention_addr
19 );
20  
21 2 my $HEX64 = qr/\A[0-9a-f]{64}\z/;
22  
23 # Match nostr: followed by bech32 characters (alphanumeric except 1bio)
24 # NIP-19 bech32 uses qpzry9x8gf2tvdw0s3jn54khce6mua7l plus separator '1'
25 2 my $NOSTR_RE = qr/nostr:([a-z]+1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{6,})/i;
26  
27 2 ▶sub extract_mentions {
Subroutine extract_mentions: called CC=6 SCAR=18.0
Pod documented
28 2 my ($content) = @_;
29 2 ▶ return () unless defined $content && length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 1 1 2
Condition66%2/3
defined $content and length $content
!ll&&!rl&&r
101
MC/DC50%1/2
defined $content and length $content
defined $content length $content
Truth table66%2/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
30  
31 1 my @mentions;
32 1 ▶ while ($content =~ /$NOSTR_RE/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /$NOSTR_RE/g) 2 1 3
33 2 my $bech32 = $1;
34 2 my $uri = $&;
35 2 my $end = pos($content);
36 2 my $start = $end - length($uri);
37  
38 2 my $decoded = eval { decode_bech32_entity($bech32) };
39 2 ▶ next unless $decoded;
Branch: 1 branch
BranchTrueFalseTotal
unless $decoded 0 2 2
40  
41 # Skip nsec per NIP-21
42 2 ▶ next if $decoded->{type} eq 'nsec';
Branch: 1 branch
BranchTrueFalseTotal
if $decoded->{"type"} eq "nsec" 0 2 2
43  
44 push @mentions, {
45 type => $decoded->{type},
46 data => $decoded->{data},
47 2 uri => $uri,
48 start => $start,
49 end => $end,
50 };
51 }
52  
53 1 return @mentions;
54 }
55  
56 2 ▶sub replace_mentions {
Subroutine replace_mentions: called CC=6 SCAR=26.5
Pod documented
57 2 my ($content, $callback) = @_;
58 2 ▶ croak "callback must be a code reference" unless ref $callback eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 1 1 2
59 1 ▶ return $content unless defined $content && length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 1 0 1
Condition33%1/3
defined $content and length $content
!ll&&!rl&&r
100
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table33%1/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
60  
61 0 my @mentions = extract_mentions($content);
62 0 ▶ return $content unless @mentions;
Branch: 1 branch
BranchTrueFalseTotal
unless @mentions 0 0 0
63  
64 # Build result from back to front to preserve offsets
65 0 for my $m (reverse @mentions) {
66 0 my $replacement = $callback->($m);
67 0 substr($content, $m->{start}, $m->{end} - $m->{start}, $replacement);
68 }
69  
70 0 return $content;
71 }
72  
73 3 ▶sub mention_pubkey {
Subroutine mention_pubkey: called CC=5 SCAR=16.8
Pod documented
74 3 my ($pubkey, %opts) = @_;
75 3 ▶ croak "pubkey must be 64-char lowercase hex" unless defined $pubkey && $pubkey =~ $HEX64;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pubkey and $pubkey =~ /$HEX64/ 2 1 3
Condition100%3/3
defined $pubkey and $pubkey =~ /$HEX64/
!ll&&!rl&&r
111
MC/DC100%2/2
defined $pubkey and $pubkey =~ /$HEX64/
defined $pubkey $pubkey =~ /$HEX64/
Truth table100%3/3
A and B
A= defined $pubkeyB= $pubkey =~ /$HEX64/
ABresult
0X0
100
111
76  
77 1 ▶ if ($opts{relays} && @{$opts{relays}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"relays"} and @{$opts{"relays"};}) 0 1 1
Condition33%1/3
$opts{"relays"} and @{$opts{"relays"};}
!ll&&!rl&&r
010
MC/DC0%0/2
$opts{"relays"} and @{$opts{"relays"};}
$opts{"relays"} @{$opts{"relays"};}
Truth table33%1/3
A and B
A= $opts{"relays"}B= @{$opts{"relays"};}
ABresult
0X0
100
111
78 0 return 'nostr:' . encode_nprofile(pubkey => $pubkey, relays => $opts{relays});
79 }
80 1 return 'nostr:' . encode_npub($pubkey);
81 }
82  
83 4 ▶sub mention_event {
Subroutine mention_event: called CC=7 SCAR=20.5
Pod documented
84 4 my ($id, %opts) = @_;
85 4 ▶ croak "event id must be 64-char lowercase hex" unless defined $id && $id =~ $HEX64;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and $id =~ /$HEX64/ 1 3 4
Condition66%2/3
defined $id and $id =~ /$HEX64/
!ll&&!rl&&r
103
MC/DC50%1/2
defined $id and $id =~ /$HEX64/
defined $id $id =~ /$HEX64/
Truth table66%2/3
A and B
A= defined $idB= $id =~ /$HEX64/
ABresult
0X0
100
111
86  
87 3 ▶ if ($opts{relays} || $opts{author} || defined $opts{kind}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"relays"} or $opts{"author"} or defined $opts{"kind"}) 3 0 3
Condition66%4/6
$opts{"relays"} or $opts{"author"} or defined $opts{"kind"}
l!l&&r!l&&!r
120
$opts{"relays"} or $opts{"author"}
l!l&&r!l&&!r
012
MC/DC0%0/3
$opts{"relays"} or $opts{"author"} or defined $opts{"kind"}
$opts{"relays"} $opts{"author"} defined $opts{"kind"}
Truth table50%2/4
A or B or C
A= $opts{"relays"}B= $opts{"author"}C= defined $opts{"kind"}
ABCresult
1XX1
01X1
0011
0000
88 return 'nostr:' . encode_nevent(
89 id => $id,
90 (relays => $opts{relays}) x !!$opts{relays},
91 (author => $opts{author}) x !!$opts{author},
92 3 ▶ (defined $opts{kind} ? (kind => $opts{kind}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'kind'} ? : 2 1 3
93 );
94 }
95 0 return 'nostr:' . encode_note($id);
96 }
97  
98 1 ▶sub mention_addr {
Subroutine mention_addr: called CC=5 SCAR=17.5
Pod documented
99 1 my (%opts) = @_;
100 1 ▶ croak "mention_addr requires 'identifier'" unless defined $opts{identifier};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"identifier"} 0 1 1
101 1 ▶ croak "mention_addr requires 'pubkey'" unless defined $opts{pubkey};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"pubkey"} 0 1 1
102 1 ▶ croak "mention_addr requires 'kind'" unless defined $opts{kind};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"kind"} 0 1 1
103  
104 return 'nostr:' . encode_naddr(
105 identifier => $opts{identifier},
106 pubkey => $opts{pubkey},
107 kind => $opts{kind},
108 1 ▶ ($opts{relays} ? (relays => $opts{relays}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'relays'} ? : 0 1 1
109 );
110 }
111  
112 2 1;
113  
114 __END__
« Negentropy.pm ↑ Summary KeyEncrypt.pm »