Create chat.html
Browse files- public/chat.html +137 -0
public/chat.html
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1.0">
|
| 6 |
+
<title>Chat</title>
|
| 7 |
+
</head>
|
| 8 |
+
<body style="margin: 0; font-family: Arial, sans-serif; background-color: #121212; color: #ffffff; display: flex; flex-direction: column; height: 100vh;">
|
| 9 |
+
<header style="background-color: #1e1e1e; padding: 10px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid #333;">
|
| 10 |
+
<a href="/" style="color: #ffffff; text-decoration: none;">⬅ Back</a>
|
| 11 |
+
<h1 style="margin: 0; font-size: 1.2rem;">Chat</h1>
|
| 12 |
+
</header>
|
| 13 |
+
<main id="chat-box" style="flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 10px; background-color: #1c1c1c;"></main>
|
| 14 |
+
<footer style="display: flex; align-items: center; padding: 10px; background-color: #1e1e1e; border-top: 1px solid #333;">
|
| 15 |
+
<input type="text" id="message-input" placeholder="Type a message" style="flex: 1; padding: 10px; border: 1px solid #444; border-radius: 20px; background: #2c2c2c; color: #fff; margin-right: 10px;">
|
| 16 |
+
<input type="file" id="image-input" style="display: none;">
|
| 17 |
+
<button id="send-image" style="background: #4caf50; border: none; color: white; padding: 10px 15px; border-radius: 50%; cursor: pointer;">📷</button>
|
| 18 |
+
<button id="send-message" style="background: #4caf50; border: none; color: white; padding: 10px 15px; border-radius: 50%; cursor: pointer;">Send</button>
|
| 19 |
+
</footer>
|
| 20 |
+
|
| 21 |
+
<!-- Key Input Modal -->
|
| 22 |
+
<div id="key-modal" style="display: flex; flex-direction: column; align-items: center; justify-content: center; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.9); z-index: 1000;">
|
| 23 |
+
<div style="background: #1e1e1e; padding: 20px; border-radius: 8px; text-align: center;">
|
| 24 |
+
<h2 style="color: #fff;">Enter Key</h2>
|
| 25 |
+
<input id="key-input" type="password" placeholder="Enter your key" style="padding: 10px; width: 100%; border: 1px solid #444; border-radius: 5px; margin-top: 10px; color: #fff; background: #2c2c2c;">
|
| 26 |
+
<button id="verify-key" style="margin-top: 10px; padding: 10px 20px; background: #4caf50; border: none; color: #fff; cursor: pointer; border-radius: 5px;">Verify</button>
|
| 27 |
+
</div>
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
<script src="/socket.io/socket.io.js"></script>
|
| 31 |
+
<script>
|
| 32 |
+
const chatBox = document.getElementById('chat-box');
|
| 33 |
+
const messageInput = document.getElementById('message-input');
|
| 34 |
+
const sendMessageButton = document.getElementById('send-message');
|
| 35 |
+
const sendImageButton = document.getElementById('send-image');
|
| 36 |
+
const imageInput = document.getElementById('image-input');
|
| 37 |
+
const keyModal = document.getElementById('key-modal');
|
| 38 |
+
const keyInput = document.getElementById('key-input');
|
| 39 |
+
const verifyKeyButton = document.getElementById('verify-key');
|
| 40 |
+
const jid = window.location.pathname.split('/chat/')[1];
|
| 41 |
+
let replyTo = null;
|
| 42 |
+
let userKey = localStorage.getItem('userKey');
|
| 43 |
+
|
| 44 |
+
// Verify the key with the server
|
| 45 |
+
async function verifyKey(key) {
|
| 46 |
+
const res = await fetch('/validate-key', {
|
| 47 |
+
method: 'POST',
|
| 48 |
+
headers: { 'Content-Type': 'application/json' },
|
| 49 |
+
body: JSON.stringify({ key }),
|
| 50 |
+
});
|
| 51 |
+
return res.ok;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// Handle key submission
|
| 55 |
+
verifyKeyButton.addEventListener('click', async () => {
|
| 56 |
+
const enteredKey = keyInput.value.trim();
|
| 57 |
+
if (!enteredKey) return alert('Please enter a key.');
|
| 58 |
+
|
| 59 |
+
const isValid = await verifyKey(enteredKey);
|
| 60 |
+
if (isValid) {
|
| 61 |
+
userKey = enteredKey;
|
| 62 |
+
localStorage.setItem('userKey', userKey);
|
| 63 |
+
keyModal.style.display = 'none';
|
| 64 |
+
loadMessages();
|
| 65 |
+
} else {
|
| 66 |
+
alert('Invalid key. Please try again.');
|
| 67 |
+
}
|
| 68 |
+
});
|
| 69 |
+
|
| 70 |
+
// Add a message to the chat box
|
| 71 |
+
function addMessageToChat(msg) {
|
| 72 |
+
const messageDiv = document.createElement('div');
|
| 73 |
+
messageDiv.style = `
|
| 74 |
+
max-width: 70%;
|
| 75 |
+
padding: 10px;
|
| 76 |
+
border-radius: 8px;
|
| 77 |
+
background-color: ${msg.sender === 'You' ? '#4caf50' : '#2c2c2c'};
|
| 78 |
+
color: #ffffff;
|
| 79 |
+
align-self: ${msg.sender === 'You' ? 'flex-end' : 'flex-start'};
|
| 80 |
+
word-wrap: break-word;
|
| 81 |
+
`;
|
| 82 |
+
messageDiv.innerHTML = `<strong>${msg.sender}</strong><br>${msg.content}`;
|
| 83 |
+
chatBox.appendChild(messageDiv);
|
| 84 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// Load chat messages
|
| 88 |
+
async function loadMessages() {
|
| 89 |
+
try {
|
| 90 |
+
const res = await fetch(`/api/messages/${jid}`, {
|
| 91 |
+
headers: { Authorization: `Bearer ${userKey}` },
|
| 92 |
+
});
|
| 93 |
+
if (!res.ok) throw new Error('Failed to load messages');
|
| 94 |
+
const messages = await res.json();
|
| 95 |
+
chatBox.innerHTML = '';
|
| 96 |
+
messages.forEach(addMessageToChat);
|
| 97 |
+
} catch (error) {
|
| 98 |
+
console.error(error);
|
| 99 |
+
chatBox.innerHTML = '<p style="color: red;">Failed to load messages.</p>';
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
// Send a message
|
| 104 |
+
sendMessageButton.addEventListener('click', async () => {
|
| 105 |
+
const message = messageInput.value.trim();
|
| 106 |
+
if (!message) return;
|
| 107 |
+
|
| 108 |
+
try {
|
| 109 |
+
await fetch('/send-message', {
|
| 110 |
+
method: 'POST',
|
| 111 |
+
headers: {
|
| 112 |
+
'Content-Type': 'application/json',
|
| 113 |
+
Authorization: `Bearer ${userKey}`,
|
| 114 |
+
},
|
| 115 |
+
body: JSON.stringify({ jid, message, replyTo }),
|
| 116 |
+
});
|
| 117 |
+
messageInput.value = '';
|
| 118 |
+
replyTo = null;
|
| 119 |
+
} catch (error) {
|
| 120 |
+
console.error('Failed to send message:', error);
|
| 121 |
+
}
|
| 122 |
+
});
|
| 123 |
+
|
| 124 |
+
// Check if the user is already verified
|
| 125 |
+
if (userKey) {
|
| 126 |
+
verifyKey(userKey).then((isValid) => {
|
| 127 |
+
if (isValid) {
|
| 128 |
+
keyModal.style.display = 'none';
|
| 129 |
+
loadMessages();
|
| 130 |
+
} else {
|
| 131 |
+
localStorage.removeItem('userKey');
|
| 132 |
+
}
|
| 133 |
+
});
|
| 134 |
+
}
|
| 135 |
+
</script>
|
| 136 |
+
</body>
|
| 137 |
+
</html>
|