annotate src/lib/Shared.luan @ 15:8b8905f63d80

add get_chats
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Nov 2024 17:36:49 -0700
parents 9f45d32670ae
children 82b55186a4a0
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
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local ipairs = Luan.ipairs or error()
1
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
4 local parse = Luan.parse or error()
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
5 local Io = require "luan:Io.luan"
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
6 local uri = Io.uri or error()
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
7 local Time = require "luan:Time.luan"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
8 local Thread = require "luan:Thread.luan"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
9 local thread_run = Thread.run or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
10 local Html = require "luan:Html.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
11 local html_encode = Html.encode or error()
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
12 local Mail = require "luan:mail/Mail.luan"
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
13 local User = require "site:/lib/User.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
14 local current_user = User.current or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
15 local get_user_by_id = User.get_by_id or error()
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
16 local Chat = require "site:/lib/Chat.luan"
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
17 local chat_search = Chat.search or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local Shared = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
22 local started = Time.now()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
23 Shared.started = started
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
24
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 function Shared.head()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 <meta name="viewport" content="width=device-width, initial-scale=1">
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
28 <title>Web Chat</title>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 <style>
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
30 @import "/site.css?s=<%=started%>";
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 </style>
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
32 <script src="/site.js?s=<%=started%>"></script>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 local function header(crumbs)
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
37 local user = current_user()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 <div header>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
40 <span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
41 <a href="/">Web Chat</a>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 <% for _, crumb in ipairs(crumbs or {}) do %>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
43 / <%=crumb%>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 <% end %>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
45 </span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
46 <span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
47 <% if user == nil then %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
48 <a href="/login.html">Login / Register</a>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
49 <% else %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
50 <a href="/account.html"><%= user.email %></a>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
51 <% end %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
52 </span>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 Shared.header = header
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 function Shared.private_header()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 header{
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 [[<a href="/private/">private</a>]]
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 [[<a href="/private/tools/">tools</a>]]
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64
1
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
65 local config_file = uri("site:/private/local/config.luano")
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
66 Shared.config_file = config_file
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
67
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
68 do
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
69 if config_file.exists() then
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
70 Shared.config = parse( config_file.read_text() )
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
71 else
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
72 Shared.config = {
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
73 mail_server = {
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
74 host = "smtpcorp.com"
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
75 port = 465
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
76 username = "xxx"
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
77 password = "xxx"
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
78 }
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
79 }
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
80 end
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
81 end
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
82
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
83 local send_mail = Mail.sender(Shared.config.mail_server).send
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
84
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
85 function Shared.send_mail_async(mail)
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
86 thread_run( function()
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
87 send_mail(mail)
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
88 end )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
89 end
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
90
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
91 function Shared.post_html(post)
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
92 local author = get_user_by_id(post.author_id)
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
93 local id = post.id
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
94 %>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
95 <div post="<%=id%>">
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
96 <div who>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
97 <span author><%=author.email%></span>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
98 <span when fix><%=post.date%></span>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
99 </div>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
100 <div text><%= html_encode(post.content) %></div>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
101 </div>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
102 <%
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
103 end
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
104
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
105 function Shared.chats_html()
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
106 local user = current_user() or error()
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
107 local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc" )
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
108 for _, chat in ipairs(chats) do
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
109 %>
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
110 <div chat="<%=chat.id%>" onclick="selectChat(this)"><%= chat.other_users_email(user) %></div>
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
111 <%
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
112 end
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
113 end
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
114
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 return Shared