91 lines
4.1 KiB
PHP
91 lines
4.1 KiB
PHP
<?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('chat_conversations', function (Blueprint $table) {
|
||
$table->id();
|
||
$table->string('type', 10); // direct | group
|
||
$table->string('name', 100)->nullable();
|
||
// Direktnachrichten: „kleinere-ID:größere-ID“ – je Paar genau ein Gespräch
|
||
$table->string('direct_key', 50)->nullable()->unique();
|
||
$table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
|
||
$table->unsignedBigInteger('last_message_id')->nullable();
|
||
$table->timestamp('last_message_at')->nullable()->index();
|
||
$table->timestamps();
|
||
});
|
||
|
||
Schema::create('chat_participants', function (Blueprint $table) {
|
||
$table->foreignId('conversation_id')->constrained('chat_conversations')->cascadeOnDelete();
|
||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||
$table->string('role', 10)->default('member'); // member | admin
|
||
$table->unsignedBigInteger('last_read_message_id')->nullable();
|
||
$table->unsignedBigInteger('notified_message_id')->nullable();
|
||
$table->boolean('muted')->default(false);
|
||
$table->timestamp('joined_at')->nullable();
|
||
$table->timestamp('left_at')->nullable();
|
||
|
||
$table->primary(['conversation_id', 'user_id']);
|
||
$table->index(['user_id', 'left_at']);
|
||
});
|
||
|
||
Schema::create('chat_messages', function (Blueprint $table) {
|
||
$table->id();
|
||
$table->foreignId('conversation_id')->constrained('chat_conversations')->cascadeOnDelete();
|
||
$table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
|
||
$table->string('kind', 10)->default('text'); // text | gif | system
|
||
$table->text('body')->nullable();
|
||
$table->string('gif_url', 500)->nullable();
|
||
$table->foreignId('reply_to_id')->nullable()->constrained('chat_messages')->nullOnDelete();
|
||
$table->timestamp('edited_at')->nullable();
|
||
$table->timestamp('deleted_at')->nullable();
|
||
// updated_at: auch bei Reaktionen – so holt die Abfrage geänderte Nachrichten nach
|
||
$table->timestamps();
|
||
|
||
$table->index(['conversation_id', 'id']);
|
||
$table->index(['conversation_id', 'updated_at']);
|
||
});
|
||
|
||
Schema::create('chat_attachments', function (Blueprint $table) {
|
||
$table->id();
|
||
$table->foreignId('message_id')->constrained('chat_messages')->cascadeOnDelete();
|
||
$table->string('path', 300);
|
||
$table->string('name', 200);
|
||
$table->string('mime', 100);
|
||
$table->unsignedBigInteger('size');
|
||
$table->unsignedInteger('width')->nullable();
|
||
$table->unsignedInteger('height')->nullable();
|
||
});
|
||
|
||
Schema::create('chat_reactions', function (Blueprint $table) {
|
||
$table->foreignId('message_id')->constrained('chat_messages')->cascadeOnDelete();
|
||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||
$table->string('emoji', 16);
|
||
|
||
$table->primary(['message_id', 'user_id', 'emoji']);
|
||
});
|
||
|
||
// Eigene GIF-Sammlung (von der Administration hochgeladen)
|
||
Schema::create('chat_gifs', function (Blueprint $table) {
|
||
$table->id();
|
||
$table->string('path', 300);
|
||
$table->string('title', 100);
|
||
$table->string('tags', 300)->nullable();
|
||
$table->unsignedInteger('width')->nullable();
|
||
$table->unsignedInteger('height')->nullable();
|
||
$table->timestamps();
|
||
});
|
||
}
|
||
|
||
public function down(): void
|
||
{
|
||
foreach (['chat_gifs', 'chat_reactions', 'chat_attachments', 'chat_messages', 'chat_participants', 'chat_conversations'] as $table) {
|
||
Schema::dropIfExists($table);
|
||
}
|
||
}
|
||
};
|