fix: Enable dynamic student/teacher contact discovery, dual REST/WebSocket persistence, and multi-path .env detection
This commit is contained in:
@@ -680,7 +680,7 @@ class TeacherPortal
|
||||
function renderConversations(convs) {
|
||||
const container = document.getElementById('conv_list_container');
|
||||
if (!convs || convs.length === 0) {
|
||||
container.innerHTML = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 12px;">لا توجد رسائل سابقة. الطلاب المسجلون سيظهرون هنا فور سؤالهم.</div>';
|
||||
container.innerHTML = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 12px;">لا يوجد طلاب مسجلون حالياً. سيظهر الطلاب هنا فور تسجيلهم.</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -693,13 +693,18 @@ class TeacherPortal
|
||||
<div class="conv-preview">${c.last_message || 'محادثة جديدة'}</div>
|
||||
</div>
|
||||
`).join('');
|
||||
|
||||
// Auto-select first conversation if none is selected
|
||||
if (!activeRecipientId && convs.length > 0) {
|
||||
selectConversation(convs[0].user_id, convs[0].full_name);
|
||||
}
|
||||
}
|
||||
|
||||
async function selectConversation(userId, name) {
|
||||
activeRecipientId = userId;
|
||||
document.getElementById('active_chat_user_name').textContent = name;
|
||||
loadConversations();
|
||||
|
||||
const items = document.querySelectorAll('.conv-item');
|
||||
|
||||
const token = localStorage.getItem('saqel_teacher_jwt');
|
||||
try {
|
||||
const res = await fetch(`/api/chat/messages?other_user_id=${userId}`, {
|
||||
@@ -709,7 +714,11 @@ class TeacherPortal
|
||||
if (res.ok && data.status === 'success') {
|
||||
const messagesArea = document.getElementById('chat_messages_area');
|
||||
messagesArea.innerHTML = '';
|
||||
data.data.messages.forEach(msg => appendChatMessage(msg));
|
||||
if (data.data.messages.length === 0) {
|
||||
messagesArea.innerHTML = '<div style="text-align: center; color: var(--text-muted); font-size: 13px; margin-top: 60px;">بدء المحادثة مع الطالب ' + name + ' — اكتب رسالتك بالأسفل.</div>';
|
||||
} else {
|
||||
data.data.messages.forEach(msg => appendChatMessage(msg));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Fetch messages error:', e);
|
||||
@@ -738,8 +747,32 @@ class TeacherPortal
|
||||
}
|
||||
|
||||
input.value = '';
|
||||
const token = localStorage.getItem('saqel_teacher_jwt');
|
||||
|
||||
// Try sending via Workerman WebSocket first
|
||||
// 1. Send via REST API to ensure DB write
|
||||
try {
|
||||
const res = await fetch('/api/chat/messages', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token
|
||||
},
|
||||
body: JSON.stringify({
|
||||
receiver_id: activeRecipientId,
|
||||
message: message,
|
||||
message_type: 'text'
|
||||
})
|
||||
});
|
||||
const data = await res.json();
|
||||
if (res.ok && data.data) {
|
||||
appendChatMessage({ message: message, is_mine: true, created_at: 'الآن' });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('REST teacher chat notice:', err);
|
||||
appendChatMessage({ message: message, is_mine: true, created_at: 'الآن' });
|
||||
}
|
||||
|
||||
// 2. Broadcast via WebSocket
|
||||
if (wsSocket && wsSocket.readyState === WebSocket.OPEN) {
|
||||
wsSocket.send(JSON.stringify({
|
||||
event: 'chat_send',
|
||||
@@ -749,21 +782,6 @@ class TeacherPortal
|
||||
message_type: 'text'
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
// Fallback to REST API
|
||||
const token = localStorage.getItem('saqel_teacher_jwt');
|
||||
await fetch('/api/chat/messages', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token
|
||||
},
|
||||
body: JSON.stringify({
|
||||
receiver_id: activeRecipientId,
|
||||
message: message
|
||||
})
|
||||
});
|
||||
appendChatMessage({ message: message, is_mine: true });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user