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 { @@ -77,7 +77,7 @@ public class ListMember {
public static String stateText(int state) {
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");
return String.join(", ",words);
}

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

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

7
static/templates/js.st

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

2
static/templates/listlist.st

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

4
static/templates/subscribe.st

@ -16,11 +16,11 @@ @@ -16,11 +16,11 @@
<fieldset>
<legend>Suscribe to "«data.list»"</legend>
<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
</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
</label>
<label>

Loading…
Cancel
Save