Mercurial Hosting > chat
comparison src/index.html.luan @ 59:8270106644db
add chat.luan
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 04 Mar 2025 08:22:45 -0700 |
| parents | 7b6691bd65c3 |
| children | 3521166513b3 |
comparison
equal
deleted
inserted
replaced
| 58:7b6691bd65c3 | 59:8270106644db |
|---|---|
| 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() | |
| 10 local Parsers = require "luan:Parsers.luan" | |
| 11 local json_string = Parsers.json_string or error() | |
| 12 local Io = require "luan:Io.luan" | 3 local Io = require "luan:Io.luan" |
| 13 local Http = require "luan:http/Http.luan" | 4 local Http = require "luan:http/Http.luan" |
| 14 local Shared = require "site:/lib/Shared.luan" | 5 local Shared = require "site:/lib/Shared.luan" |
| 15 local head = Shared.head or error() | 6 local head = Shared.head or error() |
| 16 local header = Shared.header or error() | 7 local header = Shared.header or error() |
| 17 local started = Shared.started or error() | 8 local started = Shared.started or error() |
| 18 local chats_html = Shared.chats_html or error() | 9 local chats_html = Shared.chats_html or error() |
| 19 local http_push_to_users = Shared.http_push_to_users or error() | |
| 20 local User = require "site:/lib/User.luan" | 10 local User = require "site:/lib/User.luan" |
| 21 local current_user = User.current or error() | 11 local current_user = User.current or error() |
| 22 local get_user_by_email = User.get_by_email or error() | |
| 23 local Utils = require "site:/lib/Utils.luan" | |
| 24 local to_set = Utils.to_set or error() | |
| 25 local Db = require "site:/lib/Db.luan" | |
| 26 local run_in_transaction = Db.run_in_transaction or error() | |
| 27 local Chat = require "site:/lib/Chat.luan" | 12 local Chat = require "site:/lib/Chat.luan" |
| 28 local get_chat_by_user_ids = Chat.get_by_user_ids or error() | 13 local get_chat_by_id = Chat.get_by_id or error() |
| 29 local Logging = require "luan:logging/Logging.luan" | 14 local Logging = require "luan:logging/Logging.luan" |
| 30 local logger = Logging.logger "index.html" | 15 local logger = Logging.logger "index.html" |
| 31 | 16 |
| 32 | 17 |
| 33 local function get_chat(with) | |
| 34 local ids = {} | |
| 35 for email in pairs(with) do | |
| 36 local with_user = get_user_by_email(email) or error() | |
| 37 local id = with_user.id | |
| 38 ids[#ids+1] = id | |
| 39 end | |
| 40 local need_push = false | |
| 41 local chat = run_in_transaction( function() | |
| 42 local chat = get_chat_by_user_ids(ids) | |
| 43 if chat == nil then | |
| 44 chat = Chat.new{ | |
| 45 user_ids = ids | |
| 46 } | |
| 47 chat.save() | |
| 48 need_push = true | |
| 49 end | |
| 50 return chat | |
| 51 end ) | |
| 52 if need_push then | |
| 53 local js = "getChats('"..chat.id.."')" | |
| 54 http_push_to_users( chat.user_ids, js ) | |
| 55 end | |
| 56 return chat | |
| 57 end | |
| 58 | |
| 59 return function() | 18 return function() |
| 60 local with_email = Http.request.parameters.with | |
| 61 local with = to_set(with_email) | |
| 62 local user = current_user() | 19 local user = current_user() |
| 63 if user == nil then | 20 if user == nil then |
| 64 local url = "/login.html" | 21 Http.response.send_redirect("/login.html") |
| 65 if not is_empty(with) then | |
| 66 local t = {} | |
| 67 for email in pairs(with) do | |
| 68 t[#t+1] = "with="..email | |
| 69 end | |
| 70 url = url.."?"..concat(t,"&") | |
| 71 end | |
| 72 Http.response.send_redirect(url) | |
| 73 return | 22 return |
| 74 end | 23 end |
| 24 local user_id = user.id | |
| 25 local chat = Http.request.parameters.chat | |
| 75 local selected = nil | 26 local selected = nil |
| 76 if not is_empty(with) then | 27 if chat ~= nil then |
| 77 with[user.email] = true | 28 selected = get_chat_by_id(chat) |
| 78 if size(with) > 1 then | |
| 79 selected = get_chat(with) | |
| 80 end | |
| 81 end | 29 end |
| 82 local user_id = user.id | |
| 83 Io.stdout = Http.response.text_writer() | 30 Io.stdout = Http.response.text_writer() |
| 84 %> | 31 %> |
| 85 <!doctype html> | 32 <!doctype html> |
| 86 <html> | 33 <html> |
| 87 <head> | 34 <head> |
| 159 'use strict'; | 106 'use strict'; |
| 160 | 107 |
| 161 <% | 108 <% |
| 162 if selected ~= nil then | 109 if selected ~= nil then |
| 163 %> | 110 %> |
| 164 let div = document.querySelector('div[chat="<%=selected.id%>"]'); | 111 selectChat('<%=selected.id%>'); |
| 165 selectChat(div,<%=json_string(with_email)%>); | |
| 166 <% | 112 <% |
| 167 end | 113 end |
| 168 %> | 114 %> |
| 169 setUserEventSource(<%=user_id%>); | 115 setUserEventSource(<%=user_id%>); |
| 170 lastUpdate = <%=user.last_update()%>; | 116 lastUpdate = <%=user.last_update()%>; |
