Implement photo-related enhancements and chat updates

This commit is contained in:
dev
2026-10-28 09:01:49 +03:00
parent 12034812ec
commit 617ed60e65
10 changed files with 567 additions and 102 deletions
+102 -29
View File
@@ -64,10 +64,23 @@ function chatThreadTime(ts) {
return fmtDateShort(d);
}
function chatThreadPeer(t) {
const me = (typeof currentUser !== 'undefined' && currentUser) ? currentUser.id : null;
const amA = me != null && Number(me) === Number(t.tutor_id);
if (amA) {
if (t.peer_id != null) {
return { name: t.peer_name || t.peer_username, username: t.peer_username, active: t.peer_active !== false };
}
return { name: 'Администратор', username: '', active: true };
}
// current user is B or admin: show participant A
return { name: t.tutor_name || t.tutor_username, username: t.tutor_username, active: t.tutor_active !== false };
}
function chatThreadCardHtml(t) {
const active = t.id === chatActiveId ? ' active' : '';
const unread = Number(t.unread) || 0;
const preview = (t.last_message_text || '').trim() || 'Нет сообщений';
const peer = chatThreadPeer(t);
const hidden = t.deleted_at ? ' is-hidden' : '';
const acts = isAdmin() ? (t.deleted_at
? `<span class="chat-thread-acts">
@@ -78,11 +91,11 @@ function chatThreadCardHtml(t) {
<button type="button" class="chat-thread-act" data-thread-hide="${t.id}" title="Скрыть диалог"><i data-lucide="archive"></i></button>
</span>`) : '';
return `<div class="chat-thread${active}${hidden}${unread ? ' unread' : ''}" data-thread="${t.id}" role="button" tabindex="0">
<span class="chat-thread-avatar">${esc((t.tutor_name || '?').slice(0, 1).toUpperCase())}</span>
<span class="chat-thread-avatar">${esc((peer.name || '?').slice(0, 1).toUpperCase())}</span>
<span class="chat-thread-body">
<span class="chat-thread-top">
<span class="chat-thread-name">${esc(t.tutor_name || t.tutor_username)}</span>
${t.tutor_active === false ? '<span class="chat-off" title="Пользователь отключён">отключён</span>' : ''}
<span class="chat-thread-name">${esc(peer.name || peer.username)}</span>
${peer.active === false ? '<span class="chat-off" title="Пользователь отключён">отключён</span>' : ''}
${t.deleted_at ? '<span class="chat-off chat-hidden-tag" title="Диалог скрыт">скрыт</span>' : ''}
<span class="grow"></span>
<span class="chat-thread-time">${esc(chatThreadTime(t.last_message_at || t.created_at))}</span>
@@ -331,15 +344,14 @@ function renderChatHeader() {
}
if (empty) empty.hidden = true;
if (main) main.hidden = false;
const peer = chatThreadPeer(thread);
const av = document.getElementById('chatAvatar');
const nm = document.getElementById('chatPeerName');
const sub = document.getElementById('chatPeerSub');
if (av) av.textContent = (thread.tutor_name || '?').slice(0, 1).toUpperCase();
if (nm) nm.textContent = isAdmin() ? (thread.tutor_name || thread.tutor_username) : 'Администратор';
if (av) av.textContent = (peer.name || '?').slice(0, 1).toUpperCase();
if (nm) nm.textContent = peer.name || peer.username || 'диалог';
if (sub) {
sub.textContent = isAdmin()
? `@${thread.tutor_username}${thread.tutor_active === false ? ' · отключён' : ''}`
: 'Ответ администратора';
sub.textContent = `@${peer.username || ''}${peer.active === false ? ' · отключён' : ''}`;
}
}
@@ -499,6 +511,7 @@ function selectChatThread(id) {
clearChatReply();
renderChatThreads();
loadChatMessages();
markChatRead();
}
function onChatMessage(p) {
@@ -656,23 +669,58 @@ async function deleteChatMessage(messageId) {
}
}
async function openChatNewModal() {
const modal = document.getElementById('chatNewModal');
const select = document.getElementById('chatNewUser');
if (!modal || !select) return;
try {
const users = await chatApi('/api/users', { headers: hdr() });
const tutors = (users || []).filter(u => u.role !== 'admin' && u.is_active !== false);
const known = new Set(chatThreads.map(t => t.tutor_id));
const rest = tutors.filter(u => !known.has(u.id));
const pool = rest.length ? rest : tutors;
select.innerHTML = pool.map(u => `<option value="${u.id}">${esc(u.name || u.username)} (@${esc(u.username)})</option>`).join('')
|| '<option value="">Нет доступных тьюторов</option>';
} catch (e) {
showToast(e.message || 'Не удалось загрузить пользователей', true);
let chatNewSearchTimer = null;
let chatNewSelectedId = null;
function chatNewRenderResults(items) {
const list = document.getElementById('chatNewUserList');
const input = document.getElementById('chatNewUser');
if (!list) return;
if (!items || !items.length) {
list.hidden = true;
list.innerHTML = '';
return;
}
list.innerHTML = items.map(u => `
<div class="chat-new-user-item" data-uid="${u.id}">
<span class="cn-name">${esc(u.name || u.username)}</span>
<span class="cn-sub">@${esc(u.username)}${u.role ? ' · ' + esc(u.role) : ''}</span>
</div>`).join('');
list.hidden = false;
list.querySelectorAll('.chat-new-user-item').forEach(el => {
el.addEventListener('click', () => {
const id = Number(el.dataset.uid);
const item = items.find(x => Number(x.id) === id);
if (!item) return;
chatNewSelectedId = id;
if (input) input.value = `${item.name || item.username} (@${item.username})`;
list.hidden = true;
});
});
}
async function chatNewSearch(q) {
try {
const items = await chatApi('/api/users/search?q=' + encodeURIComponent(q || ''), { headers: hdr() });
chatNewRenderResults(items || []);
} catch (e) {
chatNewRenderResults([]);
}
}
async function openChatNewModal() {
const modal = document.getElementById('chatNewModal');
const input = document.getElementById('chatNewUser');
const hidden = document.getElementById('chatNewUserId');
if (!modal || !input) return;
chatNewSelectedId = null;
if (hidden) hidden.value = '';
input.value = '';
const list = document.getElementById('chatNewUserList');
if (list) { list.hidden = true; list.innerHTML = ''; }
modal.classList.add('open');
if (input) input.focus();
chatNewSearch('');
}
async function sendChatMessage() {
@@ -715,11 +763,11 @@ async function sendChatMessage() {
}
async function submitChatNew() {
const select = document.getElementById('chatNewUser');
const input = document.getElementById('chatNewUser');
const modal = document.getElementById('chatNewModal');
const userId = Number(select.value);
const userId = chatNewSelectedId || Number((document.getElementById('chatNewUserId') || {}).value);
if (!userId) {
showToast('Выберите тьютора', true);
showToast('Выберите собеседника', true);
return;
}
try {
@@ -864,6 +912,31 @@ function initChatPage() {
newModal.addEventListener('click', e => { if (e.target === newModal) newModal.classList.remove('open'); });
}
if (newOk) newOk.addEventListener('click', submitChatNew);
const newUserInput = document.getElementById('chatNewUser');
const newUserList = document.getElementById('chatNewUserList');
if (newUserInput) {
newUserInput.addEventListener('input', () => {
chatNewSelectedId = null;
const v = newUserInput.value.trim();
clearTimeout(chatNewSearchTimer);
chatNewSearchTimer = setTimeout(() => chatNewSearch(v), 200);
});
newUserInput.addEventListener('focus', () => {
if (!newUserList || !newUserList.hidden) return;
chatNewSearch(newUserInput.value.trim());
});
}
if (newUserList) {
newUserList.addEventListener('click', e => {
const item = e.target.closest('.chat-new-user-item');
if (!item) return;
const id = Number(item.dataset.uid);
const txt = item.querySelector('.cn-name')?.textContent || '';
chatNewSelectedId = id;
if (newUserInput) newUserInput.value = txt;
newUserList.hidden = true;
});
}
document.addEventListener('paste', e => {
if (!chatActiveId) return;
@@ -900,13 +973,13 @@ function initChatPage() {
imgModal.addEventListener('click', e => e.currentTarget.classList.remove('open'));
}
if (newBtn) newBtn.hidden = !isAdmin();
if (newBtn) newBtn.hidden = false;
const side = document.getElementById('chatSide');
const layout = document.getElementById('chatLayout');
if (!isAdmin() && side) side.hidden = true;
if (!isAdmin() && layout) layout.classList.add('chat-layout-solo');
if (side) side.hidden = false;
if (layout) layout.classList.remove('chat-layout-solo');
const emptySub = document.querySelector('#chatEmpty .chat-empty-sub');
if (emptySub && !isAdmin()) emptySub.textContent = 'Напишите администратору — сообщения доставятся мгновенно';
if (emptySub && !isAdmin()) emptySub.textContent = 'Выберите диалог или начните новый — можно писать любому пользователю';
loadChatThreads().then(async () => {
if (!isAdmin()) {