Skip to content
Merged
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
@@ -1,17 +1,15 @@
package org.websoso.WSSServer.dto.comment;

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDate;
import org.websoso.WSSServer.feed.domain.Comment;
import org.websoso.WSSServer.dto.user.UserBasicInfo;
import org.websoso.WSSServer.util.TimeFormatUtil;

public record CommentGetResponse(
Long userId,
String nickname,
String avatarImage,
Long commentId,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "M월 d일", timezone = "Asia/Seoul")
LocalDate createdDate,
String createdDate,
String commentContent,
Boolean isModified,
Boolean isMyComment,
Expand All @@ -26,7 +24,7 @@ public static CommentGetResponse of(UserBasicInfo userBasicInfo, Comment comment
userBasicInfo.nickname(),
userBasicInfo.avatarImage(),
comment.getCommentId(),
comment.getCreatedDate().toLocalDate(),
TimeFormatUtil.formatRelativeDateTime(comment.getCreatedDate()),
comment.getCommentContent(),
!comment.getCreatedDate().equals(comment.getModifiedDate()),
isMyComment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static FeedGetResponse of(Feed feed, UserBasicInfo feedUserBasicInfo, Nov
feedUserBasicInfo.nickname(),
feedUserBasicInfo.avatarImage(),
feed.getFeedId(),
TimeFormatUtil.formatFeedDate(feed.getCreatedDate()),
TimeFormatUtil.formatRelativeDateTime(feed.getCreatedDate()),
feed.getFeedContent(),
feed.getLikes().size(),
isLiked,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/websoso/WSSServer/dto/feed/FeedInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static FeedInfo of(Feed feed, UserBasicInfo userBasicInfo, Novel novel, B
userBasicInfo.userId(),
userBasicInfo.nickname(),
userBasicInfo.avatarImage(),
TimeFormatUtil.formatFeedDate(feed.getCreatedDate()),
TimeFormatUtil.formatRelativeDateTime(feed.getCreatedDate()),
feed.getFeedContent(),
feed.getLikes().size(),
isLiked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static UserFeedGetResponse of(Feed feed, Novel novel, Long visitorId, Str
return new UserFeedGetResponse(
feed.getFeedId(),
feed.getFeedContent(),
TimeFormatUtil.formatFeedDate(feed.getCreatedDate()),
TimeFormatUtil.formatRelativeDateTime(feed.getCreatedDate()),
feed.getIsSpoiler(),
isModified,
likeUsers,
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/websoso/WSSServer/util/TimeFormatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ public class TimeFormatUtil {
private static final DateTimeFormatter NOTIFICATION_FMT = DateTimeFormatter.ofPattern("yyyy.MM.dd");

/**
* 피드 날짜 포맷
* 피드/댓글 날짜 포맷
* 60초 미만: 방금 전
* 1시간 미만: N분 전
* 24시간 미만: N시간 전
* 7일 미만: N일 전
* 동일 년도: M월 d일
* 과거 년도: yyyy년 M월 d일
*/
public static String formatFeedDate(LocalDateTime createdDate) {
return formatFeedDate(createdDate, Clock.systemDefaultZone());
public static String formatRelativeDateTime(LocalDateTime createdDate) {
return formatRelativeDateTime(createdDate, Clock.systemDefaultZone());
}

public static String formatFeedDate(LocalDateTime createdDate, Clock clock) {
public static String formatRelativeDateTime(LocalDateTime createdDate, Clock clock) {
LocalDateTime now = LocalDateTime.now(clock);
Duration duration = Duration.between(createdDate, now);

Expand Down
28 changes: 14 additions & 14 deletions src/test/java/org/websoso/WSSServer/util/TimeFormatUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,87 +15,87 @@ class TimeFormatUtilTest {
private static final ZoneId ZONE = ZoneId.systemDefault();

@Nested
@DisplayName("피드 날짜 포맷팅 테스트")
@DisplayName("피드/댓글 날짜 포맷팅 테스트")
class FormatFeedDate {

@DisplayName("60초 미만이면 '방금 전'을 반환한다")
@Test
void returnsJustNowForUnder60Seconds() {
Clock clock = fixedClock("2024-01-15T12:00:00");
assertThat(formatFeed(clock, 30)).isEqualTo("방금 전");
assertThat(formatFeedAndComment(clock, 30)).isEqualTo("방금 전");
}

@DisplayName("59초일 때 '방금 전'을 반환한다")
@Test
void returnsJustNowAt59Seconds() {
Clock clock = fixedClock("2024-01-15T12:00:00");
assertThat(formatFeed(clock, 59)).isEqualTo("방금 전");
assertThat(formatFeedAndComment(clock, 59)).isEqualTo("방금 전");
}

@DisplayName("60초이면 '1분 전'을 반환한다")
@Test
void returns1MinuteAt60Seconds() {
Clock clock = fixedClock("2024-01-15T12:00:00");
assertThat(formatFeed(clock, 60)).isEqualTo("1분 전");
assertThat(formatFeedAndComment(clock, 60)).isEqualTo("1분 전");
}

@DisplayName("59분이면 '59분 전'을 반환한다")
@Test
void returns59MinutesAt59Minutes() {
Clock clock = fixedClock("2024-01-15T12:00:00");
assertThat(formatFeed(clock, 59 * 60)).isEqualTo("59분 전");
assertThat(formatFeedAndComment(clock, 59 * 60)).isEqualTo("59분 전");
}

@DisplayName("1시간이면 '1시간 전'을 반환한다")
@Test
void returns1HourAt3600Seconds() {
Clock clock = fixedClock("2024-01-15T12:00:00");
assertThat(formatFeed(clock, 3600)).isEqualTo("1시간 전");
assertThat(formatFeedAndComment(clock, 3600)).isEqualTo("1시간 전");
}

@DisplayName("23시간이면 '23시간 전'을 반환한다")
@Test
void returns23HoursAt23Hours() {
Clock clock = fixedClock("2024-01-15T12:00:00");
assertThat(formatFeed(clock, 23 * 3600)).isEqualTo("23시간 전");
assertThat(formatFeedAndComment(clock, 23 * 3600)).isEqualTo("23시간 전");
}

@DisplayName("24시간이면 '1일 전'을 반환한다")
@Test
void returns1DayAt24Hours() {
Clock clock = fixedClock("2024-01-15T12:00:00");
assertThat(formatFeed(clock, 24 * 3600)).isEqualTo("1일 전");
assertThat(formatFeedAndComment(clock, 24 * 3600)).isEqualTo("1일 전");
}

@DisplayName("6일이면 '6일 전'을 반환한다")
@Test
void returns6DaysAt6Days() {
Clock clock = fixedClock("2024-01-15T12:00:00");
assertThat(formatFeed(clock, 6 * 24 * 3600)).isEqualTo("6일 전");
assertThat(formatFeedAndComment(clock, 6 * 24 * 3600)).isEqualTo("6일 전");
}

@DisplayName("같은 해이면 '월 일' 형식을 반환한다")
@Test
void returnsMonthDayForSameYear() {
Clock clock = fixedClock("2024-06-15T12:00:00");
LocalDateTime createdAt = LocalDateTime.parse("2024-01-01T00:00:00");
assertThat(TimeFormatUtil.formatFeedDate(createdAt, clock)).isEqualTo("1월 1일");
assertThat(TimeFormatUtil.formatRelativeDateTime(createdAt, clock)).isEqualTo("1월 1일");
}

@DisplayName("다른 해이면 '년 월 일' 형식을 반환한다")
@Test
void returnsYearMonthDayForDifferentYear() {
Clock clock = fixedClock("2024-01-15T12:00:00");
LocalDateTime createdAt = LocalDateTime.parse("2023-12-31T00:00:00");
assertThat(TimeFormatUtil.formatFeedDate(createdAt, clock)).isEqualTo("2023년 12월 31일");
assertThat(TimeFormatUtil.formatRelativeDateTime(createdAt, clock)).isEqualTo("2023년 12월 31일");
}

@DisplayName("미래 시각이면 '방금 전'을 반환한다")
@Test
void returnsJustNowForFutureTime() {
Clock clock = fixedClock("2024-01-15T12:00:00");
LocalDateTime future = LocalDateTime.now(clock).plusHours(1);
assertThat(TimeFormatUtil.formatFeedDate(future, clock)).isEqualTo("방금 전");
assertThat(TimeFormatUtil.formatRelativeDateTime(future, clock)).isEqualTo("방금 전");
}
}

Expand Down Expand Up @@ -178,9 +178,9 @@ private Clock fixedClock(String isoDateTime) {
);
}

private String formatFeed(Clock clock, long secondsBefore) {
private String formatFeedAndComment(Clock clock, long secondsBefore) {
LocalDateTime createdAt = LocalDateTime.now(clock).minusSeconds(secondsBefore);
return TimeFormatUtil.formatFeedDate(createdAt, clock);
return TimeFormatUtil.formatRelativeDateTime(createdAt, clock);
}

private String formatNotification(Clock clock, long secondsBefore) {
Expand Down
Loading