| 1 | package Net::Nostr::LiveActivity; | ||
| 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 | identifier | ||
| 12 | title | ||
| 13 | summary | ||
| 14 | image | ||
| 15 | streaming | ||
| 16 | recording | ||
| 17 | starts | ||
| 18 | ends | ||
| 19 | status | ||
| 20 | current_participants | ||
| 21 | total_participants | ||
| 22 | hashtags | ||
| 23 | participants | ||
| 24 | relays | ||
| 25 | pinned | ||
| 26 | activity | ||
| 27 | relay_hint | ||
| 28 | reply_to | ||
| 29 | room | ||
| 30 | service | ||
| 31 | endpoint | ||
| 32 | space_ref | ||
| 33 | hand | ||
| 34 | ); | ||
| 35 | |||
| 36 | my %KINDS = ( | ||
| 37 | 30311 => 'live_event', | ||
| 38 | 1311 => 'chat_message', | ||
| 39 | 30312 => 'meeting_space', | ||
| 40 | 30313 => 'meeting_room', | ||
| 41 | 10312 => 'room_presence', | ||
| 42 | ); | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my $class = shift; | ||
| 46 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 47 | $args{hashtags} //= []; | ||
| 48 | $args{participants} //= []; | ||
| 49 | $args{relays} //= []; | ||
| 50 | $args{pinned} //= []; | ||
| 51 | my $self = bless { %args }, $class; | ||
| 52 | my %known; @known{Class::Tiny->get_all_attributes_for($class)} = (); | ||
| 53 | my @unknown = grep { !exists $known{$_} } keys %$self; | ||
| 54 | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub live_event { | ||
| 59 | my $class = shift; | ||
| 60 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 61 | |||
| 62 | my $identifier = delete $args{identifier} | ||
| 63 | // croak "live_event requires 'identifier'"; | ||
| 64 | my $title = delete $args{title}; | ||
| 65 | my $summary = delete $args{summary}; | ||
| 66 | my $image = delete $args{image}; | ||
| 67 | my $streaming = delete $args{streaming}; | ||
| 68 | my $recording = delete $args{recording}; | ||
| 69 | my $starts = delete $args{starts}; | ||
| 70 | my $ends = delete $args{ends}; | ||
| 71 | my $status = delete $args{status}; | ||
| 72 | my $current_participants = delete $args{current_participants}; | ||
| 73 | my $total_participants = delete $args{total_participants}; | ||
| 74 | my $hashtags = delete $args{hashtags} // []; | ||
| 75 | my $participants = delete $args{participants} // []; | ||
| 76 | my $relays = delete $args{relays}; | ||
| 77 | my $pinned = delete $args{pinned} // []; | ||
| 78 | |||
| 79 | my @tags; | ||
| 80 | push @tags, ['d', $identifier]; | ||
| 81 | push @tags, ['title', $title] if defined $title; | ||
| 82 | push @tags, ['summary', $summary] if defined $summary; | ||
| 83 | push @tags, ['image', $image] if defined $image; | ||
| 84 | push @tags, ['streaming', $streaming] if defined $streaming; | ||
| 85 | push @tags, ['recording', $recording] if defined $recording; | ||
| 86 | push @tags, ['starts', $starts] if defined $starts; | ||
| 87 | push @tags, ['ends', $ends] if defined $ends; | ||
| 88 | push @tags, ['status', $status] if defined $status; | ||
| 89 | push @tags, ['current_participants', $current_participants] | ||
| 90 | if defined $current_participants; | ||
| 91 | push @tags, ['total_participants', $total_participants] | ||
| 92 | if defined $total_participants; | ||
| 93 | push @tags, ['t', $_] for @$hashtags; | ||
| 94 | push @tags, ['p', @$_] for @$participants; | ||
| 95 | push @tags, ['relays', @$relays] if $relays; | ||
| 96 | push @tags, ['pinned', $_] for @$pinned; | ||
| 97 | |||
| 98 | return Net::Nostr::Event->new( | ||
| 99 | %args, | ||
| 100 | kind => 30311, | ||
| 101 | content => '', | ||
| 102 | tags => \@tags, | ||
| 103 | ); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub chat_message { | ||
| 107 | my $class = shift; | ||
| 108 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 109 | |||
| 110 | my $activity = delete $args{activity} | ||
| 111 | // croak "chat_message requires 'activity'"; | ||
| 112 | my $relay_hint = delete $args{relay_hint}; | ||
| 113 | my $reply_to = delete $args{reply_to}; | ||
| 114 | my $content = delete $args{content} // ''; | ||
| 115 | |||
| 116 | my @tags; | ||
| 117 | if (defined $relay_hint) { | ||
| 118 | push @tags, ['a', $activity, $relay_hint, 'root']; | ||
| 119 | } else { | ||
| 120 | push @tags, ['a', $activity]; | ||
| 121 | } | ||
| 122 | push @tags, ['e', $reply_to] if defined $reply_to; | ||
| 123 | |||
| 124 | return Net::Nostr::Event->new( | ||
| 125 | %args, | ||
| 126 | kind => 1311, | ||
| 127 | content => $content, | ||
| 128 | tags => \@tags, | ||
| 129 | ); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub meeting_space { | ||
| 133 | my $class = shift; | ||
| 134 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 135 | |||
| 136 | my $identifier = delete $args{identifier} | ||
| 137 | // croak "meeting_space requires 'identifier'"; | ||
| 138 | my $room = delete $args{room} | ||
| 139 | // croak "meeting_space requires 'room'"; | ||
| 140 | my $status = delete $args{status} | ||
| 141 | // croak "meeting_space requires 'status'"; | ||
| 142 | my $service = delete $args{service} | ||
| 143 | // croak "meeting_space requires 'service'"; | ||
| 144 | my $participants = delete $args{participants} | ||
| 145 | // croak "meeting_space requires 'participants'"; | ||
| 146 | my $summary = delete $args{summary}; | ||
| 147 | my $image = delete $args{image}; | ||
| 148 | my $endpoint = delete $args{endpoint}; | ||
| 149 | my $hashtags = delete $args{hashtags} // []; | ||
| 150 | my $relays = delete $args{relays}; | ||
| 151 | |||
| 152 | my @tags; | ||
| 153 | push @tags, ['d', $identifier]; | ||
| 154 | push @tags, ['room', $room]; | ||
| 155 | push @tags, ['status', $status]; | ||
| 156 | push @tags, ['service', $service]; | ||
| 157 | push @tags, ['summary', $summary] if defined $summary; | ||
| 158 | push @tags, ['image', $image] if defined $image; | ||
| 159 | push @tags, ['endpoint', $endpoint] if defined $endpoint; | ||
| 160 | push @tags, ['t', $_] for @$hashtags; | ||
| 161 | push @tags, ['p', @$_] for @$participants; | ||
| 162 | push @tags, ['relays', @$relays] if $relays; | ||
| 163 | |||
| 164 | return Net::Nostr::Event->new( | ||
| 165 | %args, | ||
| 166 | kind => 30312, | ||
| 167 | content => '', | ||
| 168 | tags => \@tags, | ||
| 169 | ); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub meeting_room { | ||
| 173 | my $class = shift; | ||
| 174 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 175 | |||
| 176 | my $identifier = delete $args{identifier} | ||
| 177 | // croak "meeting_room requires 'identifier'"; | ||
| 178 | my $space_ref = delete $args{space_ref} | ||
| 179 | // croak "meeting_room requires 'space_ref'"; | ||
| 180 | my $title = delete $args{title} | ||
| 181 | // croak "meeting_room requires 'title'"; | ||
| 182 | my $starts = delete $args{starts} | ||
| 183 | // croak "meeting_room requires 'starts'"; | ||
| 184 | my $status = delete $args{status} | ||
| 185 | // croak "meeting_room requires 'status'"; | ||
| 186 | my $summary = delete $args{summary}; | ||
| 187 | my $image = delete $args{image}; | ||
| 188 | my $ends = delete $args{ends}; | ||
| 189 | my $current_participants = delete $args{current_participants}; | ||
| 190 | my $total_participants = delete $args{total_participants}; | ||
| 191 | my $participants = delete $args{participants} // []; | ||
| 192 | |||
| 193 | my @tags; | ||
| 194 | push @tags, ['d', $identifier]; | ||
| 195 | push @tags, ['a', @$space_ref]; | ||
| 196 | push @tags, ['title', $title]; | ||
| 197 | push @tags, ['starts', $starts]; | ||
| 198 | push @tags, ['status', $status]; | ||
| 199 | push @tags, ['summary', $summary] if defined $summary; | ||
| 200 | push @tags, ['image', $image] if defined $image; | ||
| 201 | push @tags, ['ends', $ends] if defined $ends; | ||
| 202 | push @tags, ['current_participants', $current_participants] | ||
| 203 | if defined $current_participants; | ||
| 204 | push @tags, ['total_participants', $total_participants] | ||
| 205 | if defined $total_participants; | ||
| 206 | push @tags, ['p', @$_] for @$participants; | ||
| 207 | |||
| 208 | return Net::Nostr::Event->new( | ||
| 209 | %args, | ||
| 210 | kind => 30313, | ||
| 211 | content => '', | ||
| 212 | tags => \@tags, | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | |||
| 216 | sub room_presence { | ||
| 217 | my $class = shift; | ||
| 218 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 219 | |||
| 220 | my $activity = delete $args{activity} | ||
| 221 | // croak "room_presence requires 'activity'"; | ||
| 222 | my $relay_hint = delete $args{relay_hint}; | ||
| 223 | my $hand = delete $args{hand}; | ||
| 224 | |||
| 225 | my @tags; | ||
| 226 | push @tags, ['a', $activity, $relay_hint // '', 'root']; | ||
| 227 | push @tags, ['hand', $hand] if defined $hand; | ||
| 228 | |||
| 229 | return Net::Nostr::Event->new( | ||
| 230 | %args, | ||
| 231 | kind => 10312, | ||
| 232 | content => '', | ||
| 233 | tags => \@tags, | ||
| 234 | ); | ||
| 235 | } | ||
| 236 | |||
| 237 | sub from_event { | ||
| 238 | my ($class, $event) = @_; | ||
| 239 | my $kind = $event->kind; | ||
| 240 | |||
| 241 | return undef unless exists $KINDS{$kind}; | ||
| 242 | |||
| 243 | my %attrs; | ||
| 244 | my (@hashtags, @participants, @relays, @pinned); | ||
| 245 | |||
| 246 | for my $tag (@{$event->tags}) { | ||
| 247 | next unless @$tag >= 2; | ||
| 248 | my $t = $tag->[0]; | ||
| 249 | if ($t eq 'd') { $attrs{identifier} = $tag->[1] } | ||
| 250 | elsif ($t eq 'title') { $attrs{title} = $tag->[1] } | ||
| 251 | elsif ($t eq 'summary') { $attrs{summary} = $tag->[1] } | ||
| 252 | elsif ($t eq 'image') { $attrs{image} = $tag->[1] } | ||
| 253 | elsif ($t eq 'streaming') { $attrs{streaming} = $tag->[1] } | ||
| 254 | elsif ($t eq 'recording') { $attrs{recording} = $tag->[1] } | ||
| 255 | elsif ($t eq 'starts') { $attrs{starts} = $tag->[1] } | ||
| 256 | elsif ($t eq 'ends') { $attrs{ends} = $tag->[1] } | ||
| 257 | elsif ($t eq 'status') { $attrs{status} = $tag->[1] } | ||
| 258 | elsif ($t eq 'current_participants') { $attrs{current_participants} = $tag->[1] } | ||
| 259 | elsif ($t eq 'total_participants') { $attrs{total_participants} = $tag->[1] } | ||
| 260 | elsif ($t eq 't') { push @hashtags, $tag->[1] } | ||
| 261 | elsif ($t eq 'p') { push @participants, [@{$tag}[1 .. $#$tag]] } | ||
| 262 | elsif ($t eq 'relays') { @relays = @{$tag}[1 .. $#$tag] } | ||
| 263 | elsif ($t eq 'pinned') { push @pinned, $tag->[1] } | ||
| 264 | elsif ($t eq 'a') { | ||
| 265 | if ($kind == 30313) { | ||
| 266 | $attrs{space_ref} = [@{$tag}[1 .. $#$tag]]; | ||
| 267 | } else { | ||
| 268 | $attrs{activity} = $tag->[1]; | ||
| 269 | $attrs{relay_hint} = $tag->[2] if @$tag > 2 && defined $tag->[2] && $tag->[2] ne ''; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | elsif ($t eq 'e') { $attrs{reply_to} = $tag->[1] } | ||
| 273 | elsif ($t eq 'room') { $attrs{room} = $tag->[1] } | ||
| 274 | elsif ($t eq 'service') { $attrs{service} = $tag->[1] } | ||
| 275 | elsif ($t eq 'endpoint') { $attrs{endpoint} = $tag->[1] } | ||
| 276 | elsif ($t eq 'hand') { $attrs{hand} = $tag->[1] } | ||
| 277 | } | ||
| 278 | |||
| 279 | return $class->new( | ||
| 280 | %attrs, | ||
| 281 | hashtags => \@hashtags, | ||
| 282 | participants => \@participants, | ||
| 283 | relays => \@relays, | ||
| 284 | pinned => \@pinned, | ||
| 285 | ); | ||
| 286 | } | ||
| 287 | |||
| 288 | sub validate { | ||
| 289 | my ($class, $event) = @_; | ||
| 290 | my $kind = $event->kind; | ||
| 291 | |||
| 292 | croak "live activity event MUST be kind 30311, 1311, 30312, 30313, or 10312" | ||
| 293 | unless exists $KINDS{$kind}; | ||
| 294 | |||
| 295 | my %has; | ||
| 296 | for my $tag (@{$event->tags}) { | ||
| 297 | $has{$tag->[0]} = 1; | ||
| 298 | } | ||
| 299 | |||
| 300 | if ($kind == 30311) { | ||
| 301 | croak "live event MUST have a 'd' tag" unless $has{d}; | ||
| 302 | } | ||
| 303 | |||
| 304 | if ($kind == 1311) { | ||
| 305 | croak "chat message MUST have an 'a' tag" unless $has{a}; | ||
| 306 | } | ||
| 307 | |||
| 308 | if ($kind == 30312) { | ||
| 309 | croak "meeting space MUST have a 'd' tag" unless $has{d}; | ||
| 310 | croak "meeting space MUST have a 'room' tag" unless $has{room}; | ||
| 311 | croak "meeting space MUST have a 'status' tag" unless $has{status}; | ||
| 312 | croak "meeting space MUST have a 'service' tag" unless $has{service}; | ||
| 313 | croak "meeting space MUST have a 'p' tag" unless $has{p}; | ||
| 314 | } | ||
| 315 | |||
| 316 | if ($kind == 30313) { | ||
| 317 | croak "meeting room MUST have a 'd' tag" unless $has{d}; | ||
| 318 | croak "meeting room MUST have an 'a' tag" unless $has{a}; | ||
| 319 | croak "meeting room MUST have a 'title' tag" unless $has{title}; | ||
| 320 | croak "meeting room MUST have a 'starts' tag" unless $has{starts}; | ||
| 321 | croak "meeting room MUST have a 'status' tag" unless $has{status}; | ||
| 322 | } | ||
| 323 | |||
| 324 | if ($kind == 10312) { | ||
| 325 | croak "room presence MUST have an 'a' tag" unless $has{a}; | ||
| 326 | } | ||
| 327 | |||
| 328 | return 1; | ||
| 329 | } | ||
| 330 | |||
| 331 | 1; | ||
| 332 | |||
| 333 | __END__ | ||