annotate src/lib/Shared.luan @ 73:a63faf49e1d7

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