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

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

@ -40,7 +40,12 @@ public class Database { @@ -40,7 +40,12 @@ public class Database {
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);
if (list == null) where.put(key,list = new ArrayList<>());
list.add(value);

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

@ -1,12 +1,20 @@ @@ -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 { @@ -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 { @@ -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<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 { @@ -65,9 +65,16 @@ public class MailingList {
}
public static List<MailingList> listsOf(User user) {
List<String> keys = (user.is(ADMIN)) ? null : ListMember.listsOf(user);
var list = new ArrayList<MailingList>();
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);

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

@ -48,6 +48,7 @@ public class Rest extends HttpServlet { @@ -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) {

128
src/main/java/de/srsoftware/widerhall/web/Web.java

@ -2,6 +2,7 @@ package de.srsoftware.widerhall.web; @@ -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 { @@ -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<String,Object>();
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<String,Object>();
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 { @@ -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 { @@ -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 { @@ -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);
}

26
static/templates/js.st

@ -1,6 +1,15 @@ @@ -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){ @@ -14,10 +23,18 @@ function showListList(data){
let list = data.lists[i];
let row = $('<tr/>');
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.email).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_port).appendTo(row);
$('<td/>').text(list.imap_user).appendTo(row);
@ -53,4 +70,7 @@ function start(){ @@ -53,4 +70,7 @@ function start(){
console.log("application started");
}
$(start); // document.on ready
Loading…
Cancel
Save