From 68f361e15bc7ac3fe4ae29ab099bd4ae21b9d988 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 20 Apr 2022 09:55:58 +0200 Subject: [PATCH] working on mailing list options --- pom.xml | 2 +- .../java/de/srsoftware/widerhall/Util.java | 4 ++ .../widerhall/data/MailingList.java | 21 ++++++++-- .../de/srsoftware/widerhall/web/Rest.java | 1 + .../java/de/srsoftware/widerhall/web/Web.java | 39 ++++++++++++++----- static/templates/inspect.st | 4 ++ static/templates/js.st | 2 + 7 files changed, 58 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index dc44257..14f8876 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example Widerhall - 0.0.17 + 0.0.18 diff --git a/src/main/java/de/srsoftware/widerhall/Util.java b/src/main/java/de/srsoftware/widerhall/Util.java index ce5b489..8dbee4e 100644 --- a/src/main/java/de/srsoftware/widerhall/Util.java +++ b/src/main/java/de/srsoftware/widerhall/Util.java @@ -114,4 +114,8 @@ public class Util { if (listEmail == null || listEmail.isBlank()) return null; return MailingList.load(listEmail); } + + public static boolean getCheckbox(HttpServletRequest req, String key) { + return "on".equals(req.getParameter(key)); + } } diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index de3cbe3..708d855 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -39,6 +39,7 @@ public class MailingList implements MessageHandler { private static final int STATE_ENABLED = 1; private static final int STATE_PUBLIC = 2; public static final int STATE_FORWARD_FROM = 4; + public static final int STATE_FORWARD_ATTACHED = 8; private static final int VISIBLE = 1; private static final int HIDDEN = 0; private final String name; @@ -131,8 +132,7 @@ public class MailingList implements MessageHandler { public void enable(boolean enable) throws SQLException { - state = enable ? state | STATE_ENABLED : state ^ (state & STATE_ENABLED); - Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run(); + setFlag(STATE_ENABLED,enable); if (enable) { imap.start().addListener(this); @@ -151,6 +151,15 @@ public class MailingList implements MessageHandler { } } + public void forwardAttached(boolean forward) throws SQLException { + setFlag(STATE_FORWARD_ATTACHED,forward); + } + + public void forwardFrom(boolean forward) throws SQLException { + setFlag(STATE_FORWARD_FROM,forward); + } + + /** * Create a mailing list object from a ResultSet. * This is a cached method: if the ML has been loaded before, the already-loaded object will be returned. @@ -182,8 +191,7 @@ public class MailingList implements MessageHandler { } public void hide(boolean hide) throws SQLException { - state = hide ? state ^ (state & STATE_PUBLIC) : state | STATE_PUBLIC; - Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run(); + setFlag(STATE_PUBLIC,!hide); } /** @@ -362,6 +370,11 @@ public class MailingList implements MessageHandler { smtp.login().send(email(),name(),user.email(),subject,text); } + private void setFlag(int flag, boolean on) throws SQLException { + state = on ? state | flag : state ^ (state & flag); + Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run(); + } + public Map stateMap(){ var map = new HashMap(); if (hasState(STATE_ENABLED)) map.put("enabled",VISIBLE); diff --git a/src/main/java/de/srsoftware/widerhall/web/Rest.java b/src/main/java/de/srsoftware/widerhall/web/Rest.java index ebf53d3..8b3997f 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Rest.java +++ b/src/main/java/de/srsoftware/widerhall/web/Rest.java @@ -219,6 +219,7 @@ public class Rest extends HttpServlet { if (list == null) return Map.of(ERROR,t("Mailinglist {} unknown",list.email())); var map = new HashMap<>(); if (list.hasState(MailingList.STATE_FORWARD_FROM)) map.put("forward_from",true); + if (list.hasState(MailingList.STATE_FORWARD_ATTACHED)) map.put("forward_attached",true); return map; } diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index f203674..17914e4 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -4,6 +4,7 @@ import de.srsoftware.widerhall.Util; import de.srsoftware.widerhall.data.ListMember; import de.srsoftware.widerhall.data.MailingList; import de.srsoftware.widerhall.data.User; +import org.json.simple.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -269,19 +270,37 @@ public class Web extends TemplateServlet { } private String inspect(HttpServletRequest req, HttpServletResponse resp) { - var o = req.getSession().getAttribute(USER); - if (!(o instanceof User user)) { - return redirectTo(LOGIN,resp); + var user = Util.getUser(req); + if (user == null) return redirectTo(LOGIN,resp); + + var data = new HashMap(); + var error = false; + + var list = Util.getMailingList(req); + if (list == null) { + error = true; + data.put(ERROR, t("No valid mailing list provided!")); + } else data.put(LIST, list.email()); + + if (!error && !list.mayBeAlteredBy(user)) { + error = true; + data.put(ERROR,t("You are not allowed to alter this list!")); } - var post = req.getParameterMap(); - var listEmail = req.getParameter(LIST); - var list = MailingList.load(listEmail); - if (list == null) return t("{} is does not denote a valid list",listEmail); - if (!list.mayBeAlteredBy(user)) { + if (!error){ + var dummy = req.getParameterMap(); + try { + list.forwardFrom(Util.getCheckbox(req, "forward_from")); + list.forwardAttached(Util.getCheckbox(req, "forward_attached")); + data.put(NOTES,t("Sucessfully updated MailingList!")); + } catch (SQLException e){ + LOG.warn("Failed to update MailingList:",e); + data.put(ERROR,t("Failed to update MailingList!")); + } + LOG.debug("params: {}",dummy); } - LOG.debug("POST: {}",post); - return null; + + return loadTemplate(INSPECT,data,resp); } diff --git a/static/templates/inspect.st b/static/templates/inspect.st index fec45c3..8089e37 100644 --- a/static/templates/inspect.st +++ b/static/templates/inspect.st @@ -19,6 +19,10 @@ Forward using original sender + diff --git a/static/templates/js.st b/static/templates/js.st index b249e12..5300ef8 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -57,6 +57,8 @@ function showList(listEmail){ function showListDetail(data){ console.log(data); + if (data.forward_from) $('input[name="forward_from"]').prop('checked',true); + if (data.forward_attached) $('input[name="forward_attached"]').prop('checked',true); } function showListOfEditableLists(data){