Skip to content

Commit

Permalink
More relevant diagnostic data, less privacy invasive. (LycheeOrg#2362)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Apr 9, 2024
1 parent b23304b commit 7529a66
Show file tree
Hide file tree
Showing 19 changed files with 294 additions and 32 deletions.
39 changes: 34 additions & 5 deletions app/Actions/Diagnostics/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Exceptions\Internal\QueryBuilderException;
use App\Models\Configs;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Schema;

class Configuration
Expand All @@ -25,11 +26,7 @@ public function get(): array
}

// Load settings
$settings = Configs::query()
->where('confidentiality', '<=', 2)
->select(['key', 'value'])
->orderBy('id', 'ASC')
->get();
$settings = Schema::hasColumn('configs', 'is_secret') ? $this->withIsSecret() : $this->withConfidentiality();

return $settings->map(function (Configs $setting) {
if (is_null($setting->value)) {
Expand All @@ -39,4 +36,36 @@ public function get(): array
}
})->all();
}

/**
* This is a fail safe (legacy) in case the migration 2024_04_09_121410 has not been applied.
*
* @return Collection<int,Configs>
*
* @throws QueryBuilderException
*/
private function withConfidentiality()
{
return Configs::query()
->where('confidentiality', '<=', 2)
->select(['key', 'value'])
->orderBy('id', 'ASC')
->get();
}

/**
* Normal code path.
*
* @return Collection<int,Configs>
*
* @throws QueryBuilderException
*/
private function withIsSecret()
{
return Configs::query()
->where('is_secret', '=', false)
->select(['key', 'value'])
->orderBy('id', 'ASC')
->get();
}
}
34 changes: 34 additions & 0 deletions app/Legacy/BaseConfigMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Legacy;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

/**
* Used in migrations prior 2024_04_09_121410.
*/
abstract class BaseConfigMigration extends Migration
{
/**
* @return array<int,array{key:string,value:string,confidentiality:string,cat:string,type_range:string,description:string}>
*/
abstract public function getConfigs(): array;

/**
* Run the migrations.
*/
final public function up(): void
{
DB::table('configs')->insert($this->getConfigs());
}

/**
* Reverse the migrations.
*/
final public function down(): void
{
$keys = collect($this->getConfigs())->map(fn ($v) => $v['key'])->all();
DB::table('configs')->whereIn('key', $keys)->delete();
}
}
4 changes: 2 additions & 2 deletions app/Models/Configs.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @property string|null $value
* @property string $cat
* @property string $type_range
* @property int $confidentiality
* @property bool $is_secret
* @property string $description
*
* @method static ConfigsBuilder|Configs addSelect($column)
Expand Down Expand Up @@ -71,7 +71,7 @@ class Configs extends Model
*
* @var array<int,string>
*/
protected $fillable = ['key', 'value', 'cat', 'type_range', 'confidentiality', 'description'];
protected $fillable = ['key', 'value', 'cat', 'type_range', 'is_secret', 'description'];

/**
* this is a parameter for Laravel to indicate that there is no created_at, updated_at columns.
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Extensions/BaseConfigMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
abstract class BaseConfigMigration extends Migration
{
/**
* @return array<int,array{key:string,value:string,confidentiality:string,cat:string,type_range:string,description:string}>
* @return array<int,array{key:string,value:string,is_secret:bool,cat:string,type_range:string,description:string}>
*/
abstract public function getConfigs(): array;

Expand Down
17 changes: 5 additions & 12 deletions database/migrations/2019_09_28_171753_config_fix.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

/** @noinspection PhpUndefinedClassInspection */

use App\Models\Configs;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;

Expand Down Expand Up @@ -95,18 +95,11 @@ private function missing_columns(): void
private function update_missing_fields(array &$default_values): void
{
foreach ($default_values as $value) {
$c = Configs::where('key', $value['key'])->count();
$config = Configs::updateOrCreate(
['key' => $value['key']],
[
'cat' => $value['cat'],
'type_range' => $value['type_range'],
'confidentiality' => $value['confidentiality'],
]
);
$c = DB::table('configs')->where('key', '=', $value['key'])->count();
if ($c === 0) {
$config->value = $value['value'];
$config->save();
DB::table('configs')->insert($value);
} else { // $c === 1
DB::table('configs')->where('key', '=', $value['key'])->update($value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class() extends Migration {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public function getConfigs(): array
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const GALLERY = 'Gallery';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const IMAGE_PROCESSING = 'Image Processing';
Expand Down
2 changes: 1 addition & 1 deletion database/migrations/2023_09_25_123925_config_blur_nsfw.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const MOD_NSFW = 'Mod NSFW';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const MOD_SEARCH = 'Mod Search';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const MOD_SEARCH = 'Mod Search';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const GALLERY = 'Gallery';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const GALLERY = 'Gallery';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const GALLERY = 'Gallery';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const GALLERY = 'Gallery';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const CONFIG = 'config';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Models\Extensions\BaseConfigMigration;
use App\Legacy\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const PROCESSING = 'Image Processing';
Expand Down
Loading

0 comments on commit 7529a66

Please sign in to comment.