annotate src/chat.html.luan @ 7:41d35b72c774

chat page
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Oct 2024 22:11:40 -0600
parents a49866b52cc2
children d654e3471132
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()
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
7 local is_empty = Table.is_empty or error()
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
8 local size = Table.size or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Io = require "luan:Io.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Shared = require "site:/lib/Shared.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local head = Shared.head or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local header = Shared.header or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
14 local User = require "site:/lib/User.luan"
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
15 local current_user = User.current or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
16 local get_user_by_email = User.get_by_email or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
17 local Utils = require "site:/lib/Utils.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
18 local to_set = Utils.to_set or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
19 local Db = require "site:/lib/Db.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
20 local run_in_transaction = Db.run_in_transaction or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
21 local Chat = require "site:/lib/Chat.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
22 local chat_search = Chat.search or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
25 local function get_chat(with)
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 local ids = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
28 for email in pairs(with) do
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
29 local with_user = get_user_by_email(email) or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
30 local id = with_user.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
31 t[#t+1] = "+chat_user_ids:"..id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
32 ids[#ids+1] = id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
33 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
34 local query = concat(t," ")
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
35 run_in_transaction( function()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
36 local chats = chat_search(query)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
37 local n = #chats
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
38 if n == 0 then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
39 local chat = Chat.new{
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
40 user_ids = ids
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
41 }
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
42 chat.save()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
43 return chat
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
44 elseif n == 1 then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
45 return chats[1]
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
46 else
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
47 error("multiple chats for: "..query)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
48 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
49 end )
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
50 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
51
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 return function()
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
53 local with = Http.request.parameters.with
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
54 with = to_set(with)
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
55 local user = current_user()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
56 if user == nil then
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
57 local url = "/login.html"
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
58 if not is_empty(with) then
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
59 local t = {}
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
60 for email in pairs(with) do
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
61 t[#t+1] = "with="..email
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
62 end
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
63 url = url.."?"..concat(t,"&")
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
64 end
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
65 Http.response.send_redirect(url)
4
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
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
68 if not is_empty(with) then
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
69 with[user.email] = true
5
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
70 if size(with) > 1 then
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
71 get_chat(with)
a49866b52cc2 login to chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
72 end
4
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() %>
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
81 <style>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
82 body {
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
83 height: 100vh;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
84 display: flex;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
85 flex-direction: column;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
86 }
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
87 div[content] {
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
88 margin-bottom: 0;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
89 display: flex;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
90 height: 100%;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
91 margin-left: calc(3% - 8px);
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
92 }
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
93 div[chats] {
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
94 width: 250px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
95 padding-right: 8px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
96 }
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
97 div[chats] > div {
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
98 margin-top: 2px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
99 margin-bottom: 2px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
100 padding-top: 16px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
101 padding-bottom: 16px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
102 padding-left: 8px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
103 padding-right: 8px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
104 border-radius: 4px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
105 }
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
106 div[chats] > div:hover,
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
107 div[chats] > div[selected] {
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
108 background-color: LightBlue;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
109 }
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
110 div[posts] {
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
111 padding-left: 8px;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
112 border-left: 1px solid;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
113 }
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
114 </style>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
115 <script>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
116 'use strict';
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
117
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
118 let currentChatId = null;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
119
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
120 function selectChat(div) {
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
121 let chatId = div.getAttribute('chat');
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
122 if( chatId === currentChatId )
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
123 return;
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
124 let selected = div.parentNode.querySelector('[selected]');
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
125 if( selected ) selected.removeAttribute('selected');
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
126 div.setAttribute('selected','');
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
127 ajax(`chat.js?chat=${chatId}`);
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
128 }
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
129 </script>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 </head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 <body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 <% header() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 <div content>
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
134 <div chats>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
135 <%
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
136 for _, chat in ipairs(chats) do
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
137 %>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
138 <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
139 <%
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
140 end
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
141 %>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
142 </div>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
143 <div posts></div>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 </body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 </html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 end