Browse Source

pre-filling subscription form for logged-in users

drop_old_mail
Stephan Richter 3 years ago
parent
commit
9bd2cceb7d
  1. 2
      src/main/java/de/srsoftware/widerhall/data/ListMember.java
  2. 16
      src/main/java/de/srsoftware/widerhall/web/Web.java
  3. 7
      static/templates/js.st
  4. 2
      static/templates/listlist.st
  5. 4
      static/templates/subscribe.st

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

@ -77,7 +77,7 @@ public class ListMember {
public static String stateText(int state) { public static String stateText(int state) {
var words = new ArrayList<String>(); var words = new ArrayList<String>();
if ((state & STATE_OWNER) > 0) words.add("owener"); if ((state & STATE_OWNER) > 0) words.add("owner");
if ((state & STATE_SUBSCRIBER) > 0) words.add("subscriber"); if ((state & STATE_SUBSCRIBER) > 0) words.add("subscriber");
return String.join(", ",words); return String.join(", ",words);
} }

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

@ -313,7 +313,7 @@ public class Web extends HttpServlet {
var email = req.getParameter(EMAIL); var email = req.getParameter(EMAIL);
var pass = req.getParameter(PASSWORD); var pass = req.getParameter(PASSWORD);
var list = req.getParameter(LIST); var list = req.getParameter(LIST);
var data = new HashMap<String,String>(); var data = new HashMap<String,Object>();
data.put(NAME,name); data.put(NAME,name);
data.put(EMAIL,email); data.put(EMAIL,email);
data.put(LIST,list); data.put(LIST,list);
@ -329,12 +329,12 @@ public class Web extends HttpServlet {
if (pass != null && pass.isBlank()) pass = null; if (pass != null && pass.isBlank()) pass = null;
try { try {
User.create(email,name,pass); data.put(USER,User.create(email,name,pass).safeMap());
} catch (SQLException sqle) { } catch (SQLException sqle) {
var cause = getCausingException(sqle); var cause = getCausingException(sqle);
int code = cause.getErrorCode(); int code = cause.getErrorCode();
if (code == PRIMARY_KEY_CONSTRAINT) try {// user already exists if (code == PRIMARY_KEY_CONSTRAINT) try {// user already exists
User.load(email,pass); data.put(USER,User.load(email,pass).safeMap());
// success → subscribe // success → subscribe
} catch (InvalidKeyException | SQLException e) { } catch (InvalidKeyException | SQLException e) {
// invalid credentials // invalid credentials
@ -346,9 +346,13 @@ public class Web extends HttpServlet {
ListMember.create(list,email,ListMember.STATE_SUBSCRIBER); ListMember.create(list,email,ListMember.STATE_SUBSCRIBER);
data.put(NOTES,t("Successfully subscribed '{}' to '{}'.",email,list)); data.put(NOTES,t("Successfully subscribed '{}' to '{}'.",email,list));
return loadTemplate(INDEX,data,resp); return loadTemplate(INDEX,data,resp);
} catch (SQLException e) { } catch (SQLException sqle) {
LOG.debug("List subscription failed: ",e); LOG.debug("List subscription failed: ",sqle);
data.put(ERROR,t("Subscription failed: {}",e.getMessage())); var cause = getCausingException(sqle);
int code = cause.getErrorCode();
if (code == PRIMARY_KEY_CONSTRAINT) {// user already exists
data.put(ERROR,t("You already are member of this list!",sqle.getMessage()));
} else data.put(ERROR,t("Subscription failed: {}",sqle.getMessage()));
return loadTemplate(SUBSCRIBE,data,resp); return loadTemplate(SUBSCRIBE,data,resp);
} }
} }

7
static/templates/js.st

@ -81,14 +81,15 @@ function showListList(data){
for (let i in data.lists){ for (let i in data.lists){
let list = data.lists[i]; let list = data.lists[i];
let row = $('<tr/>'); let row = $('<tr/>');
let subBtn = $('<button/>',{onclick:"subscribeTo('"+list.email.domain+"', '"+list.email.prefix+"');"}).text('subscribe');
$('<td/>').text(list.name).appendTo(row); $('<td/>').text(list.name).appendTo(row);
$('<td/>',{class:'right'}).text(list.email.prefix).appendTo(row); $('<td/>',{class:'right'}).text(list.email.prefix).appendTo(row);
$('<td/>',{class:'right'}).text('@').appendTo(row); $('<td/>',{class:'right'}).text('@').appendTo(row);
$('<td/>').text(list.email.domain).appendTo(row); $('<td/>').text(list.email.domain).appendTo(row);
$('<td/>').text(list.state).appendTo(row); $('<td/>').text(list.state).appendTo(row);
$('<td/>').html(subBtn).appendTo(row); let td = $('<td/>',{class:'actions'});
$('<button/>',{onclick:"subscribeTo('"+list.email.domain+"', '"+list.email.prefix+"');"}).text('subscribe').appendTo(td);
$('<button/>',{onclick:"unsubscribeFrom('"+list.email.domain+"', '"+list.email.prefix+"');"}).text('unsubcribe').appendTo(td);
td.appendTo(row);
row.appendTo('#listlist'); row.appendTo('#listlist');
} }

2
static/templates/listlist.st

@ -1,5 +1,5 @@
<fieldset> <fieldset>
<legend>List of mailinglists</legend> <legend>List of (public) mailinglists</legend>
<table id="listlist"> <table id="listlist">
<tr> <tr>
<th>List Name</th> <th>List Name</th>

4
static/templates/subscribe.st

@ -16,11 +16,11 @@
<fieldset> <fieldset>
<legend>Suscribe to "«data.list»"</legend> <legend>Suscribe to "«data.list»"</legend>
<label> <label>
<input type="text" name="name" value="«data.name»"> <input type="text" name="name" value=if(data.user)»«data.user.name»«else»«data.name»«endif»">
Name Name
</label> </label>
<label> <label>
<input type="text" name="email" value="«data.email»"> <input type="text" name="email" value=if(data.user)»«data.user.email»«else»«data.email»«endif»">
Email Email
</label> </label>
<label> <label>

Loading…
Cancel
Save