annotate src/lib/Shared.luan @ 59:8270106644db

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