diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index 35bc736..813ff52 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -69,59 +69,90 @@ public function getMessages($chatId) } public function storeMessage(Request $request, $chatId) -{ - $request->validate([ - 'message' => 'required|string', - ]); + { + $request->validate([ + 'message' => 'required|string', + ]); - $chat = Chat::with('members')->find($chatId); + $chat = Chat::with('members')->find($chatId); - if (!$chat || !$chat->members->contains(auth()->id())) { - return response()->json(['error' => 'Chat not found or unauthorized.'], 404); - } + if (!$chat || !$chat->members->contains(auth()->id())) { + return response()->json(['error' => 'Chat not found or unauthorized.'], 404); + } - if ($chat->members->count() > 2) { - $blockedByAnyone = $chat->members->filter(function ($member) { - return $member->id !== auth()->id(); - })->contains(function ($member) { - $friendship = Friendship::where(function ($query) use ($member) { + if ($chat->members->count() > 2) { + $blockedByAnyone = $chat->members->filter(function ($member) { + return $member->id !== auth()->id(); + })->contains(function ($member) { + $friendship = Friendship::where(function ($query) use ($member) { + $query->where('user_id', auth()->id()) + ->where('friend_id', $member->id); + })->orWhere(function ($query) use ($member) { + $query->where('user_id', $member->id) + ->where('friend_id', auth()->id()); + })->first(); + + return $friendship && $friendship->isBlocked(); + }); + + if ($blockedByAnyone) { + return response()->json(['error' => 'You are blocked by one of the members of this group.']); + } + } else { + $otherMember = $chat->members->firstWhere('id', '!=', auth()->id()); + + $friendship = Friendship::where(function ($query) use ($otherMember) { $query->where('user_id', auth()->id()) - ->where('friend_id', $member->id); - })->orWhere(function ($query) use ($member) { - $query->where('user_id', $member->id) - ->where('friend_id', auth()->id()); + ->where('friend_id', $otherMember->id); + })->orWhere(function ($query) use ($otherMember) { + $query->where('user_id', $otherMember->id) + ->where('friend_id', auth()->id()); })->first(); - return $friendship && $friendship->isBlocked(); - }); - - if ($blockedByAnyone) { - return response()->json(['error' => 'You are blocked by one of the members of this group.']); + if ($friendship && $friendship->isBlocked()) { + return response()->json(['error' => 'You are blocked by this user or you blocked this user.']); + } } - } else { - $otherMember = $chat->members->firstWhere('id', '!=', auth()->id()); - $friendship = Friendship::where(function ($query) use ($otherMember) { - $query->where('user_id', auth()->id()) - ->where('friend_id', $otherMember->id); - })->orWhere(function ($query) use ($otherMember) { - $query->where('user_id', $otherMember->id) - ->where('friend_id', auth()->id()); - })->first(); - - if ($friendship && $friendship->isBlocked()) { - return response()->json(['error' => 'You are blocked by this user or you blocked this user.']); - } + $message = new Message(); + $message->user_id = auth()->id(); + $message->chat_id = $chatId; + $message->message = $request->message; + $message->save(); + + return response()->json(['message' => $message]); } - $message = new Message(); - $message->user_id = auth()->id(); - $message->chat_id = $chatId; - $message->message = $request->message; - $message->save(); + public function storeCapsule(Request $request, $chatId) + { + // Validation + $request->validate([ + 'file' => 'required|file|mimes:jpeg,png,jpg,gif,mp3,mp4|max:1024', + 'message' => 'required|string', + ]); + + // Vérification de la présence du fichier + if ($request->hasFile('file') && $request->file('file')->isValid()) { + $media = $request->file('file'); + $mediaName = time() . '_' . $media->getClientOriginalName(); - return response()->json(['message' => $message]); -} + // Déplacer le fichier vers le répertoire public/source/media + $media->move(public_path('source/media'), $mediaName); + + // Créer un nouveau message et l'enregistrer dans la base de données + $message = new Message(); + $message->user_id = auth()->id(); + $message->chat_id = $chatId; + $message->message = $request->message; + $message->media_url = $mediaName; + + $message->save(); + + return response()->json(['message' => $message]); + } + + return response()->json(['error' => 'Le fichier n\'a pas été envoyé ou est invalide.'], 400); + } public function storeChat(Request $request) { diff --git a/resources/views/components/messaging/chatbar.blade.php b/resources/views/components/messaging/chatbar.blade.php index c77ed11..c934e1a 100644 --- a/resources/views/components/messaging/chatbar.blade.php +++ b/resources/views/components/messaging/chatbar.blade.php @@ -1,13 +1,115 @@
+ + ${message.user.name} + ${message.message} +
`; + + // Gestion des médias + if (message.media_url) { + let mediaElement = + ` ++ ${message.message} +
+