Skip to content

Commit

Permalink
Merge pull request #182 from rrze-mmz/170-add-option-to-enabledisable…
Browse files Browse the repository at this point in the history
…-frontend-comments-for-a-series

Add series allow comments toggle
  • Loading branch information
stefanosgeo authored Nov 14, 2024
2 parents d258f3f + ea1a6b5 commit e3cfeb7
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 48 deletions.
2 changes: 2 additions & 0 deletions app/Http/Requests/StoreSeriesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function rules()
'slug' => ['required'],
'password' => ['nullable', Password::min(8)->mixedCase()],
'is_public' => ['boolean'],
'allow_comments' => ['boolean'],
'image_id' => ['required', 'exists:App\Models\Image,id'],
];
}
Expand All @@ -44,6 +45,7 @@ protected function prepareForValidation(): void
$this->merge([
'slug' => Str::slug($this->title),
'is_public' => $this->is_public === 'on',
'allow_comments' => $this->allow_comments === 'on',
'presenters' => $this->presenters = $this->presenters ?? [], //set empty array if presenters array is empty
'image_id' => (isset($this->image_id)) ? $this->image_id : $settingData['default_image_id'],
]);
Expand Down
20 changes: 11 additions & 9 deletions app/Http/Requests/UpdateSeriesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@

class UpdateSeriesRequest extends FormRequest
{
protected function prepareForValidation()
{
$this->merge([
'slug' => Str::slug($this->title),
'is_public' => $this->is_public === 'on',
'presenters' => $this->presenters = $this->presenters ?? [], //set empty array if select2 presenters is empty
]);
}

/**
* Determine if the user is authorized to make this request.
*/
Expand All @@ -43,6 +34,17 @@ public function rules()
'opencast_series_id' => ['null', 'uuid'],
'password' => ['nullable', Password::min(8)->mixedCase()],
'is_public' => ['boolean'],
'allow_comments' => ['boolean'],
];
}

protected function prepareForValidation()
{
$this->merge([
'slug' => Str::slug($this->title),
'is_public' => $this->is_public === 'on',
'allow_comments' => $this->allow_comments === 'on',
'presenters' => $this->presenters = $this->presenters ?? [], //set empty array if select2 presenters is empty
]);
}
}
18 changes: 0 additions & 18 deletions app/Policies/ClipPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,21 @@
use App\Models\Clip;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ClipPolicy
{
use HandlesAuthorization;

/**
* Check whether the current user can view all clips in index
*/
public function index(User $user): bool
{
return auth()->check() && ($user->isAdmin() || $user->isAssistant());
}

/**
* Check whether the current user can create a clip
*/
public function create(User $user): bool
{
return auth()->check() && ($user->isModerator() || $user->isAssistant() || $user->isAdmin());
}

/**
* Check whether the given user can edit the given clip
*/
public function edit(User $user, Clip $clip): bool
{
return $user->is($clip->owner) || $user->is($clip->series->owner) || ($user->isAdmin() || $user->isAssistant());
Expand All @@ -50,9 +39,6 @@ public function view(?User $user, Clip $clip): bool
}
}

