Browse Source

working on mailing list options

drop_old_mail
Stephan Richter 3 years ago
parent
commit
68f361e15b
  1. 2
      pom.xml
  2. 4
      src/main/java/de/srsoftware/widerhall/Util.java
  3. 21
      src/main/java/de/srsoftware/widerhall/data/MailingList.java
  4. 1
      src/main/java/de/srsoftware/widerhall/web/Rest.java
  5. 39
      src/main/java/de/srsoftware/widerhall/web/Web.java
  6. 4
      static/templates/inspect.st
  7. 2
      static/templates/js.st

2
pom.xml

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
<groupId>org.example</groupId>
<artifactId>Widerhall</artifactId>
<version>0.0.17</version>
<version>0.0.18</version>
<build>
<plugins>
<plugin>

4
src/main/java/de/srsoftware/widerhall/Util.java

@ -114,4 +114,8 @@ public class Util { @@ -114,4 +114,8 @@ public class Util {
if (listEmail == null || listEmail.isBlank()) return null;
return MailingList.load(listEmail);
}
public static boolean getCheckbox(HttpServletRequest req, String key) {
return "on".equals(req.getParameter(key));
}
}

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

@ -39,6 +39,7 @@ public class MailingList implements MessageHandler { @@ -39,6 +39,7 @@ public class MailingList implements MessageHandler {
private static final int STATE_ENABLED = 1;
private static final int STATE_PUBLIC = 2;
public static final int STATE_FORWARD_FROM = 4;
public static final int STATE_FORWARD_ATTACHED = 8;
private static final int VISIBLE = 1;
private static final int HIDDEN = 0;
private final String name;
@ -131,8 +132,7 @@ public class MailingList implements MessageHandler { @@ -131,8 +132,7 @@ public class MailingList implements MessageHandler {
public void enable(boolean enable) throws SQLException {
state = enable ? state | STATE_ENABLED : state ^ (state & STATE_ENABLED);
Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run();
setFlag(STATE_ENABLED,enable);
if (enable) {
imap.start().addListener(this);
@ -151,6 +151,15 @@ public class MailingList implements MessageHandler { @@ -151,6 +151,15 @@ public class MailingList implements MessageHandler {
}
}
public void forwardAttached(boolean forward) throws SQLException {
setFlag(STATE_FORWARD_ATTACHED,forward);
}
public void forwardFrom(boolean forward) throws SQLException {
setFlag(STATE_FORWARD_FROM,forward);
}
/**
* Create a mailing list object from a ResultSet.
* This is a cached method: if the ML has been loaded before, the already-loaded object will be returned.
@ -182,8 +191,7 @@ public class MailingList implements MessageHandler { @@ -182,8 +191,7 @@ public class MailingList implements MessageHandler {
}
public void hide(boolean hide) throws SQLException {
state = hide ? state ^ (state & STATE_PUBLIC) : state | STATE_PUBLIC;
Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run();
setFlag(STATE_PUBLIC,!hide);
}
/**
@ -362,6 +370,11 @@ public class MailingList implements MessageHandler { @@ -362,6 +370,11 @@ public class MailingList implements MessageHandler {
smtp.login().send(email(),name(),user.email(),subject,text);
}
private void setFlag(int flag, boolean on) throws SQLException {
state = on ? state | flag : state ^ (state & flag);
Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run();
}
public Map<String,Integer> stateMap(){
var map = new HashMap<String,Integer>();
if (hasState(STATE_ENABLED)) map.put("enabled",VISIBLE);

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

@ -219,6 +219,7 @@ public class Rest extends HttpServlet { @@ -219,6 +219,7 @@ public class Rest extends HttpServlet {
if (list == null) return Map.of(ERROR,t("Mailinglist {} unknown",list.email()));
var map = new HashMap<>();
if (list.hasState(MailingList.STATE_FORWARD_FROM)) map.put("forward_from",true);
if (list.hasState(MailingList.STATE_FORWARD_ATTACHED)) map.put("forward_attached",true);
return map;
}

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

@ -4,6 +4,7 @@ import de.srsoftware.widerhall.Util; @@ -4,6 +4,7 @@ import de.srsoftware.widerhall.Util;
import de.srsoftware.widerhall.data.ListMember;
import de.srsoftware.widerhall.data.MailingList;
import de.srsoftware.widerhall.data.User;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -269,19 +270,37 @@ public class Web extends TemplateServlet { @@ -269,19 +270,37 @@ public class Web extends TemplateServlet {
}
private String inspect(HttpServletRequest req, HttpServletResponse resp) {
var o = req.getSession().getAttribute(USER);
if (!(o instanceof User user)) {
return redirectTo(LOGIN,resp);
var user = Util.getUser(req);
if (user == null) return redirectTo(LOGIN,resp);
var data = new HashMap<String,Object>();
var error = false;
var list = Util.getMailingList(req);
if (list == null) {
error = true;
data.put(ERROR, t("No valid mailing list provided!"));
} else data.put(LIST, list.email());
if (!error && !list.mayBeAlteredBy(user)) {
error = true;
data.put(ERROR,t("You are not allowed to alter this list!"));
}
var post = req.getParameterMap();
var listEmail = req.getParameter(LIST);
var list = MailingList.load(listEmail);
if (list == null) return t("{} is does not denote a valid list",listEmail);
if (!list.mayBeAlteredBy(user)) {
if (!error){
var dummy = req.getParameterMap();
try {
list.forwardFrom(Util.getCheckbox(req, "forward_from"));
list.forwardAttached(Util.getCheckbox(req, "forward_attached"));
data.put(NOTES,t("Sucessfully updated MailingList!"));
} catch (SQLException e){
LOG.warn("Failed to update MailingList:",e);
data.put(ERROR,t("Failed to update MailingList!"));
}
LOG.debug("POST: {}",post);
return null;
LOG.debug("params: {}",dummy);
}
return loadTemplate(INSPECT,data,resp);
}

4
static/templates/inspect.st

@ -19,6 +19,10 @@ @@ -19,6 +19,10 @@
<input type="checkbox" name="forward_from">
Forward using original sender
</label>
<label>
<input type="checkbox" name="forward_attached">
Append original message as attachment
</label>
<button type="submit">Save</button>
</fieldset>
</form>

2
static/templates/js.st

@ -57,6 +57,8 @@ function showList(listEmail){ @@ -57,6 +57,8 @@ function showList(listEmail){
function showListDetail(data){
console.log(data);
if (data.forward_from) $('input[name="forward_from"]').prop('checked',true);
if (data.forward_attached) $('input[name="forward_attached"]').prop('checked',true);
}
function showListOfEditableLists(data){

Loading…
Cancel
Save