comparison src/index.html.luan @ 58:7b6691bd65c3

chat_key
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 04 Mar 2025 07:38:43 -0700
parents 323ddacc1593
children 8270106644db
comparison
equal deleted inserted replaced
57:c420f39eb474 58:7b6691bd65c3
23 local Utils = require "site:/lib/Utils.luan" 23 local Utils = require "site:/lib/Utils.luan"
24 local to_set = Utils.to_set or error() 24 local to_set = Utils.to_set or error()
25 local Db = require "site:/lib/Db.luan" 25 local Db = require "site:/lib/Db.luan"
26 local run_in_transaction = Db.run_in_transaction or error() 26 local run_in_transaction = Db.run_in_transaction or error()
27 local Chat = require "site:/lib/Chat.luan" 27 local Chat = require "site:/lib/Chat.luan"
28 local chat_search = Chat.search or error() 28 local get_chat_by_user_ids = Chat.get_by_user_ids or error()
29 local Logging = require "luan:logging/Logging.luan" 29 local Logging = require "luan:logging/Logging.luan"
30 local logger = Logging.logger "index.html" 30 local logger = Logging.logger "index.html"
31 31
32 32
33 local function get_chat(with) 33 local function get_chat(with)
34 local t = {}
35 local ids = {} 34 local ids = {}
36 for email in pairs(with) do 35 for email in pairs(with) do
37 local with_user = get_user_by_email(email) or error() 36 local with_user = get_user_by_email(email) or error()
38 local id = with_user.id 37 local id = with_user.id
39 t[#t+1] = "+chat_user_ids:"..id
40 ids[#ids+1] = id 38 ids[#ids+1] = id
41 end 39 end
42 local query = concat(t," ")
43 local need_push = false 40 local need_push = false
44 local chat = run_in_transaction( function() 41 local chat = run_in_transaction( function()
45 local chats = chat_search(query) 42 local chat = get_chat_by_user_ids(ids)
46 local n = #chats 43 if chat == nil then
47 if n == 0 then 44 chat = Chat.new{
48 local chat = Chat.new{
49 user_ids = ids 45 user_ids = ids
50 } 46 }
51 chat.save() 47 chat.save()
52 need_push = true 48 need_push = true
53 return chat
54 elseif n == 1 then
55 return chats[1]
56 else
57 error("multiple chats for: "..query)
58 end 49 end
50 return chat
59 end ) 51 end )
60 if need_push then 52 if need_push then
61 local js = "getChats('"..chat.id.."')" 53 local js = "getChats('"..chat.id.."')"
62 http_push_to_users( chat.user_ids, js ) 54 http_push_to_users( chat.user_ids, js )
63 end 55 end