| 1 | package Net::Nostr::Repost; | ||
| 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 | 0 | ▶ | use Net::Nostr::Event; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | my $HEX64 = qr/\A[0-9a-f]{64}\z/; | ||
| 12 | |||
| 13 | use Class::Tiny qw( | ||
| 14 | event_id | ||
| 15 | relay_url | ||
| 16 | author_pubkey | ||
| 17 | reposted_kind | ||
| 18 | event_coordinate | ||
| 19 | embedded_event | ||
| 20 | quote_event_id | ||
| 21 | ); | ||
| 22 | |||
| 23 | my $JSON = JSON->new->utf8; | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $class = shift; | ||
| 27 | my $self = bless { Net::Nostr::_ConstructorArgs::normalize(@_) }, $class; | ||
| 28 | my %known; @known{Class::Tiny->get_all_attributes_for($class)} = (); | ||
| 29 | my @unknown = grep { !exists $known{$_} } keys %$self; | ||
| 30 | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub repost { | ||
| 35 | my $class = shift; | ||
| 36 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 37 | |||
| 38 | my $event = $args{event} // croak "repost requires 'event'"; | ||
| 39 | my $pubkey = $args{pubkey} // croak "repost requires 'pubkey'"; | ||
| 40 | my $relay_url = $args{relay_url} // croak "repost requires 'relay_url'"; | ||
| 41 | |||
| 42 | my $event_id = $event->id; | ||
| 43 | my $author_pubkey = $event->pubkey; | ||
| 44 | croak "event_id must be 64-char lowercase hex" unless $event_id =~ $HEX64; | ||
| 45 | croak "author_pubkey must be 64-char lowercase hex" unless $author_pubkey =~ $HEX64; | ||
| 46 | |||
| 47 | my $kind = $event->kind == 1 ? 6 : 16; | ||
| 48 | |||
| 49 | # Content: stringified JSON of reposted event, or empty if overridden | ||
| 50 | my $content; | ||
| 51 | if (exists $args{content}) { | ||
| 52 | $content = $args{content}; | ||
| 53 | } elsif ($event->is_protected) { | ||
| 54 | $content = ''; | ||
| 55 | } else { | ||
| 56 | $content = $JSON->encode($event->to_hash); | ||
| 57 | } | ||
| 58 | |||
| 59 | my @tags; | ||
| 60 | |||
| 61 | # MUST: e tag with relay URL | ||
| 62 | push @tags, ['e', $event_id, $relay_url]; | ||
| 63 | |||
| 64 | # SHOULD: p tag with original author | ||
| 65 | push @tags, ['p', $author_pubkey]; | ||
| 66 | |||
| 67 | # Generic repost extras (kind 16 only) | ||
| 68 | if ($kind == 16) { | ||
| 69 | # SHOULD: k tag with stringified kind | ||
| 70 | push @tags, ['k', '' . $event->kind]; | ||
| 71 | |||
| 72 | # SHOULD: a tag for addressable events | ||
| 73 | if ($event->is_addressable) { | ||
| 74 | my $coord = $event->kind . ':' . $event->pubkey . ':' . $event->d_tag; | ||
| 75 | push @tags, ['a', $coord]; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | # MAY: q tag for quote reposts | ||
| 80 | if ($args{quote}) { | ||
| 81 | push @tags, ['q', $event_id, $relay_url, $author_pubkey]; | ||
| 82 | } | ||
| 83 | |||
| 84 | delete @args{qw(event relay_url quote content)}; | ||
| 85 | return Net::Nostr::Event->new(%args, kind => $kind, content => $content, tags => \@tags); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub from_event { | ||
| 89 | my ($class, $event) = @_; | ||
| 90 | return undef unless $event->kind == 6 || $event->kind == 16; | ||
| 91 | |||
| 92 | my ($event_id, $relay_url, $author_pubkey, $reposted_kind, $event_coordinate, $quote_event_id); | ||
| 93 | |||
| 94 | for my $tag (@{$event->tags}) { | ||
| 95 | my $name = $tag->[0]; | ||
| 96 | if ($name eq 'e' && !defined $event_id) { | ||
| 97 | $event_id = $tag->[1]; | ||
| 98 | $relay_url = $tag->[2] // ''; | ||
| 99 | } elsif ($name eq 'p' && !defined $author_pubkey) { | ||
| 100 | $author_pubkey = $tag->[1]; | ||
| 101 | } elsif ($name eq 'k' && !defined $reposted_kind) { | ||
| 102 | $reposted_kind = $tag->[1]; | ||
| 103 | } elsif ($name eq 'a' && !defined $event_coordinate) { | ||
| 104 | $event_coordinate = $tag->[1]; | ||
| 105 | } elsif ($name eq 'q' && !defined $quote_event_id) { | ||
| 106 | $quote_event_id = $tag->[1]; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | # Parse embedded event from content | ||
| 111 | my $embedded; | ||
| 112 | if (defined $event->content && length $event->content) { | ||
| 113 | my $hash = eval { JSON::decode_json($event->content) }; | ||
| 114 | if ($hash && ref $hash eq 'HASH') { | ||
| 115 | $embedded = eval { Net::Nostr::Event->new(%$hash) }; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | return $class->new( | ||
| 120 | event_id => $event_id, | ||
| 121 | relay_url => $relay_url, | ||
| 122 | author_pubkey => $author_pubkey, | ||
| 123 | reposted_kind => $reposted_kind, | ||
| 124 | event_coordinate => $event_coordinate, | ||
| 125 | embedded_event => $embedded, | ||
| 126 | quote_event_id => $quote_event_id, | ||
| 127 | ); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub validate { | ||
| 131 | my ($class, $event) = @_; | ||
| 132 | |||
| 133 | croak "repost MUST be kind 6 or 16" unless $event->kind == 6 || $event->kind == 16; | ||
| 134 | |||
| 135 | my $has_e_with_relay = 0; | ||
| 136 | for my $tag (@{$event->tags}) { | ||
| 137 | if ($tag->[0] eq 'e' && defined $tag->[1] && defined $tag->[2] && length $tag->[2]) { | ||
| 138 | $has_e_with_relay = 1; | ||
| 139 | last; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | # Check for e tag at all | ||
| 144 | my $has_e = grep { $_->[0] eq 'e' } @{$event->tags}; | ||
| 145 | croak "repost MUST include an e tag" unless $has_e; | ||
| 146 | croak "repost e tag MUST include a relay URL" unless $has_e_with_relay; | ||
| 147 | |||
| 148 | if (defined $event->content && length $event->content) { | ||
| 149 | my $hash = eval { JSON::decode_json($event->content) }; | ||
| 150 | if ($hash && ref $hash eq 'HASH') { | ||
| 151 | my $embedded = eval { Net::Nostr::Event->new(%$hash) }; | ||
| 152 | croak "protected repost MUST NOT embed the reposted event" | ||
| 153 | if $embedded && $embedded->is_protected; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | return 1; | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | |||
| 162 | __END__ | ||