diff --git a/src/main/java/de/srsoftware/widerhall/Constants.java b/src/main/java/de/srsoftware/widerhall/Constants.java index fb9396b..cd57c4a 100644 --- a/src/main/java/de/srsoftware/widerhall/Constants.java +++ b/src/main/java/de/srsoftware/widerhall/Constants.java @@ -18,6 +18,8 @@ public class Constants { public static final String LIST = "list"; public static final String LOCATIONS = "locations"; public static final String NAME = "name"; + public static final String MESSAGE_ID = "message_id"; + public static final String MODERATOR = "moderator"; public static final String MONTH = "month"; public static final String NOTES = "notes"; public static final String PASSWORD = "password"; diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index 8a6e9f6..0a20462 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -328,6 +328,7 @@ public class MailingList implements MessageHandler, ProblemListener { } public boolean mayBeAlteredBy(User user) { + if (user == null) return false; if (user.hashPermission(PERMISSION_ADMIN)) return true; try { if (ListMember.load(this,user).isModerator()) return true; diff --git a/src/main/java/de/srsoftware/widerhall/data/Post.java b/src/main/java/de/srsoftware/widerhall/data/Post.java index b9b897a..586e72a 100644 --- a/src/main/java/de/srsoftware/widerhall/data/Post.java +++ b/src/main/java/de/srsoftware/widerhall/data/Post.java @@ -141,6 +141,10 @@ public class Post { return id; } + public MailingList list() { + return list; + } + public static Post load(String id) throws SQLException { var rs = Database.open().select(TABLE_NAME).where(ID,id).compile().exec(); try { @@ -161,6 +165,11 @@ public class Post { FILE,filename); } + public void remove() throws SQLException { + Database.open().deleteFrom(TABLE_NAME).where(ID,id).compile().run(); + file().delete(); + } + public Map safeMap() { return Map.of(ID,id, LIST,list.name(), @@ -186,5 +195,4 @@ public class Post { public long timestamp(){ return timestamp; } - } diff --git a/src/main/java/de/srsoftware/widerhall/web/Rest.java b/src/main/java/de/srsoftware/widerhall/web/Rest.java index 6d75c53..be6a5e4 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Rest.java +++ b/src/main/java/de/srsoftware/widerhall/web/Rest.java @@ -39,6 +39,7 @@ public class Rest extends HttpServlet { private static final String LIST_SHOW = "list/show"; private static final String LIST_TEST = "list/test"; private static final String LIST_SUBSCRIBABLE = "list/subscribable"; + private static final String MAIL_DROP = "mail/drop"; private static final String USER_ADD_PERMISSION = "user/addpermission"; private static final String USER_DROP_PERMISSION = "user/droppermission"; private static final String USER_LIST = "user/list"; @@ -61,15 +62,16 @@ public class Rest extends HttpServlet { return Map.of(SUCCESS,"Updated user permissions"); } - private Map archive(HttpServletRequest req) { + private Map archive(HttpServletRequest req, User user) { var list = Util.getMailingList(req); if (list != null){ + boolean userIsMod = list.mayBeAlteredBy(user); try { var month = req.getParameter(MONTH); if (month == null || month.isBlank()) { - return Map.of(LIST,list.email(),"summary",Post.summarize(list)); + return Map.of(LIST,list.email(),MODERATOR,userIsMod,"summary",Post.summarize(list)); } else { - return Map.of(LIST,list.email(),"posts",Post.find(list,month).stream().map(Post::safeMap).toList()); + return Map.of(LIST,list.email(),MODERATOR,userIsMod,"posts",Post.find(list,month).stream().map(Post::safeMap).toList()); } } catch (SQLException e) { e.printStackTrace(); @@ -118,6 +120,21 @@ public class Rest extends HttpServlet { if (error != null) resp.sendError(400,error); } + private Map dropMail(String messageId,User user){ + try { + var message = Post.load(messageId); + if (message == null) return Map.of(ERROR,t("Cannot remove: unknown message id")); + var allowed = message.list().mayBeAlteredBy(user); + if (allowed){ + message.remove(); + return Map.of(SUCCESS,t("Message deleted")); + } + return Map.of(ERROR,t("You are not allowed to remove messages from this list!")); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + private Map dropPermission(String userEmail, String permissions) { if (userEmail == null || userEmail.isBlank()) return Map.of(ERROR,"missing user email address!"); try { @@ -131,7 +148,7 @@ public class Rest extends HttpServlet { LOG.debug("Failed to load user for address {}",userEmail,e); return Map.of(ERROR,t("Failed to load user for address {}",userEmail)); } - return Map.of(SUCCESS,"Updated user permissions"); + return Map.of(SUCCESS,t("Updated user permissions")); } private Map enableList(MailingList list, User user, boolean enable) { @@ -156,7 +173,7 @@ public class Rest extends HttpServlet { json.put(USER,user.safeMap()); switch (path) { case LIST_ARCHIVE: - json.put("archive",archive(req)); + json.put("archive",archive(req,user)); break; case USER_LIST: try { @@ -179,7 +196,7 @@ public class Rest extends HttpServlet { } else { switch (path) { case LIST_ARCHIVE: - json.put("archive",archive(req)); + json.put("archive",archive(req, user)); break; case LIST_SUBSCRIBABLE: json.put("lists", MailingList.subscribable().stream().map(MailingList::minimalMap).toList()); @@ -245,6 +262,10 @@ public class Rest extends HttpServlet { case LIST_TEST: json.putAll(testList(list,user)); break; + case MAIL_DROP: + var messageId = req.getParameter(MESSAGE_ID); + json.putAll(dropMail(messageId,user)); + break; case USER_ADD_PERMISSION: if (user.hashPermission(User.PERMISSION_ADMIN)){ json.putAll(addPermission(userEmail,permissions)); diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index 72c7b43..ccd000d 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -139,7 +139,11 @@ public class Web extends TemplateServlet { map.put(LIST,list.email()); var month = req.getParameter(MONTH); - if (month != null && !month.isBlank())map.put(MONTH,month); + if (month != null && !month.isBlank()){ + map.put(MONTH,month); + var user = Util.getUser(req); + map.put(MODERATOR,list.mayBeAlteredBy(user)); + } return loadTemplate(ARCHIVE,map,resp); } diff --git a/static/templates/archive.st b/static/templates/archive.st index c6bd653..e10d2fc 100644 --- a/static/templates/archive.st +++ b/static/templates/archive.st @@ -18,6 +18,9 @@ Date From Subject + «if (data.moderator)» + Actions + «endif»