Skip to content

Commit

Permalink
Move profile cover data to user table
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Nov 15, 2023
1 parent 363e5c3 commit e749b4c
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 137 deletions.
5 changes: 2 additions & 3 deletions app/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ public function cover()
}

try {
$profile = $user->profileCustomization();
$profile->setCover(Request::input('cover_id'), Request::file('cover_file'));
$profile->save();
$user->cover()->set(Request::input('cover_id'), Request::file('cover_file'));
$user->save();
} catch (ImageProcessorException $e) {
return error_popup($e->getMessage());
}
Expand Down
93 changes: 93 additions & 0 deletions app/Libraries/User/Cover.php
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';
}
}
76 changes: 0 additions & 76 deletions app/Libraries/User/CoverHelper.php

This file was deleted.

26 changes: 21 additions & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use App\Libraries\Elasticsearch\Indexable;
use App\Libraries\Session\Store as SessionStore;
use App\Libraries\Transactions\AfterCommit;
use App\Libraries\Uploader;
use App\Libraries\User\Cover;
use App\Libraries\User\DatadogLoginAttempt;
use App\Libraries\User\ProfileBeatmapset;
use App\Libraries\User\UsernamesForDbLookup;
Expand Down Expand Up @@ -266,6 +268,8 @@ class User extends Model implements AfterCommit, AuthenticatableContract, HasLoc
protected $primaryKey = 'user_id';
protected $table = 'phpbb_users';

private Cover $cover;
private Uploader $customCover;
private $validateCurrentPassword = false;
private $validatePasswordConfirmation = false;
private $passwordConfirmation = null;
Expand Down Expand Up @@ -729,11 +733,6 @@ public function isSpecial()
return $this->user_id !== null && present($this->user_colour);
}

public function cover()
{
return $this->userProfileCustomization ? $this->userProfileCustomization->cover()->url() : null;
}

public function setUserTwitterAttribute($value)
{
$this->attributes['user_twitter'] = trim(ltrim($value, '@'));
Expand All @@ -753,6 +752,8 @@ public function setUserColourAttribute($value)
public function getAttribute($key)
{
return match ($key) {
'cover_preset_id',
'custom_cover_filename',
'group_id',
'osu_featurevotes',
'osu_kudosavailable',
Expand Down Expand Up @@ -2099,6 +2100,21 @@ public static function findAndValidateForPassport($username, $password)
throw OAuthServerException::invalidGrant($authError);
}

public function cover(): Cover
{
return $this->cover ??= new Cover($this);
}

public function customCover(): Uploader
{
return $this->customCover ??= new Uploader(
'user-profile-covers',
$this,
'custom_cover_filename',
['image' => ['maxDimensions' => [2400, 640]]],
);
}

public function playCount()
{
return $this->memoize(__FUNCTION__, function () {
Expand Down
44 changes: 0 additions & 44 deletions app/Models/UserProfileCustomization.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

use App\Casts\LegacyFilename;

Check failure on line 8 in app/Models/UserProfileCustomization.php

View workflow job for this annotation

GitHub Actions / Lint all

Type App\Casts\LegacyFilename is not used in this file.
use App\Libraries\Uploader;

Check failure on line 9 in app/Models/UserProfileCustomization.php

View workflow job for this annotation

GitHub Actions / Lint all

Type App\Libraries\Uploader is not used in this file.
use App\Libraries\User\CoverHelper;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Http\UploadedFile;

/**
* @property array|null $cover_json
Expand Down Expand Up @@ -52,8 +50,6 @@ class UserProfileCustomization extends Model
];
protected $primaryKey = 'user_id';

private Uploader $cover;

public static function repairExtrasOrder($value)
{
// read from inside out
Expand Down Expand Up @@ -172,19 +168,6 @@ public function setCommentsSortAttribute($value)
$this->setOption('comments_sort', $value);
}

public function getCoverFilenameAttribute(): ?string
{
return LegacyFilename::makeFromAttributes($this->cover_json['file'] ?? null);
}

public function setCoverFilenameAttribute(string $value): void
{
$this->cover_json = [
...($this->cover_json ?? []),
'file' => ['hash' => $value],
];
}

public function getForumPostsShowDeletedAttribute()
{
return $this->options['forum_posts_show_deleted'] ?? true;
Expand Down Expand Up @@ -268,33 +251,6 @@ public function setProfileCoverExpandedAttribute($value)
$this->setOption('profile_cover_expanded', get_bool($value));
}

public function cover()
{
return $this->cover ??= new Uploader(
'user-profile-covers',
$this,
'cover_filename',
['image' => ['maxDimensions' => [2400, 640]]],
);
}

public function coverHelper()
{
return new CoverHelper($this);
}

public function setCover(?string $presetId, ?UploadedFile $file)
{
$this->cover_json = [
...($this->cover_json ?? []),
'id' => CoverHelper::isValidPresetId($presetId) ? $presetId : null,
];

if ($file !== null) {
$this->cover()->store($file->getRealPath());
}
}

private function setOption($key, $value)
{
$this->options ??= [];
Expand Down
8 changes: 4 additions & 4 deletions app/Transformers/UserCompactTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ public function includeCountry(User $user)

public function includeCover(User $user)
{
$coverHelper = $this->userProfileCustomization($user)->coverHelper();
$cover = $user->cover();

return $this->primitive([
'custom_url' => $coverHelper->customUrl(),
'url' => $coverHelper->url(),
'id' => $coverHelper->presetId(),
'custom_url' => $cover->customUrl(),
'url' => $cover->url(),
'id' => $cover->presetId(),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Transformers/UserTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function transform(User $user)
$profileCustomization = $this->userProfileCustomization($user);

return array_merge($result, [
'cover_url' => $profileCustomization->cover()->url(), // TODO: deprecated.
'cover_url' => $user->cover()->url(), // TODO: deprecated.
'discord' => $user->user_discord,
'has_supported' => $user->hasSupported(),
'interests' => $user->user_interests,
Expand Down
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');
});
}
};
2 changes: 1 addition & 1 deletion resources/views/client_verifications/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="dialog-form__dialog">
<div
class="dialog-form__row dialog-form__row--header"
style="background-image: url('{{ $user->profileCustomization()->cover()->url() }}')"
style="background-image: url('{{ $user->cover()->url() }}')"
>
<div class="dialog-form__header-overlay"></div>
<a
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layout/_popup_user.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class="simple-menu simple-menu--nav2 js-click-menu js-nav2--centered-popup"
<a
href="{{ route('users.show', Auth::user()) }}"
class="simple-menu__header simple-menu__header--link js-current-user-cover"
{!! background_image(Auth::user()->cover(), false) !!}
{!! background_image(Auth::user()->cover()->url(), false) !!}
>
<img class="simple-menu__header-icon" src="/images/icons/profile.svg" alt="">
<div class="u-relative">{{ Auth::user()->username }}</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/users/posts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@section('content')
@include('layout._page_header_v4', ['params' => [
'backgroundImage' => $user->profileCustomization()->cover()->url(),
'backgroundImage' => $user->cover()->url(),
]])
<form action="{{ route('users.posts', request()->route('user')) }}">
<div class="osu-page">
Expand Down
Loading

0 comments on commit e749b4c

Please sign in to comment.