Alerts overlay: show messages approved after later donations (recent mode with client-side dedupe)
Test and Release / test (push) Successful in 1m10s
Test and Release / release (push) Successful in 5s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 12:26:03 -08:00
1 parent 875ca42054
commit f7ff1dc717
7 files changed
+170 -24

No files matched your search

+13 -4
View File
@@ -10,7 +10,7 @@ final class OverlayService {
public function __construct( private OverlayDataSource $source ) {}
public function payload( string $widget, int $now, ?int $since = null ): ?array {
public function payload( string $widget, int $now, ?int $since = null, bool $recent = false ): ?array {
$goal = $this->source->activeGoal();
$gid = $goal ? (int) $goal['id'] : 0;
switch ( $widget ) {
@@ -26,13 +26,22 @@ final class OverlayService {
$c = $gid ? $this->source->counter( $gid ) : array( 'label' => '', 'value' => 0 );
return Payloads::counter( $c['label'], $c['value'] );
case 'alerts':
$rows = $this->source->donations( $since, 20 );
if ( $recent ) {
// Newest 20 approved rows regardless of cursor. Sources that predate RecentDonationsSource
// fall back to the oldest 20 (still correct for small installs, just not "newest").
$rows = $this->source instanceof RecentDonationsSource
? $this->source->recentDonations( 20 )
: $this->source->donations( 0, 20 );
$since = null;
} else {
$rows = $this->source->donations( $since, 20 );
}
$ids = array_values( array_filter( array_map( static fn( $r ) => (int) ( $r['id'] ?? 0 ), $rows ) ) );
$bonuses = ( null !== $since && $ids ) ? $this->source->bonusesFor( $ids ) : array();
$bonuses = ( ( $recent || null !== $since ) && $ids ) ? $this->source->bonusesFor( $ids ) : array();
foreach ( $rows as $i => $r ) {
$rows[ $i ]['bonuses'] = $bonuses[ (int) ( $r['id'] ?? 0 ) ] ?? array();
}
return Payloads::alerts( $rows, $since );
return Payloads::alerts( $rows, $since, $recent ? 20 : 10, $recent );
}
return null;
}
+26 -14
View File
@@ -74,9 +74,11 @@ final class Payloads {
/**
* @param array<int,array<string,mixed>> $rows
* @param int|null $since Null means "first poll": no alerts, just a baseline last_id.
* @param int|null $since Null means "first poll": no alerts, just a baseline last_id.
* @param bool $recent Recent mode: ignore $since and return the newest $limit approved alerts (ascending id),
* so a message approved after newer donations still shows up. The client dedupes by id.
*/
public static function alerts( array $rows, ?int $since, int $limit = 10 ): array {
public static function alerts( array $rows, ?int $since, int $limit = 10, bool $recent = false ): array {
$safe = array();
foreach ( $rows as $r ) {
if ( 'completed' !== ( $r['status'] ?? '' ) || 'approved' !== ( $r['moderation'] ?? '' ) ) {
@@ -85,6 +87,11 @@ final class Payloads {
$safe[] = $r;
}
usort( $safe, static fn( $a, $b ) => (int) $a['id'] <=> (int) $b['id'] );
if ( $recent ) {
$safe = array_slice( $safe, -max( 1, $limit ) );
$alerts = array_map( array( self::class, 'alert' ), $safe );
return array( 'alerts' => $alerts, 'last_id' => $alerts ? (int) end( $alerts )['id'] : 0 );
}
$lastId = $since ?? 0;
foreach ( $safe as $r ) {
$lastId = max( $lastId, (int) $r['id'] );
@@ -97,18 +104,7 @@ final class Payloads {
if ( (int) $r['id'] <= $since ) {
continue;
}
$cur = strtoupper( (string) ( $r['currency'] ?? 'USD' ) );
$anon = ! empty( $r['is_anonymous'] );
$name = trim( (string) ( $r['donor_name'] ?? '' ) );
$alerts[] = array(
'id' => (int) $r['id'],
'name' => ( $anon || '' === $name ) ? self::ANON_NAME : $name,
'amount_minor' => (int) $r['base_minor'],
'amount_display' => self::money( (int) $r['base_minor'], $cur ),
'currency' => $cur,
'message' => (string) ( $r['message'] ?? '' ),
'bonuses' => self::bonuses( $r['bonuses'] ?? array() ),
);
$alerts[] = self::alert( $r );
}
$alerts = array_slice( $alerts, 0, max( 1, $limit ) );
if ( $alerts ) {
@@ -117,6 +113,22 @@ final class Payloads {
return array( 'alerts' => $alerts, 'last_id' => $lastId );
}
/** @param array<string,mixed> $r */
private static function alert( array $r ): array {
$cur = strtoupper( (string) ( $r['currency'] ?? 'USD' ) );
$anon = ! empty( $r['is_anonymous'] );
$name = trim( (string) ( $r['donor_name'] ?? '' ) );
return array(
'id' => (int) $r['id'],
'name' => ( $anon || '' === $name ) ? self::ANON_NAME : $name,
'amount_minor' => (int) $r['base_minor'],
'amount_display' => self::money( (int) $r['base_minor'], $cur ),
'currency' => $cur,
'message' => (string) ( $r['message'] ?? '' ),
'bonuses' => self::bonuses( $r['bonuses'] ?? array() ),
);
}
/**
* Display-safe bonus labels. Stored events should be [A-Za-z0-9_-] only; anything else is stripped
* anyway, then humanized ("big_donation" -> "Big donation"). Empty/duplicate results are dropped.
@@ -0,0 +1,15 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Overlay;
/** Optional capability: the newest approved donations, independent of any client cursor. */
interface RecentDonationsSource {
/**
* The $limit newest completed + approved donations, ascending id.
*
* @return array<int,array<string,mixed>>
*/
public function recentDonations( int $limit ): array;
}
+11 -1
View File
@@ -4,7 +4,7 @@ declare( strict_types=1 );
namespace StreamGoals\Overlay;
/** $wpdb adapter reading sg_goals, sg_donations, sg_state, sg_rules directly. */
final class WpdbDataSource implements OverlayDataSource {
final class WpdbDataSource implements OverlayDataSource, RecentDonationsSource {
private function t( string $name ): string {
global $wpdb;
@@ -102,4 +102,14 @@ final class WpdbDataSource implements OverlayDataSource {
}
return (array) $rows;
}
public function recentDonations( int $limit ): array {
global $wpdb;
$rows = $wpdb->get_results( $wpdb->prepare(
"SELECT id, base_minor, currency, donor_name, is_anonymous, message, status, moderation FROM {$this->t('donations')}
WHERE status = 'completed' AND moderation = 'approved' ORDER BY id DESC LIMIT %d",
max( 1, $limit )
), ARRAY_A );
return array_reverse( (array) $rows );
}
}