Torrent.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
« Thread.pm ↑ Summary Blossom.pm »
1 package Net::Nostr::Torrent;
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 info_hash
12 title
13 description
14 files
15 trackers
16 identifiers
17 hashtags
18 );
19  
20 sub new {
21 my $class = shift;
22 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
23 $args{files} //= [];
24 $args{trackers} //= [];
25 $args{identifiers} //= [];
26 $args{hashtags} //= [];
27 $args{description} //= '';
28 my $self = bless { %args }, $class;
29 my %known; @known{Class::Tiny->get_all_attributes_for($class)} = ();
30 my @unknown = grep { !exists $known{$_} } keys %$self;
31 croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
32 return $self;
33 }
34  
35 sub to_event {
36 my $class = shift;
37 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
38  
39 my $info_hash = delete $args{info_hash}
40 // croak "to_event requires 'info_hash'";
41 my $title = delete $args{title}
42 // croak "to_event requires 'title'";
43 my $content = delete $args{content} // '';
44 my $files = delete $args{files} // [];
45 my $trackers = delete $args{trackers} // [];
46 my $identifiers = delete $args{identifiers} // [];
47 my $hashtags = delete $args{hashtags} // [];
48  
49 my @tags;
50 push @tags, ['title', $title];
51 push @tags, ['x', $info_hash];
52  
53 for my $file (@$files) {
54 push @tags, ['file', @$file];
55 }
56  
57 for my $tracker (@$trackers) {
58 push @tags, ['tracker', $tracker];
59 }
60  
61 for my $id (@$identifiers) {
62 push @tags, ['i', $id];
63 }
64  
65 for my $tag (@$hashtags) {
66 push @tags, ['t', $tag];
67 }
68  
69 return Net::Nostr::Event->new(
70 %args,
71 kind => 2003,
72 content => $content,
73 tags => \@tags,
74 );
75 }
76  
77 sub comment {
78 my $class = shift;
79 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
80  
81 return Net::Nostr::Event->new(
82 %args,
83 kind => 2004,
84 );
85 }
86  
87 sub from_event {
88 my ($class, $event) = @_;
89 my $kind = $event->kind;
90  
91 return undef unless $kind == 2003 || $kind == 2004;
92  
93 if ($kind == 2004) {
94 return $class->new(description => $event->content);
95 }
96  
97 my ($info_hash, $title, @files, @trackers, @identifiers, @hashtags);
98  
99 for my $tag (@{$event->tags}) {
100 my $t = $tag->[0];
101 if ($t eq 'x') {
102 $info_hash = $tag->[1];
103 } elsif ($t eq 'title') {
104 $title = $tag->[1];
105 } elsif ($t eq 'file') {
106 push @files, [@{$tag}[1 .. $#$tag]];
107 } elsif ($t eq 'tracker') {
108 push @trackers, $tag->[1];
109 } elsif ($t eq 'i') {
110 push @identifiers, $tag->[1];
111 } elsif ($t eq 't') {
112 push @hashtags, $tag->[1];
113 }
114 }
115  
116 return $class->new(
117 info_hash => $info_hash,
118 title => $title,
119 description => $event->content,
120 files => \@files,
121 trackers => \@trackers,
122 identifiers => \@identifiers,
123 hashtags => \@hashtags,
124 );
125 }
126  
127 sub validate {
128 my ($class, $event) = @_;
129 my $kind = $event->kind;
130  
131 croak "torrent event MUST be kind 2003 or 2004"
132 unless $kind == 2003 || $kind == 2004;
133  
134 if ($kind == 2003) {
135 my $has_x = grep { $_->[0] eq 'x' } @{$event->tags};
136 croak "torrent MUST have an 'x' tag (info hash)" unless $has_x;
137  
138 my $has_title = grep { $_->[0] eq 'title' } @{$event->tags};
139 croak "torrent MUST have a 'title' tag" unless $has_title;
140 }
141  
142 return 1;
143 }
144  
145 1;
146  
147 __END__
« Thread.pm ↑ Summary Blossom.pm »