| 1 | package Net::Nostr::MediaAttachment; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use strictures 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 2 | ▶ | use Net::Nostr::_ConstructorArgs (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | ▶ | use Class::Tiny qw( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | url | ||||||||||||||
| 11 | m | ||||||||||||||
| 12 | x | ||||||||||||||
| 13 | ox | ||||||||||||||
| 14 | size | ||||||||||||||
| 15 | dim | ||||||||||||||
| 16 | magnet | ||||||||||||||
| 17 | i | ||||||||||||||
| 18 | blurhash | ||||||||||||||
| 19 | thumb | ||||||||||||||
| 20 | image | ||||||||||||||
| 21 | summary | ||||||||||||||
| 22 | alt | ||||||||||||||
| 23 | service | ||||||||||||||
| 24 | fallback | ||||||||||||||
| 25 | fields | ||||||||||||||
| 26 | 2 | ); | |||||||||||||
| 27 | |||||||||||||||
| 28 | # Fields that can appear multiple times | ||||||||||||||
| 29 | 2 | my %MULTI_FIELDS = (fallback => 1); | |||||||||||||
| 30 | |||||||||||||||
| 31 | # Known single-value fields (NIP-94) | ||||||||||||||
| 32 | 2 | my %KNOWN_FIELDS = map { $_ => 1 } | |||||||||||||
| 33 | qw(url m x ox size dim magnet i blurhash thumb image summary alt service); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 36 | 0 | my $class = shift; | |||||||||||||
| 37 | 0 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | |||||||||||||
| 38 | 0 | ▶ | $args{fallback} //= []; | ||||||||||||
|
Condition0%0/2
$args{"fallback"} //= []
MC/DC0%0/1
$args{"fallback"} //= []
$args{"fallback"}
Truth table0%0/2
A
A= $args{"fallback"}
| |||||||||||||||
| 39 | 0 | ▶ | $args{fields} //= {}; | ||||||||||||
|
Condition0%0/2
$args{"fields"} //= {}
MC/DC0%0/1
$args{"fields"} //= {}
$args{"fields"}
Truth table0%0/2
A
A= $args{"fields"}
| |||||||||||||||
| 40 | 0 | my $self = bless { %args }, $class; | |||||||||||||
| 41 | 0 | my %known; @known{Class::Tiny->get_all_attributes_for($class)} = (); | |||||||||||||
| 42 | 0 | my @unknown = grep { !exists $known{$_} } keys %$self; | |||||||||||||
| 43 | 0 | ▶ | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 0 | return $self; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | ▶ | sub imeta_tag { | ||||||||||||
|
Subroutine
imeta_tag: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||
| 48 | 0 | my $class = shift; | |||||||||||||
| 49 | 0 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | |||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | ▶ | my $url = $args{url} // croak "imeta_tag requires 'url'"; | ||||||||||||
|
Condition0%0/2
$args{"url"} // croak("imeta_tag requires 'url'")
MC/DC0%0/1
$args{"url"} // croak("imeta_tag requires 'url'")
$args{"url"}
Truth table0%0/2
A
A= $args{"url"}
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 0 | my @entries = ("url $url"); | |||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | my $has_other = 0; | |||||||||||||
| 56 | 0 | for my $key (qw(m x ox size dim magnet i blurhash thumb image summary alt service)) { | |||||||||||||
| 57 | 0 | ▶ | if (defined $args{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 0 | push @entries, "$key $args{$key}"; | |||||||||||||
| 59 | 0 | $has_other = 1; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | ▶ | if ($args{fallback}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 0 | for my $fb (@{$args{fallback}}) { | |||||||||||||
| 65 | 0 | push @entries, "fallback $fb"; | |||||||||||||
| 66 | 0 | $has_other = 1; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | ▶ | croak "imeta tag MUST have at least one field besides url" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | unless $has_other; | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | return ['imeta', @entries]; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | ▶ | sub from_tag { | ||||||||||||
|
Subroutine
from_tag: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 77 | 0 | my ($class, $tag) = @_; | |||||||||||||
| 78 | |||||||||||||||
| 79 | 0 | my %attrs; | |||||||||||||
| 80 | my @fallback; | ||||||||||||||
| 81 | 0 | my %fields; | |||||||||||||
| 82 | |||||||||||||||
| 83 | 0 | for my $idx (1 .. $#$tag) { | |||||||||||||
| 84 | 0 | my $entry = $tag->[$idx]; | |||||||||||||
| 85 | 0 | my ($key, $value) = $entry =~ /^(\S+)\s(.*)$/s; | |||||||||||||
| 86 | 0 | ▶ | next unless defined $key; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | |||||||||||||||
| 88 | 0 | ▶ | if ($key eq 'fallback') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 89 | 0 | push @fallback, $value; | |||||||||||||
| 90 | } elsif ($KNOWN_FIELDS{$key}) { | ||||||||||||||
| 91 | 0 | $attrs{$key} = $value; | |||||||||||||
| 92 | } else { | ||||||||||||||
| 93 | 0 | $fields{$key} = $value; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | return $class->new( | |||||||||||||
| 98 | %attrs, | ||||||||||||||
| 99 | fallback => \@fallback, | ||||||||||||||
| 100 | fields => \%fields, | ||||||||||||||
| 101 | ); | ||||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 0 | ▶ | sub from_event { | ||||||||||||
|
Subroutine
from_event: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 105 | 0 | my ($class, $event) = @_; | |||||||||||||
| 106 | |||||||||||||||
| 107 | 0 | my @attachments; | |||||||||||||
| 108 | 0 | for my $tag (@{$event->tags}) { | |||||||||||||
| 109 | 0 | ▶ | next unless $tag->[0] eq 'imeta'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | 0 | push @attachments, $class->from_tag($tag); | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | |||||||||||||||
| 113 | 0 | return @attachments; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | ▶ | sub for_url { | ||||||||||||
|
Subroutine
for_url: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 117 | 0 | my ($class, $event, $url) = @_; | |||||||||||||
| 118 | |||||||||||||||
| 119 | 0 | for my $tag (@{$event->tags}) { | |||||||||||||
| 120 | 0 | ▶ | next unless $tag->[0] eq 'imeta'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 0 | for my $entry (@$tag) { | |||||||||||||
| 122 | 0 | ▶ | if ($entry eq "url $url") { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 0 | return $class->from_tag($tag); | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | } | ||||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 0 | return undef; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 2 | 1; | |||||||||||||
| 132 | |||||||||||||||
| 133 | __END__ | ||||||||||||||