Alert media: images/GIFs/videos/sounds tied to donation amount or reward; release tag collision fix
Test and Release / test (push) Successful in 1m20s
Test and Release / release (push) Successful in 4s

- Alerts\Resolver/Media/Template/AlertMedia/DefInput/DefRepository; Alerts admin screen with Media Library picker and live preview
- Overlay: media resolved server-side from attachment IDs (MIME whitelist), playback with timeouts and autoplay-failure fallbacks
- Workflow: pick the next free minute if the version tag already exists (two pushes in one minute failed 'Create release')

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 13:01:31 -08:00
1 parent 1201db175b
commit 2c16a9ebbb
22 files changed
+1591 -22

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Overlay;
/** Optional capability: configurable alert media (definitions, triggered rewards, attachment lookup). */
interface AlertMediaSource {
/** Enabled alert definitions, raw rows. Called at most once per request. */
public function alertDefs(): array;
/**
* Rewards triggered per donation, one lookup for the whole page.
*
* @param int[] $donationIds
* @return array<int,array<int,array{action_id:int,amount_minor:int,title:string}>> donation id => rewards
*/
public function triggeredActionsFor( array $donationIds ): array;
/**
* Server-side attachment lookup (the only place media URLs come from). Null when it is gone.
*
* @return array{url:string,mime:string}|null
*/
public function attachment( int $id ): ?array;
}
+26 -1
View File
@@ -8,8 +8,24 @@ final class OverlayService {
public const WIDGETS = array( 'goal', 'timer', 'counter', 'alerts', 'actions' );
/** @var array<int,array<string,mixed>>|null */
private ?array $defs = null;
public function __construct( private OverlayDataSource $source ) {}
/** Enabled alert definitions, loaded at most once per service instance (= per request). */
private function defs(): array {
if ( null === $this->defs ) {
$this->defs = $this->source instanceof AlertMediaSource ? array_values( $this->source->alertDefs() ) : array();
}
return $this->defs;
}
/** Extra ETag input so editing media definitions invalidates cached alert payloads even when no alert is pending. */
public function etagSalt( string $widget ): string {
return 'alerts' === $widget && $this->defs() ? md5( (string) json_encode( $this->defs() ) ) : '';
}
public function payload( string $widget, int $now, ?int $since = null, bool $recent = false ): ?array {
$goal = $this->source->activeGoal();
$gid = $goal ? (int) $goal['id'] : 0;
@@ -43,7 +59,16 @@ final class OverlayService {
$rows[ $i ]['bonuses'] = $bonuses[ (int) ( $r['id'] ?? 0 ) ] ?? array();
$rows[ $i ]['actions'] = $actions[ (int) ( $r['id'] ?? 0 ) ] ?? array();
}
return Payloads::alerts( $rows, $since, $recent ? 20 : 10, $recent );
$mediaFor = null;
$defs = ( $recent || null !== $since ) ? $this->defs() : array();
if ( $defs && $ids && $this->source instanceof AlertMediaSource ) {
$src = $this->source;
$triggered = $src->triggeredActionsFor( $ids );
$mediaFor = static fn( array $r, string $name, string $amount ): ?array => \StreamGoals\Alerts\AlertMedia::build(
$r, $name, $amount, $defs, $triggered[ (int) ( $r['id'] ?? 0 ) ] ?? array(), static fn( int $id ): ?array => $src->attachment( $id )
);
}
return Payloads::alerts( $rows, $since, $recent ? 20 : 10, $recent, $mediaFor );
case 'actions':
$rows = ( $gid && $this->source instanceof ActionsSource ) ? $this->source->menuActions( $gid ) : array();
return Payloads::actionsMenu( $goal, $rows );
+16 -10
View File
@@ -78,7 +78,7 @@ final class Payloads {
* @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, bool $recent = false ): array {
public static function alerts( array $rows, ?int $since, int $limit = 10, bool $recent = false, ?callable $mediaFor = null ): array {
$safe = array();
foreach ( $rows as $r ) {
if ( 'completed' !== ( $r['status'] ?? '' ) || 'approved' !== ( $r['moderation'] ?? '' ) ) {
@@ -89,7 +89,7 @@ final class Payloads {
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 );
$alerts = array_map( static fn( array $r ): array => self::alert( $r, $mediaFor ), $safe );
return array( 'alerts' => $alerts, 'last_id' => $alerts ? (int) end( $alerts )['id'] : 0 );
}
$lastId = $since ?? 0;
@@ -104,7 +104,7 @@ final class Payloads {
if ( (int) $r['id'] <= $since ) {
continue;
}
$alerts[] = self::alert( $r );
$alerts[] = self::alert( $r, $mediaFor );
}
$alerts = array_slice( $alerts, 0, max( 1, $limit ) );
if ( $alerts ) {
@@ -113,20 +113,26 @@ final class Payloads {
return array( 'alerts' => $alerts, 'last_id' => $lastId );
}
/** @param array<string,mixed> $r */
private static function alert( array $r ): array {
/**
* @param array<string,mixed> $r
* @param (callable(array,string,string):?array)|null $mediaFor Builds the `media` block from (row, public name, amount display).
*/
private static function alert( array $r, ?callable $mediaFor = null ): array {
$cur = strtoupper( (string) ( $r['currency'] ?? 'USD' ) );
$anon = ! empty( $r['is_anonymous'] );
$name = trim( (string) ( $r['donor_name'] ?? '' ) );
$pub = ( $anon || '' === $name ) ? self::ANON_NAME : $name;
$disp = self::money( (int) $r['base_minor'], $cur );
return array(
'id' => (int) $r['id'],
'name' => ( $anon || '' === $name ) ? self::ANON_NAME : $name,
'name' => $pub,
'amount_minor' => (int) $r['base_minor'],
'amount_display' => self::money( (int) $r['base_minor'], $cur ),
'amount_display' => $disp,
'currency' => $cur,
'message' => (string) ( $r['message'] ?? '' ),
'bonuses' => self::bonuses( $r['bonuses'] ?? array() ),
'actions' => self::actionEvents( $r['actions'] ?? array() ),
'media' => $mediaFor ? $mediaFor( $r, $pub, $disp ) : null,
);
}
@@ -223,10 +229,10 @@ final class Payloads {
return array( 'active' => (bool) $list, 'currency' => $cur, 'actions' => $list );
}
/** Strong ETag over the payload, ignoring the volatile server_now so unchanged data yields 304s. */
public static function etag( array $payload ): string {
/** Strong ETag over the payload (plus an optional salt, e.g. a hash of media definitions), ignoring the volatile server_now so unchanged data yields 304s. */
public static function etag( array $payload, string $salt = '' ): string {
unset( $payload['server_now'] );
return '"' . md5( (string) json_encode( $payload, JSON_UNESCAPED_UNICODE ) ) . '"';
return '"' . md5( $salt . (string) json_encode( $payload, JSON_UNESCAPED_UNICODE ) ) . '"';
}
private static function toEpoch( ?string $v ): ?int {
+44 -3
View File
@@ -4,7 +4,10 @@ declare( strict_types=1 );
namespace StreamGoals\Overlay;
/** $wpdb adapter reading sg_goals, sg_donations, sg_state, sg_rules directly. */
final class WpdbDataSource implements OverlayDataSource, RecentDonationsSource, ActionsSource {
final class WpdbDataSource implements OverlayDataSource, RecentDonationsSource, ActionsSource, AlertMediaSource {
/** @var array<int,?array{url:string,mime:string}> */
private array $attachments = array();
private function t( string $name ): string {
global $wpdb;
@@ -117,9 +120,47 @@ final class WpdbDataSource implements OverlayDataSource, RecentDonationsSource,
return $out;
}
public function alertDefs(): array {
return ( new \StreamGoals\Alerts\DefRepository( $GLOBALS['wpdb'] ) )->enabled();
}
public function triggeredActionsFor( array $donationIds ): array {
global $wpdb;
$ids = array_values( array_unique( array_filter( array_map( 'intval', $donationIds ), static fn( $i ) => $i > 0 ) ) );
if ( ! $ids ) {
return array();
}
$in = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
$rows = $wpdb->get_results( $wpdb->prepare(
"SELECT e.donation_id, e.action_id, e.title, COALESCE(a.amount_minor,0) AS amount_minor FROM {$this->t('action_events')} e
LEFT JOIN {$this->t('actions')} a ON a.id = e.action_id WHERE e.donation_id IN ($in) ORDER BY e.id ASC",
$ids
), ARRAY_A );
$out = array();
foreach ( (array) $rows as $row ) {
$out[ (int) $row['donation_id'] ][] = array( 'action_id' => (int) $row['action_id'], 'amount_minor' => (int) $row['amount_minor'], 'title' => (string) $row['title'] );
}
return $out;
}
public function attachment( int $id ): ?array {
if ( array_key_exists( $id, $this->attachments ) ) {
return $this->attachments[ $id ];
}
$out = null;
if ( $id > 0 && 'attachment' === get_post_type( $id ) ) {
$mime = (string) get_post_mime_type( $id );
$url = wp_get_attachment_url( $id );
if ( \StreamGoals\Alerts\Media::isAllowed( $mime ) && is_string( $url ) && '' !== $url ) {
$out = array( 'url' => $url, 'mime' => $mime );
}
}
return $this->attachments[ $id ] = $out;
}
public function donations( ?int $sinceId, int $limit ): array {
global $wpdb;
$cols = 'id, base_minor, currency, donor_name, is_anonymous, message, status, moderation';
$cols = 'id, goal_id, base_minor, currency, donor_name, is_anonymous, message, status, moderation';
$where = "status = 'completed' AND moderation = 'approved'";
if ( null === $sinceId ) {
$rows = $wpdb->get_results( "SELECT $cols FROM {$this->t('donations')} WHERE $where ORDER BY id DESC LIMIT 1", ARRAY_A );
@@ -136,7 +177,7 @@ final class WpdbDataSource implements OverlayDataSource, RecentDonationsSource,
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')}
"SELECT id, goal_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 );