Skip to content

Commit

Permalink
Blocked user error message
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyNadd committed Dec 19, 2024
1 parent ef1c1db commit 79204d4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
23 changes: 23 additions & 0 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
39 changes: 36 additions & 3 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<div id="chat-area" class="flex-1 background-app flex flex-col h-full relative hidden">
<x-messaging.header />
<x-messaging.messages />
<div id="error-message" style="display: none; color: red; margin-top: 10px;"></div>
<x-messaging.chatbar />
</div>
</div>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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 =
`<p class='text-gray-300 font-medium'>Glissez et déposez votre fichier ici ou</p><p class='text-blue-400 underline'>cliquez pour sélectionner un fichier</p>`;
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 => {
Expand Down

0 comments on commit 79204d4

Please sign in to comment.