implemented hiding/unhiding of mailing lists on the database side
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package de.srsoftware.widerhall.web;
|
||||
|
||||
import de.srsoftware.widerhall.data.ListMember;
|
||||
import de.srsoftware.widerhall.data.MailingList;
|
||||
import de.srsoftware.widerhall.data.User;
|
||||
import org.json.simple.JSONObject;
|
||||
@@ -11,7 +12,9 @@ import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static de.srsoftware.widerhall.Constants.*;
|
||||
import static de.srsoftware.widerhall.Util.t;
|
||||
@@ -19,20 +22,29 @@ import static de.srsoftware.widerhall.Util.t;
|
||||
public class Rest extends HttpServlet {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Rest.class);
|
||||
private static final String LIST_LIST = "list/list";
|
||||
private static final String LIST_HIDE = "list/hide";
|
||||
private static final String LIST_SHOW = "list/show";
|
||||
private static final String USER_LIST = "user/list";
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String error = handleGet(req, resp);
|
||||
if (error != null) resp.sendError(400,error);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String error = handlePost(req, resp);
|
||||
if (error != null) resp.sendError(400,error);
|
||||
}
|
||||
|
||||
public String handleGet(HttpServletRequest req, HttpServletResponse resp){
|
||||
Object o = req.getSession().getAttribute(USER);
|
||||
JSONObject json = new JSONObject();
|
||||
var path = req.getPathInfo();
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
if (o instanceof User user){
|
||||
var path = req.getPathInfo();
|
||||
json.put(USER,user.safeMap());
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
switch (path) {
|
||||
case USER_LIST:
|
||||
json.put("users", (user.is(ADMIN) ? User.list() : List.of(user)).stream().map(User::safeMap).toList());
|
||||
@@ -44,6 +56,42 @@ public class Rest extends HttpServlet {
|
||||
json.put(ERROR,t("No handler for path '{}'!",path));
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (path) {
|
||||
case LIST_LIST:
|
||||
json.put("lists", MailingList.openLists().stream().map(MailingList::safeMap).toList());
|
||||
break;
|
||||
default:
|
||||
json.put(ERROR,"Not logged in!");
|
||||
}
|
||||
}
|
||||
try {
|
||||
resp.setContentType("application/json");
|
||||
resp.getWriter().println(json.toJSONString());
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
return t("Failed to handle request: {}",e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public String handlePost(HttpServletRequest req, HttpServletResponse resp){
|
||||
Object o = req.getSession().getAttribute(USER);
|
||||
JSONObject json = new JSONObject();
|
||||
var path = req.getPathInfo();
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
if (o instanceof User user){
|
||||
json.put(USER,user.safeMap());
|
||||
switch (path) {
|
||||
case LIST_HIDE:
|
||||
json.putAll(hideList(req,user,true));
|
||||
break;
|
||||
case LIST_SHOW:
|
||||
json.putAll(hideList(req,user,false));
|
||||
break;
|
||||
default:
|
||||
json.put(ERROR,t("No handler for path '{}'!",path));
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
json.put(ERROR,"Not logged in!");
|
||||
}
|
||||
@@ -55,4 +103,22 @@ public class Rest extends HttpServlet {
|
||||
return t("Failed to handle request: {}",e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> hideList(HttpServletRequest req, User user, boolean hide) {
|
||||
var listEmail = req.getParameter(LIST);
|
||||
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
|
||||
if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
|
||||
try {
|
||||
MailingList.hide(listEmail,hide);
|
||||
return Map.of("success",t("Mailing list '{}' was {}!",listEmail,hide ? "hidden" : "made public"));
|
||||
} catch (SQLException e) {
|
||||
LOG.error("Failed to (un)hide mailing list: ",e);
|
||||
return Map.of("error",t("Failed to update list '{}'",listEmail));
|
||||
}
|
||||
|
||||
} else {
|
||||
return Map.of("error",t("You are not allowed to edit '{}'",listEmail));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user