implemented un-subscribe

This commit is contained in:
2022-04-17 11:30:02 +02:00
parent 9bd2cceb7d
commit 6d73018247
6 changed files with 222 additions and 94 deletions

View File

@@ -32,8 +32,8 @@ public class Web extends HttpServlet {
private static final String LOGOUT = "logout";
private static final String REGISTER = "register";
private static final String SUBSCRIBE = "subscribe";
private static final String UNSUBSCRIBE = "unsubscribe";
private static final String RELOAD = "reload";
private static final String INSPECT = "inspect";
private static final String IMAP_HOST = "imap_host";
private static final String IMAP_PORT = "imap_port";
private static final String IMAP_USER = "imap_user";
@@ -140,6 +140,15 @@ public class Web extends HttpServlet {
if (error != null) resp.sendError(400,error);
}
private SQLException getCausingException(SQLException sqle) {
Throwable cause = sqle.getCause();
while (cause instanceof SQLException){
sqle = (SQLException) cause;
cause = sqle.getCause();
}
return sqle;
}
private String handleGet(HttpServletRequest req, HttpServletResponse resp) {
var o = req.getSession().getAttribute("user");
User user = o instanceof User ? (User) o : null;
@@ -148,6 +157,8 @@ public class Web extends HttpServlet {
var path = req.getPathInfo();
path = (path == null || path.equals("/")) ? INDEX : path.substring(1);
String notes = null;
var list = req.getParameter(LIST);
if (list != null && !list.isBlank()) data.put(LIST,list);
switch (path){
case RELOAD:
loadTemplates();
@@ -155,9 +166,9 @@ public class Web extends HttpServlet {
path = INDEX;
case "css":
case INDEX:
case UNSUBSCRIBE:
return loadTemplate(path,data,resp);
case SUBSCRIBE:
var list = req.getParameter(LIST);
// TODO check permission
if (MailingList.isOpen(list)) {
data.put(LIST, list);
@@ -183,9 +194,8 @@ public class Web extends HttpServlet {
}
if (user != null){
var list = req.getParameter(LIST);
if (list != null) data.put(LIST,req.getParameter(LIST));
data.put(NOTES,notes);
//data.put(NOTES,notes);
return loadTemplate(path,data,resp);
}
return redirectTo(LOGIN,resp);
@@ -225,6 +235,8 @@ public class Web extends HttpServlet {
return registerUser(req,resp);
case SUBSCRIBE:
return subscribe(req,resp);
case UNSUBSCRIBE:
return unsubscribe(req,resp);
}
return t("No handler for path {}!",path);
@@ -357,12 +369,52 @@ public class Web extends HttpServlet {
}
}
private SQLException getCausingException(SQLException sqle) {
Throwable cause = sqle.getCause();
while (cause instanceof SQLException){
sqle = (SQLException) cause;
cause = sqle.getCause();
private String unsubscribe(HttpServletRequest req, HttpServletResponse resp) {
var data = new HashMap<String,Object>();
var user = getSessionUser(req);
var email = req.getParameter(EMAIL);
var pass = req.getParameter(PASSWORD);
var list = req.getParameter(LIST);
data.put(EMAIL,email);
data.put(LIST,list);
if (user != null) data.put(USER,user.safeMap());
if (list == null || list.isBlank()){
data.put(ERROR,"No list provided by form data!");
return loadTemplate(UNSUBSCRIBE,data,resp);
}
return sqle;
if (user == null) {
if (email == null || email.isBlank()) {
data.put(ERROR, "Email is required for list un-subscription!");
return loadTemplate(UNSUBSCRIBE, data, resp);
}
if (pass != null && pass.isBlank()) pass = null;
try {
user = User.load(email,pass);
req.getSession().setAttribute(USER,user);
data.put(USER,user.safeMap());
} catch (InvalidKeyException | SQLException e) {
data.put(ERROR,"Invalid email/password combination!");
return loadTemplate(UNSUBSCRIBE,data,resp);
}
}
// if we get here, we should have a valid user
try {
ListMember.unsubscribe(list,user);
data.put(NOTES,t("Sucessfully un-subscribed from '{}'.",list));
return loadTemplate(INDEX,data,resp);
} catch (SQLException e) {
LOG.warn("Problem during unscubsription of {} from {}:",user.email(),list,e);
data.put(ERROR,"Failed to unsubscribe!");
return loadTemplate(UNSUBSCRIBE,data,resp);
}
}
private User getSessionUser(HttpServletRequest req) {
return req.getSession().getAttribute(USER) instanceof User user ? user : null;
}
}