| 
59
 | 
     1 local Luan = require "luan:Luan.luan"
 | 
| 
 | 
     2 local error = Luan.error
 | 
| 
 | 
     3 local pairs = Luan.pairs or error()
 | 
| 
 | 
     4 local Table = require "luan:Table.luan"
 | 
| 
 | 
     5 local concat = Table.concat or error()
 | 
| 
 | 
     6 local is_empty = Table.is_empty or error()
 | 
| 
83
 | 
     7 local Html = require "luan:Html.luan"
 | 
| 
 | 
     8 local url_encode = Html.url_encode or error()
 | 
| 
59
 | 
     9 local Http = require "luan:http/Http.luan"
 | 
| 
 | 
    10 local Shared = require "site:/lib/Shared.luan"
 | 
| 
 | 
    11 local http_push_to_users = Shared.http_push_to_users or error()
 | 
| 
 | 
    12 local User = require "site:/lib/User.luan"
 | 
| 
 | 
    13 local current_user = User.current or error()
 | 
| 
 | 
    14 local get_user_by_email = User.get_by_email or error()
 | 
| 
 | 
    15 local Utils = require "site:/lib/Utils.luan"
 | 
| 
 | 
    16 local to_set = Utils.to_set or error()
 | 
| 
 | 
    17 local Db = require "site:/lib/Db.luan"
 | 
| 
 | 
    18 local run_in_transaction = Db.run_in_transaction or error()
 | 
| 
 | 
    19 local Chat = require "site:/lib/Chat.luan"
 | 
| 
 | 
    20 local get_chat_by_user_ids = Chat.get_by_user_ids or error()
 | 
| 
 | 
    21 
 | 
| 
 | 
    22 
 | 
| 
 | 
    23 return function()
 | 
| 
 | 
    24 	local with = Http.request.parameters.with
 | 
| 
 | 
    25 	local user = current_user()
 | 
| 
 | 
    26 	with = to_set(with)
 | 
| 
 | 
    27 	if user == nil then
 | 
| 
 | 
    28 		local url = "/login.html"
 | 
| 
 | 
    29 		if not is_empty(with) then
 | 
| 
 | 
    30 			local t = {}
 | 
| 
 | 
    31 			for email in pairs(with) do
 | 
| 
83
 | 
    32 				t[#t+1] = "with="..url_encode(email)
 | 
| 
59
 | 
    33 			end
 | 
| 
 | 
    34 			url = url.."?"..concat(t,"&")
 | 
| 
 | 
    35 		end
 | 
| 
 | 
    36 		Http.response.send_redirect(url)
 | 
| 
 | 
    37 		return
 | 
| 
 | 
    38 	end
 | 
| 
 | 
    39 	if is_empty(with) then
 | 
| 
 | 
    40 		Http.response.send_redirect("/")
 | 
| 
 | 
    41 		return
 | 
| 
 | 
    42 	end
 | 
| 
 | 
    43 	with[user.email] = true
 | 
| 
 | 
    44 	local ids = {}
 | 
| 
 | 
    45 	for email in pairs(with) do
 | 
| 
 | 
    46 		local with_user = get_user_by_email(email) or error()
 | 
| 
 | 
    47 		local id = with_user.id
 | 
| 
 | 
    48 		ids[#ids+1] = id
 | 
| 
 | 
    49 	end
 | 
| 
 | 
    50 	local need_push = false
 | 
| 
 | 
    51 	local chat = run_in_transaction( function()
 | 
| 
 | 
    52 		local chat = get_chat_by_user_ids(ids)
 | 
| 
 | 
    53 		if chat == nil then
 | 
| 
 | 
    54 			chat = Chat.new{
 | 
| 
 | 
    55 				user_ids = ids
 | 
| 
 | 
    56 			}
 | 
| 
 | 
    57 			chat.save()
 | 
| 
 | 
    58 			need_push = true
 | 
| 
 | 
    59 		end
 | 
| 
 | 
    60 		return chat
 | 
| 
 | 
    61 	end )
 | 
| 
 | 
    62 	if need_push then
 | 
| 
 | 
    63 		local js = "getChats('"..chat.id.."')"
 | 
| 
 | 
    64 		http_push_to_users( chat.user_ids, js )
 | 
| 
 | 
    65 	end
 | 
| 
 | 
    66 	Http.response.send_redirect("/?chat="..chat.id)
 | 
| 
 | 
    67 end
 |