Changeset

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
parents 5661:e76ec7ad941e
children 5664:52db2da66680
files mod_invites_page/mod_invites_page.lua
diffstat 1 files changed, 20 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mod_invites_page/mod_invites_page.lua	Tue Aug 29 22:06:19 2023 +0700
+++ b/mod_invites_page/mod_invites_page.lua	Tue Aug 29 22:25:01 2023 +0700
@@ -1,5 +1,6 @@
 local st = require "util.stanza";
 local url_escape = require "util.http".urlencode;
+local http_formdecode = require "net.http".formdecode;
 
 local base_url = "https://"..module.host.."/";
 
@@ -96,13 +97,23 @@
 	return rendered_apps;
 end
 
+local templatePath = module:get_option_string("invites_template_html", "html");
+local function template_get(filename, lang)
+	local template = lang and templatePath.."/"..filename.."."..lang..".html" 
+		or templatePath.."/"..filename..".html";
+	return assert(module:load_resource(template):read("*a"));
+end
+
 function serve_invite_page(event)
-	local invite_page_template = assert(module:load_resource("html/invite.html")):read("*a");
-	local invalid_invite_page_template = assert(module:load_resource("html/invite_invalid.html")):read("*a");
+	local query_params = event.request.url.query and http_formdecode(event.request.url.query);
+	local lang = query_params and query_params.l;
+
+	local invite_page_template = template_get("invite", lang);
+	local invalid_invite_page_template = template_get("invite_invalid", lang);
 
 	event.response.headers["Content-Type"] = "text/html; charset=utf-8";
 
-	local invite = invites.get(event.request.url.query);
+	local invite = invites.get(event.request.url.query.t);
 	if not invite then
 		return render_html_template(invalid_invite_page_template, {
 			site_name = site_name;
@@ -128,12 +139,15 @@
 end
 
 function serve_setup_page(event, app_id)
-	local invite_page_template = assert(module:load_resource("html/client.html")):read("*a");
-	local invalid_invite_page_template = assert(module:load_resource("html/invite_invalid.html")):read("*a");
+	local query_params = event.request.url.query and http_formdecode(event.request.url.query);
+	local lang = query_params and query_params.l;
+
+	local invite_page_template = template_get("client", lang);
+	local invalid_invite_page_template = template_get("invite_invalid", lang);
 
 	event.response.headers["Content-Type"] = "text/html; charset=utf-8";
 
-	local invite = invites.get(event.request.url.query);
+	local invite = invites.get(event.request.url.query.t);
 	if not invite then
 		return render_html_template(invalid_invite_page_template, {
 			site_name = site_name;