From 853926e0f76ee2ce40a695176a4e3baa5065c991 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sat, 16 Apr 2022 12:29:49 +0200 Subject: [PATCH] working on list submission --- .../widerhall/data/MailingList.java | 7 ++++ .../java/de/srsoftware/widerhall/web/Web.java | 39 +++++++++++++++++-- static/templates/js.st | 2 +- static/templates/subscribe.st | 34 ++++++++++++++++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 static/templates/subscribe.st diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index bdb5493..89966bb 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -123,6 +123,10 @@ public class MailingList { return list; } + public static boolean isOpen(String list) { + return openLists().stream().filter(ml -> ml.email.equals(list)).count() > 0; + } + public Map safeMap() { var map = new HashMap(); String[] parts = email.split("@", 2); @@ -163,4 +167,7 @@ public class MailingList { return this; } + public String email() { + return email; + } } diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index ef3509d..421e0bf 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -30,6 +30,7 @@ public class Web extends HttpServlet { private static final String LOGIN = "login"; private static final String LOGOUT = "logout"; private static final String REGISTER = "register"; + private static final String SUBSCRIBE = "subscribe"; private static final String RELOAD = "reload"; private static final String IMAP_HOST = "imap_host"; private static final String IMAP_PORT = "imap_port"; @@ -153,14 +154,22 @@ public class Web extends HttpServlet { case "css": case INDEX: return loadTemplate(path,data,resp); + case SUBSCRIBE: + var list = req.getParameter(LIST); + // TODO check permission + if (MailingList.isOpen(list)) { + data.put(LIST, list); + return loadTemplate(path, data, resp); + } + return t("You are not allowed to subscribe to '{}'!",list); case "js": resp.setContentType("text/javascript"); - return loadTemplate(path,null,resp); + return loadTemplate(path,data,resp); case LOGIN: try { if (User.noUsers()) return loadTemplate(REGISTER, Map.of(NOTES,t("User database is empty. Create admin user first:")), resp); return loadTemplate(path,null,resp); - } catch (SQLException throwables) { + } catch (SQLException e) { return "Error reading user database!"; } case LOGOUT: @@ -204,12 +213,14 @@ public class Web extends HttpServlet { var path = req.getPathInfo(); path = path == null ? INDEX : path.substring(1); switch (path){ + case ADD_LIST: + return addList(req,resp); case LOGIN: return handleLogin(req,resp); case REGISTER: return registerUser(req,resp); - case ADD_LIST: - return addList(req,resp); + case SUBSCRIBE: + return subscribe(req,resp); } return t("No handler for path {}!",path); @@ -293,5 +304,25 @@ public class Web extends HttpServlet { } } + private String subscribe(HttpServletRequest req, HttpServletResponse resp) { + var name = req.getParameter(NAME); + var email = req.getParameter(EMAIL); + var pass = req.getParameter(PASSWORD); + var list = req.getParameter(LIST); + var data = new HashMap(); + data.put(NAME,name); + data.put(EMAIL,email); + data.put(PASSWORD,pass); + data.put(LIST,list); + if (list == null || list.isBlank()){ + data.put(ERROR,"No list provided by form data!"); + return loadTemplate(SUBSCRIBE,data,resp); + } + if (name == null || name.isBlank() || email == null || email.isBlank()){ + data.put(ERROR,"Name and email are required fields for list subscription!"); + return loadTemplate(SUBSCRIBE,data,resp); + } + return "not implemented"; + } } diff --git a/static/templates/js.st b/static/templates/js.st index 77985d3..6d11308 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -125,7 +125,7 @@ function start(){ } function subscribeTo(domain,prefix){ - window.location.href='subscribe/'+prefix+'@'+domain; + window.location.href='subscribe?list='+prefix+'@'+domain; } diff --git a/static/templates/subscribe.st b/static/templates/subscribe.st new file mode 100644 index 0000000..b36c59c --- /dev/null +++ b/static/templates/subscribe.st @@ -0,0 +1,34 @@ + + + + + + + + + + «navigation()» + «userinfo()» + «messages()» +

Widerhall Subscription

+
+ +
+ Suscribe to "«data.list»" + + + + +
+
+ + \ No newline at end of file