Browse Source

implemented listing of list members

drop_old_mail
Stephan Richter 3 years ago
parent
commit
f8ff180891
  1. 23
      src/main/java/de/srsoftware/widerhall/data/ListMember.java
  2. 21
      src/main/java/de/srsoftware/widerhall/data/User.java
  3. 32
      src/main/java/de/srsoftware/widerhall/web/Rest.java
  4. 4
      src/main/java/de/srsoftware/widerhall/web/Web.java
  5. 16
      static/templates/inspect.st
  6. 20
      static/templates/js.st
  7. 13
      static/templates/listmembers.st

23
src/main/java/de/srsoftware/widerhall/data/ListMember.java

@ -5,8 +5,10 @@ import org.slf4j.LoggerFactory; @@ -5,8 +5,10 @@ import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static de.srsoftware.widerhall.Constants.*;
import static de.srsoftware.widerhall.Constants.STATE;
@ -59,6 +61,27 @@ public class ListMember { @@ -59,6 +61,27 @@ public class ListMember {
return list;
}
public static Map<User,Integer> of(String listEmail) throws SQLException {
var rs = Database.open()
.query("SELECT * FROM "+TABLE_NAME)
.where(LIST_EMAIL,listEmail)
.exec();
var temp = new HashMap<String,Integer>();
while (rs.next()) temp.put(rs.getString(USER_EMAIL),rs.getInt(STATE));
var result = new HashMap<User,Integer>();
User.loadAll(temp.keySet())
.stream()
.forEach(user -> result.put(user,temp.get(user.email())));
return result;
}
public static String stateText(int state) {
var words = new ArrayList<String>();
if ((state & STATE_OWNER) > 0) words.add("owener");
if ((state & STATE_SUBSCRIBER) > 0) words.add("subscriber");
return String.join(", ",words);
}
private ListMember save() throws SQLException {
Database.open().insertInto(TABLE_NAME)
.values(Map.of(LIST_EMAIL,listEmail,

21
src/main/java/de/srsoftware/widerhall/data/User.java

@ -8,10 +8,7 @@ import java.security.InvalidKeyException; @@ -8,10 +8,7 @@ import java.security.InvalidKeyException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import static de.srsoftware.widerhall.data.Database.*;
import static de.srsoftware.widerhall.Constants.*;
@ -50,6 +47,22 @@ public class User { @@ -50,6 +47,22 @@ public class User {
Database.open().query(sql.toString()).run();
}
public static List<User> loadAll(Collection<String> emails) throws SQLException {
var rs = Database.open()
.query("SELECT * FROM "+TABLE_NAME)
.where(EMAIL,emails)
.exec();
var list = new ArrayList<User>();
while (rs.next()){
var email = rs.getString(EMAIL);
var name = rs.getString(NAME);
var salt = rs.getString(SALT);
var hashedPass = rs.getString(HASHED_PASS);
list.add(new User(email,name,salt,hashedPass));
}
return list;
}
public String email() {
return email;
}

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

@ -12,9 +12,11 @@ import javax.servlet.http.HttpServlet; @@ -12,9 +12,11 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static de.srsoftware.widerhall.Constants.*;
import static de.srsoftware.widerhall.Util.t;
@ -25,8 +27,10 @@ public class Rest extends HttpServlet { @@ -25,8 +27,10 @@ public class Rest extends HttpServlet {
private static final String LIST_DISABLE = "list/disable";
private static final String LIST_ENABLE = "list/enable";
private static final String LIST_HIDE = "list/hide";
private static final String LIST_MEMBERS = "list/members";
private static final String LIST_SHOW = "list/show";
private static final String USER_LIST = "user/list";
private static final String MEMBERS = "members";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
@ -98,6 +102,9 @@ public class Rest extends HttpServlet { @@ -98,6 +102,9 @@ public class Rest extends HttpServlet {
case LIST_HIDE:
json.putAll(hideList(listEmail,user,true));
break;
case LIST_MEMBERS:
json.putAll(listMembers(listEmail,user));
break;
case LIST_SHOW:
json.putAll(hideList(listEmail,user,false));
break;
@ -117,6 +124,27 @@ public class Rest extends HttpServlet { @@ -117,6 +124,27 @@ public class Rest extends HttpServlet {
}
}
private Map<String, Object> listMembers(String listEmail, User user) {
if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)) {
try {
var members = ListMember.of(listEmail)
.entrySet()
.stream()
.map(entry -> Map.of(
EMAIL,entry.getKey().email(),
NAME,entry.getKey().name(),
STATE,ListMember.stateText(entry.getValue())
))
.toList();
return Map.of(MEMBERS,members);
} catch (SQLException e) {
LOG.error("Failed to load member list: ",e);
return Map.of("error",t("Failed to load member list '{}'",listEmail));
}
}
return Map.of("error",t("You are not allowed to list members '{}'",listEmail));
}
private Map enableList(String listEmail, User user, boolean enable) {
if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
try {
@ -126,10 +154,8 @@ public class Rest extends HttpServlet { @@ -126,10 +154,8 @@ public class Rest extends HttpServlet {
LOG.error("Failed to enable/disable 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));
}
return Map.of("error",t("You are not allowed to edit '{}'",listEmail));
}
private Map<String, String> hideList(String listEmail, User user, boolean hide) {

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

@ -33,6 +33,7 @@ public class Web extends HttpServlet { @@ -33,6 +33,7 @@ public class Web extends HttpServlet {
private static final String REGISTER = "register";
private static final String SUBSCRIBE = "subscribe";
private static final String RELOAD = "reload";
private static final String INSPECT = "inspect";
private static final String IMAP_HOST = "imap_host";
private static final String IMAP_PORT = "imap_port";
private static final String IMAP_USER = "imap_user";
@ -144,7 +145,6 @@ public class Web extends HttpServlet { @@ -144,7 +145,6 @@ public class Web extends HttpServlet {
User user = o instanceof User ? (User) o : null;
var data = new HashMap<String,Object>();
if (user != null) data.put(USER,user.safeMap());
var path = req.getPathInfo();
path = (path == null || path.equals("/")) ? INDEX : path.substring(1);
String notes = null;
@ -183,6 +183,8 @@ public class Web extends HttpServlet { @@ -183,6 +183,8 @@ public class Web extends HttpServlet {
}
if (user != null){
var list = req.getParameter(LIST);
if (list != null) data.put(LIST,req.getParameter(LIST));
data.put(NOTES,notes);
return loadTemplate(path,data,resp);
}

16
static/templates/inspect.st

@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="jquery"></script>
<script src="js"></script>
<link rel="stylesheet" href="css" />
</head>
<body id="login">
«navigation()»
«userinfo()»
«messages()»
<h1>Widerhall '«data.list»' Details</h1>
«listmembers()»
</body>
</html>

20
static/templates/js.st

@ -22,6 +22,10 @@ function loadListList(){ @@ -22,6 +22,10 @@ function loadListList(){
$.getJSON('/api/list/list', showListList);
}
function loadMembers(listEmail){
$.post('/api/list/members',{list:listEmail},showMembers,'json');
}
function loadUserList(){
$.getJSON('/api/user/list', showUserList);
}
@ -34,10 +38,6 @@ function showList(listEmail){ @@ -34,10 +38,6 @@ function showList(listEmail){
$.post('/api/list/show',{list:listEmail},showListResult,'json');
}
function showListAdminList(data){
for (let i in data.lists){
let list = data.lists[i];
@ -108,6 +108,18 @@ function showListResult(result){ @@ -108,6 +108,18 @@ function showListResult(result){
alert("Api call did not return result");
}
function showMembers(data){
for (let i in data.members){
let member = data.members[i];
let row = $('<tr/>');
$('<td/>').text(member.name).appendTo(row);
$('<td/>').text(member.email).appendTo(row);
$('<td/>').text(member.state).appendTo(row);
row.appendTo('#memberlist');
}
}
function showUserList(data){
for (let i in data.users){
let user = data.users[i];

13
static/templates/listmembers.st

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
<fieldset>
<legend>Members of «data.list»</legend>
<table id="memberlist">
<tr>
<th>Name</th>
<th>Email</th>
<th>State</th>
</tr>
</table>
<script type="text/javascript">
loadMembers('«data.list»');
</script>
</fieldset>
Loading…
Cancel
Save