171 lines
6.7 KiB
Plaintext
171 lines
6.7 KiB
Plaintext
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Discord Clone - Group Chat</title>
|
|
<link rel="stylesheet" href="static/css/index.css"/>
|
|
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/default.min.css">
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"></script>
|
|
</head>
|
|
<body>
|
|
<div class="chat-container">
|
|
<!--<div class="chat-container" style="background-image: url('cdn/background.png'); backdrop-filter: blur(10px); background-size: cover; background-position: center; background-repeat: no-repeat;">-->
|
|
<!-- Chat Header -->
|
|
<div class="chat-header">
|
|
<div class="chat-title">
|
|
<img class="user-avatar" src="cdn/profile/0"></img>
|
|
<h1>Wish.com Discord frfr</h1>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Live Location Notification Bubble -->
|
|
<div class="notification-container">
|
|
<!--<div class="live-location-bubble" id="locationBubble">
|
|
<div class="map-container">
|
|
<img src="cdn/map.png" alt="Map" />
|
|
</div>
|
|
<div class="location-content">
|
|
<div class="location-icon">
|
|
<img src="cdn/icons/location.svg" alt="Location"></img>
|
|
</div>
|
|
<button class="join-button" id="joinButton">
|
|
Join
|
|
</button>
|
|
<div class="location-text">Live Location</div>
|
|
<div class="location-users" id="locationUsers">
|
|
|
|
</div>
|
|
</div>
|
|
</div>-->
|
|
</div>
|
|
|
|
<!-- Messages Container -->
|
|
<!--<div class="messages-container" style="background-image: url('cdn/background.png'); backdrop-filter: blur(10px); background-size: cover; background-position: center; background-repeat: no-repeat;">-->
|
|
<div class="messages-container"></div>
|
|
|
|
<!-- Input Container -->
|
|
<div class="input-container">
|
|
<div class="input-wrapper">
|
|
<input type="text" placeholder="Start Typing..." />
|
|
<button class="send-button">
|
|
<img src="cdn/icons/send.svg" alt="Send" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script type="module">
|
|
import markdownit from 'https://cdn.jsdelivr.net/npm/markdown-it@14.1.0/+esm';
|
|
const md = markdownit({
|
|
html: true,
|
|
linkify: true,
|
|
typographer: true,
|
|
highlight: function (str, lang) {
|
|
if (lang && hljs.getLanguage(lang)) {
|
|
try {
|
|
return hljs.highlight(str, { language: lang }).value;
|
|
} catch (__) {}
|
|
}
|
|
|
|
return ''; // use external default escaping
|
|
}
|
|
})
|
|
|
|
const user_id = {{ user_id }};
|
|
var users = {};
|
|
|
|
// Handle message sending
|
|
const input = document.querySelector("input");
|
|
const sendButton = document.querySelector(".send-button");
|
|
const messagesContainer = document.querySelector(
|
|
".messages-container",
|
|
);
|
|
|
|
function insertMessage(message) {
|
|
const date = new Date(message.timestamp).toLocaleTimeString("en-GB", {
|
|
hour: "numeric",
|
|
minute: "2-digit",
|
|
hour12: true,
|
|
});
|
|
|
|
const messageEl = document.createElement("div");
|
|
messageEl.className = "message";
|
|
messageEl.innerHTML = `
|
|
<img class="user-avatar" src="cdn/profile/${message.user_id}">
|
|
<div class="message-content">
|
|
<div class="message-header">
|
|
<span class="username">${message.display_name}</span>
|
|
<span class="timestamp">${date}</span>
|
|
</div>
|
|
<div class="message-text">${md.render(message.text)}</div>
|
|
</div>
|
|
`;
|
|
messagesContainer.appendChild(messageEl);
|
|
messagesContainer.scrollTop =
|
|
messagesContainer.scrollHeight;
|
|
}
|
|
|
|
function sendMessage() {
|
|
const message = input.value.trim();
|
|
if (message) {
|
|
fetch("/api/chat", {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
user_id: user_id,
|
|
text: message,
|
|
timestamp: new Date().getTime(),
|
|
}),
|
|
headers: {
|
|
"Content-type": "application/json; charset=UTF-8"
|
|
}
|
|
});
|
|
input.value = "";
|
|
}
|
|
}
|
|
|
|
sendButton.addEventListener("click", sendMessage);
|
|
input.addEventListener("keypress", function (e) {
|
|
if (e.key === "Enter") {
|
|
sendMessage();
|
|
}
|
|
});
|
|
|
|
async function loadData() {
|
|
try {
|
|
const userIds = await fetch("/api/users/")
|
|
.then(r => r.json());
|
|
|
|
const userPromises = userIds.map(userId =>
|
|
fetch(`/api/users/${userId}`)
|
|
.then(r => r.text())
|
|
.then(username => ({ userId, username }))
|
|
);
|
|
|
|
const userData = await Promise.all(userPromises);
|
|
|
|
userData.forEach(({ userId, username }) => {
|
|
users[userId] = username;
|
|
});
|
|
|
|
console.log('Users loaded:', users);
|
|
|
|
const messageSource = new EventSource("/api/events");
|
|
messageSource.onopen = () => messagesContainer.innerHTML = '';
|
|
messageSource.onmessage = (event) => insertMessage(JSON.parse(event.data));
|
|
messageSource.onerror = (error) => {
|
|
console.error('EventSource error:', error);
|
|
};
|
|
|
|
} catch (error) {
|
|
console.error('Error loading data:', error);
|
|
}
|
|
}
|
|
|
|
loadData();
|
|
|
|
</script>
|
|
</body>
|
|
</html>
|