Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Add page that allows to fix broken trees #2768

Merged
merged 8 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
42 changes: 42 additions & 0 deletions app/Http/Controllers/Admin/Maintenance/FullTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Http\Controllers\Admin\Maintenance;

use App\Http\Controllers\Admin\Maintenance\Model\Album;
use App\Http\Requests\Maintenance\FullTreeUpdateRequest;
use App\Http\Requests\Maintenance\MaintenanceRequest;
use App\Http\Resources\Diagnostics\AlbumTree;
use Illuminate\Routing\Controller;
use Illuminate\Support\Collection;

/**
* Maybe the album tree is broken.
* We fix it here.
*/
class FullTree extends Controller
{
/**
* Clean the path from all files excluding $this->skip.
*
* @return void
*/
public function do(FullTreeUpdateRequest $request): void
{
$keyName = 'id';
$albumInstance = new Album();
batch()->update($albumInstance, $request->albums(), $keyName);
}

/**
* Check whether there are files to be removed.
* If not, we will not display the module to reduce complexity.
*
* @return Collection<int,AlbumTree>
*/
public function check(MaintenanceRequest $request): Collection
{
$albums = Album::query()->join('base_albums', 'base_albums.id', '=', 'albums.id')->select(['albums.id', 'title', 'parent_id', '_lft', '_rgt'])->orderBy('_lft', 'asc')->toBase()->get();

return AlbumTree::collect($albums);
}
}
52 changes: 52 additions & 0 deletions app/Http/Requests/Maintenance/FullTreeUpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Http\Requests\Maintenance;

use App\Http\Requests\BaseApiRequest;
use App\Models\Configs;
use App\Policies\SettingsPolicy;
use App\Rules\AlbumIDRule;
use Illuminate\Support\Facades\Gate;

class FullTreeUpdateRequest extends BaseApiRequest
{
/**
* @var array<int,array{id:string,_lft:int,_rgt:int,parent_id:string|null}>
*/
private array $albums;

/**
* {@inheritDoc}
*/
public function authorize(): bool
{
return Gate::check(SettingsPolicy::CAN_EDIT, Configs::class);
}

public function rules(): array
{
return [
'albums' => 'required|array|min:1',
'albums.*' => 'required|array',
'albums.*.id' => ['required', new AlbumIDRule(false)],
'albums.*._lft' => 'required|integer|min:1',
'albums.*._rgt' => 'required|integer|min:1',
'albums.*.parent_id' => [new AlbumIDRule(true)],
];
}

protected function processValidatedValues(
array $values,
array $files,
): void {
$this->albums = $values['albums'];
}

/**
* @return array<int,array{id:string,_lft:int,_rgt:int}>
*/
public function albums(): array
{
return $this->albums;
}
}
31 changes: 31 additions & 0 deletions app/Http/Resources/Diagnostics/AlbumTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Resources\Diagnostics;

use App\Models\Album;
use Spatie\LaravelData\Data;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript()]
class AlbumTree extends Data
{
public function __construct(
public string $id,
public string $title,
public ?string $parent_id,
public int $_lft,
public int $_rgt,
) {
}

public static function FromModel(Album $album): AlbumTree
{
return new self(
$album->id,
$album->title,
$album->parent_id,
$album->_lft,
$album->_rgt
);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"lychee-org/nestedset": "^9.0",
"lychee-org/php-exif": "^1.0.4",
"maennchen/zipstream-php": "^3.1",
"mavinoo/laravel-batch": "^2.4",
"opcodesio/log-viewer": "dev-lycheeOrg",
"php-ffmpeg/php-ffmpeg": "^1.0",
"php-http/guzzle7-adapter": "^1.0",
Expand Down
58 changes: 57 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ function renv(string $cst, ?string $default = null): string

\SocialiteProviders\Manager\ServiceProvider::class,
// Barryvdh\Debugbar\ServiceProvider::class,
Mavinoo\Batch\BatchServiceProvider::class,

/*
* Application Service Providers...
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,6 @@ parameters:

-
message: '#Dynamic call to static method Illuminate\\Session\\Store::(has|get|now|forget)\(\).#'

-
message: '#Dynamic call to static method Kalnoy\\Nestedset\\QueryBuilder<.*>::(join|select|orderBy)\(\)#'
Loading
Loading