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

Clean up UserGroupEvent model #9486

Merged
merged 5 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions app/Models/Chat/UserChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getAttribute($key)
}

// Laravel has own hidden property
// TODO: https://github.com/ppy/osu-web/pull/9486#discussion_r1017831112
public function isHidden()
{
return (bool) $this->getRawAttribute('hidden');
Expand Down
76 changes: 59 additions & 17 deletions app/Models/UserGroupEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,38 @@
// 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\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use InvalidArgumentException;

/**
* @property User|null $actor
* @property-read User|null $actor
* @property int|null $actor_id
* @property \Carbon\Carbon $created_at
* @property array|null $details
* @property Group $group
* @property-read string $created_at_json
* @property array $details
* @property-read Group $group
* @property int $group_id
* @property bool $hidden
* @property int $id
* @property string $type
* @property User|null $user
* @property-read User|null $user
* @property int|null $user_id
*/
class UserGroupEvent extends Model
{
const GROUP_ADD = 'group_add';
const GROUP_REMOVE = 'group_remove';
const GROUP_RENAME = 'group_rename';
const USER_ADD = 'user_add';
const USER_ADD_PLAYMODES = 'user_add_playmodes';
const USER_REMOVE = 'user_remove';
const USER_REMOVE_PLAYMODES = 'user_remove_playmodes';
const USER_SET_DEFAULT = 'user_set_default';
public const GROUP_ADD = 'group_add';
public const GROUP_REMOVE = 'group_remove';
public const GROUP_RENAME = 'group_rename';
public const USER_ADD = 'user_add';
public const USER_ADD_PLAYMODES = 'user_add_playmodes';
public const USER_REMOVE = 'user_remove';
public const USER_REMOVE_PLAYMODES = 'user_remove_playmodes';
public const USER_SET_DEFAULT = 'user_set_default';

const UPDATED_AT = null;
public const UPDATED_AT = null;

protected $casts = [
'details' => 'array',
Expand All @@ -55,6 +60,10 @@ public static function logUserAdd(?User $actor, User $user, Group $group, ?array

public static function logUserAddPlaymodes(?User $actor, User $user, Group $group, array $playmodes): self
{
if (empty($playmodes)) {
throw new InvalidArgumentException('playmodes must not be empty');
}

return static::log($actor, static::USER_ADD_PLAYMODES, $user, $group, [
'details' => compact('playmodes'),
]);
Expand All @@ -67,6 +76,10 @@ public static function logUserRemove(?User $actor, User $user, Group $group): se

public static function logUserRemovePlaymodes(?User $actor, User $user, Group $group, array $playmodes): self
{
if (empty($playmodes)) {
throw new InvalidArgumentException('playmodes must not be empty');
}

return static::log($actor, static::USER_REMOVE_PLAYMODES, $user, $group, [
'details' => compact('playmodes'),
]);
Expand Down Expand Up @@ -102,18 +115,47 @@ private static function log(?User $actor, string $type, ?User $user, Group $grou
));
}

public function actor()
public function actor(): BelongsTo
{
return $this->belongsTo(User::class, 'actor_id');
}

public function group()
public function group(): BelongsTo
{
return $this->belongsTo(Group::class, 'group_id');
}

public function user()
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}

public function getAttribute($key)
{
return match ($key) {
'actor_id',
'group_id',
'id',
'type',
'user_id' => $this->getRawAttribute($key),

'details' => json_decode($this->getRawAttribute($key) ?? '[]', true),

'created_at' => $this->getTimeFast($key),

'created_at_json' => $this->getJsonTimeFast($key),

'actor',
'user' => $this->getRelationValue($key),

'group' => app('groups')->byIdOrFail($this->getRawAttribute('group_id')),
};
}

// Laravel has own hidden property
// TODO: https://github.com/ppy/osu-web/pull/9486#discussion_r1017831112
public function isHidden(): bool
{
return (bool) $this->getRawAttribute('hidden');
}
}