Browse Source

backported some code from lang_de branch

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
main
Stephan Richter 8 months ago
parent
commit
1b484760b9
  1. 2
      pom.xml
  2. 10
      src/main/java/de/srsoftware/widerhall/data/Database.java
  3. 3
      src/main/java/de/srsoftware/widerhall/data/MailingList.java
  4. 39
      src/main/java/de/srsoftware/widerhall/data/Post.java
  5. 1
      src/main/java/de/srsoftware/widerhall/mail/ImapClient.java
  6. 21
      src/main/java/de/srsoftware/widerhall/web/Rest.java
  7. 1
      src/main/java/de/srsoftware/widerhall/web/Web.java

2
pom.xml

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
<groupId>org.example</groupId>
<artifactId>Widerhall</artifactId>
<version>0.2.61</version>
<version>1.0.1</version>
<build>
<plugins>
<plugin>

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

@ -91,6 +91,7 @@ public class Database { @@ -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<String, List<Object>> where = new HashMap<>(); // buffer condition statements for select
private final HashMap<String,Object> values = new HashMap<>(); // buffer values for insert/update statements
@ -128,6 +129,10 @@ public class Database { @@ -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 { @@ -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 { @@ -187,6 +193,10 @@ public class Database {
}
}
public Request groupBy(String column) {
groupBy = column;
return this;
}
public void run() throws SQLException {
compile().run();

3
src/main/java/de/srsoftware/widerhall/data/MailingList.java

@ -13,8 +13,7 @@ import org.stringtemplate.v4.ST; @@ -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.*;

39
src/main/java/de/srsoftware/widerhall/data/Post.java

@ -15,10 +15,7 @@ import java.nio.file.Files; @@ -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; @@ -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<String, Post> cache = new HashMap<>();
private String id, fromAddr, fromName, subject, filename;
@ -115,6 +113,24 @@ public class Post { @@ -115,6 +113,24 @@ public class Post {
}
}
public static ArrayList<Post> find(MailingList list, String month, List<String> 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<Post>();
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 { @@ -183,10 +199,13 @@ public class Post {
return this;
}
public static Map<String, Object> 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<String, Object> summarize(MailingList list,List<String> 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<String,Object>();
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;

1
src/main/java/de/srsoftware/widerhall/mail/ImapClient.java

@ -156,6 +156,7 @@ public class ImapClient { @@ -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);

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

@ -62,16 +62,19 @@ public class Rest extends HttpServlet { @@ -62,16 +62,19 @@ public class Rest extends HttpServlet {
return Map.of(SUCCESS,"Updated user permissions");
}
private Map<String, Object> archive(HttpServletRequest req, User user) {
private Map<String,Object> 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 { @@ -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 { @@ -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());

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

@ -282,7 +282,6 @@ public class Web extends TemplateServlet { @@ -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);

Loading…
Cancel
Save