From 438707fca8d31dddce9ff53c57875be425f923e9 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Fri, 15 Apr 2022 18:14:08 +0200 Subject: [PATCH] working on list management --- pom.xml | 2 +- .../srsoftware/widerhall/data/Database.java | 7 +- .../srsoftware/widerhall/data/ListMember.java | 40 +++++- .../widerhall/data/MailingList.java | 9 +- .../de/srsoftware/widerhall/web/Rest.java | 1 + .../java/de/srsoftware/widerhall/web/Web.java | 128 ++++++++++-------- static/templates/js.st | 26 +++- 7 files changed, 145 insertions(+), 68 deletions(-) diff --git a/pom.xml b/pom.xml index 626e079..e9a82b6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example Widerhall - 1.0-SNAPSHOT + 1.0.0 diff --git a/src/main/java/de/srsoftware/widerhall/data/Database.java b/src/main/java/de/srsoftware/widerhall/data/Database.java index 99b4980..98b8381 100644 --- a/src/main/java/de/srsoftware/widerhall/data/Database.java +++ b/src/main/java/de/srsoftware/widerhall/data/Database.java @@ -40,7 +40,12 @@ public class Database { return this; } - public Request where(String key, Object value) { + public Request where(String key, Collection values) { + for (var val : values) where(key,val); + return this; + } + + private Request where(String key, Object value) { var list = where.get(key); if (list == null) where.put(key,list = new ArrayList<>()); list.add(value); diff --git a/src/main/java/de/srsoftware/widerhall/data/ListMember.java b/src/main/java/de/srsoftware/widerhall/data/ListMember.java index e51842f..840b6a7 100644 --- a/src/main/java/de/srsoftware/widerhall/data/ListMember.java +++ b/src/main/java/de/srsoftware/widerhall/data/ListMember.java @@ -1,12 +1,20 @@ package de.srsoftware.widerhall.data; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; -import static de.srsoftware.widerhall.Constants.INT; -import static de.srsoftware.widerhall.Constants.VARCHAR; +import static de.srsoftware.widerhall.Constants.*; +import static de.srsoftware.widerhall.Constants.STATE; public class ListMember { + private static final Logger LOG = LoggerFactory.getLogger(ListMember.class); public static final String TABLE_NAME = "ListMembers"; + public static final int STATE_OWNER = 1; private static final String LIST_EMAIL = "list_email"; private static final String USER_EMAIL = "user_email"; private static final String STATE = "state"; @@ -20,6 +28,10 @@ public class ListMember { this.state = state; } + public static ListMember create(String listEmail, String userEmail, int state) throws SQLException { + return new ListMember(listEmail,userEmail,state).save(); + } + public static void createTable() throws SQLException { var sql = new StringBuilder() .append("CREATE TABLE ").append(TABLE_NAME) @@ -30,4 +42,28 @@ public class ListMember { .append("PRIMARY KEY (").append(LIST_EMAIL).append(", ").append(USER_EMAIL).append("));"); Database.open().query(sql.toString()).run(); } + + public static List listsOf(User user) { + var list = new ArrayList(); + try { + var rs = Database.open() + .query("SELECT "+LIST_EMAIL+" FROM " + TABLE_NAME) + .where(USER_EMAIL,user.email()) + .where(STATE,STATE_OWNER) + .exec(); + while (rs.next()) list.add(rs.getString(LIST_EMAIL)); + } catch (SQLException e) { + LOG.warn("Listing memberships lists failed: ",e); + } + return list; + } + + private ListMember save() throws SQLException { + Database.open().insertInto(TABLE_NAME) + .values(Map.of(LIST_EMAIL,listEmail, + USER_EMAIL,userEmail, + STATE,state)) + .run(); + return this; + } } diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index bb3d7d6..a368612 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -65,9 +65,16 @@ public class MailingList { } public static List listsOf(User user) { + + List keys = (user.is(ADMIN)) ? null : ListMember.listsOf(user); var list = new ArrayList(); try { - var rs = Database.open().query("SELECT * FROM "+TABLE_NAME).exec(); + Database.Request q = Database.open().query("SELECT * FROM " + TABLE_NAME); + if (keys != null){ + if (keys.isEmpty()) return list; + q.where(EMAIL,keys); + } + var rs = q.exec(); while (rs.next()){ var email = rs.getString(EMAIL); var name = rs.getString(NAME); diff --git a/src/main/java/de/srsoftware/widerhall/web/Rest.java b/src/main/java/de/srsoftware/widerhall/web/Rest.java index efc1637..aa3700f 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Rest.java +++ b/src/main/java/de/srsoftware/widerhall/web/Rest.java @@ -48,6 +48,7 @@ public class Rest extends HttpServlet { json.put(ERROR,"Not logged in!"); } try { + resp.setContentType("application/json"); resp.getWriter().println(json.toJSONString()); return null; } catch (IOException e) { diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index 0f87626..3827408 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -2,6 +2,7 @@ package de.srsoftware.widerhall.web; import de.srsoftware.widerhall.Configuration; 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.slf4j.Logger; @@ -49,72 +50,76 @@ public class Web extends HttpServlet { private String addList(HttpServletRequest req, HttpServletResponse resp) { - var user = req.getSession().getAttribute(USER); - if (user == null) return redirectTo(LOGIN,resp); - var data = new HashMap(); - data.put(USER,user); - - var name = req.getParameter(NAME); - data.put(NAME,name); - - var email = req.getParameter(EMAIL); - data.put(EMAIL,email); - - var imapHost = req.getParameter(IMAP_HOST); - data.put(IMAP_HOST,imapHost); - var imapUser = req.getParameter(IMAP_USER); - data.put(IMAP_USER,imapUser); - var imapPass = req.getParameter(IMAP_PASS); - var smtpHost = req.getParameter(SMTP_HOST); - data.put(SMTP_HOST,smtpHost); - var smtpUser = req.getParameter(SMTP_USER); - data.put(SMTP_USER,smtpUser); - var smtpPass = req.getParameter(SMTP_PASS); - - Integer imapPort = 993; - data.put(IMAP_PORT,imapPort); - - Integer smtpPort = 465; - data.put(SMTP_PORT,smtpPort); - - if (name == null || name.isBlank() || email == null || email.isBlank()) { - data.put(ERROR,"List name and address are required!"); - return loadTemplate(ADD_LIST,data,resp); - } + var o = req.getSession().getAttribute(USER); + if (o instanceof User user){ + var data = new HashMap(); + data.put(USER,user); + + var name = req.getParameter(NAME); + data.put(NAME,name); + + var email = req.getParameter(EMAIL); + data.put(EMAIL,email); + + var imapHost = req.getParameter(IMAP_HOST); + data.put(IMAP_HOST,imapHost); + var imapUser = req.getParameter(IMAP_USER); + data.put(IMAP_USER,imapUser); + var imapPass = req.getParameter(IMAP_PASS); + var smtpHost = req.getParameter(SMTP_HOST); + data.put(SMTP_HOST,smtpHost); + var smtpUser = req.getParameter(SMTP_USER); + data.put(SMTP_USER,smtpUser); + var smtpPass = req.getParameter(SMTP_PASS); + + Integer imapPort = 993; + data.put(IMAP_PORT,imapPort); - if (imapHost == null || imapHost.isBlank() || imapUser == null || imapUser.isBlank() || imapPass == null || imapPass.isBlank()) { - data.put(ERROR,"IMAP credentials are required!"); - return loadTemplate(ADD_LIST,data,resp); - } + Integer smtpPort = 465; + data.put(SMTP_PORT,smtpPort); + if (name == null || name.isBlank() || email == null || email.isBlank()) { + data.put(ERROR,"List name and address are required!"); + return loadTemplate(ADD_LIST,data,resp); + } - try { - imapPort = Integer.parseInt(req.getParameter(IMAP_PORT)); - data.put(IMAP_PORT,imapPort); - } catch (NumberFormatException nfe){ - data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(IMAP_PORT))); - return loadTemplate(ADD_LIST,data,resp); - } + if (imapHost == null || imapHost.isBlank() || imapUser == null || imapUser.isBlank() || imapPass == null || imapPass.isBlank()) { + data.put(ERROR,"IMAP credentials are required!"); + return loadTemplate(ADD_LIST,data,resp); + } - if (smtpHost == null || smtpHost.isBlank() || smtpUser == null || smtpUser.isBlank() || smtpPass == null || smtpPass.isBlank()) { - data.put(ERROR,"SMTP credentials are required!"); - return loadTemplate(ADD_LIST,data,resp); - } - try { - smtpPort = Integer.parseInt(req.getParameter(SMTP_PORT)); - data.put(SMTP_PORT,smtpPort); - } catch (NumberFormatException nfe){ - data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(SMTP_PORT))); - return loadTemplate(ADD_LIST,data,resp); - } + try { + imapPort = Integer.parseInt(req.getParameter(IMAP_PORT)); + data.put(IMAP_PORT,imapPort); + } catch (NumberFormatException nfe){ + data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(IMAP_PORT))); + return loadTemplate(ADD_LIST,data,resp); + } - try { - MailingList.create(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass); - return redirectTo(INDEX,resp); - } catch (SQLException e) { - return t("Failed to create list '{}': {}",name,e.getMessage()); + if (smtpHost == null || smtpHost.isBlank() || smtpUser == null || smtpUser.isBlank() || smtpPass == null || smtpPass.isBlank()) { + data.put(ERROR,"SMTP credentials are required!"); + return loadTemplate(ADD_LIST,data,resp); + } + + try { + smtpPort = Integer.parseInt(req.getParameter(SMTP_PORT)); + data.put(SMTP_PORT,smtpPort); + } catch (NumberFormatException nfe){ + data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(SMTP_PORT))); + return loadTemplate(ADD_LIST,data,resp); + } + + try { + MailingList.create(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass); + ListMember.create(email,user.email(),ListMember.STATE_OWNER); + return redirectTo(INDEX,resp); + } catch (SQLException e) { + return t("Failed to create list '{}': {}",name,e.getMessage()); + } } + return redirectTo(LOGIN,resp); + } @@ -132,7 +137,7 @@ public class Web extends HttpServlet { private String handleGet(HttpServletRequest req, HttpServletResponse resp) { var path = req.getPathInfo(); - path = path == null ? INDEX : path.substring(1); + path = (path == null || path.equals("/")) ? INDEX : path.substring(1); String notes = null; switch (path){ case "reload": @@ -141,7 +146,9 @@ public class Web extends HttpServlet { notes = t("Templates have been reloaded"); break; case "css": + return loadTemplate(path,null,resp); case "js": + resp.setContentType("text/javascript"); return loadTemplate(path,null,resp); case LOGIN: try { @@ -154,6 +161,7 @@ public class Web extends HttpServlet { req.getSession().invalidate(); return redirectTo(INDEX,resp); case "jquery": + resp.setContentType("text/javascript"); return loadFile("jquery-3.6.0.min.js",resp); } diff --git a/static/templates/js.st b/static/templates/js.st index 27ff0c1..0218b35 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -1,6 +1,15 @@ -function dropList(email){ - console.log('dopList('+email+')'); +function disableList(listEmail){ + console.log('disableList('+listEmail+')'); } + +function dropList(listEmail){ + console.log('dopList('+listEmail+')'); +} + +function enableList(listEmail){ + console.log('enableList('+listEmail+')'); +} + function loadListList(){ $.getJSON("/api/list/list", showListList); } @@ -14,10 +23,18 @@ function showListList(data){ let list = data.lists[i]; let row = $(''); let drop = $('