| 1 | package Net::Nostr::Calendar; | ||
| 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 | description | ||
| 14 | start | ||
| 15 | end | ||
| 16 | summary | ||
| 17 | image | ||
| 18 | locations | ||
| 19 | geohash | ||
| 20 | participants | ||
| 21 | hashtags | ||
| 22 | references | ||
| 23 | calendars | ||
| 24 | calendar_events | ||
| 25 | start_tzid | ||
| 26 | end_tzid | ||
| 27 | days | ||
| 28 | event_coord | ||
| 29 | event_id | ||
| 30 | status | ||
| 31 | fb | ||
| 32 | event_author | ||
| 33 | ); | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my $class = shift; | ||
| 37 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 38 | $args{locations} //= []; | ||
| 39 | $args{participants} //= []; | ||
| 40 | $args{hashtags} //= []; | ||
| 41 | $args{references} //= []; | ||
| 42 | $args{calendars} //= []; | ||
| 43 | $args{calendar_events} //= []; | ||
| 44 | $args{days} //= []; | ||
| 45 | $args{description} //= ''; | ||
| 46 | my $self = bless { %args }, $class; | ||
| 47 | my %known; @known{Class::Tiny->get_all_attributes_for($class)} = (); | ||
| 48 | my @unknown = grep { !exists $known{$_} } keys %$self; | ||
| 49 | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Shared tag builder for common calendar event tags | ||
| 54 | |||
| 55 | sub _build_common_tags { | ||
| 56 | my ($class, $args) = @_; | ||
| 57 | |||
| 58 | my @tags; | ||
| 59 | |||
| 60 | my $identifier = delete $args->{identifier} | ||
| 61 | // croak "requires 'identifier'"; | ||
| 62 | my $title = delete $args->{title} | ||
| 63 | // croak "requires 'title'"; | ||
| 64 | |||
| 65 | push @tags, ['d', $identifier]; | ||
| 66 | push @tags, ['title', $title]; | ||
| 67 | |||
| 68 | if (defined(my $v = delete $args->{summary})) { | ||
| 69 | push @tags, ['summary', $v]; | ||
| 70 | } | ||
| 71 | if (defined(my $v = delete $args->{image})) { | ||
| 72 | push @tags, ['image', $v]; | ||
| 73 | } | ||
| 74 | if (my $locs = delete $args->{locations}) { | ||
| 75 | push @tags, ['location', $_] for @$locs; | ||
| 76 | } | ||
| 77 | if (defined(my $v = delete $args->{geohash})) { | ||
| 78 | push @tags, ['g', $v]; | ||
| 79 | } | ||
| 80 | if (my $parts = delete $args->{participants}) { | ||
| 81 | push @tags, ['p', @$_] for @$parts; | ||
| 82 | } | ||
| 83 | if (my $tags_list = delete $args->{hashtags}) { | ||
| 84 | push @tags, ['t', $_] for @$tags_list; | ||
| 85 | } | ||
| 86 | if (my $refs = delete $args->{references}) { | ||
| 87 | push @tags, ['r', $_] for @$refs; | ||
| 88 | } | ||
| 89 | if (my $cals = delete $args->{calendars}) { | ||
| 90 | push @tags, ['a', $_] for @$cals; | ||
| 91 | } | ||
| 92 | |||
| 93 | return @tags; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub date_event { | ||
| 97 | my $class = shift; | ||
| 98 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 99 | |||
| 100 | my $start = delete $args{start} | ||
| 101 | // croak "date_event requires 'start'"; | ||
| 102 | my $content = delete $args{content} // ''; | ||
| 103 | |||
| 104 | my @tags = $class->_build_common_tags(\%args); | ||
| 105 | |||
| 106 | push @tags, ['start', $start]; | ||
| 107 | if (defined(my $end = delete $args{end})) { | ||
| 108 | push @tags, ['end', $end]; | ||
| 109 | } | ||
| 110 | |||
| 111 | return Net::Nostr::Event->new( | ||
| 112 | %args, | ||
| 113 | kind => 31922, | ||
| 114 | content => $content, | ||
| 115 | tags => \@tags, | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub time_event { | ||
| 120 | my $class = shift; | ||
| 121 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 122 | |||
| 123 | my $start = delete $args{start} | ||
| 124 | // croak "time_event requires 'start'"; | ||
| 125 | my $content = delete $args{content} // ''; | ||
| 126 | |||
| 127 | my @tags = $class->_build_common_tags(\%args); | ||
| 128 | |||
| 129 | push @tags, ['start', "$start"]; | ||
| 130 | if (defined(my $end = delete $args{end})) { | ||
| 131 | push @tags, ['end', "$end"]; | ||
| 132 | } | ||
| 133 | if (my $days = delete $args{days}) { | ||
| 134 | push @tags, ['D', "$_"] for @$days; | ||
| 135 | } | ||
| 136 | if (defined(my $v = delete $args{start_tzid})) { | ||
| 137 | push @tags, ['start_tzid', $v]; | ||
| 138 | } | ||
| 139 | if (defined(my $v = delete $args{end_tzid})) { | ||
| 140 | push @tags, ['end_tzid', $v]; | ||
| 141 | } | ||
| 142 | |||
| 143 | return Net::Nostr::Event->new( | ||
| 144 | %args, | ||
| 145 | kind => 31923, | ||
| 146 | content => $content, | ||
| 147 | tags => \@tags, | ||
| 148 | ); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub calendar { | ||
| 152 | my $class = shift; | ||
| 153 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 154 | |||
| 155 | my $identifier = delete $args{identifier} | ||
| 156 | // croak "calendar requires 'identifier'"; | ||
| 157 | my $title = delete $args{title} | ||
| 158 | // croak "calendar requires 'title'"; | ||
| 159 | my $content = delete $args{content} // ''; | ||
| 160 | my $events = delete $args{events} // []; | ||
| 161 | |||
| 162 | my @tags; | ||
| 163 | push @tags, ['d', $identifier]; | ||
| 164 | push @tags, ['title', $title]; | ||
| 165 | |||
| 166 | for my $evt (@$events) { | ||
| 167 | push @tags, ['a', @$evt]; | ||
| 168 | } | ||
| 169 | |||
| 170 | return Net::Nostr::Event->new( | ||
| 171 | %args, | ||
| 172 | kind => 31924, | ||
| 173 | content => $content, | ||
| 174 | tags => \@tags, | ||
| 175 | ); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub rsvp { | ||
| 179 | my $class = shift; | ||
| 180 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 181 | |||
| 182 | my $identifier = delete $args{identifier} | ||
| 183 | // croak "rsvp requires 'identifier'"; | ||
| 184 | my $event_coord = delete $args{event_coord} | ||
| 185 | // croak "rsvp requires 'event_coord'"; | ||
| 186 | my $status = delete $args{status} | ||
| 187 | // croak "rsvp requires 'status'"; | ||
| 188 | my $content = delete $args{content} // ''; | ||
| 189 | |||
| 190 | my $event_relay = delete $args{event_relay}; | ||
| 191 | my $event_id = delete $args{event_id}; | ||
| 192 | my $event_id_relay = delete $args{event_id_relay}; | ||
| 193 | my $fb = delete $args{fb}; | ||
| 194 | my $event_author = delete $args{event_author}; | ||
| 195 | my $event_author_relay = delete $args{event_author_relay}; | ||
| 196 | |||
| 197 | my @tags; | ||
| 198 | |||
| 199 | # e tag before a tag (per spec example order) | ||
| 200 | if (defined $event_id) { | ||
| 201 | my @e = ('e', $event_id); | ||
| 202 | push @e, $event_id_relay if defined $event_id_relay; | ||
| 203 | push @tags, \@e; | ||
| 204 | } | ||
| 205 | |||
| 206 | # a tag | ||
| 207 | my @a = ('a', $event_coord); | ||
| 208 | push @a, $event_relay if defined $event_relay; | ||
| 209 | push @tags, \@a; | ||
| 210 | |||
| 211 | push @tags, ['d', $identifier]; | ||
| 212 | push @tags, ['status', $status]; | ||
| 213 | |||
| 214 | # fb MUST be omitted if status is declined | ||
| 215 | if (defined $fb && $status ne 'declined') { | ||
| 216 | push @tags, ['fb', $fb]; | ||
| 217 | } | ||
| 218 | |||
| 219 | if (defined $event_author) { | ||
| 220 | my @p = ('p', $event_author); | ||
| 221 | push @p, $event_author_relay if defined $event_author_relay; | ||
| 222 | push @tags, \@p; | ||
| 223 | } | ||
| 224 | |||
| 225 | return Net::Nostr::Event->new( | ||
| 226 | %args, | ||
| 227 | kind => 31925, | ||
| 228 | content => $content, | ||
| 229 | tags => \@tags, | ||
| 230 | ); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub from_event { | ||
| 234 | my ($class, $event) = @_; | ||
| 235 | my $kind = $event->kind; | ||
| 236 | |||
| 237 | return undef unless $kind == 31922 || $kind == 31923 | ||
| 238 | || $kind == 31924 || $kind == 31925; | ||
| 239 | |||
| 240 | if ($kind == 31924) { | ||
| 241 | return $class->_parse_calendar($event); | ||
| 242 | } | ||
| 243 | if ($kind == 31925) { | ||
| 244 | return $class->_parse_rsvp($event); | ||
| 245 | } | ||
| 246 | |||
| 247 | return $class->_parse_calendar_event($event); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub _parse_calendar_event { | ||
| 251 | my ($class, $event) = @_; | ||
| 252 | |||
| 253 | my (%attrs, @locations, @participants, @hashtags, @references, @calendars, @days); | ||
| 254 | my ($name_tag); | ||
| 255 | |||
| 256 | for my $tag (@{$event->tags}) { | ||
| 257 | my $t = $tag->[0]; | ||
| 258 | if ($t eq 'd') { $attrs{identifier} = $tag->[1] } | ||
| 259 | elsif ($t eq 'title') { $attrs{title} = $tag->[1] } | ||
| 260 | elsif ($t eq 'name') { $name_tag = $tag->[1] } | ||
| 261 | elsif ($t eq 'start') { $attrs{start} = $tag->[1] } | ||
| 262 | elsif ($t eq 'end') { $attrs{end} = $tag->[1] } | ||
| 263 | elsif ($t eq 'summary') { $attrs{summary} = $tag->[1] } | ||
| 264 | elsif ($t eq 'image') { $attrs{image} = $tag->[1] } | ||
| 265 | elsif ($t eq 'location') { push @locations, $tag->[1] } | ||
| 266 | elsif ($t eq 'g') { $attrs{geohash} = $tag->[1] } | ||
| 267 | elsif ($t eq 'p') { push @participants, [@{$tag}[1 .. $#$tag]] } | ||
| 268 | elsif ($t eq 't') { push @hashtags, $tag->[1] } | ||
| 269 | elsif ($t eq 'r') { push @references, $tag->[1] } | ||
| 270 | elsif ($t eq 'a') { push @calendars, $tag->[1] } | ||
| 271 | elsif ($t eq 'start_tzid') { $attrs{start_tzid} = $tag->[1] } | ||
| 272 | elsif ($t eq 'end_tzid') { $attrs{end_tzid} = $tag->[1] } | ||
| 273 | elsif ($t eq 'D') { push @days, $tag->[1] } | ||
| 274 | } | ||
| 275 | |||
| 276 | # Deprecated: name mapped to title | ||
| 277 | $attrs{title} //= $name_tag; | ||
| 278 | |||
| 279 | return $class->new( | ||
| 280 | %attrs, | ||
| 281 | description => $event->content, | ||
| 282 | locations => \@locations, | ||
| 283 | participants => \@participants, | ||
| 284 | hashtags => \@hashtags, | ||
| 285 | references => \@references, | ||
| 286 | calendars => \@calendars, | ||
| 287 | days => \@days, | ||
| 288 | ); | ||
| 289 | } | ||
| 290 | |||
| 291 | sub _parse_calendar { | ||
| 292 | my ($class, $event) = @_; | ||
| 293 | |||
| 294 | my ($identifier, $title, @cal_events); | ||
| 295 | |||
| 296 | for my $tag (@{$event->tags}) { | ||
| 297 | my $t = $tag->[0]; | ||
| 298 | if ($t eq 'd') { $identifier = $tag->[1] } | ||
| 299 | elsif ($t eq 'title') { $title = $tag->[1] } | ||
| 300 | elsif ($t eq 'a') { push @cal_events, [@{$tag}[1 .. $#$tag]] } | ||
| 301 | } | ||
| 302 | |||
| 303 | return $class->new( | ||
| 304 | identifier => $identifier, | ||
| 305 | title => $title, | ||
| 306 | description => $event->content, | ||
| 307 | calendar_events => \@cal_events, | ||
| 308 | ); | ||
| 309 | } | ||
| 310 | |||
| 311 | sub _parse_rsvp { | ||
| 312 | my ($class, $event) = @_; | ||
| 313 | |||
| 314 | my %attrs; | ||
| 315 | |||
| 316 | for my $tag (@{$event->tags}) { | ||
| 317 | my $t = $tag->[0]; | ||
| 318 | if ($t eq 'd') { $attrs{identifier} = $tag->[1] } | ||
| 319 | elsif ($t eq 'a') { $attrs{event_coord} = $tag->[1] } | ||
| 320 | elsif ($t eq 'e') { $attrs{event_id} = $tag->[1] } | ||
| 321 | elsif ($t eq 'status') { $attrs{status} = $tag->[1] } | ||
| 322 | elsif ($t eq 'fb') { $attrs{fb} = $tag->[1] } | ||
| 323 | elsif ($t eq 'p') { $attrs{event_author} = $tag->[1] } | ||
| 324 | } | ||
| 325 | |||
| 326 | return $class->new( | ||
| 327 | %attrs, | ||
| 328 | description => $event->content, | ||
| 329 | ); | ||
| 330 | } | ||
| 331 | |||
| 332 | sub validate { | ||
| 333 | my ($class, $event) = @_; | ||
| 334 | my $kind = $event->kind; | ||
| 335 | |||
| 336 | croak "calendar event MUST be kind 31922, 31923, 31924, or 31925" | ||
| 337 | unless $kind == 31922 || $kind == 31923 | ||
| 338 | || $kind == 31924 || $kind == 31925; | ||
| 339 | |||
| 340 | my %has; | ||
| 341 | for my $tag (@{$event->tags}) { | ||
| 342 | $has{$tag->[0]} = $tag->[1]; | ||
| 343 | } | ||
| 344 | |||
| 345 | if ($kind == 31922 || $kind == 31923) { | ||
| 346 | croak "calendar event MUST have a 'd' tag" unless exists $has{d}; | ||
| 347 | croak "calendar event MUST have a 'title' tag" unless exists $has{title}; | ||
| 348 | croak "calendar event MUST have a 'start' tag" unless exists $has{start}; | ||
| 349 | |||
| 350 | if (exists $has{end}) { | ||
| 351 | croak "start MUST be less than end" | ||
| 352 | if $has{start} ge $has{end}; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | if ($kind == 31923) { | ||
| 357 | croak "time-based calendar event MUST have a 'D' tag" unless exists $has{D}; | ||
| 358 | } | ||
| 359 | |||
| 360 | if ($kind == 31924) { | ||
| 361 | croak "calendar MUST have a 'd' tag" unless exists $has{d}; | ||
| 362 | croak "calendar MUST have a 'title' tag" unless exists $has{title}; | ||
| 363 | } | ||
| 364 | |||
| 365 | if ($kind == 31925) { | ||
| 366 | croak "RSVP MUST have a 'd' tag" unless exists $has{d}; | ||
| 367 | croak "RSVP MUST have an 'a' tag" unless exists $has{a}; | ||
| 368 | croak "RSVP MUST have a 'status' tag" unless exists $has{status}; | ||
| 369 | croak "RSVP status MUST be accepted, declined, or tentative" | ||
| 370 | unless $has{status} =~ /\A(?:accepted|declined|tentative)\z/; | ||
| 371 | } | ||
| 372 | |||
| 373 | return 1; | ||
| 374 | } | ||
| 375 | |||
| 376 | 1; | ||
| 377 | |||
| 378 | __END__ | ||