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

Update UserGroupEvent details format and log interface #9487

Merged
merged 7 commits into from
Jan 5, 2023
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
53 changes: 35 additions & 18 deletions app/Models/UserGroupEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,58 +41,75 @@ class UserGroupEvent extends Model
'hidden' => 'boolean',
];

public static function logGroupRename(?User $actor, Group $group, string $previousName, string $name): self
public static function logGroupAdd(?User $actor, Group $group): void
{
return static::log($actor, static::GROUP_RENAME, null, $group, [
static::log($actor, static::GROUP_ADD, null, $group);
}

public static function logGroupRemove(?User $actor, Group $group): void
{
static::log($actor, static::GROUP_REMOVE, null, $group);
}

public static function logGroupRename(?User $actor, Group $group, string $previousName, string $name): void
{
static::log($actor, static::GROUP_RENAME, null, $group, [
'details' => [
'group_name' => $name,
'previous_group_name' => $previousName,
],
]);
}

public static function logUserAdd(?User $actor, User $user, Group $group, ?array $playmodes = null): self
public static function logUserAdd(?User $actor, User $user, Group $group, ?array $playmodes = null): void
{
return static::log($actor, static::USER_ADD, $user, $group, [
// Never log additions to the default group
if ($group->identifier === 'default') {
return;
}

if (empty($playmodes)) {
$playmodes = null;
}

static::log($actor, static::USER_ADD, $user, $group, [
'details' => compact('playmodes'),
]);
}

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

return static::log($actor, static::USER_ADD_PLAYMODES, $user, $group, [
static::log($actor, static::USER_ADD_PLAYMODES, $user, $group, [
'details' => compact('playmodes'),
]);
}

public static function logUserRemove(?User $actor, User $user, Group $group): self
public static function logUserRemove(?User $actor, User $user, Group $group): void
{
return static::log($actor, static::USER_REMOVE, $user, $group);
static::log($actor, static::USER_REMOVE, $user, $group);
}

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

return static::log($actor, static::USER_REMOVE_PLAYMODES, $user, $group, [
static::log($actor, static::USER_REMOVE_PLAYMODES, $user, $group, [
'details' => compact('playmodes'),
]);
}

public static function logUserSetDefault(?User $actor, User $user, Group $group): self
public static function logUserSetDefault(?User $actor, User $user, Group $group): void
{
return static::log($actor, static::USER_SET_DEFAULT, $user, $group, [
'hidden' => true,
]);
static::log($actor, static::USER_SET_DEFAULT, $user, $group);
}

private static function log(?User $actor, string $type, ?User $user, Group $group, array $attributes = []): self
private static function log(?User $actor, string $type, ?User $user, Group $group, array $attributes = []): void
{
$attributes['details'] = array_merge(
[
Expand All @@ -103,7 +120,7 @@ private static function log(?User $actor, string $type, ?User $user, Group $grou
$attributes['details'] ?? [],
);

return static::create(array_merge(
(new static(array_merge(
[
'actor_id' => $actor?->getKey(),
'group_id' => $group->getKey(),
Expand All @@ -112,7 +129,7 @@ private static function log(?User $actor, string $type, ?User $user, Group $grou
'user_id' => $user?->getKey(),
],
$attributes,
));
)))->saveOrExplode();
}

public function actor(): BelongsTo
Expand All @@ -139,7 +156,7 @@ public function getAttribute($key)
'type',
'user_id' => $this->getRawAttribute($key),

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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.

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

class RequireUserGroupEventsDetailsAndType extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// The format of `details` before this was not finalised, and on
// production nothing important was logged anyway. No going back!
UserGroupEvent::truncate();

Schema::table('user_group_events', function (Blueprint $table) {
$table->json('details')->nullable(false)->change();
});
DB::statement("ALTER TABLE user_group_events CHANGE type type ENUM(
'group_add',
'group_remove',
'group_rename',
'user_add',
'user_add_playmodes',
'user_remove',
'user_remove_playmodes',
'user_set_default'
) NOT NULL");
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user_group_events', function (Blueprint $table) {
$table->json('details')->nullable()->change();
});
DB::statement("ALTER TABLE user_group_events CHANGE type type ENUM(
'group_add',
'group_remove',
'group_rename',
'user_add',
'user_add_playmodes',
'user_remove',
'user_remove_playmodes',
'user_set_default'
)");
}
}