Browse Source

implemented hiding/unhiding of mailing lists on the database side

drop_old_mail
Stephan Richter 3 years ago
parent
commit
321f05c09a
  1. 16
      config/logback.xml
  2. 7
      pom.xml
  3. 2
      src/main/java/de/srsoftware/widerhall/Application.java
  4. 5
      src/main/java/de/srsoftware/widerhall/Constants.java
  5. 14
      src/main/java/de/srsoftware/widerhall/data/Database.java
  6. 2
      src/main/java/de/srsoftware/widerhall/data/ListMember.java
  7. 92
      src/main/java/de/srsoftware/widerhall/data/MailingList.java
  8. 70
      src/main/java/de/srsoftware/widerhall/web/Rest.java
  9. 16
      src/main/resources/logback.xml
  10. 2
      static/templates/admin.st
  11. 2
      static/templates/index.st
  12. 35
      static/templates/js.st
  13. 26
      static/templates/listadminlist.st
  14. 19
      static/templates/listlist.st

16
config/logback.xml

@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
<configuration scan="true" scanPeriod="30 seconds" debug="true">
<!-- scan="true" enables automatic updates if config file changes, see http://logback.qos.ch/manual/configuration.html -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5}: %msg%n
</pattern>
</encoder>
</appender>
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
<logger name="de.srsoftware" level="DEBUG" />
</configuration>

7
pom.xml

@ -70,6 +70,13 @@ @@ -70,6 +70,13 @@
<version>1.3.0-alpha13</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.3.0-alpha13</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>

2
src/main/java/de/srsoftware/widerhall/Application.java