/**
* Check whether the current user can view the given clip comments
*/
public function viewComments(?User $user, Clip $clip): bool
{
return auth()->check() && $clip->allow_comments;
Expand All @@ -64,10 +50,6 @@ public function viewVideo(User $user, Clip $clip): bool
($user->is($clip->owner));
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function canWatchVideo(?User $user, Clip $clip): bool
{
return $clip->checkAcls();
Expand Down
20 changes: 5 additions & 15 deletions app/Policies/SeriesPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ class SeriesPolicy
{
use HandlesAuthorization;

/**
* Check whether the current user can view all series in index
*/
public function index(User $user): bool
{
return auth()->check() && ($user->isAdmin() || $user->isAssistant());
}

/**
* Check whether the current user can create a series.
*/
public function create(User $user): bool
{
return auth()->check() && ($user->isModerator() || $user->isAdmin() || $user->isAssistant());
Expand All @@ -47,9 +41,11 @@ public function view(?User $user, Series $series): Response
: Response::deny('You do not own this series');
}

/**
* Check whether the given user can edit the given series
*/
public function viewComments(?User $user, Series $series): bool
{
return auth()->check() && $series->allow_comments;
}

public function edit(User $user, Series $series): Response
{
return (
Expand All @@ -62,9 +58,6 @@ public function edit(User $user, Series $series): Response
: Response::deny('You do not own this series');
}

/**
* Check whether the current user can create a series.
*/
public function update(User $user, Series $series): Response
{
//assistants are not allowed to update series info
Expand All @@ -73,9 +66,6 @@ public function update(User $user, Series $series): Response
: Response::deny('You do not own this series');
}

/**
* Check whether the given user can delete the given series
*/
public function delete(User $user, Series $series): Response
{
return $user->is($series->owner) || $user->isAdmin()
Expand Down
3 changes: 2 additions & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function boot()
Gate::define('edit-series', [SeriesPolicy::class, 'edit']);
Gate::define('update-series', [SeriesPolicy::class, 'update']);
Gate::define('view-series', [SeriesPolicy::class, 'view']);
Gate::define('view-series-comments', [SeriesPolicy::class, 'viewComments']);
Gate::define('delete-series', [SeriesPolicy::class, 'delete']);
Gate::define('change-series-owner', [SeriesPolicy::class, 'changeOwner']);

Expand All @@ -59,7 +60,7 @@ public function boot()
Gate::define('create-clips', [ClipPolicy::class, 'create']);
Gate::define('edit-clips', [ClipPolicy::class, 'edit']);
Gate::define('view-clips', [ClipPolicy::class, 'view']);
Gate::define('view-comments', [ClipPolicy::class, 'viewComments']);
Gate::define('view-clips-comments', [ClipPolicy::class, 'viewComments']);
Gate::define('view-video', [ClipPolicy::class, 'viewVideo']);
Gate::define('watch-video', [ClipPolicy::class, 'canWatchVideo']);
Gate::define('edit-assets', [AssetPolicy::class, 'edit']);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('series', function (Blueprint $table) {
$table->boolean('allow_comments')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('series', function (Blueprint $table) {
$table->dropColumn('allow_comments');
});
}
};
5 changes: 5 additions & 0 deletions resources/views/backend/series/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class="w-4/5">
label="{{ __('common.forms.public available') }}"
field-name="is_public"
/>

<x-form.toggle-button :value="false"
label="{{ __('common.metadata.allow comments') }}"
field-name="allow_comments"
/>

<div class="flex content-center items-center mb-6">
</div>
Expand Down
5 changes: 5 additions & 0 deletions resources/views/backend/series/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class=" @if(auth()->user()->isAdmin()) w-4/5 @else w-full @endif"
label="{{__('common.forms.public available')}}"
field-name="is_public"
/>

<x-form.toggle-button :value="$series->allow_comments"
label="{{__('common.metadata.allow comments')}}"
field-name="allow_comments"
/>
</div>
@can('update-series', $series)
<div class="pt-10">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/backend/seriesClips/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class="w-4/5">
:full-col="true"
/>

<x-form.toggle-button :value="true"
<x-form.toggle-button :value="$series->allow_comments"
label="Allow comments"
field-name="allow_comments"
/>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/frontend/clips/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class="text-xs sm:text-sm mr-1 inline-flex items-center font-bold leading-sm px-
</div>
</div>
@endif
@can('view-comments', $clip)
@can('view-clips-comments', $clip)
<div class="flex flex-col pt-6 sm:pt-10">
<h2 class="border-b-2 border-black pb-2 text-lg sm:text-2xl font-semibold dark:text-white dark:border-white">
{{ __('clip.frontend.comments') }}
Expand Down
4 changes: 2 additions & 2 deletions resources/views/frontend/series/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ class="underline">
</div>


@auth()
@can('view-series-comments', $series)
<div class="flex flex-col pt-10">
<h2 class="border-b-2 border-black dark:border-white pb-2 text-2xl font-semibold">
{{ __('clip.frontend.comments') }}
</h2>
<livewire:comments-section :model="$series" :type="'frontend'" />
</div>
@endauth
@endcan

@include('backend.clips.list')
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Enums\Role;
use App\Http\Controllers\Backend\SeriesController;
use App\Models\Clip;
use App\Models\User;
use Facades\Tests\Setup\ClipFactory;
Expand All @@ -11,6 +12,7 @@
use function Pest\Laravel\patch;
use function Pest\Laravel\post;

covers(SeriesController::class);
uses()->group('backend');

it('redirects a non logged in user if tried to add a clip to a series', function () {
Expand Down
15 changes: 14 additions & 1 deletion tests/Feature/Livewire/CommentsSectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

beforeEach(function () {
// TODO: Change the autogenerated stub
signIn();
signInRole(Role::STUDENT);

$this->clip = ClipFactory::withAssets(2)->create(['allow_comments' => true]);
});
Expand All @@ -30,6 +30,19 @@
->assertDontSeeLivewire('comments-section');
});

it('does not allow comments if comments are disabled in series settings', function () {
get(route('frontend.clips.show', SeriesFactory::withClips(2)->withAssets(3)->create(['allow_comments' => false])))
->assertDontSeeLivewire('comments-section');
});

it('does not allow comments if comments are disabled in clip settings', function () {
$this->clip->allow_comments = false;
$this->clip->save();

get(route('frontend.clips.show', $this->clip))
->assertDontSeeLivewire('comments-section');
});

it('contains comments section livewire component on clip show page', function () {
get(route('frontend.clips.show', $this->clip))
->assertSeeLivewire('comments-section');
Expand Down

0 comments on commit e3cfeb7

Please sign in to comment.