comparison src/chat.html.luan @ 17:7230c821c368

push fixes
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 Nov 2024 14:44:35 -0700
parents 8b8905f63d80
children dade6a560494
comparison
equal deleted inserted replaced
16:82b55186a4a0 17:7230c821c368
12 local Shared = require "site:/lib/Shared.luan" 12 local Shared = require "site:/lib/Shared.luan"
13 local head = Shared.head or error() 13 local head = Shared.head or error()
14 local header = Shared.header or error() 14 local header = Shared.header or error()
15 local started = Shared.started or error() 15 local started = Shared.started or error()
16 local chats_html = Shared.chats_html or error() 16 local chats_html = Shared.chats_html or error()
17 local http_push_to_users = Shared.http_push_to_users or error()
17 local User = require "site:/lib/User.luan" 18 local User = require "site:/lib/User.luan"
18 local current_user = User.current or error() 19 local current_user = User.current or error()
19 local get_user_by_email = User.get_by_email or error() 20 local get_user_by_email = User.get_by_email or error()
20 local Utils = require "site:/lib/Utils.luan" 21 local Utils = require "site:/lib/Utils.luan"
21 local to_set = Utils.to_set or error() 22 local to_set = Utils.to_set or error()
35 local id = with_user.id 36 local id = with_user.id
36 t[#t+1] = "+chat_user_ids:"..id 37 t[#t+1] = "+chat_user_ids:"..id
37 ids[#ids+1] = id 38 ids[#ids+1] = id
38 end 39 end
39 local query = concat(t," ") 40 local query = concat(t," ")
40 return run_in_transaction( function() 41 local need_push = false
42 local chat = run_in_transaction( function()
41 local chats = chat_search(query) 43 local chats = chat_search(query)
42 local n = #chats 44 local n = #chats
43 if n == 0 then 45 if n == 0 then
44 local chat = Chat.new{ 46 local chat = Chat.new{
45 user_ids = ids 47 user_ids = ids
46 } 48 }
47 chat.save() 49 chat.save()
50 need_push = true
48 return chat 51 return chat
49 elseif n == 1 then 52 elseif n == 1 then
50 return chats[1] 53 return chats[1]
51 else 54 else
52 error("multiple chats for: "..query) 55 error("multiple chats for: "..query)
53 end 56 end
54 end ) 57 end )
58 if need_push then
59 local js = "getChats('"..chat.id.."')"
60 http_push_to_users( chat.user_ids, js )
61 end
62 return chat
55 end 63 end
56 64
57 return function() 65 return function()
58 local with = Http.request.parameters.with 66 local with = Http.request.parameters.with
59 with = to_set(with) 67 with = to_set(with)