Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Remove unused reference in posting models #10075

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.SQLRestriction;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand All @@ -39,13 +38,6 @@ public class AnswerPost extends Posting {
@OneToMany(mappedBy = "answerPost", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.EAGER)
private Set<Reaction> reactions = new HashSet<>();

/***
* The value 1 represents an answer post, given by the enum {{@link PostingType}}
*/
@OneToMany(mappedBy = "postId", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.LAZY)
@SQLRestriction("post_type = 1")
private Set<SavedPost> savedPosts = new HashSet<>();

@ManyToOne
@JsonIncludeProperties({ "id", "exercise", "lecture", "course", "courseWideContext", "conversation", "author" })
private Post post;
Expand Down Expand Up @@ -90,11 +82,6 @@ public void setPost(Post post) {
this.post = post;
}

@JsonIgnore
public Set<SavedPost> getSavedPosts() {
return savedPosts;
}

@JsonProperty("isSaved")
public boolean getIsSaved() {
return isSaved;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.SQLRestriction;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -57,13 +56,6 @@ public class Post extends Posting {
@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.EAGER)
private Set<AnswerPost> answers = new HashSet<>();

/***
* The value 0 represents a post, given by the enum {{@link PostingType}}
*/
@OneToMany(mappedBy = "postId", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.LAZY)
@SQLRestriction("post_type = 0")
private Set<SavedPost> savedPosts = new HashSet<>();

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "post_tag", joinColumns = @JoinColumn(name = "post_id"))
@Column(name = "text")
Expand Down Expand Up @@ -235,11 +227,6 @@ public void setVoteCount(Integer voteCount) {
this.voteCount = voteCount != null ? voteCount : 0;
}

@JsonIgnore
public Set<SavedPost> getSavedPosts() {
return savedPosts;
}

@JsonProperty("isSaved")
public boolean getIsSaved() {
return isSaved;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ private PageImpl<Post> findPostsWithSpecification(Pageable pageable, Specificati
LEFT JOIN FETCH p.conversation
LEFT JOIN FETCH p.reactions
LEFT JOIN FETCH p.tags
LEFT JOIN FETCH p.savedPosts
LEFT JOIN FETCH p.answers a
LEFT JOIN FETCH a.reactions
LEFT JOIN FETCH a.post
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public interface SavedPostRepository extends ArtemisJpaRepository<SavedPost, Lon
*/
SavedPost findSavedPostByUserIdAndPostIdAndPostType(Long userId, Long postId, PostingType postType);

/***
* Get all saved posts by connected post/answer post id and posting type. Not cached.
*
* @param postId of the bookmark
* @param postType of the bookmark
*
* @return List of all saved posts connected to the corresponding entity.
*/
List<SavedPost> findSavedPostByPostIdAndPostType(Long postId, PostingType postType);

/***
* Query all post ids that a user has saved by a certain posting type. Cached by user id and post type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import de.tum.cit.aet.artemis.communication.domain.AnswerPost;
import de.tum.cit.aet.artemis.communication.domain.Post;
import de.tum.cit.aet.artemis.communication.domain.PostingType;
import de.tum.cit.aet.artemis.communication.domain.conversation.Channel;
import de.tum.cit.aet.artemis.communication.domain.conversation.Conversation;
import de.tum.cit.aet.artemis.communication.domain.notification.SingleUserNotification;
Expand Down Expand Up @@ -209,6 +210,10 @@ public void deleteAnswerMessageById(Long courseId, Long answerMessageId) {
answerPostRepository.deleteById(answerMessageId);
preparePostForBroadcast(updatedMessage);

// Delete all connected saved posts
var savedPosts = savedPostRepository.findSavedPostByPostIdAndPostType(answerMessageId, PostingType.ANSWER);
savedPostRepository.deleteAll(savedPosts);

broadcastForPost(new PostDTO(updatedMessage, MetisCrudAction.UPDATE), course.getId(), null, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import de.tum.cit.aet.artemis.communication.domain.DisplayPriority;
import de.tum.cit.aet.artemis.communication.domain.NotificationType;
import de.tum.cit.aet.artemis.communication.domain.Post;
import de.tum.cit.aet.artemis.communication.domain.PostingType;
import de.tum.cit.aet.artemis.communication.domain.conversation.Channel;
import de.tum.cit.aet.artemis.communication.domain.conversation.Conversation;
import de.tum.cit.aet.artemis.communication.domain.conversation.GroupChat;
Expand Down Expand Up @@ -371,6 +372,10 @@ public void deleteMessageById(Long courseId, Long postId) {
conversationParticipantRepository.decrementUnreadMessagesCountOfParticipants(conversation.getId(), user.getId());
conversation = conversationService.getConversationById(conversation.getId());

// Delete all connected saved posts
var savedPosts = savedPostRepository.findSavedPostByPostIdAndPostType(postId, PostingType.POST);
savedPostRepository.deleteAll(savedPosts);

conversationService.notifyAllConversationMembersAboutUpdate(conversation);
preparePostForBroadcast(post);
broadcastForPost(new PostDTO(post, MetisCrudAction.DELETE), course.getId(), null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void cleanupArchivedSavedPosts() {
/**
* Cleans up all saved posts where the post entity does not exist anymore
*/
@Scheduled(cron = "0 0 0 * * *")
@Scheduled(cron = "0 5 0 * * *")
public void cleanupOrphanedSavedPosts() {
List<SavedPost> orphanedPosts = savedPostRepository.findOrphanedPostReferences();
if (!orphanedPosts.isEmpty()) {
Expand Down
Loading