annotate src/chat.luan @ 75:377bdda60f0b

read up to
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 09 Mar 2025 18:26:49 -0600
parents 8270106644db
children a47036fd0158
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()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Http = require "luan:http/Http.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Shared = require "site:/lib/Shared.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local http_push_to_users = Shared.http_push_to_users or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local User = require "site:/lib/User.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local current_user = User.current or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local get_user_by_email = User.get_by_email or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Utils = require "site:/lib/Utils.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local to_set = Utils.to_set or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Db = require "site:/lib/Db.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local run_in_transaction = Db.run_in_transaction or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Chat = require "site:/lib/Chat.luan"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 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
19
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 return function()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 local with = Http.request.parameters.with
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local user = current_user()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 with = to_set(with)
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 if user == nil then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local url = "/login.html"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 if not is_empty(with) then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local t = {}
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 for email in pairs(with) do
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 t[#t+1] = "with="..email
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 url = url.."?"..concat(t,"&")
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 Http.response.send_redirect(url)
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 return
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 if is_empty(with) then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 Http.response.send_redirect("/")
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 return
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 with[user.email] = true
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 local ids = {}
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 for email in pairs(with) do
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 local with_user = get_user_by_email(email) or error()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 local id = with_user.id
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 ids[#ids+1] = id
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 local need_push = false
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 local chat = run_in_transaction( function()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 local chat = get_chat_by_user_ids(ids)
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 if chat == nil then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 chat = Chat.new{
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 user_ids = ids
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 }
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 chat.save()
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 need_push = true
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 return chat
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 if need_push then
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 local js = "getChats('"..chat.id.."')"
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 http_push_to_users( chat.user_ids, js )
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 end
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 Http.response.send_redirect("/?chat="..chat.id)
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 end