From f8ff180891e6bc024c594dc7fc45c3fffd64871a Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sat, 16 Apr 2022 23:25:40 +0200 Subject: [PATCH] implemented listing of list members --- .../srsoftware/widerhall/data/ListMember.java | 23 +++++++++++++ .../de/srsoftware/widerhall/data/User.java | 21 +++++++++--- .../de/srsoftware/widerhall/web/Rest.java | 32 +++++++++++++++++-- .../java/de/srsoftware/widerhall/web/Web.java | 4 ++- static/templates/inspect.st | 16 ++++++++++ static/templates/js.st | 20 +++++++++--- static/templates/listmembers.st | 13 ++++++++ 7 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 static/templates/inspect.st create mode 100644 static/templates/listmembers.st diff --git a/src/main/java/de/srsoftware/widerhall/data/ListMember.java b/src/main/java/de/srsoftware/widerhall/data/ListMember.java index 74ed7e8..478ca4e 100644 --- a/src/main/java/de/srsoftware/widerhall/data/ListMember.java +++ b/src/main/java/de/srsoftware/widerhall/data/ListMember.java @@ -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 { return list; } + public static Map of(String listEmail) throws SQLException { + var rs = Database.open() + .query("SELECT * FROM "+TABLE_NAME) + .where(LIST_EMAIL,listEmail) + .exec(); + var temp = new HashMap(); + while (rs.next()) temp.put(rs.getString(USER_EMAIL),rs.getInt(STATE)); + var result = new HashMap(); + 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(); + 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, diff --git a/src/main/java/de/srsoftware/widerhall/data/User.java b/src/main/java/de/srsoftware/widerhall/data/User.java index 620a40b..d3f1ce1 100644 --- a/src/main/java/de/srsoftware/widerhall/data/User.java +++ b/src/main/java/de/srsoftware/widerhall/data/User.java @@ -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 { Database.open().query(sql.toString()).run(); } + public static List loadAll(Collection emails) throws SQLException { + var rs = Database.open() + .query("SELECT * FROM "+TABLE_NAME) + .where(EMAIL,emails) + .exec(); + var list = new ArrayList(); + 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; } diff --git a/src/main/java/de/srsoftware/widerhall/web/Rest.java b/src/main/java/de/srsoftware/widerhall/web/Rest.java index a85dafc..e2ee8b8 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Rest.java +++ b/src/main/java/de/srsoftware/widerhall/web/Rest.java @@ -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 { 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 { 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 { } } + private Map 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 { 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 hideList(String listEmail, User user, boolean hide) { diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index 928d689..382d50c 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -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 { User user = o instanceof User ? (User) o : null; var data = new HashMap(); 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 { } 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); } diff --git a/static/templates/inspect.st b/static/templates/inspect.st new file mode 100644 index 0000000..c4a3c82 --- /dev/null +++ b/static/templates/inspect.st @@ -0,0 +1,16 @@ + + + + + + + + + + «navigation()» + «userinfo()» + «messages()» +

Widerhall '«data.list»' Details

+ «listmembers()» + + \ No newline at end of file diff --git a/static/templates/js.st b/static/templates/js.st index 87ef385..1e8ccc7 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -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){ $.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){ alert("Api call did not return result"); } +function showMembers(data){ + for (let i in data.members){ + let member = data.members[i]; + let row = $(''); + $('').text(member.name).appendTo(row); + $('').text(member.email).appendTo(row); + $('').text(member.state).appendTo(row); + + row.appendTo('#memberlist'); + } +} + function showUserList(data){ for (let i in data.users){ let user = data.users[i]; diff --git a/static/templates/listmembers.st b/static/templates/listmembers.st new file mode 100644 index 0000000..419e5de --- /dev/null +++ b/static/templates/listmembers.st @@ -0,0 +1,13 @@ +
+ Members of «data.list» + + + + + + +
NameEmailState
+ +
\ No newline at end of file