api update
This commit is contained in:
parent
6b0770844c
commit
9dfe013cc6
@ -186,6 +186,13 @@ h1 {
|
|||||||
position: fixed;
|
position: fixed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#error {
|
||||||
|
color: red;
|
||||||
|
font-size: 15px;
|
||||||
|
margin-top: 10px;
|
||||||
|
text-align: center;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
.close {
|
.close {
|
||||||
color: #aaa;
|
color: #aaa;
|
||||||
float: right;
|
float: right;
|
||||||
|
@ -24,29 +24,14 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<input type="text" id="newRoomName" placeholder="Название комнаты">
|
<input type="text" id="newRoomName" placeholder="Название комнаты">
|
||||||
|
<input type="password" id="newRoomNickname" placeholder="Никнейм комнаты">
|
||||||
</div>
|
</div>
|
||||||
|
<div id="error"></div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button class="join-button" onclick="createRoom()">Создать</button>
|
<button class="join-button" onclick="createRoom()">Создать</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Модальное окно для добавления участников -->
|
|
||||||
<div class="overlay" id="add_members">
|
|
||||||
<div class="add-members">
|
|
||||||
<div class="add-members-header">
|
|
||||||
<span class="close" onclick="closeAdd()">×</span>
|
|
||||||
<h2>Добавить участников</h2>
|
|
||||||
</div>
|
|
||||||
<div class="add-members-body">
|
|
||||||
<input type="text" id="newMemberLogin" placeholder="Логин пользователя">
|
|
||||||
</div>
|
|
||||||
<div class="add-members-footer">
|
|
||||||
<button class="add-member-button" onclick="addMember()">Добавить</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script src="/assets/js/list-rooms.js"></script>
|
<script src="/assets/js/list-rooms.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,8 +1,38 @@
|
|||||||
function sendMessage() {
|
let currentHistoryId = 0;
|
||||||
|
let currentChatID = null;
|
||||||
|
|
||||||
|
document.addEventListener("DOMContentLoaded", async function() {
|
||||||
|
currentChatID = await getChatID();
|
||||||
|
});
|
||||||
|
|
||||||
|
async function sendMessage() {
|
||||||
const chatMessages = document.getElementById('chat-messages');
|
const chatMessages = document.getElementById('chat-messages');
|
||||||
const chatInput = document.getElementById('chat-input');
|
const chatInput = document.getElementById('chat-input');
|
||||||
const message = chatInput.value;
|
const message = chatInput.value;
|
||||||
|
|
||||||
if (message.trim() !== '') {
|
if (message.trim() !== '') {
|
||||||
|
const request = {
|
||||||
|
'chatId': currentChatID,
|
||||||
|
'LocalHistoryId': currentHistoryId,
|
||||||
|
'content': {
|
||||||
|
'text': message
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch("/internalapi/sendMessage", {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(request)
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await response.json();
|
||||||
|
|
||||||
|
if (res.update) {
|
||||||
|
const update = res.update[0];
|
||||||
|
currentHistoryId = update.HistoryId;
|
||||||
|
|
||||||
const messageElement = document.createElement('div');
|
const messageElement = document.createElement('div');
|
||||||
messageElement.classList.add('chat-message');
|
messageElement.classList.add('chat-message');
|
||||||
|
|
||||||
@ -18,7 +48,7 @@ function sendMessage() {
|
|||||||
|
|
||||||
const usernameElement = document.createElement('div');
|
const usernameElement = document.createElement('div');
|
||||||
usernameElement.classList.add('username');
|
usernameElement.classList.add('username');
|
||||||
usernameElement.textContent = 'Адель';
|
usernameElement.textContent = await getUserName();
|
||||||
|
|
||||||
const textElement = document.createElement('div');
|
const textElement = document.createElement('div');
|
||||||
textElement.classList.add('text');
|
textElement.classList.add('text');
|
||||||
@ -35,7 +65,88 @@ function sendMessage() {
|
|||||||
chatInput.value = '';
|
chatInput.value = '';
|
||||||
chatMessages.scrollTop = chatMessages.scrollHeight;
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('chat-input').addEventListener('keydown', function (event) {
|
||||||
|
if (event.key === 'Enter') {
|
||||||
|
sendMessage();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function getUserName() {
|
||||||
|
const userID = await getUserID();
|
||||||
|
const request = {
|
||||||
|
"id": userID
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch('/internalapi/getUserInfo', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(request)
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await response.json();
|
||||||
|
return res.content.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getUserID() {
|
||||||
|
const response = await fetch('/internalapi/mirror', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({})
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await response.json();
|
||||||
|
return res.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function editMessage(new_message) {
|
||||||
|
const req = {
|
||||||
|
'chatId': currentChatID,
|
||||||
|
'LocalHistoryId': currentHistoryId,
|
||||||
|
'id': getUserID(),
|
||||||
|
'content': {
|
||||||
|
'text': new_message
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const res = await fetch('/internalapi/editMessage', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(req)
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await res.json();
|
||||||
|
if (response.update) {
|
||||||
|
currentHistoryId = response.update[0].HistoryId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function openMembersList() {
|
function openMembersList() {
|
||||||
document.getElementById("members-list").style.display = "block";
|
document.getElementById("members-list").style.display = "block";
|
||||||
document.getElementById("overlay").style.display = "flex";
|
document.getElementById("overlay").style.display = "flex";
|
||||||
|
@ -1,52 +1,73 @@
|
|||||||
let rooms = {};
|
let currentRoom = null;
|
||||||
|
let currentHistoryId = 0;
|
||||||
|
|
||||||
function openRoom(currentRoom) {
|
function openModal(roomName) {
|
||||||
alert('Вы вошли в комнату: ' + currentRoom);
|
currentRoom = roomName;
|
||||||
}
|
document.getElementById('passwordModal').style.display = 'block';
|
||||||
|
|
||||||
function closeAdd() {
|
|
||||||
document.getElementById('add_members').style.display = 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
function openAdd() {
|
|
||||||
document.getElementById('add_members').style.display = 'flex';
|
|
||||||
}
|
|
||||||
|
|
||||||
function addMember() {
|
|
||||||
const login = document.getElementById('newMemberLogin').value;
|
|
||||||
if (login) {
|
|
||||||
alert(`Участник с никнеймом '${login}' добавлен`);
|
|
||||||
closeAdd();
|
|
||||||
} else {
|
|
||||||
alert('Пожалуйста, введите логин участника');
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function openCreateRoomModal() {
|
function closeModal() {
|
||||||
|
document.getElementById('passwordModal').style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
function openCreateRoomModal() {
|
||||||
document.getElementById('createRoomModal').style.display = 'block';
|
document.getElementById('createRoomModal').style.display = 'block';
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeCreateRoomModal() {
|
function closeCreateRoomModal() {
|
||||||
document.getElementById('createRoomModal').style.display = 'none';
|
document.getElementById('createRoomModal').style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
function createRoom() {
|
async function createRoom() {
|
||||||
|
const errorElement = document.getElementById('error');
|
||||||
const roomName = document.getElementById('newRoomName').value.trim();
|
const roomName = document.getElementById('newRoomName').value.trim();
|
||||||
if (roomName === '') {
|
const roomNickname = document.getElementById('newRoomNickname').value.trim();
|
||||||
alert('Пожалуйста, заполните все поля.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (rooms[roomName]) {
|
|
||||||
alert('Комната с таким названием уже существует.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
rooms[roomName] = true;
|
|
||||||
addRoomToList(roomName);
|
|
||||||
closeCreateRoomModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
function addRoomToList(roomName) {
|
|
||||||
|
errorElement.style.display = 'none';
|
||||||
|
errorElement.textContent = '';
|
||||||
|
|
||||||
|
if (roomName === '' || roomNickname === '') {
|
||||||
|
errorElement.textContent = 'Пожалуйста, заполните все поля';
|
||||||
|
errorElement.style.display = 'block';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const request = {
|
||||||
|
LocalHistoryId: currentHistoryId,
|
||||||
|
content: {
|
||||||
|
name: roomName,
|
||||||
|
nickname: roomNickname
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/internalapi/createChat', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(request)
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await response.json();
|
||||||
|
|
||||||
|
if (res.status === 0) {
|
||||||
|
addRoomToList(roomName, roomNickname);
|
||||||
|
closeCreateRoomModal();
|
||||||
|
currentHistoryId = res.update.LocalHistoryId;
|
||||||
|
window.location.href = '/chat/' + roomNickname;
|
||||||
|
} else {
|
||||||
|
throw new Error(res.error || 'Ошибка');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
alert('Ошибка создания чата: ' + error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function addRoomToList(roomName, roomNickname) {
|
||||||
const roomList = document.querySelector('.room-list');
|
const roomList = document.querySelector('.room-list');
|
||||||
|
|
||||||
const existingRoomItem = Array.from(roomList.children).find(item => item.querySelector('.room-name').textContent === roomName);
|
const existingRoomItem = Array.from(roomList.children).find(item => item.querySelector('.room-name').textContent === roomName);
|
||||||
if (existingRoomItem) {
|
if (existingRoomItem) {
|
||||||
existingRoomItem.remove();
|
existingRoomItem.remove();
|
||||||
@ -54,32 +75,54 @@ function addRoomToList(roomName) {
|
|||||||
|
|
||||||
const roomItem = document.createElement('li');
|
const roomItem = document.createElement('li');
|
||||||
roomItem.classList.add('room-item');
|
roomItem.classList.add('room-item');
|
||||||
|
|
||||||
roomItem.innerHTML = `
|
roomItem.innerHTML = `
|
||||||
<span class="room-name">${roomName}</span>
|
<span class="room-name">${roomName}</span>
|
||||||
<button class="add-members-button" onclick="openAdd()">Добавить участников</button>
|
<button class="join-button" onclick="window.location.href = '/chat/${roomNickname}'">Войти</button>
|
||||||
<button class="join-button" onclick="openRoom('${roomName}')">Войти</button>
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
roomList.appendChild(roomItem);
|
roomList.appendChild(roomItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializeRoomList() {
|
async function initializeRoomList() {
|
||||||
Object.keys(rooms).forEach(roomName => {
|
try {
|
||||||
addRoomToList(roomName);
|
const response = await fetch('/internalapi/getChatList', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({})
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
initializeRoomList();
|
const res = await response.json();
|
||||||
|
|
||||||
window.onclick = function(event) {
|
if (res.status === 0) {
|
||||||
if (event.target === document.getElementById('createRoomModal')) {
|
res.chats.forEach(chat => {
|
||||||
closeCreateRoomModal();
|
addRoomToList(chat.content.name, chat.content.nickname);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw new Error(res.error || 'Неизвестная ошибка');
|
||||||
}
|
}
|
||||||
}
|
} catch (error) {
|
||||||
|
alert('Ошибка загрузки списка чатов: ' + error.message);
|
||||||
document.getElementById('newRoomName').addEventListener('keydown', function(event) {
|
|
||||||
if (event.key === 'Enter') {
|
|
||||||
createRoom();
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', initializeRoomList);
|
||||||
|
@ -29,22 +29,40 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
form.addEventListener('submit', function(event) {
|
form.addEventListener('submit', function(event) {
|
||||||
if (!validateForm()) {
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
validateForm();
|
||||||
});
|
});
|
||||||
|
|
||||||
function validateForm() {
|
async function validateForm() {
|
||||||
const username = document.getElementById('username').value.trim();
|
const name = document.getElementById('name').value.trim();
|
||||||
const login = document.getElementById('login').value.trim();
|
const nickname = document.getElementById('nickname').value.trim();
|
||||||
const password = document.getElementById('password').value.trim();
|
|
||||||
|
|
||||||
if (username === '' || login === '' || password === '') {
|
if (name === '' || nickname === '') {
|
||||||
errorElement.textContent = 'Пожалуйста, заполните все поля';
|
errorElement.textContent = 'Пожалуйста, заполните все поля';
|
||||||
errorElement.style.display = 'block';
|
errorElement.style.display = 'block';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
// Отправка данных для регистрации
|
||||||
|
let response = await fetch('/login', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({name, nickname})
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
|
||||||
|
if (result.status === 0) {
|
||||||
|
window.location.href = '/';
|
||||||
|
} else {
|
||||||
|
throw Error(result.error);
|
||||||
|
}
|
||||||
|
} catch(error) {
|
||||||
|
errorElement.textContent = 'Попробуйте еще раз';
|
||||||
|
errorElement.style.display = 'block';
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user