]> git.sesse.net Git - skvidarsync/blob - bin/refresh-reactions.pl
Remove HKS 2024 code.
[skvidarsync] / bin / refresh-reactions.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 no warnings qw(once);
5 use Crypt::JWT;
6 use JSON::XS;
7 use LWP::UserAgent;
8 use DBI;
9 use POSIX;
10 use Time::HiRes;
11 use IO::Select;
12 use Unicode::Collate;
13 use IO::Socket::SSL;
14 binmode STDOUT, ':utf8';
15 binmode STDERR, ':utf8';
16 use utf8;
17
18 require '../include/config.pm';
19
20 my $global_ctx = IO::Socket::SSL::SSL_Context->new(
21         SSL_session_cache_size => 100,  # Probably overkill.
22 );
23 IO::Socket::SSL::set_default_context($global_ctx);
24
25 sub get_slack_name {
26         my ($ua, $userid) = @_;
27         my $req = HTTP::Request->new('GET', 'https://slack.com/api/users.info?user=' . $userid, [
28                'Authorization' => 'Bearer ' . $config::slack_oauth_token
29         ]);
30         my $start = [Time::HiRes::gettimeofday];
31         my $response = $ua->request($req);
32         log_timing($start, '/users.info');
33         die $response->status_line if !$response->is_success;
34
35         my $user_json = JSON::XS::decode_json($response->decoded_content);
36         die "Something went wrong: " . $response->decoded_content if (!defined($user_json) || !$user_json->{'ok'});
37
38         return $user_json->{'user'}{'real_name'};
39 }
40
41 sub db_connect {
42         my $dbh = DBI->connect("dbi:Pg:dbname=$config::dbname;host=127.0.0.1", $config::dbuser, $config::dbpass, {RaiseError => 1})
43                 or warn "Could not connect to Postgres: " . DBI->errstr;
44         if (!defined($dbh)) {
45                 return undef;
46         }
47         $dbh->{AutoCommit} = 0;
48         $dbh->do('LISTEN skvupdate') or return undef;
49         return $dbh;
50 }
51
52 my $channel = $ARGV[1] // 'C06C34L2R6G';
53 my $ts = $ARGV[2] // '1712686401.430939';
54
55 my $dbh = db_connect() or die;
56 my $ua = LWP::UserAgent->new(agent => 'SKVidarLang/1.0', keep_alive => 50);
57 $dbh->do('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
58
59 my $req = HTTP::Request->new('GET', 'https://slack.com/api/reactions.get?channel=' . $channel . '&timestamp=' . $ts . '&full=true', [
60        'Authorization' => 'Bearer ' . $config::slack_oauth_token
61 ]);
62 my $start = [Time::HiRes::gettimeofday];
63 my $response = $ua->request($req);
64 die $response->status_line if !$response->is_success;
65
66 my $user_json = JSON::XS::decode_json($response->decoded_content);
67 die "Something went wrong: " . $response->decoded_content if (!defined($user_json) || !$user_json->{'ok'});
68
69 #print  $response->decoded_content, "\n";
70
71 my $q = $dbh->prepare('SELECT COUNT(*) AS num FROM current_reactions WHERE channel=? AND ts=? AND userid=? AND reaction=?');
72 for my $reaction (@{$user_json->{'message'}{'reactions'}}) {
73         my $name = $reaction->{'name'};
74         for my $user (@{$reaction->{'users'}}) {
75                 $q->execute($channel, $ts, $user, $name);
76                 my $ref = $q->fetchrow_hashref;
77                 if ($ref->{'num'} == 0) {
78                         $dbh->do('INSERT INTO reaction_log (userid, channel, ts, event_type, event_ts, reaction) VALUES (?, ?, ?, ?, ?, ?)',
79                                 undef, $user, $channel, $ts, 'reaction_added', time, $name);
80                         print "$name $user\n";
81                 }
82         }
83 }
84
85 $dbh->commit;
86 $dbh->do('NOTIFY skvupdate');
87 $dbh->commit;