annotate src/chat.html.luan @ 10:f9e6a4cc4f7d

add Post
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 30 Oct 2024 23:18:45 -0600
parents b8b12fd8be22
children 8b8905f63d80
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()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
5 local range = Luan.range or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
6 local Table = require "luan:Table.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
7 local concat = Table.concat or error()
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
8 local is_empty = Table.is_empty or error()
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
9 local size = Table.size or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Io = require "luan:Io.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local Shared = require "site:/lib/Shared.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local head = Shared.head or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local header = Shared.header or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
15 local started = Shared.started or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
16 local User = require "site:/lib/User.luan"
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
17 local current_user = User.current or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
18 local get_user_by_email = User.get_by_email or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
19 local Utils = require "site:/lib/Utils.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
20 local to_set = Utils.to_set or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
21 local Db = require "site:/lib/Db.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
22 local run_in_transaction = Db.run_in_transaction or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
23 local Chat = require "site:/lib/Chat.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
24 local chat_search = Chat.search or error()
8
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
25 local Logging = require "luan:logging/Logging.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
26 local logger = Logging.logger "chat.html"
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
29 local function get_chat(with)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
30 local t = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
31 local ids = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
32 for email in pairs(with) do
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
33 local with_user = get_user_by_email(email) or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
34 local id = with_user.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
35 t[#t+1] = "+chat_user_ids:"..id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
36 ids[#ids+1] = id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
37 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
38 local query = concat(t," ")
8
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
39 return run_in_transaction( function()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
40 local chats = chat_search(query)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
41 local n = #chats
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
42 if n == 0 then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
43 local chat = Chat.new{
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
44 user_ids = ids
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
45 }
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
46 chat.save()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
47 return chat
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
48 elseif n == 1 then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
49 return chats[1]
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
50 else
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
51 error("multiple chats for: "..query)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
52 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
53 end )
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
54 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
55
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 return function()
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
57 local with = Http.request.parameters.with
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
58 with = to_set(with)
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
59 local user = current_user()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
60 if user == nil then
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
61 local url = "/login.html"
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
62 if not is_empty(with) then
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
63 local t = {}
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
64 for email in pairs(with) do
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
65 t[#t+1] = "with="..email
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
66 end
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
67 url = url.."?"..concat(t,"&")
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
68 end
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
69 Http.response.send_redirect(url)
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
70 return
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
71 end
8
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
72 local selected = nil
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
73 if not is_empty(with) then
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
74 with[user.email] = true
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
75 if size(with) > 1 then
8
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
76 selected = get_chat(with)
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
77 end
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
78 end
9
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
79 local chats = Chat.search( "chat_user_ids:"..user.id, "chat_updated desc" )
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 Io.stdout = Http.response.text_writer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 <!doctype html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 <html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 <head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 <% head() %>
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
86 <style>
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
87 @import "chat.css?s=<%=started%>";
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
88 </style>
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
89 <script src="chat.js?s=<%=started%>"></script>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 </head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 <body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 <% header() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 <div content>
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
94 <div chats>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
95 <%
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
96 for _, chat in ipairs(chats) do
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
97 %>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
98 <div chat="<%=chat.id%>" onclick="selectChat(this)"><%= chat.other_users_email(user) %></div>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
99 <%
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
100 end
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
101 %>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
102 </div>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
103 <div posts></div>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 </body>
8
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
106 <%
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
107 if selected ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
108 %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
109 <script>
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
110 let div = document.querySelector('div[chat="<%=selected.id%>"]');
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
111 selectChat(div);
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
112 </script>
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
113 <%
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
114 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
115 %>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 </html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 end