Browse Source

Löschen von Nachrichten aus dem Archiv implementiert. Dies Löst #2

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
main
Stephan Richter 8 months ago
parent
commit
89fda62c9d
  1. 2
      src/main/java/de/srsoftware/widerhall/Constants.java
  2. 1
      src/main/java/de/srsoftware/widerhall/data/MailingList.java
  3. 10
      src/main/java/de/srsoftware/widerhall/data/Post.java
  4. 33
      src/main/java/de/srsoftware/widerhall/web/Rest.java
  5. 6
      src/main/java/de/srsoftware/widerhall/web/Web.java
  6. 3
      static/templates/archive.st
  7. 9
      static/templates/js.st

2
src/main/java/de/srsoftware/widerhall/Constants.java

@ -18,6 +18,8 @@ public class Constants {
public static final String LIST = "list"; public static final String LIST = "list";
public static final String LOCATIONS = "locations"; public static final String LOCATIONS = "locations";
public static final String NAME = "name"; public static final String NAME = "name";
public static final String MESSAGE_ID = "message_id";
public static final String MODERATOR = "moderator";
public static final String MONTH = "month"; public static final String MONTH = "month";
public static final String NOTES = "notes"; public static final String NOTES = "notes";
public static final String PASSWORD = "password"; public static final String PASSWORD = "password";

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

@ -328,6 +328,7 @@ public class MailingList implements MessageHandler, ProblemListener {
} }
public boolean mayBeAlteredBy(User user) { public boolean mayBeAlteredBy(User user) {
if (user == null) return false;
if (user.hashPermission(PERMISSION_ADMIN)) return true; if (user.hashPermission(PERMISSION_ADMIN)) return true;
try { try {
if (ListMember.load(this,user).isModerator()) return true; if (ListMember.load(this,user).isModerator()) return true;

10
src/main/java/de/srsoftware/widerhall/data/Post.java

@ -141,6 +141,10 @@ public class Post {
return id; return id;
} }
public MailingList list() {
return list;
}
public static Post load(String id) throws SQLException { public static Post load(String id) throws SQLException {
var rs = Database.open().select(TABLE_NAME).where(ID,id).compile().exec(); var rs = Database.open().select(TABLE_NAME).where(ID,id).compile().exec();
try { try {
@ -161,6 +165,11 @@ public class Post {
FILE,filename); FILE,filename);
} }
public void remove() throws SQLException {
Database.open().deleteFrom(TABLE_NAME).where(ID,id).compile().run();
file().delete();
}
public Map<String,Object> safeMap() { public Map<String,Object> safeMap() {
return Map.of(ID,id, return Map.of(ID,id,
LIST,list.name(), LIST,list.name(),
@ -186,5 +195,4 @@ public class Post {
public long timestamp(){ public long timestamp(){
return timestamp; return timestamp;
} }
} }

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

@ -39,6 +39,7 @@ public class Rest extends HttpServlet {
private static final String LIST_SHOW = "list/show"; private static final String LIST_SHOW = "list/show";
private static final String LIST_TEST = "list/test"; private static final String LIST_TEST = "list/test";
private static final String LIST_SUBSCRIBABLE = "list/subscribable"; private static final String LIST_SUBSCRIBABLE = "list/subscribable";
private static final String MAIL_DROP = "mail/drop";
private static final String USER_ADD_PERMISSION = "user/addpermission"; private static final String USER_ADD_PERMISSION = "user/addpermission";
private static final String USER_DROP_PERMISSION = "user/droppermission"; private static final String USER_DROP_PERMISSION = "user/droppermission";
private static final String USER_LIST = "user/list"; private static final String USER_LIST = "user/list";
@ -61,15 +62,16 @@ public class Rest extends HttpServlet {
return Map.of(SUCCESS,"Updated user permissions"); return Map.of(SUCCESS,"Updated user permissions");
} }
private Map<String, Object> archive(HttpServletRequest req) { private Map<String, Object> archive(HttpServletRequest req, User user) {
var list = Util.getMailingList(req); var list = Util.getMailingList(req);
if (list != null){ if (list != null){
boolean userIsMod = list.mayBeAlteredBy(user);
try { try {
var month = req.getParameter(MONTH); var month = req.getParameter(MONTH);
if (month == null || month.isBlank()) { if (month == null || month.isBlank()) {
return Map.of(LIST,list.email(),"summary",Post.summarize(list)); return Map.of(LIST,list.email(),MODERATOR,userIsMod,"summary",Post.summarize(list));
} else { } else {
return Map.of(LIST,list.email(),"posts",Post.find(list,month).stream().map(Post::safeMap).toList()); return Map.of(LIST,list.email(),MODERATOR,userIsMod,"posts",Post.find(list,month).stream().map(Post::safeMap).toList());
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@ -118,6 +120,21 @@ public class Rest extends HttpServlet {
if (error != null) resp.sendError(400,error); if (error != null) resp.sendError(400,error);
} }
private Map dropMail(String messageId,User user){
try {
var message = Post.load(messageId);
if (message == null) return Map.of(ERROR,t("Cannot remove: unknown message id"));
var allowed = message.list().mayBeAlteredBy(user);
if (allowed){
message.remove();
return Map.of(SUCCESS,t("Message deleted"));
}
return Map.of(ERROR,t("You are not allowed to remove messages from this list!"));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private Map dropPermission(String userEmail, String permissions) { private Map dropPermission(String userEmail, String permissions) {
if (userEmail == null || userEmail.isBlank()) return Map.of(ERROR,"missing user email address!"); if (userEmail == null || userEmail.isBlank()) return Map.of(ERROR,"missing user email address!");
try { try {
@ -131,7 +148,7 @@ public class Rest extends HttpServlet {
LOG.debug("Failed to load user for address {}",userEmail,e); LOG.debug("Failed to load user for address {}",userEmail,e);
return Map.of(ERROR,t("Failed to load user for address {}",userEmail)); return Map.of(ERROR,t("Failed to load user for address {}",userEmail));
} }
return Map.of(SUCCESS,"Updated user permissions"); return Map.of(SUCCESS,t("Updated user permissions"));
} }
private Map enableList(MailingList list, User user, boolean enable) { private Map enableList(MailingList list, User user, boolean enable) {
@ -156,7 +173,7 @@ public class Rest extends HttpServlet {
json.put(USER,user.safeMap()); json.put(USER,user.safeMap());
switch (path) { switch (path) {
case LIST_ARCHIVE: case LIST_ARCHIVE:
json.put("archive",archive(req)); json.put("archive",archive(req,user));
break; break;
case USER_LIST: case USER_LIST:
try { try {
@ -179,7 +196,7 @@ public class Rest extends HttpServlet {
} else { } else {
switch (path) { switch (path) {
case LIST_ARCHIVE: case LIST_ARCHIVE:
json.put("archive",archive(req)); json.put("archive",archive(req, user));
break; break;
case LIST_SUBSCRIBABLE: case LIST_SUBSCRIBABLE:
json.put("lists", MailingList.subscribable().stream().map(MailingList::minimalMap).toList()); json.put("lists", MailingList.subscribable().stream().map(MailingList::minimalMap).toList());
@ -245,6 +262,10 @@ public class Rest extends HttpServlet {
case LIST_TEST: case LIST_TEST:
json.putAll(testList(list,user)); json.putAll(testList(list,user));
break; break;
case MAIL_DROP:
var messageId = req.getParameter(MESSAGE_ID);
json.putAll(dropMail(messageId,user));
break;
case USER_ADD_PERMISSION: case USER_ADD_PERMISSION:
if (user.hashPermission(User.PERMISSION_ADMIN)){ if (user.hashPermission(User.PERMISSION_ADMIN)){
json.putAll(addPermission(userEmail,permissions)); json.putAll(addPermission(userEmail,permissions));

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

@ -139,7 +139,11 @@ public class Web extends TemplateServlet {
map.put(LIST,list.email()); map.put(LIST,list.email());
var month = req.getParameter(MONTH); var month = req.getParameter(MONTH);
if (month != null && !month.isBlank())map.put(MONTH,month); if (month != null && !month.isBlank()){
map.put(MONTH,month);
var user = Util.getUser(req);
map.put(MODERATOR,list.mayBeAlteredBy(user));
}
return loadTemplate(ARCHIVE,map,resp); return loadTemplate(ARCHIVE,map,resp);
} }

3
static/templates/archive.st

@ -18,6 +18,9 @@
<th>Date</th> <th>Date</th>
<th>From</th> <th>From</th>
<th>Subject</th> <th>Subject</th>
«if (data.moderator)»
<th>Actions</th>
«endif»
</tr> </tr>
</table> </table>
<script type="text/javascript"> <script type="text/javascript">

9
static/templates/js.st

@ -16,6 +16,10 @@ function dropList(listEmail){
$.post('/api/list/drop',{list:listEmail},showListResult,'json'); $.post('/api/list/drop',{list:listEmail},showListResult,'json');
} }
function dropMail(mailId){
$.post('/api/mail/drop',{message_id:mailId},showListResult,'json');
}
function dropMember(userEmail,listEmail){ function dropMember(userEmail,listEmail){
$.post('/api/list/drop_member',{list:listEmail,email:userEmail},reload,'json'); $.post('/api/list/drop_member',{list:listEmail,email:userEmail},reload,'json');
} }
@ -78,6 +82,8 @@ function showList(listEmail){
function showListArchive(data){ function showListArchive(data){
console.log(data); console.log(data);
let moderator = data.archive.moderator;
console.log('moderator: ',moderator);
let posts = data.archive.posts; let posts = data.archive.posts;
for (let time in posts){ for (let time in posts){
let post = posts[time]; let post = posts[time];
@ -86,6 +92,9 @@ function showListArchive(data){
$('<td/>').html('<a href="'+url+'">'+post.date+'</a>').appendTo(row); $('<td/>').html('<a href="'+url+'">'+post.date+'</a>').appendTo(row);
$('<td/>').html('<a href="'+url+'">'+post.from_name+'</a>').appendTo(row); $('<td/>').html('<a href="'+url+'">'+post.from_name+'</a>').appendTo(row);
$('<td/>').html('<a href="'+url+'">'+post.subject+'</a>').appendTo(row); $('<td/>').html('<a href="'+url+'">'+post.subject+'</a>').appendTo(row);
if (moderator){
$('<button/>',{onclick:"dropMail('"+post.id+"');"}).text('Delete').appendTo($('<td/>')).appendTo(row);
}
row.appendTo($('#archive table')); row.appendTo($('#archive table'));
} }
} }

Loading…
Cancel
Save