annotate src/lib/Shared.luan @ 34:62d04ca486dd

UI
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Nov 2024 16:08:24 -0700
parents e2b7f6393dab
children 27c41f22d2a9
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()
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
12 local Http = require "luan:http/Http.luan"
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
13 local Mail = require "luan:mail/Mail.luan"
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
14 local User = require "site:/lib/User.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
15 local current_user = User.current or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
16 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
17 local Chat = require "site:/lib/Chat.luan"
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
18 local chat_search = Chat.search or error()
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
19 local Utils = require "site:/lib/Utils.luan"
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
20 local base_url = Utils.base_url or error()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local Shared = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
25 local started = Time.now()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
26 Shared.started = started
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
27
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 function Shared.head()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 <meta name="viewport" content="width=device-width, initial-scale=1">
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
31 <title>Web Chat</title>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 <style>
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
33 @import "/site.css?s=<%=started%>";
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 </style>
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
35 <script src="/site.js?s=<%=started%>"></script>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 local function header(crumbs)
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
40 local user = current_user()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 <div header>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
43 <span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
44 <a href="/">Web Chat</a>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 <% for _, crumb in ipairs(crumbs or {}) do %>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
46 / <%=crumb%>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 <% end %>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
48 </span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
49 <span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
50 <% if user == nil then %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
51 <a href="/login.html">Login / Register</a>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
52 <% else %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
53 <a href="/account.html"><%= user.email %></a>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
54 <% end %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
55 </span>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 Shared.header = header
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 function Shared.private_header()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 header{
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 [[<a href="/private/">private</a>]]
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 [[<a href="/private/tools/">tools</a>]]
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67
1
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
68 local config_file = uri("site:/private/local/config.luano")
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
69 Shared.config_file = config_file
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
70
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
71 do
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
72 if config_file.exists() then
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
73 Shared.config = parse( config_file.read_text() )
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
74 else
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
75 Shared.config = {
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
76 mail_server = {
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
77 host = "smtpcorp.com"
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
78 port = 465
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
79 username = "xxx"
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
80 password = "xxx"
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
81 }
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
82 }
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
83 end
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
84 end
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
85
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
86 local send_mail = Mail.sender(Shared.config.mail_server).send
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
87
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
88 function Shared.send_mail_async(mail)
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
89 thread_run( function()
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
90 send_mail(mail)
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
91 end )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
92 end
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
93
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
94 function Shared.post_html(post)
22
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
95 local author_id = post.author_id
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
96 local user = current_user() or error()
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
97 local author = get_user_by_id(author_id)
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
98 local id = post.id
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
99 %>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
100 <div post="<%=id%>">
34
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
101 <div who="<%=author.id%>">
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
102 <span author><%=author.email%></span>
34
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
103 <span right>
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
104 <span when fix><%=post.date%></span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
105 <span pulldown>
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
106 <img onclick="clickMenu(this)" src="/images/more_vert.svg">
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
107 <div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
108 <span onclick="editPost('<%=id%>')">Edit</span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
109 <span onclick="deletePost('<%=id%>')">Delete</span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
110 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
111 </span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
112 </span>
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
113 </div>
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
114 <div text fix><%= html_encode(post.content) %></div>
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
115 </div>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
116 <%
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
117 end
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
118
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
119 local function chat_other_users_html(chat,user)
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
120 local my_id = user.id
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
121 local is_first = true
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
122 for _, user_id in ipairs(chat.user_ids) do
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
123 if user_id ~= my_id then
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
124 local other_user = get_user_by_id(user_id) or error()
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
125 if is_first then
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
126 is_first = false
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
127 else
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
128 %>, <%
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
129 end
34
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
130 %><span email><%= other_user.email %></span><span online="<%= other_user.id %>"></span><%
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
131 end
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
132 end
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
133 end
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
134 Shared.chat_other_users_html = chat_other_users_html
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
135
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
136 function Shared.chats_html()
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
137 local user = current_user() or error()
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
138 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
139 for _, chat in ipairs(chats) do
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
140 %>
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
141 <div chat="<%=chat.id%>" onclick="selectChat(this)"><% chat_other_users_html(chat,user) %></div>
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
142 <%
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
143 end
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
144 end
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
145
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
146 function Shared.http_push_to_users(user_ids,message)
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
147 local base = base_url().."/user/"
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
148 for _, user_id in ipairs(user_ids) do
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
149 local url = base..user_id
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
150 Http.push(url,message)
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
151 end
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
152 end
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
153
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 return Shared