From 1b484760b9617736c5eaa34bb79a86f5b5ad5a54 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Fri, 15 Mar 2024 12:25:43 +0100 Subject: [PATCH] backported some code from lang_de branch Signed-off-by: Stephan Richter --- pom.xml | 2 +- .../srsoftware/widerhall/data/Database.java | 10 +++++ .../widerhall/data/MailingList.java | 3 +- .../de/srsoftware/widerhall/data/Post.java | 39 ++++++++++++++----- .../srsoftware/widerhall/mail/ImapClient.java | 1 + .../de/srsoftware/widerhall/web/Rest.java | 21 +++++++--- .../java/de/srsoftware/widerhall/web/Web.java | 1 - 7 files changed, 57 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index 5157743..04b7800 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example Widerhall - 0.2.61 + 1.0.1 diff --git a/src/main/java/de/srsoftware/widerhall/data/Database.java b/src/main/java/de/srsoftware/widerhall/data/Database.java index 9942bd3..b4bbe28 100644 --- a/src/main/java/de/srsoftware/widerhall/data/Database.java +++ b/src/main/java/de/srsoftware/widerhall/data/Database.java @@ -91,6 +91,7 @@ public class Database { */ public class Request{ + private String groupBy = null; private final StringBuilder sql; // buffer the sql to be built private final HashMap> where = new HashMap<>(); // buffer condition statements for select private final HashMap values = new HashMap<>(); // buffer values for insert/update statements @@ -128,6 +129,10 @@ public class Database { } } + private void applyGrouping(){ + if (groupBy != null && !groupBy.isBlank()) sql.append(" GROUP BY ").append(groupBy.trim()); + } + private void applySorting(){ if (!sortFields.isEmpty()) sql.append(" ORDER BY ").append(String.join(", ",sortFields)); } @@ -148,6 +153,7 @@ public class Database { var args = new ArrayList<>(); applyValues(args); applyConditions(args); + applyGrouping(); applySorting(); if (additionalArgs != null) { for (Object arg : additionalArgs) args.add(arg); @@ -187,6 +193,10 @@ public class Database { } } + public Request groupBy(String column) { + groupBy = column; + return this; + } public void run() throws SQLException { compile().run(); diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index 0a20462..d44d9cc 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -13,8 +13,7 @@ import org.stringtemplate.v4.ST; import javax.mail.*; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; -import javax.xml.crypto.Data; -import java.io.*; +import java.io.UnsupportedEncodingException; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; diff --git a/src/main/java/de/srsoftware/widerhall/data/Post.java b/src/main/java/de/srsoftware/widerhall/data/Post.java index 586e72a..288c891 100644 --- a/src/main/java/de/srsoftware/widerhall/data/Post.java +++ b/src/main/java/de/srsoftware/widerhall/data/Post.java @@ -15,10 +15,7 @@ import java.nio.file.Files; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.TreeMap; +import java.util.*; import static de.srsoftware.widerhall.Constants.*; import static de.srsoftware.widerhall.Constants.VARCHAR; @@ -26,12 +23,13 @@ import static de.srsoftware.widerhall.Constants.VARCHAR; public class Post { public static final Logger LOG = LoggerFactory.getLogger(Post.class); public static final String TABLE_NAME = "Posts"; + + private static final String DATE = "date"; + private static final String FILE = "file"; private static final String FROM_ADDR = "from_addr"; private static final String FROM_NAME = "from_name"; - private static final String PARENT = "parent"; private static final String LONG = "LONG"; - private static final String DATE = "date"; - private static final String FILE = "file"; + private static final String PARENT = "parent"; private static HashMap cache = new HashMap<>(); private String id, fromAddr, fromName, subject, filename; @@ -115,6 +113,24 @@ public class Post { } } + public static ArrayList find(MailingList list, String month, List allowedSenders) throws SQLException { + var query = Database.open() + .select(TABLE_NAME,"*","strftime('%Y-%m',date/1000,'unixepoch') as month") + .where(LIST,list.email()) + .where(MONTH,month); + if (allowedSenders != null) query = query.where(FROM_ADDR,allowedSenders); + var rs = query.sort(DATE) + .compile() + .exec(); + try { + var result = new ArrayList(); + while (rs.next()) result.add(Post.from(rs)); + return result; + } finally { + rs.close(); + } + } + private static Post from(ResultSet rs) { try { var id = rs.getString(ID); @@ -183,10 +199,13 @@ public class Post { return this; } - public static Map summarize(MailingList list) throws SQLException { - var sql = new StringBuilder("SELECT count(*) as count,strftime('%Y-%m',date/1000,'unixepoch') as month FROM Posts WHERE list = ? GROUP BY month ORDER BY month;"); + public static Map summarize(MailingList list,List limitedUsers) throws SQLException { + var sql = new StringBuilder("SELECT count(*) as count,strftime('%Y-%m',date/1000,'unixepoch') as month FROM Posts"); + var query = Database.open().query(sql).where(LIST,list.email()).groupBy(MONTH).sort(MONTH); + if (limitedUsers != null) query.where(FROM_ADDR,limitedUsers); + var rs = query.compile().exec(); + var map = new TreeMap(); - var rs = Database.open().query(sql).compile(list.email()).exec(); while (rs.next()) map.put(rs.getString("month"),rs.getInt("count")); rs.close(); return map; diff --git a/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java b/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java index f8014f1..63ceab2 100644 --- a/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java +++ b/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java @@ -156,6 +156,7 @@ public class ImapClient { var days = duration.toDays(); LOG.info("Message {} is {} days old!",message.getSubject(),days); if (days > holdTime){ + LOG.info("…removing"); Folder folder = message.getFolder(); if (!folder.isOpen()) folder.open(Folder.READ_WRITE); message.setFlag(Flags.Flag.DELETED, true); diff --git a/src/main/java/de/srsoftware/widerhall/web/Rest.java b/src/main/java/de/srsoftware/widerhall/web/Rest.java index be6a5e4..bfe4d8e 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Rest.java +++ b/src/main/java/de/srsoftware/widerhall/web/Rest.java @@ -62,16 +62,19 @@ public class Rest extends HttpServlet { return Map.of(SUCCESS,"Updated user permissions"); } - private Map archive(HttpServletRequest req, User user) { + 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); + var allEmails = user != null || list.hasState(STATE_OPEN_FOR_SUBSCRIBERS) || list.hasState(STATE_OPEN_FOR_GUESTS); + var limitedSenders = allEmails ? null : list.moderators().map(ListMember::user).map(User::email).toList(); + + String month = req.getParameter(MONTH); + boolean userIsMod = list.mayBeAlteredBy(user); if (month == null || month.isBlank()) { - return Map.of(LIST,list.email(),MODERATOR,userIsMod,"summary",Post.summarize(list)); + return Map.of(LIST,list.email(),MODERATOR,userIsMod,"summary",Post.summarize(list,limitedSenders)); } else { - return Map.of(LIST,list.email(),MODERATOR,userIsMod,"posts",Post.find(list,month).stream().map(Post::safeMap).toList()); + return Map.of(LIST,list.email(),MODERATOR,userIsMod,"posts",Post.find(list,month,limitedSenders).stream().map(Post::safeMap).toList()); } } catch (SQLException e) { e.printStackTrace(); @@ -173,6 +176,9 @@ public class Rest extends HttpServlet { json.put(USER,user.safeMap()); switch (path) { case LIST_ARCHIVE: + var list = Util.getMailingList(req); + var allowed = list.hasState(STATE_PUBLIC_ARCHIVE) || list.mayBeAlteredBy(user); + if (!allowed) return t("Sie sind nicht berechtigt, das Archiv dieser Liste einzusehen!"); json.put("archive",archive(req,user)); break; case USER_LIST: @@ -196,7 +202,10 @@ public class Rest extends HttpServlet { } else { switch (path) { case LIST_ARCHIVE: - json.put("archive",archive(req, user)); + var list = Util.getMailingList(req); + var allowed = list.hasState(STATE_PUBLIC_ARCHIVE); + if (!allowed) return t("This mailing list has no public archive!"); + json.put("archive",archive(req,user)); break; case LIST_SUBSCRIBABLE: json.put("lists", MailingList.subscribable().stream().map(MailingList::minimalMap).toList()); diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index ccd000d..ee78d74 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -282,7 +282,6 @@ public class Web extends TemplateServlet { if (user != null) data.put(USER,user.safeMap()); if (list != null) data.put(LIST,list.minimalMap()); - String notes = null; switch (path){ case ARCHIVE: return archive(req,resp);