local Luan = require "luan:Luan.luan" local error = Luan.error local ipairs = Luan.ipairs or error() local parse = Luan.parse or error() local String = require "luan:String.luan" local contains = String.contains or error() local Table = require "luan:Table.luan" local concat = Table.concat or error() local Time = require "luan:Time.luan" local Thread = require "luan:Thread.luan" local thread_run = Thread.run or error() local Html = require "luan:Html.luan" local html_encode = Html.encode or error() local url_encode = Html.url_encode or error() local Http = require "luan:http/Http.luan" local Mail = require "luan:mail/Mail.luan" local User = require "site:/lib/User.luan" local current_user = User.current or error() local get_user_by_id = User.get_by_id or error() local Chat = require "site:/lib/Chat.luan" local chat_search = Chat.search or error() local Utils = require "site:/lib/Utils.luan" local base_url = Utils.base_url or error() local to_list = Utils.to_list or error() local Db = require "site:/lib/Db.luan" local Post = require "site:/lib/Post.luan" local get_post_by_id = Post.get_by_id or error() local Config = require "site:/private/Config.luan" local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "Shared" local Shared = {} local started = Time.now() Shared.started = started local title local domain = Http.domain if domain == "chat.luan.software" then title = "Luan Chat" elseif domain == "test.chat.luan.software" then title = "Luan Chat test" elseif domain == nil then title = "Luan Chat local" else error(domain) end Shared.title = title function Shared.head() %> <%=title%> <% end local function header(crumbs) local user = current_user() %>
Luan Chat <% for _, crumb in ipairs(crumbs or {}) do %> / <%=crumb%> <% end %> <% if user == nil then %> Login / Register <% else local voice_url = user.voice_url if voice_url ~= nil then %> <% end %> <%= user.email %> <% end %>
<% end Shared.header = header function Shared.private_header() header{ [[private]] [[tools]] } end local default_from = title.." " local send_mail0 = Mail.sender(Config.mail_server).send function Shared.send_mail(mail) mail.From = mail.From or default_from send_mail0(mail) end function Shared.send_mail_async(mail) mail.From = mail.From or default_from thread_run( function() send_mail0(mail) end ) end function Shared.post_html(post) local author_id = post.author_id local user = current_user() or error() local author = get_user_by_id(author_id) local id = post.id local reply_id = post.reply local reply = reply_id and get_post_by_id(reply_id) %>
<%=author.name_html()%> <%=post.date%>
<% if reply ~= nil then %>
<%= html_encode(reply.content) %>
<%=reply.date%>
<% elseif reply_id ~= nil then %>
Message deleted
<% end %>
<%= html_encode(post.content) %>
<% end local function group_name(user_ids) local t = {nil} for _, user_id in ipairs(user_ids) do t[#t+1] = get_user_by_id(user_id).name_html() end return concat(t,", ") end Shared.group_name = group_name function Shared.chats_html() local user = current_user() or error() local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc" ) for _, chat in ipairs(chats) do local chat_id = chat.id local unread = chat.unread(user) %>
<% local user_ids = chat.other_user_ids(user.id) if #user_ids > 1 then %> <%= group_name(user_ids) %> <% else local other_user = get_user_by_id(user_ids[1]) or error() %> <%= other_user.name_html() %> <% end %> <%=unread%>
<% end end function Shared.http_push_to_users(user_ids,message) local base = base_url().."/user/" for _, user_id in ipairs(user_ids) do local url = base..user_id Http.push(url,message) end end Shared.compressed = {compressed=true} function Shared.add_with(path) local with = Http.request.parameters.with if with ~= nil then with = to_list(with) local t = {} for _, email in ipairs(with) do t[#t+1] = "with="..url_encode(email) end path = path..(contains(path,"?") and "&" or "?")..concat(t,"&") end return path end function Shared.hidden_with() local with = Http.request.parameters.with with = to_list(with) for _, email in ipairs(with) do %> <% end end return Shared