Attempt at deleting threads

This commit is contained in:
Andreas Svanberg 2025-03-17 13:40:19 +01:00
parent 6c91df6797
commit 819d5c7935
9 changed files with 68 additions and 25 deletions

@ -32,5 +32,10 @@ public interface BasicForumService extends Serializable {
boolean canDelete(ForumPost forumPost); boolean canDelete(ForumPost forumPost);
void deletePost(ForumPost post); Deletion deletePost(ForumPost post);
enum Deletion {
SINGLE_POST,
ENTIRE_THREAD,
}
} }

@ -185,15 +185,17 @@ public class BasicForumServiceImpl implements BasicForumService {
@Override @Override
@Transactional @Transactional
public void deletePost(ForumPost post) { public Deletion deletePost(ForumPost post) {
if (!canDelete(post)) { if (!canDelete(post)) {
throw new PostCantBeDeletedException(); throw new PostCantBeDeletedException();
} }
if (isInitialPost(post)) { if (isInitialPost(post)) {
threadRepository.delete(post.getForumThread()); threadRepository.delete(post.getForumThread());
return Deletion.ENTIRE_THREAD;
} else { } else {
post.setDeleted(true); post.setDeleted(true);
postRepository.save(post); postRepository.save(post);
return Deletion.SINGLE_POST;
} }
} }

@ -24,4 +24,8 @@ public interface ProjectForumService {
List<Pair<ProjectThread, ForumPost>> latestPost(Project a, int amount); List<Pair<ProjectThread, ForumPost>> latestPost(Project a, int amount);
long getUnreadThreadsCount(Project project, User user); long getUnreadThreadsCount(Project project, User user);
boolean canDelete(ProjectThread projectThread, ForumPost forumPost);
void deletePost(ProjectThread projectThread, ForumPost forumPost);
} }

@ -121,6 +121,20 @@ public class ProjectForumServiceImpl implements ProjectForumService {
return basicForumService.countUnreadThreads(list, user); return basicForumService.countUnreadThreads(list, user);
} }
@Override
public boolean canDelete(ProjectThread projectThread, ForumPost forumPost) {
return basicForumService.canDelete(forumPost);
}
@Override
@Transactional
public void deletePost(ProjectThread projectThread, ForumPost forumPost) {
BasicForumService.Deletion deletion = basicForumService.deletePost(forumPost);
if (deletion == BasicForumService.Deletion.ENTIRE_THREAD) {
projectThreadRepository.delete(projectThread);
}
}
@Override @Override
public ProjectThread findOne(long threadId) { public ProjectThread findOne(long threadId) {
return projectThreadRepository.findOne(threadId); return projectThreadRepository.findOne(threadId);

@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@ -57,7 +58,10 @@ public class BasicForumServiceIntegrationTest extends IntegrationTest {
setLoggedInAs(commenter); setLoggedInAs(commenter);
assertTrue(basicForumService.canDelete(reply)); assertTrue(basicForumService.canDelete(reply));
assertDoesNotThrow(() -> basicForumService.deletePost(reply)); assertDoesNotThrow(() -> {
BasicForumService.Deletion deletion = basicForumService.deletePost(reply);
assertEquals(BasicForumService.Deletion.SINGLE_POST, deletion);
});
} }
@Test @Test
@ -204,7 +208,8 @@ public class BasicForumServiceIntegrationTest extends IntegrationTest {
setLoggedInAs(op); setLoggedInAs(op);
basicForumService.deletePost(post); BasicForumService.Deletion deletion = basicForumService.deletePost(post);
assertEquals(BasicForumService.Deletion.ENTIRE_THREAD, deletion);
ForumThread threadById = basicForumService.findThreadById(thread.getId()); ForumThread threadById = basicForumService.findThreadById(thread.getId());
assertNull(threadById, "Thread was not deleted"); assertNull(threadById, "Thread was not deleted");

@ -10,4 +10,10 @@ public interface ForumThread<A> extends IClusterable {
ForumPost reply(A a, User poster, String content, Set<Attachment> attachments); ForumPost reply(A a, User poster, String content, Set<Attachment> attachments);
String getSubject(A a); String getSubject(A a);
void setRead(User user, A a); void setRead(User user, A a);
default boolean canDelete(A a, ForumPost forumPost) {
return false;
}
default void deletePost(A a, ForumPost forumPost) {}
} }

@ -1,6 +1,5 @@
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.link.Link;
@ -13,11 +12,9 @@ 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 {
@ -26,9 +23,6 @@ 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)));
@ -75,22 +69,23 @@ public class ForumPostPanel extends Panel {
@Override @Override
public void onClick() { public void onClick() {
ForumPost post = getModelObject(); ForumPost post = getModelObject();
basicForumService.deletePost(post); deletePost(post).run();
onPostDeleted();
} }
@Override @Override
protected void onConfigure() { protected void onConfigure() {
super.onConfigure(); super.onConfigure();
setVisible(allowDeletion() && basicForumService.canDelete(getModelObject())); setVisible(deletePost(getModelObject()) != null);
} }
} }
); );
} }
protected boolean allowDeletion() { /**
return false; * @param forumPost the post to delete
* @return a runnable that deletes the post, or {@code null} if it can not be deleted
*/
protected Runnable deletePost(ForumPost forumPost) {
return null;
} }
protected void onPostDeleted() {}
} }

@ -40,4 +40,14 @@ public class ProjectForumThread implements ForumThread<ProjectThread> {
public void setRead(User user, ProjectThread thread) { public void setRead(User user, ProjectThread thread) {
projectForumService.markRead(user, thread); projectForumService.markRead(user, thread);
} }
@Override
public boolean canDelete(ProjectThread projectThread, ForumPost forumPost) {
return projectForumService.canDelete(projectThread, forumPost);
}
@Override
public void deletePost(ProjectThread projectThread, ForumPost forumPost) {
projectForumService.deletePost(projectThread, forumPost);
}
} }

@ -62,14 +62,16 @@ public class ViewForumThreadPanel<A> extends GenericPanel<A> {
item.add( item.add(
new ForumPostPanel(POST, item.getModel()) { new ForumPostPanel(POST, item.getModel()) {
@Override @Override
protected boolean allowDeletion() { protected Runnable deletePost(ForumPost forumPost) {
return true; if (forumThread.canDelete(ViewForumThreadPanel.this.getModelObject(), forumPost)) {
} return () -> {
forumThread.deletePost(ViewForumThreadPanel.this.getModelObject(), forumPost);
@Override // Refresh the list of posts
protected void onPostDeleted() { listView.detach();
// Refresh the list of posts };
listView.detach(); } else {
return null;
}
} }
} }
); );