Mercurial Hosting > chat
diff src/add_to_chat.js.luan @ 83:a47036fd0158
group chat
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 11 Mar 2025 13:56:59 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/add_to_chat.js.luan Tue Mar 11 13:56:59 2025 -0600 @@ -0,0 +1,44 @@ +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 Html = require "luan:Html.luan" +local url_encode = Html.url_encode or error() +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" +local User = require "site:/lib/User.luan" +local get_user_by_email = User.get_by_email or error() +local get_user_by_id = User.get_by_id or error() +local current_user = User.current or error() +local Chat = require "site:/lib/Chat.luan" +local get_chat_by_id = Chat.get_by_id or error() +local Utils = require "site:/lib/Utils.luan" +local list_to_set = Utils.list_to_set or error() + + +return function() + local email = Http.request.parameters.email or error() + local add_user = get_user_by_email(email) + Io.stdout = Http.response.text_writer() + if add_user == nil then +%> + addToChatError('email not found'); +<% + return + end + local user = current_user() or error() + local chat = Http.request.parameters.chat or error() + chat = get_chat_by_id(chat) or error() + local set = list_to_set(chat.user_ids) + set[user.id] or error() + set[add_user.id] = true + local t = {} + for id in pairs(set) do + local user = get_user_by_id(id) + t[#t+1] = "with="..url_encode(user.email) + end +%> + location = 'chat?<%= concat(t,"&") %>'; +<% +end