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