Add OBS overlays (goal, timer, counter, alerts) with ETag polling
Align timer state key with rules engine (timer_ends_at). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7155396312
commit
65082fb2f5
10 files changed
+815
No files matched your search
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Rest;
|
||||
|
||||
use StreamGoals\Core\Settings;
|
||||
use StreamGoals\Overlay\OverlayService;
|
||||
use StreamGoals\Overlay\Payloads;
|
||||
use StreamGoals\Overlay\Router;
|
||||
use StreamGoals\Overlay\WpdbDataSource;
|
||||
|
||||
/** GET streamgoals/v1/overlay/{token}/{widget} — public-safe, ETag'd JSON for OBS polling. */
|
||||
final class OverlayController {
|
||||
|
||||
public static function register(): void {
|
||||
add_action( 'rest_api_init', array( self::class, 'routes' ) );
|
||||
Router::register();
|
||||
}
|
||||
|
||||
public static function routes(): void {
|
||||
register_rest_route( 'streamgoals/v1', '/overlay/(?P<token>[A-Za-z0-9]{16,128})/(?P<widget>goal|timer|counter|alerts)', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( self::class, 'handle' ),
|
||||
'permission_callback' => '__return_true', // capability is the unguessable token, checked in handle()
|
||||
'args' => array(
|
||||
'since' => array( 'type' => 'integer', 'minimum' => 0, 'sanitize_callback' => 'absint' ),
|
||||
),
|
||||
) );
|
||||
}
|
||||
|
||||
/** Lazily generates the token (32 hex chars) on first use. */
|
||||
public static function token(): string {
|
||||
$token = (string) Settings::get( 'overlay_token', '' );
|
||||
if ( strlen( $token ) < 32 ) {
|
||||
$token = bin2hex( random_bytes( 16 ) );
|
||||
Settings::update( array( 'overlay_token' => $token ) );
|
||||
}
|
||||
return $token;
|
||||
}
|
||||
|
||||
public static function verify_token( string $given ): bool {
|
||||
return hash_equals( self::token(), $given );
|
||||
}
|
||||
|
||||
public static function handle( \WP_REST_Request $req ): \WP_REST_Response|\WP_Error {
|
||||
if ( ! self::verify_token( (string) $req['token'] ) ) {
|
||||
return new \WP_Error( 'sg_not_found', 'Not found.', array( 'status' => 404 ) );
|
||||
}
|
||||
$since = $req->has_param( 'since' ) && '' !== (string) $req->get_param( 'since' ) ? absint( $req->get_param( 'since' ) ) : null;
|
||||
$payload = ( new OverlayService( new WpdbDataSource() ) )->payload( (string) $req['widget'], time(), $since );
|
||||
if ( null === $payload ) {
|
||||
return new \WP_Error( 'sg_not_found', 'Not found.', array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$etag = Payloads::etag( $payload );
|
||||
$headers = array( 'ETag' => $etag, 'Cache-Control' => 'private, max-age=1, must-revalidate', 'X-Robots-Tag' => 'noindex' );
|
||||
$inm = (string) $req->get_header( 'if_none_match' );
|
||||
if ( '' !== $inm && in_array( $etag, array_map( 'trim', explode( ',', str_replace( 'W/', '', $inm ) ) ), true ) ) {
|
||||
$res = new \WP_REST_Response( null, 304 );
|
||||
} else {
|
||||
$res = new \WP_REST_Response( $payload, 200 );
|
||||
}
|
||||
foreach ( $headers as $k => $v ) {
|
||||
$res->header( $k, $v );
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user