新增聊天室成就系统与消息保留策略

This commit is contained in:
pllx
2026-04-30 16:19:49 +08:00
parent 92e3dd0cdf
commit f354516869
26 changed files with 1966 additions and 14 deletions
@@ -0,0 +1,36 @@
<?php
/**
* 文件功能:用户成就测试工厂。
*
* 为成就相关 Feature Test 快速生成解锁或进度记录。
*/
namespace Database\Factories;
use App\Models\User;
use App\Models\UserAchievement;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* 类功能:生成用户成就模型的默认测试数据。
*
* @extends Factory<UserAchievement>
*/
class UserAchievementFactory extends Factory
{
/**
* 定义模型的默认测试状态。
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'user_id' => User::factory(),
'achievement_key' => 'chat_first_message',
'progress_value' => 1,
'metadata' => ['threshold' => 1],
];
}
}
@@ -0,0 +1,37 @@
<?php
/**
* 文件功能:用户成就进度测试工厂。
*
* 为成就进度相关测试生成默认记录。
*/
namespace Database\Factories;
use App\Models\User;
use App\Models\UserAchievementProgress;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* 类功能:生成用户成就进度模型的默认测试数据。
*
* @extends Factory<UserAchievementProgress>
*/
class UserAchievementProgressFactory extends Factory
{
/**
* 定义模型的默认测试状态。
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'user_id' => User::factory(),
'achievement_key' => 'chat_first_message',
'progress_value' => 0,
'threshold_value' => 1,
'last_scanned_at' => now(),
];
}
}
@@ -0,0 +1,31 @@
<?php
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('messages', function (Blueprint $table) {
$table->string('retention_type', 30)
->default('user_chat')
->index()
->comment('消息保留类型:user_chat/system_notice/game_notice/ephemeral_notice');
});
}
/**
* 回滚迁移:移除聊天消息保留类型字段。
*/
public function down(): void
{
Schema::table('messages', function (Blueprint $table) {
$table->dropColumn('retention_type');
});
}
};
@@ -0,0 +1,36 @@
<?php
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::create('user_achievements', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete()->comment('用户ID');
$table->string('achievement_key', 80)->comment('成就唯一标识');
$table->unsignedBigInteger('progress_value')->default(0)->comment('当前进度快照');
$table->timestamp('achieved_at')->nullable()->index()->comment('达成时间');
$table->timestamp('notified_at')->nullable()->comment('通知时间');
$table->json('metadata')->nullable()->comment('成就解锁附加信息');
$table->timestamps();
$table->unique(['user_id', 'achievement_key']);
$table->index(['achievement_key', 'achieved_at']);
});
}
/**
* 回滚迁移:删除用户成就解锁记录表。
*/
public function down(): void
{
Schema::dropIfExists('user_achievements');
}
};
@@ -0,0 +1,34 @@
<?php
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::create('user_achievement_progress', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete()->comment('用户ID');
$table->string('achievement_key', 80)->comment('成就唯一标识');
$table->unsignedBigInteger('progress_value')->default(0)->comment('当前进度值');
$table->unsignedBigInteger('threshold_value')->default(0)->comment('达成门槛快照');
$table->timestamp('last_scanned_at')->nullable()->index()->comment('最近扫描时间');
$table->timestamps();
$table->unique(['user_id', 'achievement_key']);
});
}
/**
* 回滚迁移:删除用户成就进度快照表。
*/
public function down(): void
{
Schema::dropIfExists('user_achievement_progress');
}
};