|
| 1 | +use v5.24.0; |
| 2 | +use warnings; |
| 3 | +package Synergy::Reactor::Notion; |
| 4 | + |
| 5 | +use Moose; |
| 6 | +use DateTime; |
| 7 | +with 'Synergy::Role::Reactor::EasyListening'; |
| 8 | + |
| 9 | +use experimental qw(signatures lexical_subs); |
| 10 | +use namespace::clean; |
| 11 | + |
| 12 | +use utf8; |
| 13 | + |
| 14 | +sub listener_specs { |
| 15 | + return ( |
| 16 | + { |
| 17 | + name => 'my_projects', |
| 18 | + method => 'handle_my_projects', |
| 19 | + exclusive => 1, |
| 20 | + predicate => sub ($self, $e) { |
| 21 | + return unless $e->was_targeted; |
| 22 | + return unless fc $e->text eq 'my projects'; |
| 23 | + }, |
| 24 | + }, |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +has api_token => ( |
| 29 | + is => 'ro', |
| 30 | + isa => 'Str', |
| 31 | + required => 1, |
| 32 | +); |
| 33 | + |
| 34 | +has project_db_id => ( |
| 35 | + is => 'ro', |
| 36 | + isa => 'Str', |
| 37 | + required => 1, |
| 38 | +); |
| 39 | + |
| 40 | +has username_domain => ( |
| 41 | + is => 'ro', |
| 42 | + isa => 'Str', |
| 43 | + required => 1, |
| 44 | +); |
| 45 | + |
| 46 | +sub handle_my_projects ($self, $event) { |
| 47 | + $event->mark_handled; |
| 48 | + |
| 49 | + my $db_id = $self->project_db_id; |
| 50 | + my $token = $self->api_token; |
| 51 | + |
| 52 | + my $res = $self->hub->http_post( |
| 53 | + "https://api.notion.com/v1/databases/$db_id/query", |
| 54 | + |
| 55 | + 'User-Agent' => 'Synergy/2021.05', |
| 56 | + Authorization => "Bearer $token", |
| 57 | + 'Notion-Version' => '2021-05-13', |
| 58 | + 'Content-Type' => 'application/json', |
| 59 | + Content => "{}", # TODO: filter here for real |
| 60 | + )->get; # TODO: use sequencing |
| 61 | + |
| 62 | + my $data = JSON::MaybeXS->new->decode( $res->decoded_content(charset => undef) ); |
| 63 | + my @pages = $data->{results}->@*; |
| 64 | + |
| 65 | + my $reply = q{}; |
| 66 | + my $slack = q{}; |
| 67 | + |
| 68 | + my $email = $event->from_user->username . '@' . $self->username_domain; |
| 69 | + |
| 70 | + for my $page (@pages) { |
| 71 | + next if $page->{properties}{'On Hold'}{checkbox}; |
| 72 | + |
| 73 | + my $stage = $page->{properties}{Stage}{select}{name}; |
| 74 | + next if $stage eq 'Done'; |
| 75 | + |
| 76 | + my @people = ( |
| 77 | + $page->{properties}{'Team (Active)'}{people}->@*, |
| 78 | + $page->{properties}{'Coordinator'}{people}->@*, |
| 79 | + ); |
| 80 | + |
| 81 | + next unless grep {; $_->{person}{email} eq $email } @people; |
| 82 | + my $title = join q{ - }, map {; $_->{plain_text} } $page->{properties}{Name}{title}->@*; |
| 83 | + |
| 84 | + my $safe_title = $title; |
| 85 | + $safe_title =~ s{[^A-Za-z0-9]}{-}g; |
| 86 | + |
| 87 | + my $id = $page->{id} =~ s/-//gr; |
| 88 | + |
| 89 | + my $href = "https://www.notion.so/$safe_title-$id"; |
| 90 | + |
| 91 | + $reply .= "$title ($stage)\n"; |
| 92 | + $slack .= sprintf "<%s|%s> (%s)\n", $href, $title, $stage; |
| 93 | + } |
| 94 | + |
| 95 | + unless (length $reply) { |
| 96 | + return $event->reply("Looks like you've got no projects right now!"); |
| 97 | + } |
| 98 | + |
| 99 | + $event->reply($reply, { slack => $slack }); |
| 100 | +} |
| 101 | + |
| 102 | +1; |
0 commit comments