grouping mail archive by year-month
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>org.example</groupId>
|
<groupId>org.example</groupId>
|
||||||
<artifactId>Widerhall</artifactId>
|
<artifactId>Widerhall</artifactId>
|
||||||
<version>0.2.40</version>
|
<version>0.2.41</version>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ public class Constants {
|
|||||||
public static final String LIST = "list";
|
public static final String LIST = "list";
|
||||||
public static final String LOCATIONS = "locations";
|
public static final String LOCATIONS = "locations";
|
||||||
public static final String NAME = "name";
|
public static final String NAME = "name";
|
||||||
|
public static final String MONTH = "month";
|
||||||
public static final String NOTES = "notes";
|
public static final String NOTES = "notes";
|
||||||
public static final String PASSWORD = "password";
|
public static final String PASSWORD = "password";
|
||||||
public static final String PERMISSIONS = "permissions";
|
public static final String PERMISSIONS = "permissions";
|
||||||
|
|||||||
@@ -143,11 +143,14 @@ public class Database {
|
|||||||
* finalize sql, save sql and arguments as compiled request
|
* finalize sql, save sql and arguments as compiled request
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public CompiledRequest compile(){
|
public CompiledRequest compile(Object ...additionalArgs){
|
||||||
var args = new ArrayList<>();
|
var args = new ArrayList<>();
|
||||||
applyValues(args);
|
applyValues(args);
|
||||||
applyConditions(args);
|
applyConditions(args);
|
||||||
applySorting();
|
applySorting();
|
||||||
|
if (additionalArgs != null) {
|
||||||
|
for (Object arg : additionalArgs) args.add(arg);
|
||||||
|
}
|
||||||
return new CompiledRequest(sql.toString(),args);
|
return new CompiledRequest(sql.toString(),args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,12 +12,11 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.sql.Array;
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static de.srsoftware.widerhall.Constants.*;
|
import static de.srsoftware.widerhall.Constants.*;
|
||||||
@@ -98,8 +97,14 @@ public class Post {
|
|||||||
return new File(filename);
|
return new File(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ArrayList<Post> find(MailingList list) throws SQLException {
|
public static ArrayList<Post> find(MailingList list, String month) throws SQLException {
|
||||||
var rs = Database.open().select(TABLE_NAME).where(LIST,list.email()).sort(DATE).compile().exec();
|
var rs = Database.open()
|
||||||
|
.select(TABLE_NAME,"*","strftime('%Y-%m',date/1000,'unixepoch') as month")
|
||||||
|
.where(LIST,list.email())
|
||||||
|
.where(MONTH,month)
|
||||||
|
.sort(DATE)
|
||||||
|
.compile()
|
||||||
|
.exec();
|
||||||
try {
|
try {
|
||||||
var result = new ArrayList<Post>();
|
var result = new ArrayList<Post>();
|
||||||
while (rs.next()) result.add(Post.from(rs));
|
while (rs.next()) result.add(Post.from(rs));
|
||||||
@@ -160,7 +165,7 @@ public class Post {
|
|||||||
LIST,list.name(),
|
LIST,list.name(),
|
||||||
FROM_NAME,fromName,
|
FROM_NAME,fromName,
|
||||||
SUBJECT,Util.dropEmail(subject),
|
SUBJECT,Util.dropEmail(subject),
|
||||||
DATE,timestamp);
|
DATE, new Timestamp(timestamp).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Post save() throws SQLException {
|
private Post save() throws SQLException {
|
||||||
@@ -168,6 +173,15 @@ public class Post {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HashMap<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;");
|
||||||
|
var map = new HashMap<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;
|
||||||
|
}
|
||||||
|
|
||||||
public long timestamp(){
|
public long timestamp(){
|
||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import javax.servlet.http.HttpServlet;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Member;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -62,17 +61,22 @@ public class Rest extends HttpServlet {
|
|||||||
return Map.of(SUCCESS,"Updated user permissions");
|
return Map.of(SUCCESS,"Updated user permissions");
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<? extends Object> archive(HttpServletRequest req) {
|
private Map<String, Object> archive(HttpServletRequest req) {
|
||||||
var list = Util.getMailingList(req);
|
var list = Util.getMailingList(req);
|
||||||
if (list != null){
|
if (list != null){
|
||||||
try {
|
try {
|
||||||
return Post.find(list).stream().map(Post::safeMap).toList();
|
var month = req.getParameter(MONTH);
|
||||||
|
if (month == null || month.isBlank()) {
|
||||||
|
return Map.of(LIST,list.email(),"summary",Post.summarize(list));
|
||||||
|
} else {
|
||||||
|
return Map.of(LIST,list.email(),"posts",Post.find(list,month).stream().map(Post::safeMap).toList());
|
||||||
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LOG.debug("list: {}",list.email());
|
LOG.debug("list: {}",list.email());
|
||||||
return List.of();
|
return Map.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -134,8 +134,13 @@ public class Web extends TemplateServlet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String archive(HttpServletRequest req, HttpServletResponse resp) {
|
private String archive(HttpServletRequest req, HttpServletResponse resp) {
|
||||||
|
var map = new HashMap<String,Object>();
|
||||||
var list = Util.getMailingList(req);
|
var list = Util.getMailingList(req);
|
||||||
return loadTemplate(ARCHIVE,Map.of(LIST,list.email()),resp);
|
map.put(LIST,list.email());
|
||||||
|
|
||||||
|
var month = req.getParameter(MONTH);
|
||||||
|
if (month != null && !month.isBlank())map.put(MONTH,month);
|
||||||
|
return loadTemplate(ARCHIVE,map,resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String confirm(HttpServletRequest req, HttpServletResponse resp) {
|
private String confirm(HttpServletRequest req, HttpServletResponse resp) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
«userinfo()»
|
«userinfo()»
|
||||||
«messages()»
|
«messages()»
|
||||||
<h1>Widerhall List Archive</h1>
|
<h1>Widerhall List Archive</h1>
|
||||||
|
«if(data.month)»
|
||||||
<table id="archive">
|
<table id="archive">
|
||||||
<tr>
|
<tr>
|
||||||
<th>Date</th>
|
<th>Date</th>
|
||||||
@@ -18,9 +19,20 @@
|
|||||||
<th>Subject</th>
|
<th>Subject</th>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
<script type="text/javascript">
|
||||||
|
loadArchive('«data.list»','«data.month»');
|
||||||
|
</script>
|
||||||
|
«else»
|
||||||
|
<table id="archive">
|
||||||
|
<tr>
|
||||||
|
<th>Month</th>
|
||||||
|
<th>Number of messages</th>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<script type="text/javascript">
|
||||||
|
loadArchiveSummary('«data.list»');
|
||||||
|
</script>
|
||||||
|
«endif»
|
||||||
«footer()»
|
«footer()»
|
||||||
</body>
|
</body>
|
||||||
<script type="text/javascript">
|
|
||||||
loadArchive('«data.list»');
|
|
||||||
</script>
|
|
||||||
</html>
|
</html>
|
||||||
@@ -40,8 +40,12 @@ function hideList(listEmail){
|
|||||||
$.post('/api/list/hide',{list:listEmail},showListResult,'json');
|
$.post('/api/list/hide',{list:listEmail},showListResult,'json');
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadArchive(listEmail){
|
function loadArchive(listEmail,month){
|
||||||
$.get('/api/list/archive?list='+listEmail,showListArchive,'json');
|
$.get('/api/list/archive?month='+month+'&list='+listEmail,showListArchive,'json');
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadArchiveSummary(listEmail){
|
||||||
|
$.get('/api/list/archive?list='+listEmail,showListArchiveSummary,'json');
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadListDetail(listEmail){
|
function loadListDetail(listEmail){
|
||||||
@@ -73,15 +77,28 @@ function showList(listEmail){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function showListArchive(data){
|
function showListArchive(data){
|
||||||
for (let time in data.archive){
|
console.log(data);
|
||||||
let post = data.archive[time];
|
let posts = data.archive.posts;
|
||||||
|
for (let time in posts){
|
||||||
|
let post = posts[time];
|
||||||
let row = $('<tr/>');
|
let row = $('<tr/>');
|
||||||
var url = 'post?id='+post.id;
|
var url = 'post?id='+post.id;
|
||||||
$('<td/>').html('<a href="'+url+'">'+new Date(post.date)+'</a>').appendTo(row);
|
$('<td/>').html('<a href="'+url+'">'+post.date+'</a>').appendTo(row);
|
||||||
$('<td/>').html('<a href="'+url+'">'+post.from_name+'</a>').appendTo(row);
|
$('<td/>').html('<a href="'+url+'">'+post.from_name+'</a>').appendTo(row);
|
||||||
$('<td/>').html('<a href="'+url+'">'+post.subject+'</a>').appendTo(row);
|
$('<td/>').html('<a href="'+url+'">'+post.subject+'</a>').appendTo(row);
|
||||||
row.appendTo($('#archive'));
|
row.appendTo($('#archive'));
|
||||||
console.log(post);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function showListArchiveSummary(data){
|
||||||
|
console.log(data);
|
||||||
|
let summary = data.archive.summary;
|
||||||
|
for (let month in summary){
|
||||||
|
let url = 'archive?month='+month+'&list='+data.archive.list;
|
||||||
|
let row = $('<tr/>');
|
||||||
|
$('<td/>').html('<a href="'+url+'">'+month+'</a>').appendTo(row);
|
||||||
|
$('<td/>').html('<a href="'+url+'">'+summary[month]+'</a>').appendTo(row);
|
||||||
|
row.appendTo($('#archive'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user