Browse Source

working on list management

drop_old_mail
Stephan Richter 3 years ago
parent
commit
438707fca8
  1. 2
      pom.xml
  2. 7
      src/main/java/de/srsoftware/widerhall/data/Database.java
  3. 40
      src/main/java/de/srsoftware/widerhall/data/ListMember.java
  4. 9
      src/main/java/de/srsoftware/widerhall/data/MailingList.java
  5. 1
      src/main/java/de/srsoftware/widerhall/web/Rest.java
  6. 128
      src/main/java/de/srsoftware/widerhall/web/Web.java
  7. 26
      static/templates/js.st

2
pom.xml

@ -6,7 +6,7 @@
<groupId>org.example</groupId> <groupId>org.example</groupId>
<artifactId>Widerhall</artifactId> <artifactId>Widerhall</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0.0</version>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

7
src/main/java/de/srsoftware/widerhall/data/Database.java

@ -40,7 +40,12 @@ public class Database {
return this; return this;
} }
public Request where(String key, Object value) { public Request where(String key, Collection<? extends Object> values) {
for (var val : values) where(key,val);
return this;
}
private Request where(String key, Object value) {
var list = where.get(key); var list = where.get(key);
if (list == null) where.put(key,list = new ArrayList<>()); if (list == null) where.put(key,list = new ArrayList<>());
list.add(value); list.add(value);

40
src/main/java/de/srsoftware/widerhall/data/ListMember.java

@ -1,12 +1,20 @@
package de.srsoftware.widerhall.data; package de.srsoftware.widerhall.data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException; 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.*;
import static de.srsoftware.widerhall.Constants.VARCHAR; import static de.srsoftware.widerhall.Constants.STATE;
public class ListMember { public class ListMember {
private static final Logger LOG = LoggerFactory.getLogger(ListMember.class);
public static final String TABLE_NAME = "ListMembers"; 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 LIST_EMAIL = "list_email";
private static final String USER_EMAIL = "user_email"; private static final String USER_EMAIL = "user_email";
private static final String STATE = "state"; private static final String STATE = "state";
@ -20,6 +28,10 @@ public class ListMember {
this.state = state; 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 { public static void createTable() throws SQLException {
var sql = new StringBuilder() var sql = new StringBuilder()
.append("CREATE TABLE ").append(TABLE_NAME) .append("CREATE TABLE ").append(TABLE_NAME)
@ -30,4 +42,28 @@ public class ListMember {
.append("PRIMARY KEY (").append(LIST_EMAIL).append(", ").append(USER_EMAIL).append("));"); .append("PRIMARY KEY (").append(LIST_EMAIL).append(", ").append(USER_EMAIL).append("));");
Database.open().query(sql.toString()).run(); Database.open().query(sql.toString()).run();
} }
public static List<String> listsOf(User user) {
var list = new ArrayList<String>();
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;
}
} }

9
src/main/java/de/srsoftware/widerhall/data/MailingList.java

@ -65,9 +65,16 @@ public class MailingList {
} }
public static List<MailingList> listsOf(User user) { public static List<MailingList> listsOf(User user) {
List<String> keys = (user.is(ADMIN)) ? null : ListMember.listsOf(user);
var list = new ArrayList<MailingList>(); var list = new ArrayList<MailingList>();
try { 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()){ while (rs.next()){
var email = rs.getString(EMAIL); var email = rs.getString(EMAIL);
var name = rs.getString(NAME); var name = rs.getString(NAME);

1
src/main/java/de/srsoftware/widerhall/web/Rest.java

@ -48,6 +48,7 @@ public class Rest extends HttpServlet {
json.put(ERROR,"Not logged in!"); json.put(ERROR,"Not logged in!");
} }
try { try {
resp.setContentType("application/json");
resp.getWriter().println(json.toJSONString()); resp.getWriter().println(json.toJSONString());
return null; return null;
} catch (IOException e) { } catch (IOException e) {

128
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.Configuration;
import de.srsoftware.widerhall.Util; import de.srsoftware.widerhall.Util;
import de.srsoftware.widerhall.data.ListMember;
import de.srsoftware.widerhall.data.MailingList; import de.srsoftware.widerhall.data.MailingList;
import de.srsoftware.widerhall.data.User; import de.srsoftware.widerhall.data.User;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -49,72 +50,76 @@ public class Web extends HttpServlet {
private String addList(HttpServletRequest req, HttpServletResponse resp) { private String addList(HttpServletRequest req, HttpServletResponse resp) {
var user = req.getSession().getAttribute(USER); var o = req.getSession().getAttribute(USER);
if (user == null) return redirectTo(LOGIN,resp); if (o instanceof User user){
var data = new HashMap<String,Object>(); var data = new HashMap<String,Object>();
data.put(USER,user); data.put(USER,user);
var name = req.getParameter(NAME); var name = req.getParameter(NAME);
data.put(NAME,name); data.put(NAME,name);
var email = req.getParameter(EMAIL); var email = req.getParameter(EMAIL);
data.put(EMAIL,email); data.put(EMAIL,email);
var imapHost = req.getParameter(IMAP_HOST); var imapHost = req.getParameter(IMAP_HOST);
data.put(IMAP_HOST,imapHost); data.put(IMAP_HOST,imapHost);
var imapUser = req.getParameter(IMAP_USER); var imapUser = req.getParameter(IMAP_USER);
data.put(IMAP_USER,imapUser); data.put(IMAP_USER,imapUser);
var imapPass = req.getParameter(IMAP_PASS); var imapPass = req.getParameter(IMAP_PASS);
var smtpHost = req.getParameter(SMTP_HOST); var smtpHost = req.getParameter(SMTP_HOST);
data.put(SMTP_HOST,smtpHost); data.put(SMTP_HOST,smtpHost);
var smtpUser = req.getParameter(SMTP_USER); var smtpUser = req.getParameter(SMTP_USER);
data.put(SMTP_USER,smtpUser); data.put(SMTP_USER,smtpUser);
var smtpPass = req.getParameter(SMTP_PASS); var smtpPass = req.getParameter(SMTP_PASS);
Integer imapPort = 993; Integer imapPort = 993;
data.put(IMAP_PORT,imapPort); 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);
}
if (imapHost == null || imapHost.isBlank() || imapUser == null || imapUser.isBlank() || imapPass == null || imapPass.isBlank()) { Integer smtpPort = 465;
data.put(ERROR,"IMAP credentials are required!"); data.put(SMTP_PORT,smtpPort);
return loadTemplate(ADD_LIST,data,resp);
}
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 { if (imapHost == null || imapHost.isBlank() || imapUser == null || imapUser.isBlank() || imapPass == null || imapPass.isBlank()) {
imapPort = Integer.parseInt(req.getParameter(IMAP_PORT)); data.put(ERROR,"IMAP credentials are required!");
data.put(IMAP_PORT,imapPort); return loadTemplate(ADD_LIST,data,resp);
} catch (NumberFormatException nfe){ }
data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(IMAP_PORT)));
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 { try {
smtpPort = Integer.parseInt(req.getParameter(SMTP_PORT)); imapPort = Integer.parseInt(req.getParameter(IMAP_PORT));
data.put(SMTP_PORT,smtpPort); data.put(IMAP_PORT,imapPort);
} catch (NumberFormatException nfe){ } catch (NumberFormatException nfe){
data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(SMTP_PORT))); data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(IMAP_PORT)));
return loadTemplate(ADD_LIST,data,resp); return loadTemplate(ADD_LIST,data,resp);
} }
try { if (smtpHost == null || smtpHost.isBlank() || smtpUser == null || smtpUser.isBlank() || smtpPass == null || smtpPass.isBlank()) {
MailingList.create(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass); data.put(ERROR,"SMTP credentials are required!");
return redirectTo(INDEX,resp); return loadTemplate(ADD_LIST,data,resp);
} catch (SQLException e) { }
return t("Failed to create list '{}': {}",name,e.getMessage());
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) { private String handleGet(HttpServletRequest req, HttpServletResponse resp) {
var path = req.getPathInfo(); var path = req.getPathInfo();
path = path == null ? INDEX : path.substring(1); path = (path == null || path.equals("/")) ? INDEX : path.substring(1);
String notes = null; String notes = null;
switch (path){ switch (path){
case "reload": case "reload":
@ -141,7 +146,9 @@ public class Web extends HttpServlet {
notes = t("Templates have been reloaded"); notes = t("Templates have been reloaded");
break; break;
case "css": case "css":
return loadTemplate(path,null,resp);
case "js": case "js":
resp.setContentType("text/javascript");
return loadTemplate(path,null,resp); return loadTemplate(path,null,resp);
case LOGIN: case LOGIN:
try { try {
@ -154,6 +161,7 @@ public class Web extends HttpServlet {
req.getSession().invalidate(); req.getSession().invalidate();
return redirectTo(INDEX,resp); return redirectTo(INDEX,resp);
case "jquery": case "jquery":
resp.setContentType("text/javascript");
return loadFile("jquery-3.6.0.min.js",resp); return loadFile("jquery-3.6.0.min.js",resp);
} }

26
static/templates/js.st

@ -1,6 +1,15 @@
function dropList(email){ function disableList(listEmail){
console.log('dopList('+email+')'); console.log('disableList('+listEmail+')');
} }
function dropList(listEmail){
console.log('dopList('+listEmail+')');
}
function enableList(listEmail){
console.log('enableList('+listEmail+')');
}
function loadListList(){ function loadListList(){
$.getJSON("/api/list/list", showListList); $.getJSON("/api/list/list", showListList);
} }
@ -14,10 +23,18 @@ function showListList(data){
let list = data.lists[i]; let list = data.lists[i];
let row = $('<tr/>'); let row = $('<tr/>');
let drop = $('<button/>').text('drop list').click(() => dropList(list.email)) let drop = $('<button/>').text('drop list').click(() => dropList(list.email))
let start = $('<button/>').text('enable').click(() => enableList(list.email))
let stop = $('<button/>').text('disable').click(() => disableList(list.email))
let actions = $('<td/>');
$('<td/>').text(list.name).appendTo(row); $('<td/>').text(list.name).appendTo(row);
$('<td/>').text(list.email).appendTo(row); $('<td/>').text(list.email).appendTo(row);
$('<td/>').text(list.state).appendTo(row); $('<td/>').text(list.state).appendTo(row);
drop.appendTo($('<td/>')).appendTo(row);
start.appendTo(actions);
stop.appendTo(actions);
drop.appendTo(actions);
actions.appendTo(row);
$('<td/>').text(list.imap_host).appendTo(row); $('<td/>').text(list.imap_host).appendTo(row);
$('<td/>').text(list.imap_port).appendTo(row); $('<td/>').text(list.imap_port).appendTo(row);
$('<td/>').text(list.imap_user).appendTo(row); $('<td/>').text(list.imap_user).appendTo(row);
@ -53,4 +70,7 @@ function start(){
console.log("application started"); console.log("application started");
} }
$(start); // document.on ready $(start); // document.on ready
Loading…
Cancel
Save