Skip to content

Commit

Permalink
#2611 Route publishing page now shows an option to enable/disable rou…
Browse files Browse the repository at this point in the history
…te publishing.

When disabled, any scheduled route publishes will not trigger.
  • Loading branch information
Wotuu committed Nov 15, 2024
1 parent 8ed0c73 commit 3e9c3d5
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 12 deletions.
13 changes: 13 additions & 0 deletions app/Http/Controllers/Ajax/AjaxTeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\Team\TeamDefaultRoleFormRequest;
use App\Http\Requests\Team\TeamRoutePublishingFormRequest;
use App\Models\DungeonRoute\DungeonRoute;
use App\Models\Patreon\PatreonAdFreeGiveaway;
use App\Models\Patreon\PatreonBenefit;
Expand Down Expand Up @@ -41,6 +42,18 @@ public function changeDefaultRole(TeamDefaultRoleFormRequest $request, Team $tea
return response()->noContent();
}

/**
* @throws AuthorizationException
*/
public function changeRoutePublishing(TeamRoutePublishingFormRequest $request, Team $team): Response
{
$this->authorize('change-route-publishing', $team);

$team->update(['route_publishing_enabled' => $request->get('enabled')]);

return response()->noContent();
}

/**
* @return array|Application|ResponseFactory|Response
*
Expand Down
30 changes: 30 additions & 0 deletions app/Http/Requests/Team/TeamRoutePublishingFormRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\Team;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
* User wants to change the route publishing settings of the team
*/
class TeamRoutePublishingFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'enabled' => ['required', Rule::in(0, 1)],
];
}
}
22 changes: 12 additions & 10 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@
* @property string $description
* @property string $invite_code
* @property string $default_role
* @property boolean $route_publishing_enabled
*
* @property Collection<TeamUser> $teamUsers
* @property Collection<User> $members
* @property Collection<DungeonRoute> $dungeonroutes
* @property Collection<DungeonRoute> $dungeonRoutes
*
* @mixin Eloquent
*/
class Team extends Model
{
use HasIconFile;
use GeneratesPublicKey;

protected $visible = ['name', 'description', 'public_key'];

protected $fillable = ['default_role'];

use GeneratesPublicKey;
protected $fillable = ['default_role', 'route_publishing_enabled'];

/**
* https://stackoverflow.com/a/34485411/771270
Expand All @@ -57,7 +57,7 @@ public function members(): BelongsToMany
return $this->belongsToMany(User::class, 'team_users');
}

public function dungeonroutes(): HasMany
public function dungeonRoutes(): HasMany
{
return $this->hasMany(DungeonRoute::class);
}
Expand All @@ -67,7 +67,7 @@ public function dungeonroutes(): HasMany
*/
public function getVisibleRouteCount(): int
{
return $this->dungeonroutes()->whereIn('published_state_id', PublishedState::whereIn('name', [
return $this->dungeonRoutes()->whereIn('published_state_id', PublishedState::whereIn('name', [
PublishedState::TEAM, PublishedState::WORLD, PublishedState::WORLD_WITH_LINK,
])->get()->pluck('id'))->count();
}
Expand All @@ -76,6 +76,7 @@ public function getVisibleRouteCount(): int
* Checks if a user can add/remove a route to this team or not.
*
* @param User $user
* @return bool
*/
public function canAddRemoveRoute(User $user): bool
{
Expand Down Expand Up @@ -259,6 +260,7 @@ public function isUserAdmin(User $user): bool
public function isUserCollaborator(User $user): bool
{
$userRole = $this->getUserRole($user);

return $userRole !== null && $this->getUserRole($user) !== TeamUser::ROLE_MEMBER;
}

Expand Down Expand Up @@ -329,7 +331,7 @@ public function removeMember(User $member): bool
$member->patreonAdFreeGiveaway->delete();
}

$this->dungeonroutes()->where('team_id', $this->id)->where('author_id', $member->id)->update(['team_id' => null]);
$this->dungeonRoutes()->where('team_id', $this->id)->where('author_id', $member->id)->update(['team_id' => null]);
$result = TeamUser::where('team_id', $this->id)->where('user_id', $member->id)->delete();
} catch (Exception) {
logger()->error('Unable to remove member from team', [
Expand Down Expand Up @@ -379,7 +381,7 @@ public function getNewAdminUponAdminAccountDeletion(User $user): ?User
));
}

$roles = TeamUser::ALL_ROLES;
$roles = TeamUser::ALL_ROLES;
/** @var TeamUser|null $newOwner */
$newOwner = $this->teamUsers->where('user_id', '!=', $user->id)
->sortByDesc(static fn($obj, $key) => $roles[$obj->role])
Expand All @@ -391,7 +393,7 @@ public function getNewAdminUponAdminAccountDeletion(User $user): ?User
public function getAvailableTags(): Collection
{
return Tag::where('tag_category_id', TagCategory::ALL[TagCategory::DUNGEON_ROUTE_TEAM])
->whereIn('model_id', $this->dungeonroutes->pluck('id'))
->whereIn('model_id', $this->dungeonRoutes->pluck('id'))
->get();
}

Expand All @@ -416,7 +418,7 @@ protected static function boot(): void
}
// Delete all tags team tags belonging to our routes
Tag::where('tag_category_id', TagCategory::ALL[TagCategory::DUNGEON_ROUTE_TEAM])
->whereIn('model_id', $team->dungeonroutes->pluck('id')->toArray())
->whereIn('model_id', $team->dungeonRoutes->pluck('id')->toArray())
->delete();
// Remove all users associated with this team
TeamUser::where('team_id', $team->id)->delete();
Expand Down
5 changes: 5 additions & 0 deletions app/Policies/TeamPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function changeDefaultRole(User $user, Team $team): bool
return $team->getUserRole($user) === TeamUser::ROLE_ADMIN;
}

public function changeRoutePublishing(User $user, Team $team): bool
{
return $team->getUserRole($user) === TeamUser::ROLE_ADMIN;
}

public function removeMember(User $user, Team $team, User $member): bool
{
return $team->canRemoveMember($user, $member);
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('teams', function (Blueprint $table) {
$table->boolean('route_publishing_enabled')->default(false)->after('default_role');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('teams', function (Blueprint $table) {
$table->dropColumn('route_publishing_enabled');
});
}
};
1 change: 1 addition & 0 deletions lang/en_US/js.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@
'admin_label' => 'Own',
'change_role_success' => 'Permissions updated',
'change_default_role_success' => 'Default role updated',
'change_route_publishing_success' => 'Route publishing settings updated',
'team_admin' => 'Administrate team',
'team_moderator' => 'Moderate team',
'team_collaborator' => 'Edit routes',
Expand Down
5 changes: 4 additions & 1 deletion lang/en_US/view_team.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
'members' => 'Members',
],
'routepublishing' => [
'title' => 'Route publishing',
'title' => 'Route publishing',
'warning' => 'When route publishing is enabled, all team members with role Moderator or Admin can adjust the published state of all your routes attached to this team.',
'description' => 'Route publishing allows you to publish routes with the world on a schedule. You can set a date/time per route when they will automatically be put in Published state and be visible to the world.',
'enabled' => 'Enabled',
],
'routes' => [
'title' => 'Route list',
Expand Down
16 changes: 15 additions & 1 deletion resources/assets/js/custom/inline/team/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ class TeamEdit extends InlineCode {
}
});
});

