From afef7028bedbe5e1b69e9061d6c71b548cdecc2b Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 28 Oct 2023 19:34:40 +0200 Subject: [PATCH] Split out create_reaction_log() into its own function. --- bin/sync.pl | 70 +++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/bin/sync.pl b/bin/sync.pl index 22dbd9e..aecc86d 100644 --- a/bin/sync.pl +++ b/bin/sync.pl @@ -178,6 +178,43 @@ sub find_where_each_name_is { return %seen_names; } +# Add the reaction log. (This only takes into account the last change +# for each user; earlier ones are irrelevant and don't count. But it +# doesn't deduplicate across reactions. Meh.) +sub create_reaction_log { + my ($dbh, $invitation_ts, $slack_userid_to_real_name, $slack_userid_to_slack_name) = @_; + + my $q = $dbh->prepare('select userid,event_type,reaction,to_char(event_ts,\'YYYY-mm-dd HH24:MI\') as event_ts from ( select distinct on (channel,ts,userid,reaction) userid,event_type,reaction,timestamptz \'1970-01-01 utc\' + event_ts::float * interval \'1 second\' as event_ts from reaction_log where channel=? and ts=? and reaction in (\'heart\',\'open_mouth\') order by channel,ts,userid,reaction,event_ts desc ) t1 where event_ts > current_timestamp - interval \'8 hours\' order by event_ts desc limit 50'); + $q->execute($config::invitation_channel, $invitation_ts); + my @recent_changes = (); + while (my $ref = $q->fetchrow_hashref) { + my $msg = $ref->{'event_ts'}; + if ($ref->{'event_type'} eq 'reaction_added') { + $msg .= ' +'; + } else { + $msg .= ' –'; + } + if ($ref->{'reaction'} eq 'open_mouth') { + $msg .= '😮'; + } else { + $msg .= '❤️'; + } + $msg .= ' '; + if (exists($slack_userid_to_real_name->{$ref->{'userid'}})) { + $msg .= $slack_userid_to_real_name->{$ref->{'userid'}}; + } elsif (exists($slack_userid_to_slack_name->{$ref->{'userid'}})) { + $msg .= $slack_userid_to_slack_name->{$ref->{'userid'}} . ' (fant ikke regneark-navn)'; + } else { + # Should only happen if we didn't see the initial reaction_add, only reaction_remove. + $msg .= $ref->{'userid'} . ' (fant ikke Slack-navn)'; + } + push @recent_changes, { values => [{ userEnteredValue => { stringValue => $msg } }] }; + } + while (scalar @recent_changes < 50) { + push @recent_changes, { values => [{ userEnteredValue => { stringValue => '' } }] }; + } + return @recent_changes; +} skv_log("Siste sync startet: " . POSIX::ctime(time)); @@ -432,38 +469,7 @@ if (scalar @diffs > 0) { } } -# Add the reaction log. (This only takes into account the last change -# for each user; earlier ones are irrelevant and don't count. But it -# doesn't deduplicate across reactions. Meh.) -$q = $dbh->prepare('select userid,event_type,reaction,to_char(event_ts,\'YYYY-mm-dd HH24:MI\') as event_ts from ( select distinct on (channel,ts,userid,reaction) userid,event_type,reaction,timestamptz \'1970-01-01 utc\' + event_ts::float * interval \'1 second\' as event_ts from reaction_log where channel=? and ts=? and reaction in (\'heart\',\'open_mouth\') order by channel,ts,userid,reaction,event_ts desc ) t1 where event_ts > current_timestamp - interval \'8 hours\' order by event_ts desc limit 50'); -$q->execute($config::invitation_channel, $invitation_ts); -my @recent_changes = (); -while (my $ref = $q->fetchrow_hashref) { - my $msg = $ref->{'event_ts'}; - if ($ref->{'event_type'} eq 'reaction_added') { - $msg .= ' +'; - } else { - $msg .= ' –'; - } - if ($ref->{'reaction'} eq 'open_mouth') { - $msg .= '😮'; - } else { - $msg .= '❤️'; - } - $msg .= ' '; - if (exists($slack_userid_to_real_name{$ref->{'userid'}})) { - $msg .= $slack_userid_to_real_name{$ref->{'userid'}}; - } elsif (exists($slack_userid_to_slack_name{$ref->{'userid'}})) { - $msg .= $slack_userid_to_slack_name{$ref->{'userid'}} . ' (fant ikke regneark-navn)'; - } else { - # Should only happen if we didn't see the initial reaction_add, only reaction_remove. - $msg .= $ref->{'userid'} . ' (fant ikke Slack-navn)'; - } - push @recent_changes, { values => [{ userEnteredValue => { stringValue => $msg } }] }; -} -while (scalar @recent_changes < 50) { - push @recent_changes, { values => [{ userEnteredValue => { stringValue => '' } }] }; -} +my @recent_changes = create_reaction_log($dbh, $invitation_ts, \%slack_userid_to_real_name, \%slack_userid_to_slack_name); push @yellow_updates, { updateCells => { rows => \@recent_changes, -- 2.39.2