Allow deletion of forum replies #111

Open
ansv7779 wants to merge 5 commits from remove-forum-post into develop
7 changed files with 97 additions and 7 deletions
Showing only changes of commit 3933cfa2be - Show all commits

View File

@ -348,14 +348,16 @@ public class CoreConfig {
ForumPostReadStateRepository readStateRepository, ForumPostReadStateRepository readStateRepository,
AbstractThreadRepository threadRepository, AbstractThreadRepository threadRepository,
FileService fileService, FileService fileService,
EventBus eventBus EventBus eventBus,
CurrentUser currentUser
) { ) {
return new BasicForumServiceImpl( return new BasicForumServiceImpl(
forumPostRepository, forumPostRepository,
readStateRepository, readStateRepository,
threadRepository, threadRepository,
fileService, fileService,
eventBus eventBus,
currentUser
); );
} }

View File

@ -27,4 +27,8 @@ public interface BasicForumService extends Serializable {
ForumPost getLastPost(ForumThread forumThread); ForumPost getLastPost(ForumThread forumThread);
boolean hasAttachments(ForumThread forumThread); boolean hasAttachments(ForumThread forumThread);
boolean canDelete(ForumPost forumPost);
void deletePost(ForumPost post);
} }

View File

@ -10,6 +10,7 @@ import se.su.dsv.scipro.file.FileService;
import se.su.dsv.scipro.forum.dataobjects.ForumPost; import se.su.dsv.scipro.forum.dataobjects.ForumPost;
import se.su.dsv.scipro.forum.dataobjects.ForumPostReadState; import se.su.dsv.scipro.forum.dataobjects.ForumPostReadState;
import se.su.dsv.scipro.forum.dataobjects.ForumThread; import se.su.dsv.scipro.forum.dataobjects.ForumThread;
import se.su.dsv.scipro.system.CurrentUser;
import se.su.dsv.scipro.system.User; import se.su.dsv.scipro.system.User;
public class BasicForumServiceImpl implements BasicForumService { public class BasicForumServiceImpl implements BasicForumService {
@ -19,6 +20,7 @@ public class BasicForumServiceImpl implements BasicForumService {
private final ForumPostReadStateRepository readStateRepository; private final ForumPostReadStateRepository readStateRepository;
private final FileService fileService; private final FileService fileService;
private final EventBus eventBus; private final EventBus eventBus;
private final CurrentUser currentUserProvider;
@Inject @Inject
public BasicForumServiceImpl( public BasicForumServiceImpl(
@ -26,13 +28,15 @@ public class BasicForumServiceImpl implements BasicForumService {
final ForumPostReadStateRepository readStateRepository, final ForumPostReadStateRepository readStateRepository,
AbstractThreadRepository threadRepository, AbstractThreadRepository threadRepository,
final FileService fileService, final FileService fileService,
final EventBus eventBus final EventBus eventBus,
final CurrentUser currentUserProvider
) { ) {
this.postRepository = postRepository; this.postRepository = postRepository;
this.readStateRepository = readStateRepository; this.readStateRepository = readStateRepository;
this.threadRepository = threadRepository; this.threadRepository = threadRepository;
this.fileService = fileService; this.fileService = fileService;
this.eventBus = eventBus; this.eventBus = eventBus;
this.currentUserProvider = currentUserProvider;
} }
@Override @Override
@ -151,4 +155,38 @@ public class BasicForumServiceImpl implements BasicForumService {
} }
return false; return false;
} }
@Override
public boolean canDelete(ForumPost forumPost) {
ForumPost initialPost = forumPost.getForumThread().getPosts().get(0);
if (forumPost.equals(initialPost)) {
// The initial post in a thread can never be deleted
return false;
}
User user = currentUserProvider.get();
// Current user can be null meaning the call came from the system
if (user == null) {
// Allow the system to delete any post
return true;
}
return Objects.equals(forumPost.getPostedBy(), user);
}
@Override
@Transactional
public void deletePost(ForumPost post) {
if (!canDelete(post)) {
throw new PostCantBeDeletedException();
}
post.setDeleted(true);
postRepository.save(post);
}
private static final class PostCantBeDeletedException extends IllegalArgumentException {
public PostCantBeDeletedException() {
super("User is not allowed to delete post");
}
}
} }

View File

