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