$(this.options.routePublishingEnabledSelector).on('change', function(){
$.ajax({
type: 'PUT',
url: `/ajax/team/${self.options.teamPublicKey}/routepublishing`,
dataType: 'json',
data: {
enabled: parseInt($(this).val())
},
success: function () {
showSuccessNotification(lang.get('messages.change_route_publishing_success'));
}
});
});
}

/**
Expand Down Expand Up @@ -250,7 +264,7 @@ class TeamEdit extends InlineCode {
}
}

let result = '';
let result;
if (roles.length === 0) {
let icon = self._getIcon(data);

Expand Down
3 changes: 3 additions & 0 deletions resources/views/team/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
@include('common.general.inline', ['path' => 'team/edit', 'options' => [
'dependenciesById' => [$routesTableInlineId],
'routesTableInlineId' => $routesTableInlineId,
'routePublishingEnabledSelector' => '#route_publishing_enabled_checkbox',
'data' => $data,
'teamName' => $team->name,
'teamPublicKey' => $team->public_key,
Expand Down
11 changes: 11 additions & 0 deletions resources/views/team/edittabs/routepublishing.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@
<h4>
{{ __('view_team.edittabs.routepublishing.title') }}
</h4>
@component('common.general.alert', ['type' => 'warning', 'name' => 'team-route-publishing-warning'])
{{ __('view_team.edittabs.routepublishing.warning') }}
@endcomponent

<div class="form-group">
{{ __('view_team.edittabs.routepublishing.description') }}
</div>
<div class="form-group">
{!! Form::label('route_publishing_enabled', __('view_team.edittabs.routepublishing.enabled')) !!}
{!! Form::checkbox('route_publishing_enabled', 1, $team->route_publishing_enabled ?? 0, [
'id' => 'route_publishing_enabled_checkbox',
'class' => 'form-control left_checkbox'
]) !!}
</div>
<div class="form-group">
@include('common.dungeonroute.table', [
'inlineId' => $inlineId,
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@
// Teams
Route::prefix('team/{team}')->group(static function () {
Route::put('/changedefaultrole', (new AjaxTeamController())->changeDefaultRole(...));
Route::put('/routepublishing', (new AjaxTeamController())->changeRoutePublishing(...));
Route::put('/changerole', (new AjaxTeamController())->changeRole(...));
Route::post('/route/{dungeonroute}', (new AjaxTeamController())->addRoute(...));
Route::delete('/member/{user}', (new AjaxTeamController())->removeMember(...));
Expand Down

0 comments on commit 3e9c3d5

Please sign in to comment.