Comparison

mod_invites_page/mod_invites_page.lua @ 5662:ebc09159b94a

mod_invites_page: load `invites_template_html` from config which support multiple languages and parse the correct query params accordingly.
author Trần H. Trung <xmpp:trần.h.trung@trung.fun>
date Tue, 29 Aug 2023 22:25:01 +0700
parent 5630:efdaffc878a9
comparison
equal deleted inserted replaced
5661:e76ec7ad941e 5662:ebc09159b94a
1 local st = require "util.stanza"; 1 local st = require "util.stanza";
2 local url_escape = require "util.http".urlencode; 2 local url_escape = require "util.http".urlencode;
3 local http_formdecode = require "net.http".formdecode;
3 4
4 local base_url = "https://"..module.host.."/"; 5 local base_url = "https://"..module.host.."/";
5 6
6 local render_html_template = require"util.interpolation".new("%b{}", st.xml_escape, { 7 local render_html_template = require"util.interpolation".new("%b{}", st.xml_escape, {
7 urlescape = url_escape; 8 urlescape = url_escape;
94 table.insert(rendered_apps, app); 95 table.insert(rendered_apps, app);
95 end 96 end
96 return rendered_apps; 97 return rendered_apps;
97 end 98 end
98 99
100 local templatePath = module:get_option_string("invites_template_html", "html");
101 local function template_get(filename, lang)
102 local template = lang and templatePath.."/"..filename.."."..lang..".html"
103 or templatePath.."/"..filename..".html";
104 return assert(module:load_resource(template):read("*a"));
105 end
106
99 function serve_invite_page(event) 107 function serve_invite_page(event)
100 local invite_page_template = assert(module:load_resource("html/invite.html")):read("*a"); 108 local query_params = event.request.url.query and http_formdecode(event.request.url.query);
101 local invalid_invite_page_template = assert(module:load_resource("html/invite_invalid.html")):read("*a"); 109 local lang = query_params and query_params.l;
110
111 local invite_page_template = template_get("invite", lang);
112 local invalid_invite_page_template = template_get("invite_invalid", lang);
102 113
103 event.response.headers["Content-Type"] = "text/html; charset=utf-8"; 114 event.response.headers["Content-Type"] = "text/html; charset=utf-8";
104 115
105 local invite = invites.get(event.request.url.query); 116 local invite = invites.get(event.request.url.query.t);
106 if not invite then 117 if not invite then
107 return render_html_template(invalid_invite_page_template, { 118 return render_html_template(invalid_invite_page_template, {
108 site_name = site_name; 119 site_name = site_name;
109 static = base_url.."/static"; 120 static = base_url.."/static";
110 }); 121 });
126 event.response.headers["Link"] = ([[<%s>; rel="alternate"]]):format(template_vars.uri); 137 event.response.headers["Link"] = ([[<%s>; rel="alternate"]]):format(template_vars.uri);
127 return invite_page; 138 return invite_page;
128 end 139 end
129 140
130 function serve_setup_page(event, app_id) 141 function serve_setup_page(event, app_id)
131 local invite_page_template = assert(module:load_resource("html/client.html")):read("*a"); 142 local query_params = event.request.url.query and http_formdecode(event.request.url.query);
132 local invalid_invite_page_template = assert(module:load_resource("html/invite_invalid.html")):read("*a"); 143 local lang = query_params and query_params.l;
144
145 local invite_page_template = template_get("client", lang);
146 local invalid_invite_page_template = template_get("invite_invalid", lang);
133 147
134 event.response.headers["Content-Type"] = "text/html; charset=utf-8"; 148 event.response.headers["Content-Type"] = "text/html; charset=utf-8";
135 149
136 local invite = invites.get(event.request.url.query); 150 local invite = invites.get(event.request.url.query.t);
137 if not invite then 151 if not invite then
138 return render_html_template(invalid_invite_page_template, { 152 return render_html_template(invalid_invite_page_template, {
139 site_name = site_name; 153 site_name = site_name;
140 static = base_url.."/static"; 154 static = base_url.."/static";
141 }); 155 });