2024-08-14 22:07:59 +00:00
|
|
|
|
let rooms = {};
|
|
|
|
|
let roomToDelete = null;
|
2024-08-11 12:17:53 +00:00
|
|
|
|
let currentRoom = null;
|
2024-08-14 22:07:59 +00:00
|
|
|
|
let currentHistoryId = 0;
|
|
|
|
|
|
|
|
|
|
function openRoom(currentRoom) {
|
|
|
|
|
alert('Вы вошли в комнату: ' + currentRoom);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function closeAdd() {
|
|
|
|
|
document.getElementById('add_members').style.display = 'none';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openAdd() {
|
|
|
|
|
document.getElementById('add_members').style.display = 'flex';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openConfirm(roomNickname) {
|
|
|
|
|
roomToDelete = roomNickname;
|
|
|
|
|
document.getElementById("delete-chat").style.display = "flex";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function closeConfirm() {
|
|
|
|
|
roomToDelete = null;
|
|
|
|
|
document.getElementById("delete-chat").style.display = "none";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deleteChat() {
|
|
|
|
|
if (roomToDelete && rooms[roomToDelete]) {
|
|
|
|
|
delete rooms[roomToDelete];
|
|
|
|
|
removeRoomFromList(roomToDelete);
|
|
|
|
|
closeConfirm();
|
|
|
|
|
} else {
|
|
|
|
|
alert("Не удалось найти выбранную комнату.");
|
2024-08-11 12:17:53 +00:00
|
|
|
|
}
|
2024-08-14 22:07:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addMember() {
|
|
|
|
|
const login = document.getElementById('newMemberLogin').value;
|
|
|
|
|
if (login) {
|
|
|
|
|
alert(`Участник с никнеймом '${login}' добавлен`);
|
|
|
|
|
closeAdd();
|
|
|
|
|
} else {
|
|
|
|
|
alert('Пожалуйста, введите логин участника');
|
2024-08-11 12:17:53 +00:00
|
|
|
|
}
|
2024-08-14 22:07:59 +00:00
|
|
|
|
}
|
2024-08-05 11:42:32 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
function openCreateRoomModal() {
|
|
|
|
|
document.getElementById('createRoomModal').style.display = 'block';
|
|
|
|
|
}
|
2024-08-11 12:17:53 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
function closeCreateRoomModal() {
|
|
|
|
|
document.getElementById('createRoomModal').style.display = 'none';
|
|
|
|
|
}
|
2024-08-11 12:17:53 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
async function createRoom() {
|
|
|
|
|
const errorElement = document.getElementById('error');
|
|
|
|
|
const roomName = document.getElementById('newRoomName').value.trim();
|
|
|
|
|
const roomNickname = document.getElementById('newRoomNickname').value.trim();
|
2024-08-11 12:17:53 +00:00
|
|
|
|
|
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
errorElement.style.display = 'none';
|
|
|
|
|
errorElement.textContent = '';
|
2024-08-11 12:17:53 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
if (roomName === '' || roomNickname === '') {
|
|
|
|
|
errorElement.textContent = 'Пожалуйста, заполните все поля';
|
|
|
|
|
errorElement.style.display = 'block';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const request = {
|
|
|
|
|
LocalHistoryId: currentHistoryId,
|
|
|
|
|
content: {
|
|
|
|
|
name: roomName,
|
|
|
|
|
nickname: roomNickname
|
2024-08-11 12:17:53 +00:00
|
|
|
|
}
|
2024-08-14 22:07:59 +00:00
|
|
|
|
};
|
2024-08-11 12:17:53 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
try {
|
|
|
|
|
const response = await fetch('/internalapi/createChat', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(request)
|
|
|
|
|
});
|
2024-08-11 12:17:53 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
const res = await response.json();
|
|
|
|
|
|
|
|
|
|
if (res.status === 0) {
|
|
|
|
|
addRoomToList(roomName, roomNickname);
|
|
|
|
|
rooms[roomNickname] = true;
|
|
|
|
|
closeCreateRoomModal();
|
|
|
|
|
currentHistoryId = res.update.LocalHistoryId;
|
|
|
|
|
window.location.href = '/chat/' + roomNickname;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(res.error || 'Ошибка');
|
2024-08-11 12:17:53 +00:00
|
|
|
|
}
|
2024-08-14 22:07:59 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
alert('Ошибка создания чата: ' + error.message);
|
2024-08-05 11:42:32 +00:00
|
|
|
|
}
|
2024-08-14 22:07:59 +00:00
|
|
|
|
}
|
2024-08-05 11:42:32 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
function addRoomToList(roomName) {
|
|
|
|
|
const roomList = document.querySelector('.room-list');
|
|
|
|
|
const existingRoomItem = Array.from(roomList.children).find(item => item.querySelector('.room-name').textContent === roomName);
|
|
|
|
|
if (existingRoomItem) {
|
|
|
|
|
existingRoomItem.remove();
|
|
|
|
|
}
|
2024-08-05 11:42:32 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
const roomItem = document.createElement('li');
|
|
|
|
|
roomItem.classList.add('room-item');
|
2024-08-05 11:42:32 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
roomItem.innerHTML = `
|
|
|
|
|
<span class="room-name">${roomName}</span>
|
|
|
|
|
<button class="delete-chat-button" onclick="openConfirm('${roomNickname}')">Удалить чат</button>
|
|
|
|
|
<button class="add-members-button" onclick="openAdd()">Добавить участников</button>
|
|
|
|
|
<button class="join-button" onclick="window.location.href = '/chat/${roomNickname}'">Войти</button>
|
|
|
|
|
`;
|
2024-08-05 11:42:32 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
roomList.appendChild(roomItem);
|
|
|
|
|
}
|
2024-08-05 11:42:32 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
function removeRoomFromList(roomName, roomNickname) {
|
|
|
|
|
const roomList = document.querySelector('.room-list');
|
|
|
|
|
const roomItem = Array.from(roomList.children).find(item => item.querySelector('.room-name').textContent === roomName);
|
|
|
|
|
if (roomItem) {
|
|
|
|
|
roomList.removeChild(roomItem);
|
2024-08-05 11:42:32 +00:00
|
|
|
|
}
|
2024-08-14 22:07:59 +00:00
|
|
|
|
}
|
2024-08-10 03:09:51 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
async function initializeRoomList() {
|
|
|
|
|
try {
|
2024-08-11 12:17:53 +00:00
|
|
|
|
const response = await fetch('/internalapi/getChatList', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const res = await response.json();
|
2024-08-14 22:07:59 +00:00
|
|
|
|
|
|
|
|
|
if (res.status === 0) {
|
|
|
|
|
res.chats.forEach(chat => {
|
|
|
|
|
addRoomToList(chat.content.name, chat.content.nickname);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(res.error || 'Неизвестная ошибка');
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
alert('Ошибка загрузки списка чатов: ' + error.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function getChatID() {
|
|
|
|
|
const chatNickname = window.location.pathname.split('/').pop();
|
|
|
|
|
const response = await fetch('/internalapi/getChatList', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const res = await response.json();
|
|
|
|
|
for (const chat of res.chats) {
|
|
|
|
|
if (chat.content.nickname === chatNickname) {
|
|
|
|
|
return chat.id;
|
2024-08-11 12:17:53 +00:00
|
|
|
|
}
|
2024-08-10 03:09:51 +00:00
|
|
|
|
}
|
2024-08-14 22:07:59 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
window.onclick = function(event) {
|
|
|
|
|
if (event.target === document.getElementById('createRoomModal')) {
|
|
|
|
|
closeCreateRoomModal();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-11 12:17:53 +00:00
|
|
|
|
|
2024-08-14 22:07:59 +00:00
|
|
|
|
document.getElementById('newRoomName').addEventListener('keydown', function(event) {
|
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
|
createRoom();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
document.addEventListener('DOMContentLoaded', initializeRoomList);
|