annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local pairs = Luan.pairs or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Table = require "luan:Table.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local concat = Table.concat or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Html = require "luan:Html.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local url_encode = Html.url_encode or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Io = require "luan:Io.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Http = require "luan:http/Http.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local User = require "site:/lib/User.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local get_user_by_email = User.get_by_email or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local get_user_by_id = User.get_by_id or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local current_user = User.current or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local Chat = require "site:/lib/Chat.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local get_chat_by_id = Chat.get_by_id or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local Utils = require "site:/lib/Utils.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local list_to_set = Utils.list_to_set or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 return function()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 local email = Http.request.parameters.email or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 local add_user = get_user_by_email(email)
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 Io.stdout = Http.response.text_writer()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 if add_user == nil then
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 %>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 addToChatError('email not found');
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 <%
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 return
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 end
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 local user = current_user() or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 local chat = Http.request.parameters.chat or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 chat = get_chat_by_id(chat) or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 local set = list_to_set(chat.user_ids)
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 set[user.id] or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 set[add_user.id] = true
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 local t = {}
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 for id in pairs(set) do
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 local user = get_user_by_id(id)
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 t[#t+1] = "with="..url_encode(user.email)
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 end
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 %>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 location = 'chat?<%= concat(t,"&") %>';
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 <%
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 end