working on editing of list properties
This commit is contained in:
@@ -5,6 +5,7 @@ public class Constants {
|
||||
public static final String BASE = "base";
|
||||
public static final String BASE_URL = "base_url";
|
||||
public static final String DB = "database";
|
||||
public static final String DOMAIN = "domain";
|
||||
public static final String EMAIL = "email";
|
||||
public static final String ERROR = "error";
|
||||
public static final String HOST = "host";
|
||||
@@ -17,6 +18,7 @@ public class Constants {
|
||||
public static final String NOTES = "notes";
|
||||
public static final String PASSWORD = "password";
|
||||
public static final Object PORT = "port";
|
||||
public static final String PREFIX = "prefix";
|
||||
public static final String PROTOCOL = "mail.store.protocol";
|
||||
public static final String STATE = "state";
|
||||
public static final String USER = "user";
|
||||
|
||||
@@ -66,6 +66,12 @@ public class MailingList {
|
||||
Database.open().query(sql.toString()).run();
|
||||
}
|
||||
|
||||
public static void enable(String listEmail, boolean enable) throws SQLException {
|
||||
// https://stackoverflow.com/questions/16440831/bitwise-xor-in-sqlite-bitwise-not-not-working-as-i-expect
|
||||
String expression = enable ? "state = state | "+ENABLED : "state = (~(state & "+ENABLED+"))&(state|"+ENABLED+")";
|
||||
Database.open().query("UPDATE " + TABLE_NAME + " SET "+expression).where(EMAIL, listEmail).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);
|
||||
@@ -119,7 +125,8 @@ public class MailingList {
|
||||
|
||||
public Map<String, Object> safeMap() {
|
||||
var map = new HashMap<String,Object>();
|
||||
map.put(EMAIL,email);
|
||||
String[] parts = email.split("@", 2);
|
||||
map.put(EMAIL,Map.of(PREFIX,parts[0],DOMAIN,parts[1]));
|
||||
map.put(NAME, name);
|
||||
if (imapHost != null) map.put(IMAP_HOST, imapHost);
|
||||
if (imapPort != 0) map.put(IMAP_PORT, imapPort);
|
||||
|
||||
@@ -22,6 +22,8 @@ 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_DISABLE = "list/disable";
|
||||
private static final String LIST_ENABLE = "list/enable";
|
||||
private static final String LIST_HIDE = "list/hide";
|
||||
private static final String LIST_SHOW = "list/show";
|
||||
private static final String USER_LIST = "user/list";
|
||||
@@ -79,14 +81,25 @@ public class Rest extends HttpServlet {
|
||||
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) {
|
||||
|
||||
var listEmail = req.getParameter(LIST);
|
||||
if (listEmail == null || listEmail.isBlank()) {
|
||||
json.putAll(Map.of(ERROR,"no list email provided!"));
|
||||
} else switch (path) {
|
||||
case LIST_DISABLE:
|
||||
json.putAll(enableList(listEmail,user,false));
|
||||
break;
|
||||
case LIST_ENABLE:
|
||||
json.putAll(enableList(listEmail,user,true));
|
||||
break;
|
||||
case LIST_HIDE:
|
||||
json.putAll(hideList(req,user,true));
|
||||
json.putAll(hideList(listEmail,user,true));
|
||||
break;
|
||||
case LIST_SHOW:
|
||||
json.putAll(hideList(req,user,false));
|
||||
json.putAll(hideList(listEmail,user,false));
|
||||
break;
|
||||
default:
|
||||
json.put(ERROR,t("No handler for path '{}'!",path));
|
||||
@@ -104,9 +117,22 @@ public class Rest extends HttpServlet {
|
||||
}
|
||||
}
|
||||
|
||||
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!");
|
||||
private Map enableList(String listEmail, User user, boolean enable) {
|
||||
if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
|
||||
try {
|
||||
MailingList.enable(listEmail,enable);
|
||||
return Map.of("success",t("Mailing list '{}' was {}!",listEmail,enable ? "enabled" : "disabled"));
|
||||
} catch (SQLException e) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> hideList(String listEmail, User user, boolean hide) {
|
||||
if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
|
||||
try {
|
||||
MailingList.hide(listEmail,hide);
|
||||
|
||||
Reference in New Issue
Block a user