diff src/chat.js @ 15:8b8905f63d80

add get_chats
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Nov 2024 17:36:49 -0700
parents 0df3a63a895f
children 82b55186a4a0
line wrap: on
line diff
--- a/src/chat.js	Fri Nov 01 11:22:01 2024 -0600
+++ b/src/chat.js	Sun Nov 03 17:36:49 2024 -0700
@@ -3,6 +3,13 @@
 let currentChatId = null;
 let eventSource;
 
+function setUserEventSource(userId) {
+	let userEventSource = new EventSource(`${location.origin}/user/${userId}`);
+	userEventSource.onmessage = function(event) {
+		eval(event.data);
+	};
+}
+
 function selectChat(div) {
 	let chatId = div.getAttribute('chat');
 	if( chatId === currentChatId )
@@ -70,3 +77,20 @@
 	fixDates();
 	input.scrollIntoView({block: 'end'});
 }
+
+function getChats(chatId) {
+	let first = document.querySelector('div[chat]');
+	if( !first || first.getAttribute('chat') != chatId ) {
+		// console.log('getChats');
+		ajax('get_chats.js');
+	}
+}
+
+function gotChats(html) {
+	document.querySelector('div[chats]').innerHTML = html;
+	if( currentChatId ) {
+		let current = document.querySelector(`div[chat="${currentChatId}"]`);
+		current.setAttribute('selected','');
+		current.scrollIntoViewIfNeeded(false);
+	}
+}