diff --git a/src/Enum/ConstantEnum.php b/src/Enum/ConstantEnum.php index be651f7..45a8a6c 100644 --- a/src/Enum/ConstantEnum.php +++ b/src/Enum/ConstantEnum.php @@ -8,4 +8,6 @@ class ConstantEnum extends Enum { public const PER_PAGE = 15; public const DIRECTORY = 'webinar'; + public const REDIS_IMAGES_KEY = 'signed_urls_index'; + public const REDIS_IMAGES_TTL = 90; } diff --git a/src/Services/WebinarService.php b/src/Services/WebinarService.php index 2031674..097e7d8 100644 --- a/src/Services/WebinarService.php +++ b/src/Services/WebinarService.php @@ -25,6 +25,7 @@ use EscolaLms\Youtube\Services\Contracts\YoutubeServiceContract; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Storage; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -337,11 +338,16 @@ private function canGenerateJitsi(Webinar $webinar): bool $now = now(); $endDate = $this->getWebinarEndDate($webinar); $activeTo = $webinar->active_to ? Carbon::make($webinar->active_to) : null; - return $webinar->isPublished() && + $baseConditions = $webinar->isPublished() && $endDate && ($activeTo && $now->getTimestamp() >= $activeTo->getTimestamp()) && - $now->getTimestamp() <= $endDate->getTimestamp() && - $webinar->hasYT(); + $now->getTimestamp() <= $endDate->getTimestamp(); + + if (!$baseConditions) { + return false; + } + + return !$this->youtubeServiceContract->isConfigured() || $webinar->hasYT(); } /** @@ -382,6 +388,13 @@ public function generateSignedScreenUrls(GenerateSignedScreenUrlsDto $dto): arra return array_map(function ($file) use ($directory) { $filename = $file['filename']; + if (config('cache.default') === 'redis') { + $key = 'signed_urls:' . md5($directory . $filename); + Redis::command('SETEX', [$key, ConstantEnum::REDIS_IMAGES_TTL, $directory . $filename]); + Redis::command('HSET', [ConstantEnum::REDIS_IMAGES_KEY, $key, 1]); + Redis::command('EXPIRE', [ConstantEnum::REDIS_IMAGES_KEY, ConstantEnum::REDIS_IMAGES_TTL]); + } + return array_merge( ['filename' => $filename], Storage::temporaryUploadUrl($directory . $filename, now()->addMinutes(5)) diff --git a/tests/APIs/WebinarApiTest.php b/tests/APIs/WebinarApiTest.php index 80b9885..76d3a56 100644 --- a/tests/APIs/WebinarApiTest.php +++ b/tests/APIs/WebinarApiTest.php @@ -48,6 +48,7 @@ protected function setUp(): void public function testWebinarsList(): void { $webinarService = $this->mock(YoutubeServiceContract::class); + $webinarService->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $webinarService->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect()); $this->response = $this->actingAs($this->user, 'api')->get('/api/admin/webinars'); $this->response->assertOk(); @@ -56,6 +57,7 @@ public function testWebinarsList(): void public function testWebinarsListWithFilter(): void { $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); $filterData = [ 'name=' . $this->webinar->name, @@ -75,6 +77,7 @@ public function testWebinarsListWithFilter(): void public function testWebinarsListWithSorts(): void { $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); $testWebinar = Webinar::factory()->create([ @@ -174,6 +177,7 @@ public function testWebinarsListUnauthorized(): void public function testWebinarsListForApi(): void { $webinarService = $this->mock(YoutubeServiceContract::class); + $webinarService->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $webinarService->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect()); $this->response = $this->get('/api/webinars'); $this->response->assertOk(); @@ -182,6 +186,7 @@ public function testWebinarsListForApi(): void public function testWebinarsListWithFilterForApi(): void { $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); $filterData = [ 'name=' . $this->webinar->name, @@ -201,6 +206,7 @@ public function testWebinarsListWithFilterForApi(): void public function testWebinarsListWithOrderForApi(): void { $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); Webinar::query()->delete(); @@ -253,6 +259,7 @@ public function testWebinarsListWithOrderForApi(): void public function testWebinarsListWithFilterOnlyIncomingForApi(): void { $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); $this->webinar->update([ @@ -378,6 +385,7 @@ public function testWebinarAssignableUsersSearch(): void public function testWebinarListOwn(): void { $webinarService = $this->mock(YoutubeServiceContract::class); + $webinarService->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(false); $webinarService->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect()); Webinar::factory()->count(3)->create(); diff --git a/tests/APIs/WebinarDestroyApiTest.php b/tests/APIs/WebinarDestroyApiTest.php index 9704a06..1c03d48 100644 --- a/tests/APIs/WebinarDestroyApiTest.php +++ b/tests/APIs/WebinarDestroyApiTest.php @@ -42,6 +42,7 @@ public function testWebinarDestroy(): void { $this->initVariable(); $webinarService = $this->mock(YoutubeServiceContract::class); + $webinarService->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $webinarService->shouldReceive('getYtLiveStream')->once()->andReturn(collect()); $response = $this->actingAs($this->user, 'api')->json( 'DELETE', diff --git a/tests/APIs/WebinarGenerateJitsiTest.php b/tests/APIs/WebinarGenerateJitsiTest.php index e63d964..f16d918 100644 --- a/tests/APIs/WebinarGenerateJitsiTest.php +++ b/tests/APIs/WebinarGenerateJitsiTest.php @@ -37,6 +37,7 @@ public function testGenerateJitsiUnAuthorized(): void public function testGenerateJitsiWithWebinar(): void { $webinarService = $this->mock(YoutubeServiceContract::class); + $webinarService->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $webinarService->shouldReceive('getYtLiveStream')->once()->andReturn(collect(['s'])); $this->webinar = Webinar::factory([ 'status' => WebinarStatusEnum::PUBLISHED, diff --git a/tests/APIs/WebinarListForUserTest.php b/tests/APIs/WebinarListForUserTest.php index 4f03f83..13f75fc 100644 --- a/tests/APIs/WebinarListForUserTest.php +++ b/tests/APIs/WebinarListForUserTest.php @@ -43,6 +43,7 @@ private function initVariable(): void public function testWebinarListForUser(): void { $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); $this->initVariable(); $this->response = $this->actingAs($this->user, 'api')->json('GET', $this->apiUrl); @@ -91,6 +92,7 @@ public function durationProvider(): array public function testWebinarListOnlyIncoming(string $duration): void { $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); $student = User::factory()->create(); diff --git a/tests/APIs/WebinarShowApiTest.php b/tests/APIs/WebinarShowApiTest.php index 5e78554..df20bd5 100644 --- a/tests/APIs/WebinarShowApiTest.php +++ b/tests/APIs/WebinarShowApiTest.php @@ -37,6 +37,7 @@ public function testWebinarShowUnauthorized(): void public function testWebinarShow(): void { $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); $response = $this->actingAs($this->user, 'api')->json( 'GET', @@ -55,6 +56,7 @@ public function testWebinarShow(): void public function testConsultationShowAPI() { $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); $response = $this->actingAs($this->user, 'api')->json( 'GET', diff --git a/tests/APIs/WebinarStoreApiTest.php b/tests/APIs/WebinarStoreApiTest.php index 211e714..f4c08e6 100644 --- a/tests/APIs/WebinarStoreApiTest.php +++ b/tests/APIs/WebinarStoreApiTest.php @@ -57,6 +57,7 @@ public function testWebinarStore(): void $ytLiveDtoMock = new YTLiveDtoMock(); $youtubeServiceContract = $this->mock(YoutubeServiceContract::class); + $youtubeServiceContract->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $youtubeServiceContract->shouldReceive('generateYTStream')->once()->andReturn($ytLiveDtoMock); $youtubeServiceContract->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect([1])); diff --git a/tests/APIs/WebinarUpdateApiTest.php b/tests/APIs/WebinarUpdateApiTest.php index 6d87274..0222b54 100644 --- a/tests/APIs/WebinarUpdateApiTest.php +++ b/tests/APIs/WebinarUpdateApiTest.php @@ -60,6 +60,7 @@ public function testWebinarUpdate(): void ); $ytLiveDtoMock = new YTLiveDtoMock(); $webinarService = $this->mock(YoutubeServiceContract::class); + $webinarService->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $webinarService->shouldReceive('updateYTStream')->zeroOrMoreTimes()->andReturn($ytLiveDtoMock); $webinarService->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect(['s'])); @@ -117,6 +118,7 @@ public function testWebinarClearTagsUpdate(): void ); $ytLiveDtoMock = new YTLiveDtoMock(); $webinarService = $this->mock(YoutubeServiceContract::class); + $webinarService->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $webinarService->shouldReceive('updateYTStream')->zeroOrMoreTimes()->andReturn($ytLiveDtoMock); $webinarService->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect(['s'])); @@ -155,6 +157,7 @@ public function testWebinarUpdateTrainers(): void $ytLiveDtoMock = new YTLiveDtoMock(); $webinarService = $this->mock(YoutubeServiceContract::class); + $webinarService->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $webinarService->shouldReceive('updateYTStream')->zeroOrMoreTimes()->andReturn($ytLiveDtoMock); $webinarService->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect(['s'])); @@ -203,6 +206,7 @@ public function testWebinarUpdateImageAndLogotypeFromExistingFiles(): void $ytLiveDtoMock = new YTLiveDtoMock(); $webinarService = $this->mock(YoutubeServiceContract::class); + $webinarService->shouldReceive('isConfigured')->zeroOrMoreTimes()->andReturn(true); $webinarService->shouldReceive('updateYTStream')->zeroOrMoreTimes()->andReturn($ytLiveDtoMock); $webinarService->shouldReceive('getYtLiveStream')->zeroOrMoreTimes()->andReturn(collect(['s'])); diff --git a/tests/TestCase.php b/tests/TestCase.php index 18355a1..4b0fb65 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -21,6 +21,15 @@ class TestCase extends \EscolaLms\Core\Tests\TestCase protected function setUp(): void { parent::setUp(); + + config([ + 'services.youtube.client_id' => 'test_client_id', + 'services.youtube.client_secret' => 'test_secret', + 'services.youtube.api_key' => 'test_api_key', + 'services.youtube.refresh_token' => 'test_refresh_token', + 'services.youtube.redirect_url' => 'redirect_url', + ]); + Passport::useClientModel(Client::class); }