175 lines
5.7 KiB
JavaScript
175 lines
5.7 KiB
JavaScript
let members = [
|
|
{ username: 'Адель', nickname: 'cold_siemens52', avatar: 'https://sun9-59.userapi.com/impg/t8GhZ7FkynVifY1FQCnaf31tGprbV_rfauZzgg/fSq4lyc6V0U.jpg?size=1280x1280&quality=96&sign=e3c309a125cb570d2e18465eba65f940&type=album' },
|
|
{ username: 'Антон', nickname: 'antyak_01', avatar: 'https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png' },
|
|
{ username: 'Владимир', nickname: 'kkrkk2006', avatar: 'https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png' }
|
|
];
|
|
let currentHistoryId = 0;
|
|
let currentChatID = null;
|
|
function renderMembersList() {
|
|
const membersListBody = document.getElementById('members-list-body');
|
|
membersListBody.innerHTML = '';
|
|
|
|
members.forEach((member, index) => {
|
|
const memberItem = document.createElement('li');
|
|
memberItem.innerHTML = `
|
|
<img src="${member.avatar}" alt="${member.username}">
|
|
<a href="profile.html">${member.username}</a>
|
|
<button class="delete-member" onclick="deleteMember(${index})">Удалить из чата</button>
|
|
`;
|
|
membersListBody.appendChild(memberItem);
|
|
});
|
|
}
|
|
|
|
function deleteMember(index) {
|
|
members.splice(index, 1);
|
|
renderMembersList();
|
|
}
|
|
|
|
async function sendMessage() {
|
|
const chatMessages = document.getElementById('chat-messages');
|
|
const chatInput = document.getElementById('chat-input');
|
|
const message = chatInput.value;
|
|
|
|
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');
|
|
messageElement.classList.add('chat-message');
|
|
|
|
const avatarElement = document.createElement('div');
|
|
avatarElement.classList.add('avatar');
|
|
|
|
const avatarImage = document.createElement('img');
|
|
avatarImage.src = 'https://sun9-59.userapi.com/impg/t8GhZ7FkynVifY1FQCnaf31tGprbV_rfauZzgg/fSq4lyc6V0U.jpg?size=1280x1280&quality=96&sign=e3c309a125cb570d2e18465eba65f940&type=album';
|
|
avatarElement.appendChild(avatarImage);
|
|
|
|
const messageContentElement = document.createElement('div');
|
|
messageContentElement.classList.add('message-content');
|
|
|
|
const usernameElement = document.createElement('div');
|
|
usernameElement.classList.add('username');
|
|
usernameElement.textContent = await getUserName(); // Отображение имени пользователя
|
|
|
|
const textElement = document.createElement('div');
|
|
textElement.classList.add('text');
|
|
textElement.textContent = message;
|
|
|
|
messageContentElement.appendChild(usernameElement);
|
|
messageContentElement.appendChild(textElement);
|
|
|
|
messageElement.appendChild(avatarElement);
|
|
messageElement.appendChild(messageContentElement);
|
|
|
|
chatMessages.appendChild(messageElement);
|
|
|
|
chatInput.value = '';
|
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
function openMembersList() {
|
|
renderMembersList();
|
|
document.getElementById("members-list").style.display = "block";
|
|
document.getElementById("overlay").style.display = "flex";
|
|
}
|
|
|
|
function closeMembersList() {
|
|
document.getElementById("members-list").style.display = "none";
|
|
document.getElementById("overlay").style.display = "none";
|
|
}
|
|
|
|
document.getElementById('chat-input').addEventListener('keydown', function (event) {
|
|
if (event.key === 'Enter') {
|
|
sendMessage();
|
|
}
|
|
});
|
|
|
|
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 editChat() {
|
|
const newName = prompt("Введите новое имя комнаты:");
|
|
if (newName) {
|
|
document.getElementById('room-name').textContent = newName;
|
|
}
|
|
}
|
|
|
|
function exitChat() {
|
|
window.location.href = 'list-rooms.nytl.html';
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", async function() {
|
|
currentChatID = await getChatID();
|
|
}); |