changeset 30:f4708943f29e

add heartbeat
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 09 Nov 2024 19:17:21 -0700
parents 5beadcd541aa
children a0820965ba04
files src/add_post.js.luan src/chat.js src/delete_user.js.luan src/heartbeat.js.luan src/index.html.luan src/lib/User.luan
diffstat 6 files changed, 61 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/add_post.js.luan	Wed Nov 06 23:05:45 2024 -0700
+++ b/src/add_post.js.luan	Sat Nov 09 19:17:21 2024 -0700
@@ -25,9 +25,9 @@
 	local chat = Http.request.parameters.chat or error()
 	local content = Http.request.parameters.content or error()
 	local post
+	local now = time_now()
 	run_in_transaction( function()
 		chat = get_chat_by_id(chat) or error()
-		local now = time_now()
 		post = new_post{
 			chat_id = chat.id
 			author_id = user.id
@@ -41,6 +41,6 @@
 	local html = `post_html(post)`
 	local js = "added("..json_string(html)..")"
 	chat.http_push(js)
-	js = "getChats('"..chat.id.."')"
+	js = "getChats('"..chat.id.."',"..now..")"
 	http_push_to_users( chat.user_ids, js )
 end
--- a/src/chat.js	Wed Nov 06 23:05:45 2024 -0700
+++ b/src/chat.js	Sat Nov 09 19:17:21 2024 -0700
@@ -3,6 +3,7 @@
 let title = document.title;
 let currentChatId = null;
 let eventSource;
+let lastUpdate;
 
 function evalEvent(event) {
 	// console.log(event);
@@ -143,12 +144,14 @@
 		document.title = title + ' *';
 }
 
-function getChats(chatId) {
+function getChats(chatId,updated) {
 	let first = document.querySelector('div[chat]');
 	if( !first || first.getAttribute('chat') != chatId ) {
 		// console.log('getChats');
 		ajax('get_chats.js');
 	}
+	if( updated )
+		lastUpdate = updated;
 }
 
 function gotChats(html) {
@@ -197,3 +200,14 @@
 		pulldown.scrollIntoViewIfNeeded(false);
 	}
 }
+
+setInterval(function(){
+	ajax(`heartbeat.js?last_update=${lastUpdate}`);
+}, 60000 );
+
+function resync(updated) {
+	lastUpdate = updated;
+	currentChatId = null;
+	document.querySelector('div[posts]').innerHTML = '';
+	ajax('get_chats.js');
+}
--- a/src/delete_user.js.luan	Wed Nov 06 23:05:45 2024 -0700
+++ b/src/delete_user.js.luan	Sat Nov 09 19:17:21 2024 -0700
@@ -21,7 +21,7 @@
 	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" )
+		local chats = chat_search( "chat_user_ids:"..user.id )
 		for _, chat in ipairs(chats) do
 			for _, user_id in ipairs(chat.user_ids) do
 				user_ids[user_id] = true
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/heartbeat.js.luan	Sat Nov 09 19:17:21 2024 -0700
@@ -0,0 +1,30 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local String = require "luan:String.luan"
+local to_number = String.to_number or error()
+local Time = require "luan:Time.luan"
+local time_now = Time.now 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 Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "heartbeat.js"
+
+
+return function()
+	local last_update = Http.request.parameters.last_update or error()
+	last_update = to_number(last_update) or error()
+	local user = current_user() or error()
+	local user_last_update = user.last_update()
+	local now = time_now()
+	if now - user_last_update < 10000 or last_update >= user_last_update then
+		logger.info "ok"
+		return
+	end
+	logger.info "update"
+	Io.stdout = Http.response.text_writer()
+%>
+	resync(<%=user_last_update%>);
+<%
+end
--- a/src/index.html.luan	Wed Nov 06 23:05:45 2024 -0700
+++ b/src/index.html.luan	Sat Nov 09 19:17:21 2024 -0700
@@ -138,6 +138,7 @@
 	end
 %>
 			setUserEventSource(<%=user.id%>);
+			lastUpdate = <%=user.last_update()%>;
 		</script>
 	</body>
 </html>
--- a/src/lib/User.luan	Wed Nov 06 23:05:45 2024 -0700
+++ b/src/lib/User.luan	Sat Nov 09 19:17:21 2024 -0700
@@ -17,6 +17,7 @@
 local Db = require "site:/lib/Db.luan"
 local run_in_transaction = Db.run_in_transaction or error()
 local Chat = require "site:/lib/Chat.luan"
+local chat_search = Chat.search or error()
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "User"
 
@@ -52,7 +53,7 @@
 	function user.delete()
 		run_in_transaction( function()
 			local id = user.id
-			local chats = Chat.search("chat_user_ids:"..id)
+			local chats = chat_search("chat_user_ids:"..id)
 			for _, chat in ipairs(chats) do
 				chat.delete()
 			end
@@ -68,6 +69,16 @@
 		Http.request.cookies.password = user.password or error()
 	end
 
+	function user.last_update()
+		local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc", 1 )
+		local n = #chats
+		if n == 0 then
+			return 0
+		elseif n == 1 then
+			return chats[1].updated
+		else error() end
+	end
+
 	return user
 end