fix(#135): use user configured timezone in Playlists

This commit is contained in:
Benjamin Nussbaum 2025-12-29 22:16:29 +01:00
parent d4b5cf99d5
commit e6d66af298
2 changed files with 63 additions and 6 deletions

View file

@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;
class Playlist extends Model class Playlist extends Model
{ {
@ -37,21 +38,32 @@ class Playlist extends Model
return false; return false;
} }
// Check weekday // Get user's timezone or fall back to app timezone
if ($this->weekdays !== null && ! in_array(now()->dayOfWeek, $this->weekdays)) { $timezone = $this->device->user->timezone ?? config('app.timezone');
$now = now($timezone);
// Check weekday (using timezone-aware time)
if ($this->weekdays !== null && ! in_array($now->dayOfWeek, $this->weekdays)) {
return false; return false;
} }
if ($this->active_from !== null && $this->active_until !== null) { if ($this->active_from !== null && $this->active_until !== null) {
$now = now(); // Create timezone-aware datetime objects for active_from and active_until
$activeFrom = $now->copy()
->setTimeFrom($this->active_from)
->timezone($timezone);
$activeUntil = $now->copy()
->setTimeFrom($this->active_until)
->timezone($timezone);
// Handle time ranges that span across midnight // Handle time ranges that span across midnight
if ($this->active_from > $this->active_until) { if ($activeFrom > $activeUntil) {
// Time range spans midnight (e.g., 09:01 to 03:58) // Time range spans midnight (e.g., 09:01 to 03:58)
if ($now >= $this->active_from || $now <= $this->active_until) { if ($now >= $activeFrom || $now <= $activeUntil) {
return true; return true;
} }
} elseif ($now >= $this->active_from && $now <= $this->active_until) { } elseif ($now >= $activeFrom && $now <= $activeUntil) {
return true; return true;
} }

View file

@ -130,3 +130,48 @@ test('playlist isActiveNow handles normal time ranges correctly', function (): v
Carbon::setTestNow(Carbon::create(2024, 1, 1, 20, 0, 0)); Carbon::setTestNow(Carbon::create(2024, 1, 1, 20, 0, 0));
expect($playlist->isActiveNow())->toBeFalse(); expect($playlist->isActiveNow())->toBeFalse();
}); });
test('playlist scheduling respects user timezone preference', function (): void {
// Create a user with a timezone that's UTC+1 (e.g., Europe/Berlin)
// This simulates the bug where setting 00:15 doesn't work until one hour later
$user = User::factory()->create([
'timezone' => 'Europe/Berlin', // UTC+1 in winter, UTC+2 in summer
]);
$device = Device::factory()->create(['user_id' => $user->id]);
// Create a playlist that should be active from 00:15 to 01:00 in the user's timezone
$playlist = Playlist::factory()->create([
'device_id' => $device->id,
'is_active' => true,
'active_from' => '00:15',
'active_until' => '01:00',
'weekdays' => null,
]);
// Set test time to 00:15 in the user's timezone (Europe/Berlin)
// In January, Europe/Berlin is UTC+1, so 00:15 Berlin time = 23:15 UTC the previous day
// But Carbon::setTestNow uses UTC by default, so we need to set it to the UTC equivalent
// For January 1, 2024 at 00:15 Berlin time (UTC+1), that's December 31, 2023 at 23:15 UTC
$berlinTime = Carbon::create(2024, 1, 1, 0, 15, 0, 'Europe/Berlin');
Carbon::setTestNow($berlinTime->utc());
// The playlist should be active at 00:15 in the user's timezone
// This test should pass after the fix, but will fail with the current bug
expect($playlist->isActiveNow())->toBeTrue();
// Test at 00:30 in user's timezone - should still be active
$berlinTime = Carbon::create(2024, 1, 1, 0, 30, 0, 'Europe/Berlin');
Carbon::setTestNow($berlinTime->utc());
expect($playlist->isActiveNow())->toBeTrue();
// Test at 01:15 in user's timezone - should NOT be active (past the end time)
$berlinTime = Carbon::create(2024, 1, 1, 1, 15, 0, 'Europe/Berlin');
Carbon::setTestNow($berlinTime->utc());
expect($playlist->isActiveNow())->toBeFalse();
// Test at 00:10 in user's timezone - should NOT be active (before start time)
$berlinTime = Carbon::create(2024, 1, 1, 0, 10, 0, 'Europe/Berlin');
Carbon::setTestNow($berlinTime->utc());
expect($playlist->isActiveNow())->toBeFalse();
});