diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index 6563935..c6126a7 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -188,6 +188,29 @@ public function storeCapsule(Request $request, $chatId) 'message' => 'required|string' ]); + $chat = Chat::with('members')->find($chatId); + + if (!$chat || !$chat->members->contains(auth()->id())) { + return response()->json(['error' => 'Chat not found or unauthorized.'], 404); + } + + // Logic to check if the user is blocked by the other member or if the other member is blocked + if ($chat->members->count() == 2) { + $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.']); + } + } + // Check if the file is valid if ($request->hasFile('file') && $request->file('file')->isValid()) { $media = $request->file('file'); diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 11f03d1..566e84c 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -12,6 +12,7 @@ @@ -49,6 +50,11 @@ function loadChat(chatId, discussionName, discussionPicture, members, newOpening deletedMessages = []; messagesContainer.innerHTML = ''; + // Reset the message content and the error message + let errorMessage = document.getElementById('error-message'); + document.getElementById('message-content').value = ''; + errorMessage.style.display = 'none'; + // Update the hidden input with the chat ID const hiddenInput = document.getElementById('discussion-id'); if (hiddenInput) hiddenInput.value = chatId; @@ -291,6 +297,19 @@ function startAutoRefresh(interval) { body: JSON.stringify({ message: messageContent }) + }) + .then(response => { + return response.json(); + }) + .then(data => { + const errorMessage = document.getElementById('error-message'); + if (data.error) { + errorMessage.textContent = data.error; + errorMessage.style.display = 'block'; + } else { + document.getElementById('message-content').value = ''; + errorMessage.style.display = 'none'; + } }); document.getElementById('message-content').value = ''; } catch (error) { @@ -546,7 +565,7 @@ function capsuleForm() { }) .then(response => response.json()) .then(data => { - if (data.message.id) { + if (data.message && data.message.id) { // Close the modal and reset the form this.chatMessage = ''; this.file = null; @@ -555,8 +574,22 @@ function capsuleForm() { document.getElementById('date-time').value = ''; this.$dispatch('close'); } else { - console.log(data) - alert('Erreur lors de l\'envoi de la capsule'); + const errorMessage = document.getElementById('error-message'); + if (data.error == "You are blocked by this user or you blocked this user.") { + errorMessage.textContent = data.error; + errorMessage.style.display = 'block'; + // Close the modal and reset the form + this.chatMessage = ''; + this.file = null; + document.getElementById('file-info').innerHTML = + `

Glissez et déposez votre fichier ici ou

cliquez pour sélectionner un fichier

`; + document.getElementById('date-time').value = ''; + this.$dispatch('close'); + } else { + document.getElementById('message-content').value = ''; + errorMessage.style.display = 'none'; + alert('Erreur lors de l\'envoi'); + } } }) .catch(error => {