annotate src/chat.luan @ 83:a47036fd0158

group chat
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Mar 2025 13:56:59 -0600
parents 8270106644db
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local pairs = Luan.pairs or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Table = require "luan:Table.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local concat = Table.concat or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local is_empty = Table.is_empty or error()
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
7 local Html = require "luan:Html.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
8 local url_encode = Html.url_encode or error()
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Http = require "luan:http/Http.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Shared = require "site:/lib/Shared.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local http_push_to_users = Shared.http_push_to_users or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local User = require "site:/lib/User.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local current_user = User.current or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local get_user_by_email = User.get_by_email or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Utils = require "site:/lib/Utils.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local to_set = Utils.to_set or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Db = require "site:/lib/Db.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local run_in_transaction = Db.run_in_transaction or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local Chat = require "site:/lib/Chat.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local get_chat_by_user_ids = Chat.get_by_user_ids or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 return function()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 local with = Http.request.parameters.with
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 local user = current_user()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 with = to_set(with)
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 if user == nil then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local url = "/login.html"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 if not is_empty(with) then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 local t = {}
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 for email in pairs(with) do
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
32 t[#t+1] = "with="..url_encode(email)
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 url = url.."?"..concat(t,"&")
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 Http.response.send_redirect(url)
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 return
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 if is_empty(with) then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 Http.response.send_redirect("/")
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 return
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 with[user.email] = true
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 local ids = {}
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 for email in pairs(with) do
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 local with_user = get_user_by_email(email) or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 local id = with_user.id
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 ids[#ids+1] = id
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 local need_push = false
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 local chat = run_in_transaction( function()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 local chat = get_chat_by_user_ids(ids)
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 if chat == nil then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 chat = Chat.new{
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 user_ids = ids
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 }
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 chat.save()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 need_push = true
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 return chat
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 end )
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 if need_push then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 local js = "getChats('"..chat.id.."')"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 http_push_to_users( chat.user_ids, js )
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 Http.response.send_redirect("/?chat="..chat.id)
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 end