Merge branch 'main' into lang_de

This commit is contained in:
2022-04-22 13:06:41 +02:00
17 changed files with 613 additions and 145 deletions

View File

@@ -21,16 +21,20 @@ import java.util.Map;
import static de.srsoftware.widerhall.Constants.*;
import static de.srsoftware.widerhall.Util.t;
import static de.srsoftware.widerhall.data.MailingList.*;
public class Rest extends HttpServlet {
private static final Logger LOG = LoggerFactory.getLogger(Rest.class);
private static final String LIST_ADD_MOD = "list/add_mod";
private static final String LIST_ARCHIVE = "list/archive";
private static final String LIST_DISABLE = "list/disable";
private static final String LIST_EDITABLE = "list/editable";
private static final String LIST_DROP_MEMBER = "list/drop_member";
private static final String LIST_DROP_MOD = "list/drop_mod";
private static final String LIST_DETAIL = "list/detail";
private static final String LIST_ENABLE = "list/enable";
private static final String LIST_HIDE = "list/hide";
private static final String LIST_MEMBERS = "list/members";
private static final String LIST_MODERATED = "list/moderated";
private static final String LIST_SHOW = "list/show";
private static final String LIST_TEST = "list/test";
private static final String LIST_SUBSCRIBABLE = "list/subscribable";
@@ -56,6 +60,19 @@ public class Rest extends HttpServlet {
return Map.of(SUCCESS,"Nutzer-Berechtigungen aktualisiert");
}
private List<? extends Object> archive(HttpServletRequest req) {
var list = Util.getMailingList(req);
if (list != null){
try {
return Post.find(list).stream().map(Post::safeMap).toList();
} catch (SQLException e) {
e.printStackTrace();
}
}
LOG.debug("list: {}",list.email());
return List.of();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String error = handleGet(req, resp);
@@ -105,6 +122,9 @@ public class Rest extends HttpServlet {
if (user != null){
json.put(USER,user.safeMap());
switch (path) {
case LIST_ARCHIVE:
json.put("archive",archive(req));
break;
case USER_LIST:
try {
json.put("users", (user.hashPermission(User.PERMISSION_ADMIN) ? User.loadAll() : List.of(user)).stream().map(User::safeMap).toList());
@@ -113,8 +133,8 @@ public class Rest extends HttpServlet {
json.put(ERROR,"Laden der Nutzerliste fehlgeschlagen");
}
break;
case LIST_EDITABLE:
json.put("lists", MailingList.editableBy(user).stream().map(MailingList::safeMap).toList());
case LIST_MODERATED:
json.put("lists", MailingList.moderatedBy(user).stream().map(MailingList::safeMap).toList());
break;
case LIST_SUBSCRIBABLE:
json.put("lists", MailingList.subscribable(user).stream().map(MailingList::minimalMap).toList());
@@ -144,18 +164,7 @@ public class Rest extends HttpServlet {
}
}
private List<? extends Object> archive(HttpServletRequest req) {
var list = Util.getMailingList(req);
if (list != null){
try {
return Post.find(list).stream().map(Post::safeMap).toList();
} catch (SQLException e) {
e.printStackTrace();
}
}
LOG.debug("list: {}",list.email());
return List.of();
}
public String handlePost(HttpServletRequest req, HttpServletResponse resp){
@@ -170,12 +179,21 @@ public class Rest extends HttpServlet {
var userEmail = req.getParameter(EMAIL);
var permissions = req.getParameter(PERMISSIONS);
switch (path) {
case LIST_ADD_MOD:
json.putAll(listAddMod(list,userEmail,user));
break;
case LIST_DETAIL:
json.putAll(listDetail(list,user));
break;
case LIST_DISABLE:
json.putAll(enableList(list,user,false));
break;
case LIST_DROP_MEMBER:
json.putAll(listDropMember(list,userEmail,user));
break;
case LIST_DROP_MOD:
json.putAll(listDropMod(list,userEmail,user));
break;
case LIST_ENABLE:
json.putAll(enableList(list,user,true));
break;
@@ -229,18 +247,65 @@ public class Rest extends HttpServlet {
}
}
private Map listAddMod(MailingList list, String userEmail, User user) {
ListMember moderator = null;
try {
moderator = ListMember.load(list,user);
} catch (SQLException e) {
LOG.warn("Failed to load list member for {}/{}",user.email(),list.email(),e);
return Map.of(ERROR,t("Failed to load list member for {}/{}",user.email(),list.email()));
}
if (moderator == null) return Map.of(ERROR,t("{} is not a member of {}",user.email(),list.email()));
var error = moderator.addNewModerator(userEmail);
return error == null ? Map.of(SUCCESS,t("{} is now a moderator of {}",userEmail,list.email())) : Map.of(ERROR,error);
}
private Map listDetail(MailingList list, User user) {
if (list == null) return Map.of(ERROR,"Keine Listen-Email übertragen!");
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);
if (list.hasState(MailingList.STATE_HIDE_RECEIVERS)) map.put("hide_receivers",true);
if (list.hasState(MailingList.STATE_REPLY_TO_LIST)) map.put("reply_to_list",true);
if (list.hasState(MailingList.STATE_OPEN)) map.put("open",true);
if (list.hasState(MailingList.STATE_PUBLIC_ARCHIVE)) map.put("archive",true);
if (list.hasState(MailingList.STATE_FORWARD_FROM)) map.put(KEY_FORWARD_FROM,true);
if (list.hasState(MailingList.STATE_FORWARD_ATTACHED)) map.put(KEY_FORWARD_ATTACHED,true);
if (list.hasState(MailingList.STATE_HIDE_RECEIVERS)) map.put(KEY_HIDE_RECEIVERS,true);
if (list.hasState(MailingList.STATE_REPLY_TO_LIST)) map.put(KEY_REPLY_TO_LIST,true);
if (list.isOpenForGuests()) map.put(KEY_OPEN_FOR_GUESTS,true);
if (list.isOpenForSubscribers()) map.put(KEY_OPEN_FOR_SUBSCRIBERS,true);
if (list.hasState(MailingList.STATE_PUBLIC_ARCHIVE)) map.put(KEY_ARCHIVE,true);
if (list.hasState(STATE_MODS_CAN_EDIT_MODS)) map.put(KEY_MODS_CAN_EDIT_MODS,true);
return map;
}
private Map listDropMember(MailingList list, String userEmail, User user) {
ListMember moderator = null;
try {
moderator = ListMember.load(list,user);
} catch (SQLException e) {
LOG.warn("Failed to load list member for {}/{}",user.email(),list.email(),e);
return Map.of(ERROR,t("Failed to load list member for {}/{}",user.email(),list.email()));
}
if (moderator == null) return Map.of(ERROR,t("{} is not a member of {}",user.email(),list.email()));
var error = moderator.dropMember(userEmail);
return error == null ? Map.of(SUCCESS,t("{} is now a moderator of {}",userEmail,list.email())) : Map.of(ERROR,error);
}
private Map listDropMod(MailingList list, String userEmail, User user) {
ListMember moderator = null;
try {
moderator = ListMember.load(list,user);
} catch (SQLException e) {
LOG.warn("Failed to load list member for {}/{}",user.email(),list.email(),e);
return Map.of(ERROR,t("Failed to load list member for {}/{}",user.email(),list.email()));
}
if (moderator == null) return Map.of(ERROR,t("{} is not a member of {}",user.email(),list.email()));
var error = moderator.dropModerator(userEmail);
return error == null ? Map.of(SUCCESS,t("{} is now a moderator of {}",userEmail,list.email())) : Map.of(ERROR,error);
}
private Map<String, Object> listMembers(MailingList list, User user) {
if (list == null) return Map.of(ERROR,"Keine Listen-Email übertragen!");
if (!list.membersMayBeListedBy(user)) Map.of(ERROR,t("Es ist dir nicht gestattet, die Mitglieder von '{}' aufzulisten",list.email()));
@@ -249,7 +314,7 @@ public class Rest extends HttpServlet {
.stream()
.map(ListMember::safeMap)
.toList();
return Map.of(MEMBERS,members);
return Map.of(MEMBERS,members,LIST,list.minimalMap());
} catch (SQLException e) {
LOG.error("Laden der Mitglieder-Liste fehlgeschlagen: ",e);
return Map.of(ERROR,t("Laden der Mitglieder-Liste von '{}' fehlgeschlagen!",list.email()));

View File

@@ -5,7 +5,6 @@ import de.srsoftware.widerhall.data.ListMember;
import de.srsoftware.widerhall.data.MailingList;
import de.srsoftware.widerhall.data.Post;
import de.srsoftware.widerhall.data.User;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -21,6 +20,7 @@ import java.util.Map;
import static de.srsoftware.widerhall.Constants.*;
import static de.srsoftware.widerhall.Util.t;
import static de.srsoftware.widerhall.data.MailingList.*;
public class Web extends TemplateServlet {
public static final String WEB_ROOT = "/web";
@@ -295,17 +295,19 @@ public class Web extends TemplateServlet {
if (!error && !list.mayBeAlteredBy(user)) {
error = true;
data.put(ERROR,t("Es ist Ihnen nicht gestattet, diese Mailinglist zu verändern!"));
data.put(ERROR,t("Es ist Ihnen nicht gestattet, die Einselltungen dieser Mailingliste zu verändern!"));
}
if (!error){
try {
list.forwardFrom(Util.getCheckbox(req, "forward_from"))
.forwardAttached(Util.getCheckbox(req, "forward_attached"))
.hideReceivers(Util.getCheckbox(req, "hide_receivers"))
.replyToList(Util.getCheckbox(req, "reply_to_list"))
.open(Util.getCheckbox(req,"open"))
.archive(Util.getCheckbox(req,"archive"));
list.forwardFrom(Util.getCheckbox(req, KEY_FORWARD_FROM))
.forwardAttached(Util.getCheckbox(req, KEY_FORWARD_ATTACHED))
.hideReceivers(Util.getCheckbox(req, KEY_HIDE_RECEIVERS))
.replyToList(Util.getCheckbox(req, KEY_REPLY_TO_LIST))
.modsMayNominateMods(Util.getCheckbox(req, KEY_MODS_CAN_EDIT_MODS))
.openForGuests(Util.getCheckbox(req,KEY_OPEN_FOR_GUESTS))
.openForSubscribers(Util.getCheckbox(req,KEY_OPEN_FOR_SUBSCRIBERS))
.archive(Util.getCheckbox(req,KEY_ARCHIVE));
data.put(NOTES,t("Mailing-Liste aktualisiert!"));
} catch (SQLException e){
LOG.warn("Aktualisierung der Mailing-Liste fehlgeschlagen:",e);
@@ -328,7 +330,7 @@ public class Web extends TemplateServlet {
return loadTemplate("post",map,resp);
} catch (SQLException | IOException e) {
LOG.debug("Failed to load post from file!",e);
return t("Failed to load post from file!");
return t("Laden der Nachricht aus Datei fehlgeschlagen!");
}
}
@@ -481,10 +483,24 @@ public class Web extends TemplateServlet {
return loadTemplate(UNSUBSCRIBE,data,resp);
}
}
// if we get here, we should have a valid user
ListMember member = null;
try {
ListMember.unsubscribe(list,user);
member = ListMember.load(list,user);
} catch (SQLException e) {
LOG.debug("Laden des Listenmitglieds für {}/{} fehlgeschlagen",user.email(),list.email(),e);
data.put(ERROR, t("Laden des Listenmitglieds für {}/{} fehlgeschlagen",user.email(),list.email()));
return loadTemplate(UNSUBSCRIBE,data,resp);
}
if (member == null){
data.put(ERROR, t("{} ist kein Mitglied von {}",user.email(),list.email()));
return loadTemplate(UNSUBSCRIBE,data,resp);
}
// if we get here, we should have a valid member object
try {
member.unsubscribe();
data.put(NOTES,t("'{}' erfolgreich abbestellt.",list.email()));
return loadTemplate(INDEX,data,resp);
} catch (SQLException e) {
LOG.warn("Es ist ein Problem beim Entfernen von {} aus der Liste {} aufgetreten:",user.email(),list.email(),e);