From 8b5df135373bbd4c29752e7f7ed5fb092b487853 Mon Sep 17 00:00:00 2001 From: swaparup36 Date: Sat, 22 Mar 2025 03:14:42 +0530 Subject: [PATCH 1/2] added a close button to delete the message chat in messages --- blt/urls.py | 2 ++ website/templates/messaging.html | 54 +++++++++++++++++++++++++++++++- website/views/user.py | 15 +++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/blt/urls.py b/blt/urls.py index 02f59ae14b..adfccd1f5f 100644 --- a/blt/urls.py +++ b/blt/urls.py @@ -303,6 +303,7 @@ contributors_view, create_wallet, delete_notification, + delete_thread, deletions, fetch_notifications, follow_user, @@ -1096,6 +1097,7 @@ path("api/messaging/set-public-key/", set_public_key, name="set_public_key"), path("api/messaging//get-public-key/", get_public_key, name="get_public_key"), path("repository//activity-data/", repo_activity_data, name="repo_activity_data"), + path("api/messaging/thread//delete/", delete_thread, name="delete_thread"), ] if settings.DEBUG: diff --git a/website/templates/messaging.html b/website/templates/messaging.html index 7a8c654336..8b5d90c158 100644 --- a/website/templates/messaging.html +++ b/website/templates/messaging.html @@ -25,11 +25,12 @@ class="w-[30%] bg-[#e74c3c] p-[10px] overflow-y-auto">

Conversations

{% for thread in threads %} -
{% for participant in thread.participants.all %} {% if participant != request.user %}{{ participant.username }}{% endif %} {% endfor %} + {% if participant != request.user %}{% endif %}
{% endfor %} @@ -239,9 +240,11 @@

Conversations

isConnecting = true; updateConnectionStatus(false); try { + console.log("Attempting WebSocket connection..."); // to remove const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://'; socket = new WebSocket(protocol + window.location.host + `/ws/messaging/${threadId}/`); socket.onopen = function() { + console.log("WebSocket connection established"); // to remove isConnecting = false; reconnectAttempts = 0; updateConnectionStatus(true); @@ -255,11 +258,13 @@

Conversations

} }; socket.onerror = function(error) { + console.error("WebSocket error:", error); isConnecting = false; updateConnectionStatus(false); }; socket.onmessage = handleWebSocketMessage; } catch (error) { + console.error("Error creating WebSocket:", error); // to remove isConnecting = false; updateConnectionStatus(false); } @@ -404,6 +409,53 @@

Conversations

document.getElementById("unlock-button").addEventListener("click", async function() { await initializeEncryptionKeys(); }); + + // ===== Delete Thread ===== + + document.querySelectorAll('.del-thread').forEach(deleteBtn => { + deleteBtn.addEventListener('click', async function(e) { + e.stopPropagation(); + const threadElement = this.closest('.conversation'); + const threadId = threadElement.dataset.id; + + if (confirm('Are you sure you want to delete this conversation? This cannot be undone.')) { + try { + const response = await fetch(`/api/messaging/thread/${threadId}/delete/`, { + method: 'POST', + headers: { + 'X-CSRFToken': getCookie('csrftoken'), + 'Content-Type': 'application/json' + } + }); + + const data = await response.json(); + if (data.status === 'success') { + // Remove the thread from UI + threadElement.remove(); + + // Clear chat window if this was the active thread + if (currentThreadId === threadId) { + document.getElementById('message-list').innerHTML = '

Welcome to BLT DMs, search a user to start chatting.

'; + document.getElementById('message-input').style.display = 'none'; + document.getElementById('send-message').style.display = 'none'; + document.getElementById('welcome-message').style.display = 'block'; + + // Close WebSocket connection + if (socket) { + socket.close(); + } + currentThreadId = null; + } + } else { + alert('Failed to delete conversation'); + } + } catch (error) { + console.error('Error deleting thread:', error); + alert('Failed to delete conversation'); + } + } + }); + }); }); {% endblock %} diff --git a/website/views/user.py b/website/views/user.py index 7028899e97..4f5a475eda 100644 --- a/website/views/user.py +++ b/website/views/user.py @@ -1104,6 +1104,21 @@ def view_thread(request, thread_id): return JsonResponse(data, safe=False) +@login_required +def delete_thread(request, thread_id): + if request.method == "POST": + try: + thread = Thread.objects.get(id=thread_id) + # Check if user is a participant + if request.user in thread.participants.all(): + thread.delete() + return JsonResponse({"status": "success"}) + return JsonResponse({"status": "error", "message": "Unauthorized"}, status=403) + except Thread.DoesNotExist: + return JsonResponse({"status": "error", "message": "Thread not found"}, status=404) + return JsonResponse({"status": "error", "message": "Invalid method"}, status=405) + + @login_required @require_http_methods(["GET"]) def get_public_key(request, thread_id): From dfaf884ead6a358941f5c99e5b7053735e1ddb42 Mon Sep 17 00:00:00 2001 From: swaparup36 Date: Sun, 23 Mar 2025 02:05:05 +0530 Subject: [PATCH 2/2] removed all console logs --- website/templates/messaging.html | 3 --- 1 file changed, 3 deletions(-) diff --git a/website/templates/messaging.html b/website/templates/messaging.html index 8b5d90c158..aa7fca3937 100644 --- a/website/templates/messaging.html +++ b/website/templates/messaging.html @@ -240,11 +240,9 @@

Conversations

isConnecting = true; updateConnectionStatus(false); try { - console.log("Attempting WebSocket connection..."); // to remove const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://'; socket = new WebSocket(protocol + window.location.host + `/ws/messaging/${threadId}/`); socket.onopen = function() { - console.log("WebSocket connection established"); // to remove isConnecting = false; reconnectAttempts = 0; updateConnectionStatus(true); @@ -264,7 +262,6 @@

Conversations

}; socket.onmessage = handleWebSocketMessage; } catch (error) { - console.error("Error creating WebSocket:", error); // to remove isConnecting = false; updateConnectionStatus(false); }