]> git.sesse.net Git - skvidarsync/commitdiff
Split out create_reaction_log() into its own function.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 28 Oct 2023 17:34:40 +0000 (19:34 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 28 Oct 2023 17:34:40 +0000 (19:34 +0200)
bin/sync.pl

index 22dbd9ece2c3682faeab6377ab68dae250f7d782..aecc86dae67333f0f396e31033fe00f16e41536a 100644 (file)
@@ -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,