forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move profile cover data to user table
- Loading branch information
Showing
12 changed files
with
154 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Libraries\User; | ||
|
||
use App\Casts\LegacyFilename; | ||
use App\Models\User; | ||
|
||
class Cover | ||
{ | ||
private const AVAILABLE_PRESET_IDS = ['1', '2', '3', '4', '5', '6', '7', '8']; | ||
|
||
public static function isValidPresetId(int|null|string $presetId): bool | ||
{ | ||
return $presetId !== null | ||
&& in_array((string) $presetId, static::AVAILABLE_PRESET_IDS, true); | ||
} | ||
|
||
public function __construct(private User $user) | ||
{ | ||
$userProfileCustomization = $this->user->userProfileCustomization; | ||
$oldCoverJson = $userProfileCustomization?->cover_json; | ||
|
||
// Migrate from userProfileCustomization if exists | ||
if ($oldCoverJson !== null) { | ||
\DB::transaction(function () use ($oldCoverJson, $userProfileCustomization) { | ||
$this->user->fill([ | ||
'cover_preset_id' => $oldCoverJson['id'] ?? null, | ||
'custom_cover_filename' => LegacyFilename::makeFromAttributes($oldCoverJson['file'] ?? null), | ||
])->saveOrExplode(); | ||
$userProfileCustomization->fill(['cover_json' => null])->saveOrExplode(); | ||
}); | ||
} | ||
} | ||
|
||
public function customUrl(): ?string | ||
{ | ||
return $this->user->customCover()->url(); | ||
} | ||
|
||
public function presetId(): ?string | ||
{ | ||
if ($this->hasCustomCover()) { | ||
return null; | ||
} | ||
|
||
$id = $this->user->getKey(); | ||
|
||
if ($id === null || $id < 1) { | ||
return null; | ||
} | ||
|
||
$presetId = (string) $this->user->cover_preset_id; | ||
|
||
return static::isValidPresetId($presetId) | ||
? $presetId | ||
: static::AVAILABLE_PRESET_IDS[$id % count(static::AVAILABLE_PRESET_IDS)]; | ||
} | ||
|
||
public function set(?string $presetId, ?UploadedFile $file): void | ||
{ | ||
$this->user->cover_preset_id = static::isValidPresetId($presetId) ? $presetId : null; | ||
|
||
if ($file !== null) { | ||
$this->user->customCover()->store($file->getRealPath()); | ||
} | ||
} | ||
|
||
public function url(): ?string | ||
{ | ||
return $this->hasCustomCover() | ||
? $this->customUrl() | ||
: $this->presetUrl(); | ||
} | ||
|
||
private function hasCustomCover(): bool | ||
{ | ||
return !isset($this->user->cover_preset_id) && isset($this->user->custom_cover_filename); | ||
} | ||
|
||
private function presetUrl(): ?string | ||
{ | ||
$presetId = $this->presetId(); | ||
|
||
return $presetId === null | ||
? null | ||
: \Config::get('app.url').'/images/headers/profile-covers/c'.$presetId.'.jpg'; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
database/migrations/2023_11_15_081321_add_cover_columns_to_users.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('phpbb_users', function (Blueprint $table) { | ||
$table->unsignedMediumInteger('cover_preset_id')->nullable(true); | ||
$table->string('custom_cover_filename')->nullable(true); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('phpbb_users', function (Blueprint $table) { | ||
$table->dropColumn('cover_preset_id'); | ||
$table->dropColumn('custom_cover_filename'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.