annotate src/lib/Shared.luan @ 79:b5a316575e64

reply
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 10 Mar 2025 21:41:53 -0600
parents a63faf49e1d7
children a47036fd0158
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()
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
5 local Time = require "luan:Time.luan"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
6 local Thread = require "luan:Thread.luan"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
7 local thread_run = Thread.run or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
8 local Html = require "luan:Html.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
9 local html_encode = Html.encode or error()
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
10 local Http = require "luan:http/Http.luan"
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
11 local Mail = require "luan:mail/Mail.luan"
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
12 local User = require "site:/lib/User.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
13 local current_user = User.current or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
14 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
15 local Chat = require "site:/lib/Chat.luan"
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
16 local chat_search = Chat.search or error()
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
17 local Utils = require "site:/lib/Utils.luan"
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
18 local base_url = Utils.base_url or error()
69
e229f4aacde0 config in lucene
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
19 local Db = require "site:/lib/Db.luan"
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
20 local Post = require "site:/lib/Post.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
21 local get_post_by_id = Post.get_by_id or error()
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
22 local Logging = require "luan:logging/Logging.luan"
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
23 local logger = Logging.logger "Shared"
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local Shared = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
28 local started = Time.now()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
29 Shared.started = started
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
30
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
31 local title
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
32 local domain = Http.domain
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
33 if domain == "chat.luan.software" then
51
38c209714df9 rename to Luan Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
34 title = "Luan Chat"
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
35 elseif domain == "test.chat.luan.software" then
51
38c209714df9 rename to Luan Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
36 title = "Luan Chat test"
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
37 elseif domain == nil then
51
38c209714df9 rename to Luan Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
38 title = "Luan Chat local"
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
39 else
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
40 error(domain)
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
41 end
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
42 Shared.title = title
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
43
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 function Shared.head()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 <meta name="viewport" content="width=device-width, initial-scale=1">
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
47 <title><%=title%></title>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 <style>
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
49 @import "/site.css?s=<%=started%>";
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 </style>
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
51 <script src="/site.js?s=<%=started%>"></script>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 local function header(crumbs)
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
56 local user = current_user()
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 <div header>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
59 <span>
51
38c209714df9 rename to Luan Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
60 <a href="/">Luan Chat</a>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 <% for _, crumb in ipairs(crumbs or {}) do %>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
62 / <%=crumb%>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 <% end %>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
64 </span>
41
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
65 <span right>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
66 <% if user == nil then %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
67 <a href="/login.html">Login / Register</a>
41
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
68 <% else
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
69 local voice_url = user.voice_url
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
70 if voice_url ~= nil then
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
71 %>
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
72 <a href="<%=voice_url%>" title="Call" target="voice"><img phone src="/images/call.svg"></a>
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
73 <%
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
74 end
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
75 %>
2
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
76 <a href="/account.html"><%= user.email %></a>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
77 <% end %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
78 </span>
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 Shared.header = header
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 function Shared.private_header()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 header{
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 [[<a href="/private/">private</a>]]
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 [[<a href="/private/tools/">tools</a>]]
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90
1
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
91 do
69
e229f4aacde0 config in lucene
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
92 local doc = Db.get_document("type:config")
e229f4aacde0 config in lucene
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
93 if doc ~= nil then
e229f4aacde0 config in lucene
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
94 Shared.config = parse( doc.config )
1
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
95 else
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
96 Shared.config = {
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
97 mail_server = {
69
e229f4aacde0 config in lucene
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
98 host = "mail.smtp2go.com"
1
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
99 port = 465
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
100 username = "xxx"
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
101 password = "xxx"
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
102 }
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
103 }
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
104 end
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
105 end
b116b6ef07bf add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
106
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
107 local default_from = title.." <chat@luan.software>"
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
108 local send_mail0 = Mail.sender(Shared.config.mail_server).send
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
109 function Shared.send_mail(mail)
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
110 mail.From = mail.From or default_from
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
111 send_mail0(mail)
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
112 end
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
113
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
114 function Shared.send_mail_async(mail)
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
115 mail.From = mail.From or default_from
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
116 thread_run( function()
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
117 send_mail0(mail)
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
118 end )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
119 end
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
120
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
121 function Shared.post_html(post)
22
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
122 local author_id = post.author_id
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
123 local user = current_user() or error()
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
124 local author = get_user_by_id(author_id)
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
125 local id = post.id
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
126 local reply = post.reply
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
127 reply = reply and get_post_by_id(reply)
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
128 %>
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
129 <div post="<%=id%>" author="<%=author.id%>" id="p<%=id%>" fix>
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
130 <div who>
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
131 <span author><%=author.name_html()%></span>
34
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
132 <span right>
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
133 <span when><%=post.date%></span>
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
134 <span pulldown></span>
34
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
135 </span>
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
136 </div>
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
137 <% if reply ~= nil then %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
138 <div quote>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
139 <blockquote><%= html_encode(reply.content) %></blockquote>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
140 <div><a when href="#p<%=reply.id%>"><%=reply.date%></a></div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
141 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
142 <% end %>
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
143 <div text><%= html_encode(post.content) %></div>
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
144 </div>
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
145 <%
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
146 end
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
147
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
148 function Shared.chats_html()
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
149 local user = current_user() or error()
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
150 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
151 for _, chat in ipairs(chats) do
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
152 local chat_id = chat.id
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
153 local user_id = chat.other_user_id(user.id)
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
154 local other_user = get_user_by_id(user_id) or error()
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
155 local unread = chat.unread(user)
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
156 %>
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
157 <div chat="<%=chat_id%>" onclick="selectChat('<%=chat_id%>')">
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
158 <%= other_user.name_html() %>
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
159 <span online="<%= other_user.id %>"></span>
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
160 <span unread="<%=unread%>"><%=unread%></span>
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
161 </div>
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
162 <%
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
163 end
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
164 end
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
165
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
166 function Shared.http_push_to_users(user_ids,message)
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
167 local base = base_url().."/user/"
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
168 for _, user_id in ipairs(user_ids) do
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
169 local url = base..user_id
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
170 Http.push(url,message)
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
171 end
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
172 end
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
173
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
174 Shared.compressed = {compressed=true}
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
175
0
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 return Shared