Skip to content

Commit

Permalink
chat is now polymorph and user based
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Mar 27, 2024
1 parent 665fff4 commit d59d326
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 15 deletions.
7 changes: 4 additions & 3 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 1 addition & 3 deletions app/Models/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function addInput(string $message, bool $in = true, bool $isChatIgnored =
'is_chat_ignored' => $isChatIgnored,
]);


return $message;
}

Expand Down Expand Up @@ -97,7 +96,6 @@ public function getOpenAiImage(): string
return '<img src="data:image/png;base64, '.$result->data[0]->b64_json.'" loading="lazy" />';
}


public function chatable()
{
return $this->morphTo();
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion app/Models/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function documents(): HasMany
return $this->hasMany(Document::class);
}


public function chats(): MorphMany
{
return $this->morphMany(Chat::class, 'chatable');
Expand Down
4 changes: 2 additions & 2 deletions database/factories/ChatFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}
4 changes: 1 addition & 3 deletions tests/Feature/Http/Controllers/ChatControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit d59d326

Please sign in to comment.