Mercurial Hosting > chat
comparison src/index.html.luan @ 21:5a56297713a3
move chat to home
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 05 Nov 2024 12:57:41 -0700 |
| parents | src/chat.html.luan@dade6a560494 |
| children | c54c806fcc6e |
comparison
equal
deleted
inserted
replaced
| 20:dade6a560494 | 21:5a56297713a3 |
|---|---|
| 1 local Luan = require "luan:Luan.luan" | 1 local Luan = require "luan:Luan.luan" |
| 2 local error = Luan.error | 2 local error = Luan.error |
| 3 local ipairs = Luan.ipairs or error() | |
| 4 local pairs = Luan.pairs or error() | |
| 5 local range = Luan.range or error() | |
| 6 local Table = require "luan:Table.luan" | |
| 7 local concat = Table.concat or error() | |
| 8 local is_empty = Table.is_empty or error() | |
| 9 local size = Table.size or error() | |
| 3 local Io = require "luan:Io.luan" | 10 local Io = require "luan:Io.luan" |
| 4 local Http = require "luan:http/Http.luan" | 11 local Http = require "luan:http/Http.luan" |
| 5 local Shared = require "site:/lib/Shared.luan" | 12 local Shared = require "site:/lib/Shared.luan" |
| 6 local head = Shared.head or error() | 13 local head = Shared.head or error() |
| 7 local header = Shared.header or error() | 14 local header = Shared.header or error() |
| 15 local started = Shared.started or error() | |
| 16 local chats_html = Shared.chats_html or error() | |
| 17 local http_push_to_users = Shared.http_push_to_users or error() | |
| 18 local User = require "site:/lib/User.luan" | |
| 19 local current_user = User.current or error() | |
| 20 local get_user_by_email = User.get_by_email or error() | |
| 21 local Utils = require "site:/lib/Utils.luan" | |
| 22 local to_set = Utils.to_set or error() | |
| 23 local Db = require "site:/lib/Db.luan" | |
| 24 local run_in_transaction = Db.run_in_transaction or error() | |
| 25 local Chat = require "site:/lib/Chat.luan" | |
| 26 local chat_search = Chat.search or error() | |
| 27 local Logging = require "luan:logging/Logging.luan" | |
| 28 local logger = Logging.logger "index.html" | |
| 8 | 29 |
| 9 | 30 |
| 31 local function get_chat(with) | |
| 32 local t = {} | |
| 33 local ids = {} | |
| 34 for email in pairs(with) do | |
| 35 local with_user = get_user_by_email(email) or error() | |
| 36 local id = with_user.id | |
| 37 t[#t+1] = "+chat_user_ids:"..id | |
| 38 ids[#ids+1] = id | |
| 39 end | |
| 40 local query = concat(t," ") | |
| 41 local need_push = false | |
| 42 local chat = run_in_transaction( function() | |
| 43 local chats = chat_search(query) | |
| 44 local n = #chats | |
| 45 if n == 0 then | |
| 46 local chat = Chat.new{ | |
| 47 user_ids = ids | |
| 48 } | |
| 49 chat.save() | |
| 50 need_push = true | |
| 51 return chat | |
| 52 elseif n == 1 then | |
| 53 return chats[1] | |
| 54 else | |
| 55 error("multiple chats for: "..query) | |
| 56 end | |
| 57 end ) | |
| 58 if need_push then | |
| 59 local js = "getChats('"..chat.id.."')" | |
| 60 http_push_to_users( chat.user_ids, js ) | |
| 61 end | |
| 62 return chat | |
| 63 end | |
| 64 | |
| 10 return function() | 65 return function() |
| 66 local with = Http.request.parameters.with | |
| 67 with = to_set(with) | |
| 68 local user = current_user() | |
| 69 if user == nil then | |
| 70 local url = "/login.html" | |
| 71 if not is_empty(with) then | |
| 72 local t = {} | |
| 73 for email in pairs(with) do | |
| 74 t[#t+1] = "with="..email | |
| 75 end | |
| 76 url = url.."?"..concat(t,"&") | |
| 77 end | |
| 78 Http.response.send_redirect(url) | |
| 79 return | |
| 80 end | |
| 81 local selected = nil | |
| 82 if not is_empty(with) then | |
| 83 with[user.email] = true | |
| 84 if size(with) > 1 then | |
| 85 selected = get_chat(with) | |
| 86 end | |
| 87 end | |
| 11 Io.stdout = Http.response.text_writer() | 88 Io.stdout = Http.response.text_writer() |
| 12 %> | 89 %> |
| 13 <!doctype html> | 90 <!doctype html> |
| 14 <html> | 91 <html> |
| 15 <head> | 92 <head> |
| 16 <% head() %> | 93 <% head() %> |
| 17 <style> | 94 <style> |
| 18 h1 { | 95 @import "chat.css?s=<%=started%>"; |
| 19 margin-bottom: 0; | |
| 20 } | |
| 21 h3 { | |
| 22 margin-top: 8px; | |
| 23 } | |
| 24 </style> | 96 </style> |
| 97 <script src="chat.js?s=<%=started%>"></script> | |
| 25 </head> | 98 </head> |
| 26 <body> | 99 <body> |
| 27 <% header() %> | 100 <% header() %> |
| 28 <div content> | 101 <div content> |
| 29 <h1>Web Chat</h1> | 102 <div chats> |
| 30 <h3>A free web-based instant messaging service</h3> | 103 <% chats_html() %> |
| 31 <p><a href="https://hg.reactionary.software/repo/chat/">source code</a></p> | 104 </div> |
| 105 <div posts></div> | |
| 32 </div> | 106 </div> |
| 107 <dialog delete_chat> | |
| 108 <h2>Delete Chat</h2> | |
| 109 <p>Are you sure that you want to delete this chat?</p> | |
| 110 <div buttons> | |
| 111 <button cancel onclick="closeModal(this)">Cancel</button> | |
| 112 <button go onclick="doDeleteChat(this)">Delete</button> | |
| 113 </div> | |
| 114 </dialog> | |
| 115 <script> | |
| 116 <% | |
| 117 if selected ~= nil then | |
| 118 %> | |
| 119 let div = document.querySelector('div[chat="<%=selected.id%>"]'); | |
| 120 selectChat(div); | |
| 121 <% | |
| 122 end | |
| 123 %> | |
| 124 setUserEventSource(<%=user.id%>); | |
| 125 </script> | |
| 33 </body> | 126 </body> |
| 34 </html> | 127 </html> |
| 35 <% | 128 <% |
| 36 end | 129 end |
