14 binmode STDOUT, ':utf8';
15 binmode STDERR, ':utf8';
18 require '../include/config.pm';
20 my $global_ctx = IO::Socket::SSL::SSL_Context->new(
21 SSL_session_cache_size => 100, # Probably overkill.
23 IO::Socket::SSL::set_default_context($global_ctx);
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
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;
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'});
38 return $user_json->{'user'}{'real_name'};
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;
47 $dbh->{AutoCommit} = 0;
48 $dbh->do('LISTEN skvupdate') or return undef;
52 my $channel = $ARGV[1] // 'C06C34L2R6G';
53 my $ts = $ARGV[2] // '1712686401.430939';
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');
59 my $req = HTTP::Request->new('GET', 'https://slack.com/api/reactions.get?channel=' . $channel . '×tamp=' . $ts . '&full=true', [
60 'Authorization' => 'Bearer ' . $config::slack_oauth_token
62 my $start = [Time::HiRes::gettimeofday];
63 my $response = $ua->request($req);
64 die $response->status_line if !$response->is_success;
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'});
69 #print $response->decoded_content, "\n";
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";
86 $dbh->do('NOTIFY skvupdate');