@ -7,9 +7,10 @@
<body> <body>
<wicket:panel> <wicket:panel>
<div class="messageWrap"> <div class="messageWrap">
<div class="forumBlueBackground"> <div class="forumBlueBackground d-flex justify-content-between">
<!-- DATE ROW--> <!-- DATE ROW-->
<wicket:container wicket:id="dateCreated"/> <span wicket:id="dateCreated"></span>
<button wicket:id="delete" class="btn btn-sm btn-outline-danger ms-auto">Delete</button>
</div> </div>
<div class="forumGrayBackground"> <div class="forumGrayBackground">
<div class="vertAlign"> <div class="vertAlign">

View File

@ -1,7 +1,9 @@
package se.su.dsv.scipro.forum.panels.threaded; package se.su.dsv.scipro.forum.panels.threaded;
import jakarta.inject.Inject;
import org.apache.wicket.Component; import org.apache.wicket.Component;
import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel; import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel; import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LambdaModel; import org.apache.wicket.model.LambdaModel;
@ -11,9 +13,11 @@ import se.su.dsv.scipro.components.ListAdapterModel;
import se.su.dsv.scipro.components.SmarterLinkMultiLineLabel; import se.su.dsv.scipro.components.SmarterLinkMultiLineLabel;
import se.su.dsv.scipro.data.enums.DateStyle; import se.su.dsv.scipro.data.enums.DateStyle;
import se.su.dsv.scipro.file.FileReference; import se.su.dsv.scipro.file.FileReference;
import se.su.dsv.scipro.forum.BasicForumService;
import se.su.dsv.scipro.forum.dataobjects.ForumPost; import se.su.dsv.scipro.forum.dataobjects.ForumPost;
import se.su.dsv.scipro.profile.UserLinkPanel; import se.su.dsv.scipro.profile.UserLinkPanel;
import se.su.dsv.scipro.repository.panels.ViewAttachmentPanel; import se.su.dsv.scipro.repository.panels.ViewAttachmentPanel;
import se.su.dsv.scipro.session.SciProSession;
public class ForumPostPanel extends Panel { public class ForumPostPanel extends Panel {
@ -22,6 +26,9 @@ public class ForumPostPanel extends Panel {
public static final String CONTENT = "content"; public static final String CONTENT = "content";
public static final String ATTACHMENT = "attachment"; public static final String ATTACHMENT = "attachment";
@Inject
private BasicForumService basicForumService;
public ForumPostPanel(String id, final IModel<ForumPost> model) { public ForumPostPanel(String id, final IModel<ForumPost> model) {
super(id); super(id);
add(new UserLinkPanel(POSTED_BY, LambdaModel.of(model, ForumPost::getPostedBy, ForumPost::setPostedBy))); add(new UserLinkPanel(POSTED_BY, LambdaModel.of(model, ForumPost::getPostedBy, ForumPost::setPostedBy)));
@ -62,5 +69,28 @@ public class ForumPostPanel extends Panel {
} }
} }
); );
add(
new Link<>("delete", model) {
@Override
public void onClick() {
ForumPost post = getModelObject();
basicForumService.deletePost(post);
onPostDeleted();
} }
@Override
protected void onConfigure() {
super.onConfigure();
setVisible(allowDeletion() && basicForumService.canDelete(getModelObject()));
}
}
);
}
protected boolean allowDeletion() {
return true;
}
protected void onPostDeleted() {}
} }

View File

@ -58,7 +58,16 @@ public class ViewForumThreadPanel<A> extends GenericPanel<A> {
new ListView<>(POST_LIST, new PostProvider()) { new ListView<>(POST_LIST, new PostProvider()) {
@Override @Override
protected void populateItem(ListItem<ForumPost> item) { protected void populateItem(ListItem<ForumPost> item) {
item.add(new ForumPostPanel(POST, item.getModel())); ListView<ForumPost> listView = this;
item.add(
new ForumPostPanel(POST, item.getModel()) {
@Override
protected void onPostDeleted() {
// Refresh the list of posts
listView.detach();
}
}
);
} }
} }
); );

View File

@ -22,7 +22,13 @@ interface Event {
@Override @Override
public Component component(String id, IModel<Event> model) { public Component component(String id, IModel<Event> model) {
return new ForumPostPanel(id, model.map(Message.class::cast).map(m -> m.forumPost)); return new ForumPostPanel(id, model.map(Message.class::cast).map(m -> m.forumPost)) {
@Override
protected boolean allowDeletion() {
// Do not allow deleting forum posts in the timeline
return false;
}
};
} }
@Override @Override