changeset 17:7230c821c368

push fixes
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 Nov 2024 14:44:35 -0700
parents 82b55186a4a0
children 0721dcf222e1
files src/chat.html.luan src/chat.js src/delete_chat.js.luan src/delete_user.js.luan src/lib/Utils.luan
diffstat 5 files changed, 56 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/chat.html.luan	Sun Nov 03 21:22:06 2024 -0700
+++ b/src/chat.html.luan	Mon Nov 04 14:44:35 2024 -0700
@@ -14,6 +14,7 @@
 local header = Shared.header or error()
 local started = Shared.started or error()
 local chats_html = Shared.chats_html or error()
+local http_push_to_users = Shared.http_push_to_users or error()
 local User = require "site:/lib/User.luan"
 local current_user = User.current or error()
 local get_user_by_email = User.get_by_email or error()
@@ -37,7 +38,8 @@
 		ids[#ids+1] = id
 	end
 	local query = concat(t," ")
-	return run_in_transaction( function()
+	local need_push = false
+	local chat = run_in_transaction( function()
 		local chats = chat_search(query)
 		local n = #chats
 		if n == 0 then
@@ -45,6 +47,7 @@
 				user_ids = ids
 			}
 			chat.save()
+			need_push = true
 			return chat
 		elseif n == 1 then
 			return chats[1]
@@ -52,6 +55,11 @@
 			error("multiple chats for: "..query)
 		end
 	end )
+	if need_push then
+		local js = "getChats('"..chat.id.."')"
+		http_push_to_users( chat.user_ids, js )
+	end
+	return chat
 end
 
 return function()
--- a/src/chat.js	Sun Nov 03 21:22:06 2024 -0700
+++ b/src/chat.js	Mon Nov 04 14:44:35 2024 -0700
@@ -4,6 +4,7 @@
 let eventSource;
 
 function evalEvent(event) {
+	// console.log(event);
 	eval(event.data);
 }
 
--- a/src/delete_chat.js.luan	Sun Nov 03 21:22:06 2024 -0700
+++ b/src/delete_chat.js.luan	Mon Nov 04 14:44:35 2024 -0700
@@ -7,13 +7,17 @@
 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()
+local Utils = require "site:/lib/Utils.luan"
+local is_in_list = Utils.is_in_list or error()
 
 
 return function()
 	local chat = Http.request.parameters.chat or error()
 	chat = get_chat_by_id(chat) or error()
+	local user_ids = chat.user_ids
 	local user = current_user() or error()
+	is_in_list( user.id, user_ids ) or error()
 	chat.delete()
 	local js = "getChats(null)"
-	http_push_to_users( chat.user_ids, js )
+	http_push_to_users( user_ids, js )
 end
--- a/src/delete_user.js.luan	Sun Nov 03 21:22:06 2024 -0700
+++ b/src/delete_user.js.luan	Mon Nov 04 14:44:35 2024 -0700
@@ -1,15 +1,36 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local stringify = Luan.stringify or error()
 local Io = require "luan:Io.luan"
 local Http = require "luan:http/Http.luan"
 local User = require "site:/lib/User.luan"
 local current_user = User.current or error()
+local Chat = require "site:/lib/Chat.luan"
+local chat_search = Chat.search or error()
+local Utils = require "site:/lib/Utils.luan"
+local list_to_set = Utils.list_to_set or error()
+local set_to_list = Utils.set_to_list or error()
+local Shared = require "site:/lib/Shared.luan"
+local http_push_to_users = Shared.http_push_to_users or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "delete_user.js"
 
 
 return function()
 	local user = current_user()
 	if user ~= nil then
+		local user_ids = list_to_set{}
+		local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc" )
+		for _, chat in ipairs(chats) do
+			for _, user_id in ipairs(chat.user_ids) do
+				user_ids[user_id] = true
+			end
+		end
+		user_ids = set_to_list(user_ids)
+		local js = "getChats(null)"
 		user.delete()
+		http_push_to_users( user_ids, js )
 	end
 	Io.stdout = Http.response.text_writer()
 %>
--- a/src/lib/Utils.luan	Sun Nov 03 21:22:06 2024 -0700
+++ b/src/lib/Utils.luan	Mon Nov 04 14:44:35 2024 -0700
@@ -2,6 +2,7 @@
 local error = Luan.error
 local type = Luan.type or error()
 local ipairs = Luan.ipairs or error()
+local pairs = Luan.pairs or error()
 local set_metatable = Luan.set_metatable or error()
 local Http = require "luan:http/Http.luan"
 
@@ -48,4 +49,23 @@
 	end
 end
 
+function Utils.set_to_list(set)
+	local list = {}
+	for k, v in pairs(set) do
+		if v then
+			list[#list+1] = k
+		end
+	end
+	return list
+end
+
+function Utils.is_in_list(el,list)
+	for _, v in ipairs(list) do
+		if v == el then
+			return true
+		end
+	end
+	return false
+end
+
 return Utils