annotate src/invite.js.luan @ 85:2158a5950fcd

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Mar 2025 14:13:53 -0600
parents a47036fd0158
children 7b339b1ccd11
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
56
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local Parsers = require "luan:Parsers.luan"
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local json_string = Parsers.json_string or error()
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 60
diff changeset
5 local Html = require "luan:Html.luan"
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 60
diff changeset
6 local url_encode = Html.url_encode or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 60
diff changeset
7 local Io = require "luan:Io.luan"
56
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Http = require "luan:http/Http.luan"
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local User = require "site:/lib/User.luan"
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local get_user_by_email = User.get_by_email or error()
60
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
11 local get_or_create_user_by_email = User.get_or_create_by_email or error()
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
12 local current_user = User.current or error()
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
13 local Shared = require "site:/lib/Shared.luan"
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
14 local send_mail = Shared.send_mail or error()
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
15 local Utils = require "site:/lib/Utils.luan"
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
16 local base_url = Utils.base_url or error()
56
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 return function()
60
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
20 local user = current_user() or error()
56
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 local email = Http.request.parameters.email or error()
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 Io.stdout = Http.response.text_writer()
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 if get_user_by_email(email) ~= nil then
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 %>
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 60
diff changeset
25 location = 'chat?with=<%=url_encode(email)%>';
56
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 <%
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 return
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 end
60
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
29 local invitee = get_or_create_user_by_email(email)
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
30 local password = invitee.password
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 60
diff changeset
31 local url = base_url().."/do_login.html?user="..invitee.id.."&password="..password.."&with="..url_encode(user.email)
60
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
32 local who = user.name or user.email
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
33 send_mail {
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
34 To = email
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
35 Subject = "Chat with "..who
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
36 body = `%>
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
37 <%=who%> has invited you to chat on Luan Chat.
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
38
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
39 Here is the link to login:
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
40
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
41 <%= url %>
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
42
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
43 Or login with your email and the password: <%=password%>
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
44 <% `
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
45 }
56
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 %>
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 openInvite(<%=json_string(email)%>);
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 <%
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 end