diff src/lib/Chat.luan @ 53:9298b04607ae

add unread
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 03 Mar 2025 19:39:30 -0700
parents 42b741a1d5c6
children d21ae4920aac
line wrap: on
line diff
--- a/src/lib/Chat.luan	Mon Mar 03 12:29:06 2025 -0700
+++ b/src/lib/Chat.luan	Mon Mar 03 19:39:30 2025 -0700
@@ -61,6 +61,28 @@
 		error()
 	end
 
+	local function get_chatuser_id(user)
+		return chat.id.."~"..user.id
+	end
+
+	function chat.unread(user)
+		local doc = Db.get_document("chatuser_id:"..get_chatuser_id(user))
+		local read_date = doc and doc.read_date or 0
+		return Db.count("+post_chat_id:"..chat.id.." +post_date:["..read_date.." TO *]")
+	end
+
+	function chat.read(user)
+		run_in_transaction( function()
+			local chatuser_id = get_chatuser_id(user)
+			local doc = Db.get_document("chatuser_id:"..chatuser_id) or {
+				type = "chatuser"
+				chatuser_id = chatuser_id
+			}
+			doc.read_date = time_now()
+			Db.save(doc)
+		end )
+	end
+
 	return chat
 end