changeset 16:82b55186a4a0

fix delete_chat
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Nov 2024 21:22:06 -0700
parents 8b8905f63d80
children 7230c821c368
files src/add_post.js.luan src/chat.js src/delete_chat.js.luan src/lib/Shared.luan
diffstat 4 files changed, 30 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/add_post.js.luan	Sun Nov 03 17:36:49 2024 -0700
+++ b/src/add_post.js.luan	Sun Nov 03 21:22:06 2024 -0700
@@ -17,8 +17,7 @@
 local new_post = Post.new or error()
 local Shared = require "site:/lib/Shared.luan"
 local post_html = Shared.post_html or error()
-local Utils = require "site:/lib/Utils.luan"
-local base_url = Utils.base_url or error()
+local http_push_to_users = Shared.http_push_to_users or error()
 
 
 return function()
@@ -43,9 +42,5 @@
 	local js = "added("..json_string(html)..")"
 	chat.http_push(js)
 	js = "getChats('"..chat.id.."')"
-	local base = base_url().."/user/"
-	for _, user_id in ipairs(chat.user_ids) do
-		local url = base..user_id
-		Http.push(url,js)
-	end
+	http_push_to_users( chat.user_ids, js )
 end
--- a/src/chat.js	Sun Nov 03 17:36:49 2024 -0700
+++ b/src/chat.js	Sun Nov 03 21:22:06 2024 -0700
@@ -3,11 +3,13 @@
 let currentChatId = null;
 let eventSource;
 
+function evalEvent(event) {
+	eval(event.data);
+}
+
 function setUserEventSource(userId) {
 	let userEventSource = new EventSource(`${location.origin}/user/${userId}`);
-	userEventSource.onmessage = function(event) {
-		eval(event.data);
-	};
+	userEventSource.onmessage = evalEvent;
 }
 
 function selectChat(div) {
@@ -22,9 +24,7 @@
 
 	if(eventSource)  eventSource.close();
 	eventSource = new EventSource(`${location.origin}/chat/${chatId}`);
-	eventSource.onmessage = function(event) {
-		eval(event.data);
-	};
+	eventSource.onmessage = evalEvent;
 }
 
 function gotChat(html) {
@@ -90,7 +90,12 @@
 	document.querySelector('div[chats]').innerHTML = html;
 	if( currentChatId ) {
 		let current = document.querySelector(`div[chat="${currentChatId}"]`);
-		current.setAttribute('selected','');
-		current.scrollIntoViewIfNeeded(false);
+		if( current ) {
+			current.setAttribute('selected','');
+			current.scrollIntoViewIfNeeded(false);
+		} else {
+			currentChatId = null;
+			document.querySelector('div[posts]').innerHTML = '';
+		}
 	}
 }
--- a/src/delete_chat.js.luan	Sun Nov 03 17:36:49 2024 -0700
+++ b/src/delete_chat.js.luan	Sun Nov 03 21:22:06 2024 -0700
@@ -5,6 +5,8 @@
 local current_user = User.current or error()
 local Chat = require "site:/lib/Chat.luan"
 local get_chat_by_id = Chat.get_by_id or error()
+local Shared = require "site:/lib/Shared.luan"
+local http_push_to_users = Shared.http_push_to_users or error()
 
 
 return function()
@@ -12,5 +14,6 @@
 	chat = get_chat_by_id(chat) or error()
 	local user = current_user() or error()
 	chat.delete()
-	chat.http_push("location = '/chat.html'")
+	local js = "getChats(null)"
+	http_push_to_users( chat.user_ids, js )
 end
--- a/src/lib/Shared.luan	Sun Nov 03 17:36:49 2024 -0700
+++ b/src/lib/Shared.luan	Sun Nov 03 21:22:06 2024 -0700
@@ -9,12 +9,15 @@
 local thread_run = Thread.run or error()
 local Html = require "luan:Html.luan"
 local html_encode = Html.encode or error()
+local Http = require "luan:http/Http.luan"
 local Mail = require "luan:mail/Mail.luan"
 local User = require "site:/lib/User.luan"
 local current_user = User.current or error()
 local get_user_by_id = User.get_by_id or error()
 local Chat = require "site:/lib/Chat.luan"
 local chat_search = Chat.search or error()
+local Utils = require "site:/lib/Utils.luan"
+local base_url = Utils.base_url or error()
 
 
 local Shared = {}
@@ -112,4 +115,12 @@
 	end
 end
 
+function Shared.http_push_to_users(user_ids,message)
+	local base = base_url().."/user/"
+	for _, user_id in ipairs(user_ids) do
+		local url = base..user_id
+		Http.push(url,message)
+	end
+end
+
 return Shared