Alert media: images/GIFs/videos/sounds tied to donation amount or reward; release tag collision fix
- 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:
1 parent
1201db175b
commit
2c16a9ebbb
22 files changed
+1591
-22
No files matched your search
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Alerts;
|
||||
|
||||
use StreamGoals\Admin\Input;
|
||||
|
||||
/** Pure validation/sanitizing for the alert definition form. */
|
||||
final class DefInput {
|
||||
|
||||
/**
|
||||
* @param int[] $validGoalIds Existing goal ids (0 = all goals always allowed).
|
||||
* @param int[] $enabledActionIds Enabled rewards that may be chosen as a trigger.
|
||||
* @param callable(int):?array{mime:string,size?:int} $attachment Lookup of an attachment by id (null = missing).
|
||||
* @return array{data:?array,error:?string,warnings:string[]}
|
||||
*/
|
||||
public static function validate( array $post, int $minMinor, string $currency, array $validGoalIds, array $enabledActionIds, callable $attachment, int $defaultSort = 0 ): array {
|
||||
$fail = static fn( string $m ): array => array( 'data' => null, 'error' => $m, 'warnings' => array() );
|
||||
|
||||
$title = Input::text( $post['title'] ?? '', 200 );
|
||||
if ( '' === $title ) {
|
||||
return $fail( 'Please give this alert a name, for example "Big donation fireworks".' );
|
||||
}
|
||||
if ( mb_strlen( $title ) > 120 ) {
|
||||
return $fail( 'The name can be at most 120 characters.' );
|
||||
}
|
||||
$goal = Input::int( $post['goal_id'] ?? '0', 0, PHP_INT_MAX >> 1 );
|
||||
if ( null === $goal || ( 0 !== $goal && ! in_array( $goal, array_map( 'intval', $validGoalIds ), true ) ) ) {
|
||||
return $fail( 'Please choose all goals or one of your goals.' );
|
||||
}
|
||||
$trigger = is_scalar( $post['trigger_type'] ?? null ) ? (string) $post['trigger_type'] : '';
|
||||
$min = 0;
|
||||
$action = 0;
|
||||
if ( 'amount' === $trigger ) {
|
||||
$min = Input::amountToMinor( $post['amount'] ?? '', $currency );
|
||||
if ( null === $min || $min <= 0 ) {
|
||||
return $fail( 'Please enter the smallest donation that should trigger this alert, for example 10.00.' );
|
||||
}
|
||||
if ( $min < $minMinor ) {
|
||||
return $fail( 'The amount must be at least the minimum donation (' . \StreamGoals\Admin\Format::money( $minMinor, $currency ) . ').' );
|
||||
}
|
||||
} elseif ( 'reward' === $trigger ) {
|
||||
$action = Input::int( $post['action_id'] ?? '', 1, PHP_INT_MAX >> 1 );
|
||||
if ( null === $action || ! in_array( $action, array_map( 'intval', $enabledActionIds ), true ) ) {
|
||||
return $fail( 'Please choose one of your turned-on rewards.' );
|
||||
}
|
||||
} else {
|
||||
return $fail( 'Please choose when this alert plays.' );
|
||||
}
|
||||
|
||||
$warnings = array();
|
||||
$visualId = 0;
|
||||
$visual = '';
|
||||
$sound = 0;
|
||||
$vRaw = Input::int( $post['visual_id'] ?? '0', 0, PHP_INT_MAX >> 1 );
|
||||
$sRaw = Input::int( $post['sound_id'] ?? '0', 0, PHP_INT_MAX >> 1 );
|
||||
if ( null === $vRaw || null === $sRaw ) {
|
||||
return $fail( 'The chosen media file is not valid. Please pick it again.' );
|
||||
}
|
||||
if ( $vRaw > 0 ) {
|
||||
$a = $attachment( $vRaw );
|
||||
if ( ! is_array( $a ) ) {
|
||||
return $fail( 'The picture or video you chose no longer exists in your Media Library. Please pick it again.' );
|
||||
}
|
||||
$type = Media::visualType( $a['mime'] ?? null );
|
||||
if ( null === $type ) {
|
||||
return $fail( 'The picture or video must be PNG, JPEG, GIF, WebP, MP4 or WebM. Other file types are not allowed.' );
|
||||
}
|
||||
$visualId = $vRaw;
|
||||
$visual = $type;
|
||||
self::sizeWarning( $a, 'picture or video', 'video' === $type, $warnings );
|
||||
}
|
||||
if ( $sRaw > 0 ) {
|
||||
$a = $attachment( $sRaw );
|
||||
if ( ! is_array( $a ) ) {
|
||||
return $fail( 'The sound you chose no longer exists in your Media Library. Please pick it again.' );
|
||||
}
|
||||
if ( ! Media::isSound( $a['mime'] ?? null ) ) {
|
||||
return $fail( 'The sound must be an MP3, OGG or WAV file.' );
|
||||
}
|
||||
$sound = $sRaw;
|
||||
self::sizeWarning( $a, 'sound', false, $warnings );
|
||||
}
|
||||
|
||||
$vol = Input::int( $post['volume'] ?? '80', 0, 100 );
|
||||
if ( null === $vol ) {
|
||||
return $fail( 'Volume must be a whole number from 0 to 100.' );
|
||||
}
|
||||
$dur = Media::secondsToMs( $post['duration'] ?? '6' );
|
||||
if ( null === $dur || (int) round( (float) $post['duration'] * 1000 ) !== $dur ) {
|
||||
return $fail( 'How long the picture stays must be between 1 and 60 seconds.' );
|
||||
}
|
||||
$message = Input::text( $post['message'] ?? '', 400 );
|
||||
if ( mb_strlen( $message ) > 255 ) {
|
||||
return $fail( 'The message can be at most 255 characters.' );
|
||||
}
|
||||
$sortRaw = $post['sort'] ?? '';
|
||||
$sort = ( is_scalar( $sortRaw ) && '' === trim( (string) $sortRaw ) ) ? $defaultSort : Input::int( $sortRaw, 0, 99999 );
|
||||
if ( null === $sort ) {
|
||||
return $fail( 'The sort order must be a whole number from 0 to 99999.' );
|
||||
}
|
||||
return array(
|
||||
'data' => array(
|
||||
'goal_id' => $goal,
|
||||
'trigger_type' => $trigger,
|
||||
'min_minor' => $min,
|
||||
'action_id' => $action,
|
||||
'title' => $title,
|
||||
'visual_id' => $visualId,
|
||||
'visual_type' => $visual,
|
||||
'sound_id' => $sound,
|
||||
'volume' => $vol,
|
||||
'duration_ms' => $dur,
|
||||
'message' => $message,
|
||||
'sort' => $sort,
|
||||
),
|
||||
'error' => null,
|
||||
'warnings' => $warnings,
|
||||
);
|
||||
}
|
||||
|
||||
/** @param string[] $warnings */
|
||||
private static function sizeWarning( array $a, string $what, bool $video, array &$warnings ): void {
|
||||
if ( (int) ( $a['size'] ?? 0 ) > Media::WARN_BYTES ) {
|
||||
$warnings[] = 'The ' . $what . ' is over 25 MB.' . ( $video ? ' Big videos can make OBS stutter or load slowly; a short, compressed clip works much better.' : ' Large files take longer to load, so the alert may start late.' );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user