Skip to content

Commit

Permalink
[hotfix] fix: 댓글 전체를 차단된 사용자가 작성해도 노트 조회되도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
elive7 committed Nov 24, 2024
1 parent 68c0c4c commit ae1c555
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,8 @@ public List<Like> getLikes() {
public List<Bookmark> getBookmarks() {
return bookmarks.stream().filter(bookmark -> bookmark.isInUse()).collect(Collectors.toList());
}

public void setComments(List<Comment> comments) {
this.comments = comments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static com.projectlyrics.server.domain.note.entity.QNote.note;
import static com.projectlyrics.server.domain.song.entity.QSong.song;

import com.projectlyrics.server.domain.comment.domain.Comment;
import com.projectlyrics.server.domain.common.util.QueryDslUtils;
import com.projectlyrics.server.domain.note.entity.Note;
import com.projectlyrics.server.domain.note.entity.NoteStatus;
Expand Down Expand Up @@ -47,32 +48,44 @@ public Optional<Note> findById(Long id) {

@Override
public Optional<Note> findById(Long id, Long userId) {
return Optional.ofNullable(
jpaQueryFactory
.selectFrom(note)
.leftJoin(note.lyrics).fetchJoin()
.join(note.publisher).fetchJoin()
.join(note.song).fetchJoin()
.join(song.artist).fetchJoin()
.leftJoin(note.comments, comment)
.on(comment.deletedAt.isNull()
.and(comment.writer.id.notIn(
JPAExpressions.select(block.blocked.id)
.from(block)
.where(
block.blocker.id.eq(userId),
block.deletedAt.isNull()
)
))
).fetchJoin()
.where(
note.id.eq(id),
note.deletedAt.isNull()
)
.fetchOne()
);
// 1. Note 객체를 조회
Note n = jpaQueryFactory
.selectFrom(note)
.leftJoin(note.lyrics).fetchJoin()
.join(note.publisher).fetchJoin()
.join(note.song).fetchJoin()
.join(song.artist).fetchJoin()
.leftJoin(note.comments, comment)
.where(
note.id.eq(id),
note.deletedAt.isNull()
)
.fetchOne();

// 2. 만약 Note가 조회되었다면 댓글을 필터링하여 적용
if (n != null) {
List<Comment> filteredComments = jpaQueryFactory
.selectFrom(comment)
.leftJoin(block)
.on(block.blocked.eq(comment.writer)
.and(block.blocker.id.eq(userId))
.and(block.deletedAt.isNull()))
.where(
comment.note.id.eq(id),
comment.deletedAt.isNull(),
block.id.isNull()
)
.fetch();

// 3. 필터링된 댓글을 Note 객체에 업데이트
n.setComments(filteredComments);
}

return Optional.ofNullable(n);
}



@Override
public Slice<Note> findAllByUserId(boolean hasLyrics, Long artistId, Long userId, Long cursorId, Pageable pageable) {
List<Note> content = jpaQueryFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ void setUp() {
);
}

@Test
void 노트_id_일치하는_노트_조회시_모든_노트의_댓글의_작성자를_차단해도_노트정보를_조회해야_한다() {
// given
Note note = noteCommandService.create(likedArtistSongNoteRequest, user.getId());
commentCommandRepository.save(CommentFixture.create(note, user1));
commentCommandRepository.save(CommentFixture.create(note, user2));
blockCommandRepository.save(BlockFixture.create(user, user1));
blockCommandRepository.save(BlockFixture.create(user, user2));

// when
NoteDetailResponse result = sut.getNoteById(note.getId(), user.getId());

// then
assertAll(
() -> assertThat(result.id()).isNotNull(),
() -> assertThat(result.comments().size()).isEqualTo(0)
);
}

@Test
void 사용자_id_일치하는_작성자와_연관된_노트_리스트를_최신순으로_조회해야_한다() {
// given
Expand Down

0 comments on commit ae1c555

Please sign in to comment.