view src/add_to_chat.js.luan @ 93:d0566cc4a2ac default tip

uploa fix
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Apr 2025 19:47:48 -0600
parents a47036fd0158
children
line wrap: on
line source

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