changeset 58:7b6691bd65c3

chat_key
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 04 Mar 2025 07:38:43 -0700
parents c420f39eb474
children 8270106644db
files src/index.html.luan src/lib/Chat.luan src/lib/Db.luan
diffstat 3 files changed, 39 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/index.html.luan	Tue Mar 04 07:05:12 2025 -0700
+++ b/src/index.html.luan	Tue Mar 04 07:38:43 2025 -0700
@@ -25,37 +25,29 @@
 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 get_chat_by_user_ids = Chat.get_by_user_ids or error()
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "index.html"
 
 
 local function get_chat(with)
-	local t = {}
 	local ids = {}
 	for email in pairs(with) do
 		local with_user = get_user_by_email(email) or error()
 		local id = with_user.id
-		t[#t+1] = "+chat_user_ids:"..id
 		ids[#ids+1] = id
 	end
-	local query = concat(t," ")
 	local need_push = false
 	local chat = run_in_transaction( function()
-		local chats = chat_search(query)
-		local n = #chats
-		if n == 0 then
-			local chat = Chat.new{
+		local chat = get_chat_by_user_ids(ids)
+		if chat == nil then
+			chat = Chat.new{
 				user_ids = ids
 			}
 			chat.save()
 			need_push = true
-			return chat
-		elseif n == 1 then
-			return chats[1]
-		else
-			error("multiple chats for: "..query)
 		end
+		return chat
 	end )
 	if need_push then
 		local js = "getChats('"..chat.id.."')"
--- a/src/lib/Chat.luan	Tue Mar 04 07:05:12 2025 -0700
+++ b/src/lib/Chat.luan	Tue Mar 04 07:38:43 2025 -0700
@@ -3,6 +3,9 @@
 local ipairs = Luan.ipairs or error()
 local Time = require "luan:Time.luan"
 local time_now = Time.now or error()
+local Table = require "luan:Table.luan"
+local sort = Table.sort or error()
+local concat = Table.concat or error()
 local Http = require "luan:http/Http.luan"
 local Db = require "site:/lib/Db.luan"
 local run_in_transaction = Db.run_in_transaction or error()
@@ -18,6 +21,7 @@
 		id = doc.id
 		user_ids = doc.chat_user_ids
 		updated = doc.chat_updated
+		key = doc.chat_key
 	}
 end
 
@@ -27,11 +31,18 @@
 		id = chat.id
 		chat_user_ids = chat.user_ids or error()
 		chat_updated = chat.updated or error()
+		chat_key = chat.key or error()
 	}
 end
 
+local function get_chat_key(user_ids)
+	sort(user_ids)
+	return concat(user_ids,"~")
+end
+
 function Chat.new(chat)
 	chat.updated = chat.updated or time_now()
+	chat.key = chat.key or get_chat_key(chat.user_ids)
 
 	function chat.save()
 		local doc = to_doc(chat)
@@ -103,4 +114,10 @@
 	return doc and doc.type=="chat" and from_doc(doc) or nil
 end
 
+function Chat.get_by_user_ids(user_ids)
+	local key = get_chat_key(user_ids)
+	local doc = Db.get_document("chat_key:"..key)
+	return doc and from_doc(doc)
+end
+
 return Chat
--- a/src/lib/Db.luan	Tue Mar 04 07:05:12 2025 -0700
+++ b/src/lib/Db.luan	Tue Mar 04 07:38:43 2025 -0700
@@ -2,6 +2,9 @@
 local error = Luan.error
 local new_error = Luan.new_error or error()
 local ipairs = Luan.ipairs or error()
+local Table = require "luan:Table.luan"
+local sort = Table.sort or error()
+local concat = Table.concat or error()
 local Lucene = require "luan:lucene/Lucene.luan"
 local Io = require "luan:Io.luan"
 local uri = Io.uri or error()
@@ -24,6 +27,7 @@
 
 Db.indexed_fields.chat_user_ids = Lucene.type.long
 Db.indexed_fields.chat_updated = Lucene.type.long
+Db.indexed_fields.chat_key = Lucene.type.string
 
 Db.indexed_fields.post_chat_id = Lucene.type.long
 Db.indexed_fields.post_date = Lucene.type.long
@@ -35,6 +39,12 @@
 	logger.error(new_error("not in transaction"))
 end
 
+-- copied from Chat
+local function get_chat_key(user_ids)
+	sort(user_ids)
+	return concat(user_ids,"~")
+end
+
 Db.update{
 	[1] = function()
 		local docs = Db.search("type:post",1,1000000)
@@ -49,6 +59,13 @@
 	[3] = function()
 		Db.delete("type:chatuser")
 	end
+	[4] = function()
+		local docs = Db.search("type:chat",1,1000000)
+		for _, doc in ipairs(docs) do
+			doc.chat_key = get_chat_key(doc.chat_user_ids)
+			Db.save(doc)
+		end
+	end
 }
 
 if Http.is_serving then