@ -14,6 +14,8 @@ import org.json.simple.JSONObject; @@ -14,6 +14,8 @@ import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.file.Path;
public class Application {
private static final Logger LOG = LoggerFactory.getLogger(Application.class);

5
src/main/java/de/srsoftware/widerhall/Constants.java

@ -2,7 +2,9 @@ package de.srsoftware.widerhall; @@ -2,7 +2,9 @@ package de.srsoftware.widerhall;
public class Constants {
public static final String ADMIN = "Admin";
public static final String BASE = "base";
public static final String BASE_URL = "base_url";
public static final String DB = "database";
public static final String EMAIL = "email";
public static final String ERROR = "error";
public static final String HOST = "host";
@ -10,6 +12,7 @@ public class Constants { @@ -10,6 +12,7 @@ public class Constants {
public static final String INBOX = "inbox";
public static final String INDEX = "index";
public static final String INT = "INT";
public static final String LIST = "list";
public static final String NAME = "name";
public static final String NOTES = "notes";
public static final String PASSWORD = "password";
@ -20,8 +23,6 @@ public class Constants { @@ -20,8 +23,6 @@ public class Constants {
public static final String VARCHAR = "VARCHAR(255)";
public static final String DB = "database";
public static final String BASE = "base";
public static final String CONFIG = "configuration";
public static final String LOCATIONS = "locations";
}

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

@ -80,6 +80,20 @@ public class Database { @@ -80,6 +80,20 @@ public class Database {
var marks = String.join(", ",arr);
sb.append("(").append(marks).append(")");
}
if (!where.isEmpty()){
var clauses = new ArrayList<String>();
sb.append(" WHERE ");
for (var entry : where.entrySet()){
var arr = new String[entry.getValue().size()];
Arrays.fill(arr,"?");
var marks = String.join(", ",arr);
clauses.add("("+entry.getKey()+" IN ("+marks+"))");
args.addAll(entry.getValue());
}
sb.append(String.join(" AND ",clauses));
}
var sql = sb.toString();
LOG.debug(sql);
try {

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

@ -43,7 +43,7 @@ public class ListMember { @@ -43,7 +43,7 @@ public class ListMember {
Database.open().query(sql.toString()).run();
}
public static List<String> listsOf(User user) {
public static List<String> listsOwnedBy(User user) {
var list = new ArrayList<String>();
try {
var rs = Database.open()

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

@ -25,10 +25,10 @@ public class MailingList { @@ -25,10 +25,10 @@ public class MailingList {
private final String name;
private final String email;
public static final String TABLE_NAME = "Lists";
private final String imapPass,smtpPass,imapHost,smtpHost,imapUser,smtpUser;
private final int imapPort,smtpPort,state;
private final String imapPass, smtpPass, imapHost, smtpHost, imapUser, smtpUser;
private final int imapPort, smtpPort, state;
public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state){
public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state) {
this.email = email;
this.name = name;
this.imapHost = imapHost;
@ -43,7 +43,7 @@ public class MailingList { @@ -43,7 +43,7 @@ public class MailingList {
}
public static MailingList create(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass) throws SQLException {
return new MailingList(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass,ENABLED).save();
return new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass, ENABLED).save();
}
public static void createTable() throws SQLException {
@ -65,18 +65,22 @@ public class MailingList { @@ -65,18 +65,22 @@ public class MailingList {
Database.open().query(sql.toString()).run();
}
public static void hide(String listEmail, boolean hide) throws SQLException {
// https://stackoverflow.com/questions/16440831/bitwise-xor-in-sqlite-bitwise-not-not-working-as-i-expect
String expression = hide ? "state = (~(state & "+PUBLIC+"))&(state|"+PUBLIC+")" : ("state = state | "+PUBLIC);
Database.open().query("UPDATE " + TABLE_NAME + " SET "+expression).where(EMAIL, listEmail).run();
}
public static List<MailingList> listsOf(User user) {
List<String> keys = (user.is(ADMIN)) ? null : ListMember.listsOf(user);
List<String> keys = (user.is(ADMIN)) ? null : ListMember.listsOwnedBy(user);
var list = new ArrayList<MailingList>();
if (keys != null && keys.isEmpty()) return list;
try {
Database.Request q = Database.open().query("SELECT * FROM " + TABLE_NAME);
if (keys != null){
if (keys.isEmpty()) return list;
q.where(EMAIL,keys);
}
var rs = q.exec();
while (rs.next()){
Database.Request query = Database.open().query("SELECT * FROM " + TABLE_NAME);
if (keys != null) query.where(EMAIL, keys);
var rs = query.exec();
while (rs.next()) {
var email = rs.getString(EMAIL);
var name = rs.getString(NAME);
var imapHost = rs.getString(IMAP_HOST);
@ -88,46 +92,60 @@ public class MailingList { @@ -88,46 +92,60 @@ public class MailingList {
var smtpUser = rs.getString(SMTP_USER);
var smtpPass = rs.getString(SMTP_PASS);
var state = rs.getInt(STATE);
list.add(new MailingList(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass,state));
list.add(new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass, state));
}
} catch (SQLException e) {
LOG.warn("Listing mailing lists failed: ",e);
LOG.warn("Listing mailing lists failed: ", e);
}
return list;
}
public static List<MailingList> openLists() {
var list = new ArrayList<MailingList>();
try {
var rs = Database.open().query("SELECT *, (" + STATE + " & " + PUBLIC + ") as test FROM " + TABLE_NAME).where("test", PUBLIC).exec();
while (rs.next()) {
var email = rs.getString(EMAIL);
var name = rs.getString(NAME);
var state = rs.getInt(STATE);
list.add(new MailingList(email, name, null, 0, null, null, null, 0, null, null, state));
}
} catch (SQLException e) {
LOG.warn("Listing mailing lists failed: ", e);
}
return list;
}
public Map<String,Object> safeMap() {
return Map.of(EMAIL,email,NAME,name,
IMAP_HOST,imapHost,IMAP_PORT,imapPort,IMAP_USER,imapUser,
SMTP_HOST,smtpHost,SMTP_PORT,smtpPort,SMTP_USER,smtpUser,
STATE,stateName(state));
public Map<String, Object> safeMap() {
return Map.of(EMAIL, email, NAME, name,
IMAP_HOST, imapHost, IMAP_PORT, imapPort, IMAP_USER, imapUser,
SMTP_HOST, smtpHost, SMTP_PORT, smtpPort, SMTP_USER, smtpUser,
STATE, stateString(state));
}
private static String stateName(int state) {
switch (state){
case ENABLED:
return "enabled";
default:
return "disabled";
}
private static String stateString(int state) {
var states = new ArrayList<String>();
states.add((state & ENABLED) == ENABLED ? "enabled" : "disabled");
states.add((state & PUBLIC) == PUBLIC ? "public" : "hidden");
return String.join(", ", states);
}
private MailingList save() throws SQLException {
Database.open().insertInto(TABLE_NAME)
.values(Map.ofEntries(
Map.entry(EMAIL,email),
Map.entry(NAME,name),
Map.entry(IMAP_HOST,imapHost),
Map.entry(IMAP_PORT,imapPort),
Map.entry(IMAP_USER,imapUser),
Map.entry(IMAP_PASS,imapPass),
Map.entry(SMTP_HOST,smtpHost),
Map.entry(SMTP_PORT,smtpPort),
Map.entry(SMTP_USER,smtpUser),
Map.entry(SMTP_PASS,smtpPass),
Map.entry(STATE,state)))
Map.entry(EMAIL, email),
Map.entry(NAME, name),
Map.entry(IMAP_HOST, imapHost),
Map.entry(IMAP_PORT, imapPort),
Map.entry(IMAP_USER, imapUser),
Map.entry(IMAP_PASS, imapPass),
Map.entry(SMTP_HOST, smtpHost),
Map.entry(SMTP_PORT, smtpPort),
Map.entry(SMTP_USER, smtpUser),
Map.entry(SMTP_PASS, smtpPass),
Map.entry(STATE, state)))
.run();
return this;
}
}

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

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package de.srsoftware.widerhall.web;
import de.srsoftware.widerhall.data.ListMember;
import de.srsoftware.widerhall.data.MailingList;
import de.srsoftware.widerhall.data.User;
import org.json.simple.JSONObject;
@ -11,7 +12,9 @@ import javax.servlet.http.HttpServlet; @@ -11,7 +12,9 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import static de.srsoftware.widerhall.Constants.*;
import static de.srsoftware.widerhall.Util.t;
@ -19,20 +22,29 @@ import static de.srsoftware.widerhall.Util.t; @@ -19,20 +22,29 @@ import static de.srsoftware.widerhall.Util.t;
public class Rest extends HttpServlet {
private static final Logger LOG = LoggerFactory.getLogger(Rest.class);
private static final String LIST_LIST = "list/list";
private static final String LIST_HIDE = "list/hide";
private static final String LIST_SHOW = "list/show";
private static final String USER_LIST = "user/list";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String error = handleGet(req, resp);
if (error != null) resp.sendError(400,error);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String error = handlePost(req, resp);
if (error != null) resp.sendError(400,error);
}
public String handleGet(HttpServletRequest req, HttpServletResponse resp){
Object o = req.getSession().getAttribute(USER);
JSONObject json = new JSONObject();
var path = req.getPathInfo();
path = path == null ? INDEX : path.substring(1);
if (o instanceof User user){
var path = req.getPathInfo();
json.put(USER,user.safeMap());
path = path == null ? INDEX : path.substring(1);
switch (path) {
case USER_LIST:
json.put("users", (user.is(ADMIN) ? User.list() : List.of(user)).stream().map(User::safeMap).toList());
@ -44,6 +56,42 @@ public class Rest extends HttpServlet { @@ -44,6 +56,42 @@ public class Rest extends HttpServlet {
json.put(ERROR,t("No handler for path '{}'!",path));
break;
}
} else {
switch (path) {
case LIST_LIST:
json.put("lists", MailingList.openLists().stream().map(MailingList::safeMap).toList());
break;
default:
json.put(ERROR,"Not logged in!");
}
}
try {
resp.setContentType("application/json");
resp.getWriter().println(json.toJSONString());
return null;
} catch (IOException e) {
return t("Failed to handle request: {}",e.getMessage());
}
}
public String handlePost(HttpServletRequest req, HttpServletResponse resp){
Object o = req.getSession().getAttribute(USER);
JSONObject json = new JSONObject();
var path = req.getPathInfo();
path = path == null ? INDEX : path.substring(1);
if (o instanceof User user){
json.put(USER,user.safeMap());
switch (path) {
case LIST_HIDE:
json.putAll(hideList(req,user,true));
break;
case LIST_SHOW:
json.putAll(hideList(req,user,false));
break;
default:
json.put(ERROR,t("No handler for path '{}'!",path));
break;
}
} else {
json.put(ERROR,"Not logged in!");
}
@ -55,4 +103,22 @@ public class Rest extends HttpServlet { @@ -55,4 +103,22 @@ public class Rest extends HttpServlet {
return t("Failed to handle request: {}",e.getMessage());
}
}
private Map<String, String> hideList(HttpServletRequest req, User user, boolean hide) {
var listEmail = req.getParameter(LIST);
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
try {
MailingList.hide(listEmail,hide);
return Map.of("success",t("Mailing list '{}' was {}!",listEmail,hide ? "hidden" : "made public"));
} catch (SQLException e) {
LOG.error("Failed to (un)hide 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));
}
}
}

16
src/main/resources/logback.xml

@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
<configuration scan="true" scanPeriod="30 seconds" debug="true">
<!-- scan="true" enables automatic updates if config file changes, see http://logback.qos.ch/manual/configuration.html -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5}: %msg%n
</pattern>
</encoder>
</appender>
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
<logger name="de.srsoftware" level="DEBUG" />
</configuration>

2
static/templates/admin.st

@ -12,6 +12,6 @@ @@ -12,6 +12,6 @@
<h1>Widerhall Administration</h1>
«messages()»
«userlist()»
«listlist()»
«listadminlist()»
</body>
</html>

2
static/templates/index.st

@ -11,5 +11,7 @@ @@ -11,5 +11,7 @@
«userinfo()»
«messages()»
<h1>Widerhall Index page</h1>
«messages()»
«listlist()»
</body>
</html>

35
static/templates/js.st

@ -11,22 +11,47 @@ function enableList(listEmail){ @@ -11,22 +11,47 @@ function enableList(listEmail){
}
function hideList(listEmail){
console.log('hideList('+listEmail+')');
$.post('/api/list/hide',{list:listEmail},showListResult,'json');
}
function loadListAdminList(){
$.getJSON('/api/list/list', showListAdminList);
}
function loadListList(){
$.getJSON("/api/list/list", showListList);
$.getJSON('/api/list/list', showListAdminList);
}
function loadUserList(){
$.getJSON("/api/user/list", showUserList);
$.getJSON('/api/user/list', showUserList);
}
function reload(){
window.location.reload(true);
}
function showList(listEmail){
console.log('showList('+listEmail+')');
$.post('/api/list/show',{list:listEmail},showListResult,'json');
}
function showListResult(result){
console.log(result);
if ('error' in result){
alert(result.error);
return;
}
if ('success' in result){
alert(result.success);
reload();
return;
}
alert("Api call did not return result");
}
function showListAdminList(data){
}
function showListList(data){
function showListAdminList(data){
for (let i in data.lists){
let list = data.lists[i];
let row = $('<tr/>');

26
static/templates/listadminlist.st

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
<fieldset>
<legend>List of mailinglists</legend>
<table id="listlist">
<tr>
<th colspan="4">List</th>
<th colspan="3">IMAP</th>
<th colspan="3">SMTP</th>
</tr>
<tr>
<th>Name</th>
<th>Address</th>
<th>State</th>
<th>Actions</th>
<th>Host</th>
<th>Port</th>
<th>User</th>
<th>Host</th>
<th>Port</th>
<th>User</th>
</tr>
</table>
<a href="add_list">Add new mailing list</a>
<script type="text/javascript">
loadListAdminList();
</script>
</fieldset>

19
static/templates/listlist.st

@ -2,24 +2,11 @@ @@ -2,24 +2,11 @@
<legend>List of mailinglists</legend>
<table id="listlist">
<tr>
<th colspan="4">List</th>
<th colspan="3">IMAP</th>
<th colspan="3">SMTP</th>
</tr>
<tr>
<th>Name</th>
<th>Address</th>
<th>State</th>
<th>Actions</th>
<th>Host</th>
<th>Port</th>
<th>User</th>
<th>Host</th>
<th>Port</th>
<th>User</th>
<th colspan="4">List Name</th>
<th colspan="3">List Address</th>
<th colspan="3">Actions</th>
</tr>
</table>
<a href="add_list">Add new mailing list</a>
<script type="text/javascript">
loadListList();
</script>

Loading…
Cancel
Save