Skip to content

Commit

Permalink
feat: view liked
Browse files Browse the repository at this point in the history
  • Loading branch information
doxxx93 committed Feb 22, 2024
1 parent 4bdc926 commit 1f42625
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.Map;
import java.util.stream.Collectors;

import static org.palette.easeltimelineservice.service.RedisKeyConstants.FOLLOWER_PAINT_TIMELINE_PREFIX;

Expand All @@ -20,18 +22,35 @@ public class FollowerPaintMapService {
public void addPaintToFollowersTimeline(List<Long> followerIds, Long paintId) {
followerIds.forEach(userId -> {
final String key = RedisKeyUtil.constructKey(FOLLOWER_PAINT_TIMELINE_PREFIX.getKey(), userId);
redistemplate.opsForList()
.leftPush(key, paintId.toString());
Map<String, String> paintData = new HashMap<>();
paintData.put(String.valueOf(paintId), String.valueOf(false));
redistemplate.opsForHash().putAll(key, paintData);
redistemplate.expire(key, Duration.ofDays(5));
});
}

public List<Long> getFollowingTimelinePaintIds(final Long userId) {
final String key = RedisKeyUtil.constructKey(FOLLOWER_PAINT_TIMELINE_PREFIX.getKey(), userId);
return Optional.ofNullable(redistemplate.opsForList().range(key, 0, -1))
.orElse(List.of())
.stream()
.map(Long::parseLong)
.toList();
Map<Object, Object> entries = redistemplate.opsForHash().entries(key);
return entries.keySet().stream()
.map(paintId -> Long.parseLong((String) paintId))
.collect(Collectors.toList());
}

public void like(final Long userId, final Long paintId) {
final String key = RedisKeyUtil.constructKey(FOLLOWER_PAINT_TIMELINE_PREFIX.getKey(), userId);
redistemplate.opsForHash().put(key, paintId.toString(), String.valueOf(true));
}

public void unlike(final Long userId, final Long paintId) {
final String key = RedisKeyUtil.constructKey(FOLLOWER_PAINT_TIMELINE_PREFIX.getKey(), userId);
redistemplate.opsForHash().put(key, paintId.toString(), String.valueOf(false));
}

public boolean isLiked(final Long userId, final Long paintId) {
final String key = RedisKeyUtil.constructKey(FOLLOWER_PAINT_TIMELINE_PREFIX.getKey(), userId);
String isLiked = (String) redistemplate.opsForHash().get(key, paintId.toString());
System.out.println("isLiked: " + isLiked);
return Boolean.parseBoolean(isLiked);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,31 @@ public static PaintResponse from(Paint paint) {
);
}

public static PaintResponse of(Paint paint, PaintMetrics metrics) {
// public static PaintResponse of(Paint paint, PaintMetrics metrics) {
// return new PaintResponse(
// paint.getId(),
// paint.getIsReply(),
// paint.getAuthorId(),
// paint.getAuthorUsername(),
// paint.getAuthorNickname(),
// paint.getAuthorImagePath(),
// paint.getAuthorStatus(),
// Optional.ofNullable(paint.getQuotePaint()).map(PaintResponse::from).orElse(null),
// paint.getCreatedAt(),
// paint.getText(),
// metrics.replyCount(),
// metrics.repaintCount(),
// metrics.likeCount(),
// false,
// false,
// false,
// metrics.viewCount(),
// new Entities(paint.getHashtagRecords(), paint.getMentionRecords()),
// new Includes(paint.getMediaRecords(), paint.getMentionRecords(), paint.getLinkRecord())
// );
// }

public static PaintResponse of(Paint paint, PaintMetrics metrics, boolean isLiked) {
return new PaintResponse(
paint.getId(),
paint.getIsReply(),
Expand All @@ -65,7 +89,7 @@ public static PaintResponse of(Paint paint, PaintMetrics metrics) {
metrics.replyCount(),
metrics.repaintCount(),
metrics.likeCount(),
false,
isLiked,
false,
false,
metrics.viewCount(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ public List<PaintResponse> getFollowingTimeline(final Long userId) {
final List<Paint> paints = paintCacheService.getPaints(paintIds);
return paints.stream().map(paint -> {
PaintMetrics metrics = paintMetricsService.getPaintMetrics(paint.getId());
return PaintResponse.of(paint, metrics);
final boolean liked = followerPaintMapService.isLiked(userId, paint.getId());
return PaintResponse.of(paint, metrics, liked);
}
).toList();
}

public void handleLikedPaintEvent(final LikedPaintEvent likedPaintEvent) {
followerPaintMapService.like(likedPaintEvent.likingUserId(), likedPaintEvent.paintId());
paintMetricsService.incrementLikeCount(likedPaintEvent.paintId());
}

public void handleUnlikedPaintEvent(final UnlikedPaintEvent unlikedPaintEvent) {
followerPaintMapService.unlike(unlikedPaintEvent.unlikingUserId(), unlikedPaintEvent.paintId());
paintMetricsService.decrementLikeCount(unlikedPaintEvent.paintId());
}

Expand All @@ -61,7 +64,8 @@ public List<PaintResponse> getForYouTimeline(final Long userId) {

return filteredPaints.stream().map(paint -> {
PaintMetrics metrics = paintMetricsService.getPaintMetrics(paint.getId());
return PaintResponse.of(paint, metrics);
final boolean liked = followerPaintMapService.isLiked(userId, paint.getId());
return PaintResponse.of(paint, metrics, liked);
}
).toList();
}
Expand Down

0 comments on commit 1f42625

Please sign in to comment.