Löschen von Nachrichten aus dem Archiv implementiert. Dies Löst #2
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -18,6 +18,8 @@ public class Constants {
|
||||
public static final String LIST = "list";
|
||||
public static final String LOCATIONS = "locations";
|
||||
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 NOTES = "notes";
|
||||
public static final String PASSWORD = "password";
|
||||
|
||||
@@ -328,6 +328,7 @@ public class MailingList implements MessageHandler, ProblemListener {
|
||||
}
|
||||
|
||||
public boolean mayBeAlteredBy(User user) {
|
||||
if (user == null) return false;
|
||||
if (user.hashPermission(PERMISSION_ADMIN)) return true;
|
||||
try {
|
||||
if (ListMember.load(this,user).isModerator()) return true;
|
||||
|
||||
@@ -141,6 +141,10 @@ public class Post {
|
||||
return id;
|
||||
}
|
||||
|
||||
public MailingList list() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public static Post load(String id) throws SQLException {
|
||||
var rs = Database.open().select(TABLE_NAME).where(ID,id).compile().exec();
|
||||
try {
|
||||
@@ -161,6 +165,11 @@ public class Post {
|
||||
FILE,filename);
|
||||
}
|
||||
|
||||
public void remove() throws SQLException {
|
||||
Database.open().deleteFrom(TABLE_NAME).where(ID,id).compile().run();
|
||||
file().delete();
|
||||
}
|
||||
|
||||
public Map<String,Object> safeMap() {
|
||||
return Map.of(ID,id,
|
||||
LIST,list.name(),
|
||||
@@ -186,5 +195,4 @@ public class Post {
|
||||
public long timestamp(){
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ public class Rest extends HttpServlet {
|
||||
private static final String LIST_SHOW = "list/show";
|
||||
private static final String LIST_TEST = "list/test";
|
||||
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_DROP_PERMISSION = "user/droppermission";
|
||||
private static final String USER_LIST = "user/list";
|
||||
@@ -61,15 +62,16 @@ public class Rest extends HttpServlet {
|
||||
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);
|
||||
if (list != null){
|
||||
boolean userIsMod = list.mayBeAlteredBy(user);
|
||||
try {
|
||||
var month = req.getParameter(MONTH);
|
||||
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 {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
@@ -118,6 +120,21 @@ public class Rest extends HttpServlet {
|
||||
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) {
|
||||
if (userEmail == null || userEmail.isBlank()) return Map.of(ERROR,"missing user email address!");
|
||||
try {
|
||||
@@ -131,7 +148,7 @@ public class Rest extends HttpServlet {
|
||||
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(SUCCESS,"Updated user permissions");
|
||||
return Map.of(SUCCESS,t("Updated user permissions"));
|
||||
}
|
||||
|
||||
private Map enableList(MailingList list, User user, boolean enable) {
|
||||
@@ -156,7 +173,7 @@ public class Rest extends HttpServlet {
|
||||
json.put(USER,user.safeMap());
|
||||
switch (path) {
|
||||
case LIST_ARCHIVE:
|
||||
json.put("archive",archive(req));
|
||||
json.put("archive",archive(req,user));
|
||||
break;
|
||||
case USER_LIST:
|
||||
try {
|
||||
@@ -179,7 +196,7 @@ public class Rest extends HttpServlet {
|
||||
} else {
|
||||
switch (path) {
|
||||
case LIST_ARCHIVE:
|
||||
json.put("archive",archive(req));
|
||||
json.put("archive",archive(req, user));
|
||||
break;
|
||||
case LIST_SUBSCRIBABLE:
|
||||
json.put("lists", MailingList.subscribable().stream().map(MailingList::minimalMap).toList());
|
||||
@@ -245,6 +262,10 @@ public class Rest extends HttpServlet {
|
||||
case LIST_TEST:
|
||||
json.putAll(testList(list,user));
|
||||
break;
|
||||
case MAIL_DROP:
|
||||
var messageId = req.getParameter(MESSAGE_ID);
|
||||
json.putAll(dropMail(messageId,user));
|
||||
break;
|
||||
case USER_ADD_PERMISSION:
|
||||
if (user.hashPermission(User.PERMISSION_ADMIN)){
|
||||
json.putAll(addPermission(userEmail,permissions));
|
||||
|
||||
@@ -139,7 +139,11 @@ public class Web extends TemplateServlet {
|
||||
map.put(LIST,list.email());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
<th>Date</th>
|
||||
<th>From</th>
|
||||
<th>Subject</th>
|
||||
«if (data.moderator)»
|
||||
<th>Actions</th>
|
||||
«endif»
|
||||
</tr>
|
||||
</table>
|
||||
<script type="text/javascript">
|
||||
|
||||
@@ -16,6 +16,10 @@ function dropList(listEmail){
|
||||
$.post('/api/list/drop',{list:listEmail},showListResult,'json');
|
||||
}
|
||||
|
||||
function dropMail(mailId){
|
||||
$.post('/api/mail/drop',{message_id:mailId},showListResult,'json');
|
||||
}
|
||||
|
||||
function dropMember(userEmail,listEmail){
|
||||
$.post('/api/list/drop_member',{list:listEmail,email:userEmail},reload,'json');
|
||||
}
|
||||
@@ -78,6 +82,8 @@ function showList(listEmail){
|
||||
|
||||
function showListArchive(data){
|
||||
console.log(data);
|
||||
let moderator = data.archive.moderator;
|
||||
console.log('moderator: ',moderator);
|
||||
let posts = data.archive.posts;
|
||||
for (let time in posts){
|
||||
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.from_name+'</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'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user