Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Enum/ConstantEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
19 changes: 16 additions & 3 deletions src/Services/WebinarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 8 additions & 0 deletions tests/APIs/WebinarApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand All @@ -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([
Expand Down Expand Up @@ -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();
Expand All @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions tests/APIs/WebinarDestroyApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tests/APIs/WebinarGenerateJitsiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tests/APIs/WebinarListForUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions tests/APIs/WebinarShowApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tests/APIs/WebinarStoreApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));

Expand Down
4 changes: 4 additions & 0 deletions tests/APIs/WebinarUpdateApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));

Expand Down Expand Up @@ -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']));

Expand Down Expand Up @@ -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']));

Expand Down Expand Up @@ -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']));

Expand Down
9 changes: 9 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading