Comparison

mod_welcome_page/mod_welcome_page.lua @ 4184:9127fa98ee1e

mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
author Matthew Wild <mwild1@gmail.com>
date Fri, 09 Oct 2020 12:19:46 +0100
child 4193:8d1e996034ee
comparison
equal deleted inserted replaced
4183:ad9ce6750880 4184:9127fa98ee1e
1 local st = require "util.stanza";
2 local url_escape = require "util.http".urlencode;
3 local render_html_template = require"util.interpolation".new("%b{}", st.xml_escape, {
4 urlescape = url_escape;
5 });
6
7 local template_path = module:get_option_string("welcome_page_template_path", module:get_directory().."/html");
8 local user_vars = module:get_option("welcome_page_variables", {});
9 local site_name = module:get_option("site_name", module.host);
10 local invite_only = module:get_option_boolean("registration_invite_only", true);
11 local open_registration = module:get_option_boolean("welcome_page_open_registration", not invite_only);
12
13 module:depends("http");
14 local invites = module:depends("invites");
15
16 local function load_template(path)
17 local template_file, err = io.open(path);
18 if not template_file then
19 error("Unable to load template file: "..tostring(err));
20 end
21 local template = template_file:read("*a");
22 template_file:close();
23 return template;
24 end
25
26 local template = load_template(template_path.."/index.html");
27
28 local function serve_page(event)
29 event.response.headers["Content-Type"] = "text/html; charset=utf-8";
30 return render_html_template(template, {
31 site_name = site_name;
32 request = event.request;
33 var = user_vars;
34 });
35 end
36
37 local function handle_submit(event)
38 local submission = { allowed = open_registration, request = event.request };
39 module:fire_event("mod_welcome_page/submission", submission);
40 if not submission.allowed then
41 event.response.headers["Content-Type"] = "text/html; charset=utf-8";
42 return render_html_template(template, {
43 site_name = site_name;
44 request = event.request;
45 var = user_vars;
46 message = {
47 class = "alert-danger";
48 text = submission.reason or "Account creation is not possible at this time";
49 };
50 });
51 end
52
53 local invite = invites.create_account(nil, { source = module.name });
54 if not invite then
55 return 500;
56 end
57
58 event.response.headers.Location = invite.landing_page or invite.uri;
59
60 return 303;
61 end
62
63 local http_files = module:depends("http_files");
64
65 module:provides("http", {
66 route = {
67 ["GET"] = serve_page;
68 ["GET /*"] = http_files.serve({ path = template_path });
69 ["POST"] = handle_submit;
70 };
71 });