File size: 17,793 Bytes
6c5dab0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
// Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyDN6tU7Ln3JgeFP7jTprAjSgXDfzunDGQI",
authDomain: "gay-db7b1.firebaseapp.com",
databaseURL: "https://gay-db7b1-default-rtdb.firebaseio.com",
projectId: "gay-db7b1",
storageBucket: "gay-db7b1.appspot.com",
messagingSenderId: "959507085021",
appId: "1:959507085021:web:755901093c2c81d0fb4d34",
measurementId: "G-1900F4D48J"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.database();
// Global variables
let currentRoomId = "general";
let currentUser = null;
// DOM elements
const loginForm = document.getElementById("login-form");
const registerForm = document.getElementById("register-form");
const toggleAuth = document.getElementById("toggle-auth");
const authTitle = document.getElementById("auth-title");
// Toggle between login and register forms
toggleAuth.addEventListener("click", () => {
loginForm.classList.toggle("hidden");
registerForm.classList.toggle("hidden");
authTitle.textContent = loginForm.classList.contains("hidden") ? "Đăng ký" : "Đăng nhập";
toggleAuth.textContent = loginForm.classList.contains("hidden") ? "Đã có tài khoản? Đăng nhập" : "Chưa có tài khoản? Đăng ký";
});
// Register form handler
registerForm.addEventListener("submit", (e) => {
e.preventDefault();
const username = document.getElementById("register-username").value;
const email = document.getElementById("register-email").value;
const password = document.getElementById("register-password").value;
auth.createUserWithEmailAndPassword(email, password).then(userCredential => {
return userCredential.user.updateProfile({ displayName: username });
}).then(() => {
document.getElementById("display-name").textContent = auth.currentUser.displayName;
showChat();
}).catch(error => alert(error.message));
});
// Login form handler
loginForm.addEventListener("submit", (e) => {
e.preventDefault();
const email = document.getElementById("login-email").value;
const password = document.getElementById("login-password").value;
auth.signInWithEmailAndPassword(email, password).then(() => {
document.getElementById("display-name").textContent = auth.currentUser.displayName || auth.currentUser.email;
showChat();
}).catch(error => alert(error.message));
});
// Google login handler
document.getElementById("google-login").addEventListener("click", () => {
const provider = new firebase.auth.GoogleAuthProvider();
auth.signInWithPopup(provider).then(() => {
document.getElementById("display-name").textContent = auth.currentUser.displayName;
showChat();
}).catch(error => alert(error.message));
});
// Show chat section
function showChat() {
document.getElementById("auth-section").classList.add("hidden");
document.getElementById("chat-section").classList.remove("hidden");
currentUser = auth.currentUser;
// Initialize room functionality
initializeRooms();
loadUserRooms();
setupRoomListeners();
}
// Send message function
function sendMessage() {
const input = document.getElementById("chat-input");
const imageInput = document.getElementById("imageInput");
const videoInput = document.getElementById("videoInput");
const text = input.value.trim();
const images = imageInput.files;
const videos = videoInput.files;
if (!text && images.length === 0 && videos.length === 0) return;
// Send text message if exists
if (text) {
db.ref(`rooms/${currentRoomId}/messages`).push({
name: auth.currentUser.displayName || auth.currentUser.email,
text: text,
uid: auth.currentUser.uid,
timestamp: Date.now()
});
// Update room last message
updateRoomLastMessage(currentRoomId, text);
}
// Send images
if (images.length > 0) {
sendImages(images);
}
// Send videos
if (videos.length > 0) {
sendVideos(videos);
}
// Clear inputs
input.value = "";
imageInput.value = "";
videoInput.value = "";
document.getElementById("mediaPreview").innerHTML = "";
}
// Send images function
async function sendImages(files) {
for (const file of files) {
try {
const base64 = await toBase64(file);
const imageUrl = await uploadToImgbb(base64);
if (imageUrl) {
db.ref(`rooms/${currentRoomId}/messages`).push({
name: auth.currentUser.displayName || auth.currentUser.email,
text: "",
image: imageUrl,
uid: auth.currentUser.uid,
timestamp: Date.now()
});
// Update room last message
updateRoomLastMessage(currentRoomId, "📷 Hình ảnh");
}
} catch (error) {
console.error("Error uploading image:", error);
alert("Lỗi upload ảnh!");
}
}
}
// Send videos function
async function sendVideos(files) {
for (const file of files) {
try {
showUploadProgress("Đang upload video...");
const videoUrl = await uploadVideo(file);
hideUploadProgress();
if (videoUrl) {
db.ref(`rooms/${currentRoomId}/messages`).push({
name: auth.currentUser.displayName || auth.currentUser.email,
text: "",
video: videoUrl,
uid: auth.currentUser.uid,
timestamp: Date.now()
});
// Update room last message
updateRoomLastMessage(currentRoomId, "🎥 Video");
}
} catch (error) {
hideUploadProgress();
console.error("Error uploading video:", error);
alert("Lỗi upload video!");
}
}
}
// Convert file to base64
function toBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
// Upload image to imgbb
async function uploadToImgbb(base64) {
const apiKey = "eb64cc8f94a115e3a412694f2a0f10b3";
const formData = new FormData();
formData.append("key", apiKey);
formData.append("image", base64);
try {
const response = await fetch("https://api.imgbb.com/1/upload", {
method: "POST",
body: formData
});
const result = await response.json();
return result.data.url;
} catch (error) {
console.error("Error uploading to imgbb:", error);
return null;
}
}
// Upload video to server
async function uploadVideo(file) {
const formData = new FormData();
formData.append("file", file);
try {
const response = await fetch("http://192.168.2.47:5000/upload", {
method: "POST",
body: formData,
});
const result = await response.json();
// Check if upload was successful
if (result.status === "success" && result.data && result.data.url) {
return result.data.url;
} else {
console.error("Upload failed:", result);
return null;
}
} catch (error) {
console.error("Error uploading video:", error);
return null;
}
}
// Show upload progress
function showUploadProgress(message) {
const preview = document.getElementById("mediaPreview");
preview.innerHTML = `
<div class="upload-progress">
<div class="upload-progress-bar" style="width: 100%">${message}</div>
</div>
`;
}
// Hide upload progress
function hideUploadProgress() {
document.getElementById("mediaPreview").innerHTML = "";
}
// Preview selected images
document.getElementById("imageInput").addEventListener("change", function() {
const files = this.files;
const preview = document.getElementById("mediaPreview");
preview.innerHTML = "";
for (const file of files) {
const reader = new FileReader();
reader.onload = () => {
const img = document.createElement("img");
img.src = reader.result;
img.title = file.name;
preview.appendChild(img);
};
reader.readAsDataURL(file);
}
});
// Preview selected videos
document.getElementById("videoInput").addEventListener("change", function() {
const files = this.files;
const preview = document.getElementById("mediaPreview");
preview.innerHTML = "";
for (const file of files) {
const reader = new FileReader();
reader.onload = () => {
const video = document.createElement("video");
video.src = reader.result;
video.title = file.name;
video.controls = true;
video.muted = true;
preview.appendChild(video);
};
reader.readAsDataURL(file);
}
});
// Allow Enter key to send message
document.getElementById("chat-input").addEventListener("keydown", function(event) {
if (event.key === "Enter") {
event.preventDefault();
sendMessage();
}
});
// Listen for new messages
const chatBox = document.getElementById("chat-box");
let messagesListener = null;
function listenToMessages() {
// Remove previous listener
if (messagesListener) {
messagesListener.off();
}
// Clear chat box
chatBox.innerHTML = "";
// Listen to current room messages
messagesListener = db.ref(`rooms/${currentRoomId}/messages`);
messagesListener.on("child_added", snapshot => {
const msg = snapshot.val();
const div = document.createElement("div");
div.className = "message " + (msg.uid === auth.currentUser?.uid ? "mine" : "other");
let content = `<strong>${msg.name}:</strong><br>`;
// Add text if exists
if (msg.text) {
content += `<span>${msg.text}</span>`;
}
// Add image if exists
if (msg.image) {
content += `<br><a href="${msg.image}" target="_blank">
<img src="${msg.image}" alt="Image" />
</a>`;
}
// Add video if exists
if (msg.video) {
content += `<br><video controls>
<source src="${msg.video}" type="video/mp4">
<source src="${msg.video}" type="video/webm">
Your browser does not support the video tag.
</video>`;
}
div.innerHTML = content;
chatBox.appendChild(div);
chatBox.scrollTop = chatBox.scrollHeight;
});
}
// Room management functions
function initializeRooms() {
// Create general room if not exists
db.ref('rooms/general').once('value', snapshot => {
if (!snapshot.exists()) {
db.ref('rooms/general').set({
name: "Room chung",
description: "Phòng chat chung cho tất cả mọi người",
createdBy: auth.currentUser.uid,
createdAt: Date.now(),
members: {
[auth.currentUser.uid]: {
name: auth.currentUser.displayName || auth.currentUser.email,
joinedAt: Date.now()
}
}
});
} else {
// Add current user to general room if not already member
db.ref(`rooms/general/members/${auth.currentUser.uid}`).set({
name: auth.currentUser.displayName || auth.currentUser.email,
joinedAt: Date.now()
});
}
});
// Switch to general room
switchToRoom("general", "Room chung");
}
function loadUserRooms() {
const roomList = document.getElementById("room-list");
// Listen for rooms where user is a member
db.ref('rooms').on('value', snapshot => {
roomList.innerHTML = "";
const rooms = snapshot.val();
if (rooms) {
Object.keys(rooms).forEach(roomId => {
const room = rooms[roomId];
if (room.members && room.members[auth.currentUser.uid]) {
createRoomListItem(roomId, room);
}
});
}
});
}
function createRoomListItem(roomId, room) {
const roomList = document.getElementById("room-list");
const roomItem = document.createElement("div");
roomItem.className = `room-item ${roomId === currentRoomId ? 'active' : ''}`;
roomItem.dataset.roomId = roomId;
roomItem.onclick = () => switchToRoom(roomId, room.name);
roomItem.innerHTML = `
<div class="room-info">
<div class="room-name">${room.name}</div>
<div class="room-last-message">${room.lastMessage || "Chưa có tin nhắn"}</div>
</div>
`;
roomList.appendChild(roomItem);
}
function switchToRoom(roomId, roomName) {
currentRoomId = roomId;
document.getElementById("current-room-name").textContent = roomName;
// Update active room in UI
document.querySelectorAll(".room-item").forEach(item => {
item.classList.remove("active");
});
// Find and activate current room by checking data attribute
document.querySelectorAll(".room-item").forEach(item => {
if (item.dataset.roomId === roomId) {
item.classList.add("active");
}
});
// Load messages for this room
listenToMessages();
}
function updateRoomLastMessage(roomId, message) {
db.ref(`rooms/${roomId}/lastMessage`).set(message);
db.ref(`rooms/${roomId}/lastMessageTime`).set(Date.now());
}
function createRoom() {
const roomName = prompt("Nhập tên phòng chat:");
if (!roomName || !roomName.trim()) return;
const roomDescription = prompt("Nhập mô tả phòng (tùy chọn):") || `Phòng chat ${roomName.trim()}`;
const roomId = 'room_' + Date.now();
db.ref(`rooms/${roomId}`).set({
name: roomName.trim(),
description: roomDescription.trim(),
createdBy: auth.currentUser.uid,
createdAt: Date.now(),
members: {
[auth.currentUser.uid]: {
name: auth.currentUser.displayName || auth.currentUser.email,
joinedAt: Date.now()
}
}
}).then(() => {
switchToRoom(roomId, roomName.trim());
alert(`Đã tạo phòng "${roomName.trim()}" thành công!`);
});
}
function setupRoomListeners() {
// Room list toggle
document.getElementById("roomListToggle").addEventListener("click", () => {
const sidebar = document.getElementById("room-sidebar");
sidebar.classList.toggle("show");
});
// Create room button
document.getElementById("createRoomBtn").addEventListener("click", createRoom);
// Join room button
document.getElementById("joinRoomBtn").addEventListener("click", showJoinRoomDialog);
// Room search functionality
const searchInput = document.getElementById("roomSearchInput");
searchInput.addEventListener("input", handleRoomSearch);
searchInput.addEventListener("focus", () => {
if (searchInput.value.trim()) {
document.getElementById("search-results").classList.remove("hidden");
}
});
// Hide search results when clicking outside
document.addEventListener("click", (event) => {
const sidebar = document.getElementById("room-sidebar");
const searchResults = document.getElementById("search-results");
if (!sidebar.contains(event.target)) {
searchResults.classList.add("hidden");
}
});
}
// Room search functionality
function handleRoomSearch() {
const searchTerm = document.getElementById("roomSearchInput").value.trim().toLowerCase();
const searchResults = document.getElementById("search-results");
const searchRoomList = document.getElementById("search-room-list");
if (!searchTerm) {
searchResults.classList.add("hidden");
return;
}
searchResults.classList.remove("hidden");
searchRoomList.innerHTML = "";
// Search all rooms in database
db.ref('rooms').once('value', snapshot => {
const rooms = snapshot.val();
let foundRooms = [];
if (rooms) {
Object.keys(rooms).forEach(roomId => {
const room = rooms[roomId];
if (room.name && room.name.toLowerCase().includes(searchTerm)) {
foundRooms.push({ id: roomId, ...room });
}
});
}
if (foundRooms.length === 0) {
searchRoomList.innerHTML = '<div class="no-results">Không tìm thấy phòng nào</div>';
} else {
foundRooms.forEach(room => {
createSearchResultItem(room.id, room);
});
}
});
}
function createSearchResultItem(roomId, room) {
const searchRoomList = document.getElementById("search-room-list");
const roomItem = document.createElement("div");
const isMember = room.members && room.members[auth.currentUser.uid];
roomItem.className = `room-item search-result`;
roomItem.dataset.roomId = roomId;
// Count members
const memberCount = room.members ? Object.keys(room.members).length : 0;
roomItem.innerHTML = `
<div class="room-info">
<div class="room-name">${room.name}</div>
<div class="room-last-message">${room.description || "Không có mô tả"}</div>
<div class="room-member-count">${memberCount} thành viên</div>
</div>
${isMember ?
`<button class="room-join-btn" onclick="switchToRoom('${roomId}', '${room.name}'); hideSearchResults();">Vào phòng</button>` :
`<button class="room-join-btn" onclick="joinRoom('${roomId}', '${room.name}')">Tham gia</button>`
}
`;
searchRoomList.appendChild(roomItem);
}
function joinRoom(roomId, roomName) {
// Add user to room members
db.ref(`rooms/${roomId}/members/${auth.currentUser.uid}`).set({
name: auth.currentUser.displayName || auth.currentUser.email,
joinedAt: Date.now()
}).then(() => {
// Switch to the joined room
switchToRoom(roomId, roomName);
hideSearchResults();
alert(`Đã tham gia phòng "${roomName}" thành công!`);
}).catch(error => {
console.error("Error joining room:", error);
alert("Lỗi khi tham gia phòng!");
});
}
function hideSearchResults() {
document.getElementById("search-results").classList.add("hidden");
document.getElementById("roomSearchInput").value = "";
}
function showJoinRoomDialog() {
const roomId = prompt("Nhập ID phòng muốn tham gia:");
if (!roomId || !roomId.trim()) return;
// Check if room exists
db.ref(`rooms/${roomId}`).once('value', snapshot => {
if (snapshot.exists()) {
const room = snapshot.val();
joinRoom(roomId, room.name);
} else {
alert("Không tìm thấy phòng với ID này!");
}
});
}
|