Mercurial Hosting > chat
changeset 59:8270106644db
add chat.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 04 Mar 2025 08:22:45 -0700 |
parents | 7b6691bd65c3 |
children | 3521166513b3 |
files | src/about.html.luan src/account.html.luan src/chat.js src/chat.luan src/do_login.html.luan src/index.html.luan src/invite.js.luan src/lib/Shared.luan |
diffstat | 8 files changed, 81 insertions(+), 71 deletions(-) [+] |
line wrap: on
line diff
--- a/src/about.html.luan Tue Mar 04 07:38:43 2025 -0700 +++ b/src/about.html.luan Tue Mar 04 08:22:45 2025 -0700 @@ -28,7 +28,7 @@ <li>voice, video, and screen share - Use <a href="https://talky.io/">Talky</a> or <a href="https://www.zoom.com/">Zoom</a> instead. With Talky, you can set a Talky URL to be your voice URL for easy access. With Zoom, post a meeting URL in chat.</li> </ul> - <p>For questions, <a href="https://chat.luan.software/?with=fschmidt@gmail.com">contact me</a>. This is an open <a href="https://hg.reactionary.software/repo/chat/">source</a> project.</p> + <p>For questions, <a href="https://chat.luan.software/chat?with=fschmidt@gmail.com">contact me</a>. This is an open <a href="https://hg.reactionary.software/repo/chat/">source</a> project.</p> </div> </body> </html>
--- a/src/account.html.luan Tue Mar 04 07:38:43 2025 -0700 +++ b/src/account.html.luan Tue Mar 04 08:22:45 2025 -0700 @@ -142,7 +142,7 @@ <div content> <h1>Your Account</h1> <p><a href="about.html">About Luan Chat</a></p> - <p>Your URL: <%= base_url() %>/?with=<%=user.email%></p> + <p>Your URL: <%= base_url() %>/chat?with=<%=user.email%></p> <p>Your username: <span username></span> <a href="javascript:editUsername()">Edit</a></p> <p><span notify></span> <a href="javascript:editNotify()">Edit</a></p> <p><span voice></span> <a href="javascript:editVoice()">Edit</a></p>
--- a/src/chat.js Tue Mar 04 07:38:43 2025 -0700 +++ b/src/chat.js Tue Mar 04 08:22:45 2025 -0700 @@ -16,17 +16,17 @@ userEventSource.onmessage = evalEvent; } -function selectChat(div,email) { +function selectChat(chatId) { document.querySelector('div[chat_content]').setAttribute('show','posts'); - let chatId = div.getAttribute('chat'); if( chatId === currentChatId ) return; + let div = document.querySelector(`div[chat="${chatId}"]`); let selected = div.parentNode.querySelector('[selected]'); if( selected ) selected.removeAttribute('selected'); div.setAttribute('selected',''); ajax(`get_chat.js?chat=${chatId}`); currentChatId = chatId; - history.replaceState(null,null,`?with=${email}`); + history.replaceState(null,null,`?chat=${chatId}`); clearUnread(); if(eventSource) eventSource.close();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/chat.luan Tue Mar 04 08:22:45 2025 -0700 @@ -0,0 +1,65 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local pairs = Luan.pairs or error() +local Table = require "luan:Table.luan" +local concat = Table.concat or error() +local is_empty = Table.is_empty or error() +local Http = require "luan:http/Http.luan" +local Shared = require "site:/lib/Shared.luan" +local http_push_to_users = Shared.http_push_to_users or error() +local User = require "site:/lib/User.luan" +local current_user = User.current or error() +local get_user_by_email = User.get_by_email or error() +local Utils = require "site:/lib/Utils.luan" +local to_set = Utils.to_set or error() +local Db = require "site:/lib/Db.luan" +local run_in_transaction = Db.run_in_transaction or error() +local Chat = require "site:/lib/Chat.luan" +local get_chat_by_user_ids = Chat.get_by_user_ids or error() + + +return function() + local with = Http.request.parameters.with + local user = current_user() + with = to_set(with) + if user == nil then + local url = "/login.html" + if not is_empty(with) then + local t = {} + for email in pairs(with) do + t[#t+1] = "with="..email + end + url = url.."?"..concat(t,"&") + end + Http.response.send_redirect(url) + return + end + if is_empty(with) then + Http.response.send_redirect("/") + return + end + with[user.email] = true + local ids = {} + for email in pairs(with) do + local with_user = get_user_by_email(email) or error() + local id = with_user.id + ids[#ids+1] = id + end + local need_push = false + local chat = run_in_transaction( function() + local chat = get_chat_by_user_ids(ids) + if chat == nil then + chat = Chat.new{ + user_ids = ids + } + chat.save() + need_push = true + end + return chat + end ) + if need_push then + local js = "getChats('"..chat.id.."')" + http_push_to_users( chat.user_ids, js ) + end + Http.response.send_redirect("/?chat="..chat.id) +end
--- a/src/do_login.html.luan Tue Mar 04 07:38:43 2025 -0700 +++ b/src/do_login.html.luan Tue Mar 04 08:22:45 2025 -0700 @@ -43,7 +43,7 @@ for _, email in ipairs(with) do t[#t+1] = "with="..email end - location = "/?"..concat(t,"&") + location = "/chat?"..concat(t,"&") end %> <script> location = '<%=location%>'; </script>
--- a/src/index.html.luan Tue Mar 04 07:38:43 2025 -0700 +++ b/src/index.html.luan Tue Mar 04 08:22:45 2025 -0700 @@ -1,14 +1,5 @@ local Luan = require "luan:Luan.luan" local error = Luan.error -local ipairs = Luan.ipairs or error() -local pairs = Luan.pairs or error() -local range = Luan.range or error() -local Table = require "luan:Table.luan" -local concat = Table.concat or error() -local is_empty = Table.is_empty or error() -local size = Table.size or error() -local Parsers = require "luan:Parsers.luan" -local json_string = Parsers.json_string or error() local Io = require "luan:Io.luan" local Http = require "luan:http/Http.luan" local Shared = require "site:/lib/Shared.luan" @@ -16,70 +7,26 @@ local header = Shared.header or error() local started = Shared.started or error() local chats_html = Shared.chats_html or error() -local http_push_to_users = Shared.http_push_to_users or error() local User = require "site:/lib/User.luan" local current_user = User.current or error() -local get_user_by_email = User.get_by_email or error() -local Utils = require "site:/lib/Utils.luan" -local to_set = Utils.to_set or error() -local Db = require "site:/lib/Db.luan" -local run_in_transaction = Db.run_in_transaction or error() local Chat = require "site:/lib/Chat.luan" -local get_chat_by_user_ids = Chat.get_by_user_ids or error() +local get_chat_by_id = Chat.get_by_id or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "index.html" -local function get_chat(with) - local ids = {} - for email in pairs(with) do - local with_user = get_user_by_email(email) or error() - local id = with_user.id - ids[#ids+1] = id - end - local need_push = false - local chat = run_in_transaction( function() - local chat = get_chat_by_user_ids(ids) - if chat == nil then - chat = Chat.new{ - user_ids = ids - } - chat.save() - need_push = true - end - return chat - end ) - if need_push then - local js = "getChats('"..chat.id.."')" - http_push_to_users( chat.user_ids, js ) - end - return chat -end - return function() - local with_email = Http.request.parameters.with - local with = to_set(with_email) local user = current_user() if user == nil then - local url = "/login.html" - if not is_empty(with) then - local t = {} - for email in pairs(with) do - t[#t+1] = "with="..email - end - url = url.."?"..concat(t,"&") - end - Http.response.send_redirect(url) + Http.response.send_redirect("/login.html") return end + local user_id = user.id + local chat = Http.request.parameters.chat local selected = nil - if not is_empty(with) then - with[user.email] = true - if size(with) > 1 then - selected = get_chat(with) - end + if chat ~= nil then + selected = get_chat_by_id(chat) end - local user_id = user.id Io.stdout = Http.response.text_writer() %> <!doctype html> @@ -161,8 +108,7 @@ <% if selected ~= nil then %> - let div = document.querySelector('div[chat="<%=selected.id%>"]'); - selectChat(div,<%=json_string(with_email)%>); + selectChat('<%=selected.id%>'); <% end %>
--- a/src/invite.js.luan Tue Mar 04 07:38:43 2025 -0700 +++ b/src/invite.js.luan Tue Mar 04 08:22:45 2025 -0700 @@ -13,7 +13,7 @@ Io.stdout = Http.response.text_writer() if get_user_by_email(email) ~= nil then %> - location = '?with=<%=email%>'; + location = 'chat?with=<%=email%>'; <% return end
--- a/src/lib/Shared.luan Tue Mar 04 07:38:43 2025 -0700 +++ b/src/lib/Shared.luan Tue Mar 04 08:22:45 2025 -0700 @@ -7,8 +7,6 @@ local Time = require "luan:Time.luan" local Thread = require "luan:Thread.luan" local thread_run = Thread.run or error() -local Parsers = require "luan:Parsers.luan" -local json_string = Parsers.json_string or error() local Html = require "luan:Html.luan" local html_encode = Html.encode or error() local Http = require "luan:http/Http.luan" @@ -145,11 +143,12 @@ 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 user_id = chat.other_user_id(user.id) local other_user = get_user_by_id(user_id) or error() local unread = chat.unread(user) %> - <div chat="<%=chat.id%>" onclick='selectChat(this,<%=json_string(other_user.email)%>)'> + <div chat="<%=chat_id%>" onclick="selectChat('<%=chat_id%>')"> <%= other_user.name_html() %> <span online="<%= other_user.id %>"></span> <span unread="<%=unread%>"><%=unread%></span>