From d59d326178c8689d730b9cbb2db0e0dfed5352ad Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Wed, 27 Mar 2024 08:46:27 -0400 Subject: [PATCH] chat is now polymorph and user based --- app/Http/Controllers/ChatController.php | 7 ++++--- app/Http/Controllers/CollectionController.php | 5 ++--- app/Models/Chat.php | 4 +--- app/Models/Collection.php | 1 - database/factories/ChatFactory.php | 4 ++-- tests/Feature/Http/Controllers/ChatControllerTest.php | 4 +--- 6 files changed, 10 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index 6b9f213d..e52d6355 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -6,11 +6,11 @@ use App\Http\Resources\CollectionResource; use App\Models\Chat; use App\Models\Collection; -use Illuminate\Http\Request; class ChatController extends Controller { - public function storeCollectionChat(Collection $collection) { + public function storeCollectionChat(Collection $collection) + { $chat = new Chat(); $chat->chatable_id = $collection->id; $chat->chatable_type = Collection::class; @@ -25,7 +25,8 @@ public function storeCollectionChat(Collection $collection) { ]); } - public function showCollectionChat(Collection $collection, Chat $chat) { + public function showCollectionChat(Collection $collection, Chat $chat) + { return inertia('Collection/Chat', [ 'collection' => new CollectionResource($collection), diff --git a/app/Http/Controllers/CollectionController.php b/app/Http/Controllers/CollectionController.php index 5ad5be4f..a3a22d0d 100644 --- a/app/Http/Controllers/CollectionController.php +++ b/app/Http/Controllers/CollectionController.php @@ -7,7 +7,6 @@ use App\Http\Resources\CollectionResource; use App\Http\Resources\DocumentResource; use App\Jobs\ProcessFileJob; -use App\Models\Chat; use App\Models\Collection; use App\Models\Document; @@ -46,11 +45,11 @@ public function store() public function show(Collection $collection) { - $chatResource = $collection->chats()->where("user_id", auth()->user()->id) + $chatResource = $collection->chats()->where('user_id', auth()->user()->id) ->latest('id') ->first(); - if($chatResource?->id) { + if ($chatResource?->id) { $chatResource = new ChatResource($chatResource); } diff --git a/app/Models/Chat.php b/app/Models/Chat.php index 7c362846..a3afc6e0 100644 --- a/app/Models/Chat.php +++ b/app/Models/Chat.php @@ -34,7 +34,6 @@ public function addInput(string $message, bool $in = true, bool $isChatIgnored = 'is_chat_ignored' => $isChatIgnored, ]); - return $message; } @@ -97,7 +96,6 @@ public function getOpenAiImage(): string return ''; } - public function chatable() { return $this->morphTo(); @@ -116,7 +114,7 @@ public function messages(): HasMany return $this->hasMany(Message::class); } - public function user() : BelongsTo + public function user(): BelongsTo { return $this->belongsTo(User::class); } diff --git a/app/Models/Collection.php b/app/Models/Collection.php index 15ed542f..87dbbc7e 100644 --- a/app/Models/Collection.php +++ b/app/Models/Collection.php @@ -39,7 +39,6 @@ public function documents(): HasMany return $this->hasMany(Document::class); } - public function chats(): MorphMany { return $this->morphMany(Chat::class, 'chatable'); diff --git a/database/factories/ChatFactory.php b/database/factories/ChatFactory.php index a5f6c776..2fc2a540 100644 --- a/database/factories/ChatFactory.php +++ b/database/factories/ChatFactory.php @@ -20,8 +20,8 @@ public function definition(): array { return [ 'user_id' => User::factory(), - "chatable_id" => Collection::factory(), - "chatable_type" => Collection::class, + 'chatable_id' => Collection::factory(), + 'chatable_type' => Collection::class, ]; } } diff --git a/tests/Feature/Http/Controllers/ChatControllerTest.php b/tests/Feature/Http/Controllers/ChatControllerTest.php index f4b4b056..3dde1f67 100644 --- a/tests/Feature/Http/Controllers/ChatControllerTest.php +++ b/tests/Feature/Http/Controllers/ChatControllerTest.php @@ -4,8 +4,6 @@ use App\Models\Collection; use App\Models\User; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class ChatControllerTest extends TestCase @@ -21,7 +19,7 @@ public function test_can_create_chat_and_redirect(): void $this->assertDatabaseCount('chats', 0); $this->actingAs($user)->post(route('chats.collection.store', [ 'collection' => $collection->id, - ]))->assertRedirect(); + ]))->assertRedirect(); $this->assertDatabaseCount('chats', 1); } }