Files
Atomcms-edit/app/Models/RadioSongPlay.php
T
root 0c6c558a59 Add radio embed widget, SSE real-time, song history, moderation panel, and Auto DJ
- Embed widget: standalone iframe player with dark/light/transparent themes, copy-paste embed code admin page
- Real-time SSE: streaming now-playing/listeners/dj events, replaces polling in radio-player and embed
- Song history: auto-records song changes to radio_song_plays table, Filament resource to view
- DJ moderation: unified panel for shouts approval, song request queue, DJ applications
- Auto DJ: playlist management with round-robin playback when no DJ is live
- Refactored radio-player Alpine component to use EventSource API with auto-reconnect
2026-05-24 14:07:32 +02:00

68 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Models\Miscellaneous\WebsiteSetting;
use App\Services\Community\RadioScheduleService;
use App\Services\Community\RadioStreamService;
use Illuminate\Database\Eloquent\Model;
/**
* @method static \Illuminate\Database\Eloquent\Builder<static>|RadioSongPlay where($column, $operator = null, $value = null)
* @method static \Illuminate\Database\Eloquent\Builder<static>|RadioSongPlay create($attributes = [])
* @method static \Illuminate\Database\Eloquent\Builder<static>|RadioSongPlay orderBy($column, $direction = 'asc')
* @method static \Illuminate\Database\Eloquent\Builder<static>|RadioSongPlay latest($column = 'played_at')
*
* @property string $title
* @property string|null $artist
* @property string|null $album
* @property string|null $artwork_url
* @property int|null $duration
* @property \Carbon\Carbon $played_at
* @property int|null $dj_id
* @property array|null $metadata
*/
class RadioSongPlay extends Model
{
#[\Override]
protected $table = 'radio_song_plays';
#[\Override]
protected $guarded = ['id', 'created_at', 'updated_at'];
#[\Override]
protected $casts = [
'played_at' => 'datetime',
'metadata' => 'array',
'duration' => 'integer',
];
public static function recordNowPlaying(?string $title, ?string $artist = null, ?array $extra = null): ?self
{
if (! $title) {
return null;
}
$last = self::latest('played_at')->first();
if ($last && $last->title === $title && $last->artist === $artist) {
return null;
}
$djId = WebsiteSetting::where('key', 'radio_current_dj_id')->first()?->value;
return self::create([
'title' => $title,
'artist' => $artist,
'album' => $extra['album'] ?? null,
'artwork_url' => $extra['artwork_url'] ?? null,
'duration' => $extra['duration'] ?? null,
'played_at' => $extra['played_at'] ?? now(),
'dj_id' => $djId ? (int) $djId : null,
'metadata' => $extra['metadata'] ?? null,
]);
}
}