annotate src/chat.html.luan @ 4:2da10ece826f

add Chat
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 27 Oct 2024 20:39:18 -0600
parents src/login_sent.html.luan@2c63b10781e1
children a49866b52cc2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
3 local ipairs = Luan.ipairs or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
4 local pairs = Luan.pairs or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
5 local Table = require "luan:Table.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
6 local concat = Table.concat or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Io = require "luan:Io.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Shared = require "site:/lib/Shared.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local head = Shared.head or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local header = Shared.header or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
12 local User = require "site:/lib/User.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
13 local current_user = User.current_required or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
14 local get_user_by_id = User.get_by_id or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
15 local get_user_by_email = User.get_by_email or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
16 local Utils = require "site:/lib/Utils.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
17 local to_set = Utils.to_set or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
18 local Db = require "site:/lib/Db.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
19 local run_in_transaction = Db.run_in_transaction or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
20 local Chat = require "site:/lib/Chat.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
21 local chat_search = Chat.search or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
24 local function other_users(user,chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
25 local my_id = user.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
26 local t = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
27 for _, user_id in ipairs(chat.user_ids) do
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
28 if user_id ~= my_id then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
29 local other_user = get_user_by_id(user_id) or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
30 t[#t+1] = other_user.email
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
31 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
32 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
33 return concat( t, ", " )
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
34 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
35
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
36 local function get_chat(with)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
37 local t = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
38 local ids = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
39 for email in pairs(with) do
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
40 local with_user = get_user_by_email(email) or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
41 local id = with_user.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
42 t[#t+1] = "+chat_user_ids:"..id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
43 ids[#ids+1] = id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
44 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
45 local query = concat(t," ")
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
46 run_in_transaction( function()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
47 local chats = chat_search(query)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
48 local n = #chats
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
49 if n == 0 then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
50 local chat = Chat.new{
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
51 user_ids = ids
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
52 }
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
53 chat.save()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
54 return chat
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
55 elseif n == 1 then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
56 return chats[1]
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
57 else
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
58 error("multiple chats for: "..query)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
59 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
60 end )
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
61 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
62
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 return function()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
64 local user = current_user()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
65 if user == nil then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
66 return
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
67 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
68 local with = Http.request.parameters.with
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
69 if with ~= nil then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
70 with = to_set(with)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
71 with[user.email] = true
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
72 get_chat(with)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
73 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
74 local chats = user.get_chats()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 Io.stdout = Http.response.text_writer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 <!doctype html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 <html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 <head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 <% head() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 </head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 <body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 <% header() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 <div content>
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
85 <h1>Chat</h1>
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
86 <% for _, chat in ipairs(chats) do %>
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
87 <p><%= other_users(user,chat) %></p>
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
88 <% end %>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 </body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 </html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 end