Skip to content
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
47 changes: 21 additions & 26 deletions src/main/java/org/websoso/WSSServer/controller/FeedController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,31 @@
import org.websoso.WSSServer.dto.feed.FeedsGetResponse;
import org.websoso.WSSServer.dto.feed.InterestFeedsGetResponse;
import org.websoso.WSSServer.dto.popularFeed.PopularFeedsGetResponse;
import org.websoso.WSSServer.service.FeedService;
import org.websoso.WSSServer.service.PopularFeedService;
import org.websoso.WSSServer.facade.FeedFacade;
import org.websoso.WSSServer.service.UserService;

@RequestMapping("/feeds")
@RestController
@RequiredArgsConstructor
public class FeedController {

private final FeedService feedService;
private final FeedFacade feedFacade;
private final UserService userService;
private final PopularFeedService popularFeedService;

@PostMapping
public ResponseEntity<Void> createFeed(Principal principal,
@Valid @RequestBody FeedCreateRequest request) {
@Valid @RequestBody FeedCreateRequest request) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.createFeed(user, request);

return ResponseEntity
.status(CREATED)
.build();
feedFacade.createFeed(user, request);
return ResponseEntity.status(CREATED).build();
}

@PutMapping("/{feedId}")
public ResponseEntity<Void> updateFeed(Principal principal,
@PathVariable("feedId") Long feedId,
@Valid @RequestBody FeedUpdateRequest request) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.updateFeed(user, feedId, request);
feedFacade.updateFeed(user, feedId, request);

return ResponseEntity
.status(NO_CONTENT)
Expand All @@ -69,7 +64,7 @@ public ResponseEntity<Void> updateFeed(Principal principal,
public ResponseEntity<Void> deleteFeed(Principal principal,
@PathVariable("feedId") Long feedId) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.deleteFeed(user, feedId);
feedFacade.deleteFeed(user, feedId);

return ResponseEntity
.status(NO_CONTENT)
Expand All @@ -80,7 +75,7 @@ public ResponseEntity<Void> deleteFeed(Principal principal,
public ResponseEntity<Void> likeFeed(Principal principal,
@PathVariable("feedId") Long feedId) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.likeFeed(user, feedId);
feedFacade.likeFeed(user, feedId);

return ResponseEntity
.status(NO_CONTENT)
Expand All @@ -91,7 +86,7 @@ public ResponseEntity<Void> likeFeed(Principal principal,
public ResponseEntity<Void> unLikeFeed(Principal principal,
@PathVariable("feedId") Long feedId) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.unLikeFeed(user, feedId);
feedFacade.unlikeFeed(user, feedId);

return ResponseEntity
.status(NO_CONTENT)
Expand All @@ -105,7 +100,7 @@ public ResponseEntity<FeedGetResponse> getFeed(Principal principal,

return ResponseEntity
.status(OK)
.body(feedService.getFeedById(user, feedId));
.body(feedFacade.getFeed(user, feedId));
}

@GetMapping("/popular")
Expand All @@ -115,7 +110,7 @@ public ResponseEntity<PopularFeedsGetResponse> getPopularFeeds(Principal princip
userService.getUserOrException(Long.valueOf(principal.getName()));
return ResponseEntity
.status(OK)
.body(popularFeedService.getPopularFeeds(user));
.body(feedFacade.getPopularFeeds(user));
}

@GetMapping
Expand All @@ -127,7 +122,7 @@ public ResponseEntity<FeedsGetResponse> getFeeds(Principal principal,

return ResponseEntity
.status(OK)
.body(feedService.getFeeds(user, category, lastFeedId, size));
.body(feedFacade.getFeeds(user, category, lastFeedId, size));
}

@GetMapping("/interest")
Expand All @@ -137,15 +132,15 @@ public ResponseEntity<InterestFeedsGetResponse> getInterestFeeds(Principal princ
: userService.getUserOrException(Long.valueOf(principal.getName()));
return ResponseEntity
.status(OK)
.body(feedService.getInterestFeeds(user));
.body(feedFacade.getInterestFeeds(user));
}

@PostMapping("/{feedId}/comments")
public ResponseEntity<Void> createComment(Principal principal,
@PathVariable("feedId") Long feedId,
@Valid @RequestBody CommentCreateRequest request) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.createComment(user, feedId, request);
feedFacade.createComment(user, feedId, request);

return ResponseEntity
.status(NO_CONTENT)
Expand All @@ -158,7 +153,7 @@ public ResponseEntity<Void> updateComment(Principal principal,
@PathVariable("commentId") Long commentId,
@Valid @RequestBody CommentUpdateRequest request) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.updateComment(user, feedId, commentId, request);
feedFacade.updateComment(user, feedId, commentId, request);

return ResponseEntity
.status(NO_CONTENT)
Expand All @@ -170,7 +165,7 @@ public ResponseEntity<Void> deleteComment(Principal principal,
@PathVariable("feedId") Long feedId,
@PathVariable("commentId") Long commentId) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.deleteComment(user, feedId, commentId);
feedFacade.deleteComment(user, feedId, commentId);

return ResponseEntity
.status(NO_CONTENT)
Expand All @@ -184,14 +179,14 @@ public ResponseEntity<CommentsGetResponse> getComments(Principal principal,

return ResponseEntity
.status(OK)
.body(feedService.getComments(user, feedId));
.body(feedFacade.getComments(user, feedId));
}

@PostMapping("/{feedId}/spoiler")
public ResponseEntity<Void> reportFeedSpoiler(Principal principal,
@PathVariable("feedId") Long feedId) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.reportFeed(user, feedId, SPOILER);
feedFacade.reportFeed(user, feedId, SPOILER);

return ResponseEntity
.status(CREATED)
Expand All @@ -202,7 +197,7 @@ public ResponseEntity<Void> reportFeedSpoiler(Principal principal,
public ResponseEntity<Void> reportedFeedImpertinence(Principal principal,
@PathVariable("feedId") Long feedId) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.reportFeed(user, feedId, IMPERTINENCE);
feedFacade.reportFeed(user, feedId, IMPERTINENCE);

return ResponseEntity
.status(CREATED)
Expand All @@ -214,7 +209,7 @@ public ResponseEntity<Void> reportCommentSpoiler(Principal principal,
@PathVariable("feedId") Long feedId,
@PathVariable("commentId") Long commentId) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.reportComment(user, feedId, commentId, SPOILER);
feedFacade.reportComment(user, feedId, commentId, SPOILER);

return ResponseEntity
.status(CREATED)
Expand All @@ -226,7 +221,7 @@ public ResponseEntity<Void> reportCommentImpertinence(Principal principal,
@PathVariable("feedId") Long feedId,
@PathVariable("commentId") Long commentId) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
feedService.reportComment(user, feedId, commentId, IMPERTINENCE);
feedFacade.reportComment(user, feedId, commentId, IMPERTINENCE);

return ResponseEntity
.status(CREATED)
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/websoso/WSSServer/domain/Feed.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ public boolean isNovelChanged(Long novelId) {
return !Objects.equals(this.novelId, novelId);
}

public void hideFeed() {
public void hide() {
this.isHidden = true;
}

public void spoiler() {
this.isSpoiler = true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public enum CustomFeedError implements ICustomError {
LIKE_USER_NOT_FOUND("FEED-003", "해당 사용자가 이 피드에 좋아요를 누르지 않았습니다.", NOT_FOUND),
INVALID_LIKE_COUNT("FEED-004", "좋아요 수가 유효하지 않습니다.", BAD_REQUEST),
HIDDEN_FEED_ACCESS("FEED-005", "이 피드는 숨겨져 있어 접근할 수 없습니다.", FORBIDDEN),
BLOCKED_USER_ACCESS("FEED-006", "해당 사용자와 피드 작성자가 차단 상태이므로 이 피드에 접근할 수 없습니다.", FORBIDDEN),
BLOCKED_USER_ACCESS("FEED-006", "차단한 사용자의 피드에 접근할 수 없습니다.", FORBIDDEN),
SELF_REPORT_NOT_ALLOWED("FEED-007", "자신의 피드를 신고할 수 없습니다.", BAD_REQUEST),
ALREADY_REPORTED_FEED("FEED-008", "이미 사용자가 신고한 피드입니다.", CONFLICT);
ALREADY_REPORTED_FEED("FEED-008", "이미 사용자가 신고한 피드입니다.", CONFLICT),
NOT_LIKED("FEED-009", "사용자가 해당 피드에 좋아요를 누르지 않았습니다.", BAD_REQUEST);

private final String code;
private final String description;
Expand Down
187 changes: 187 additions & 0 deletions src/main/java/org/websoso/WSSServer/facade/FeedFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package org.websoso.WSSServer.facade;

import static org.websoso.WSSServer.domain.common.DiscordWebhookMessageType.REPORT;
import static org.websoso.WSSServer.exception.error.CustomFeedError.BLOCKED_USER_ACCESS;
import static org.websoso.WSSServer.exception.error.CustomFeedError.HIDDEN_FEED_ACCESS;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.websoso.WSSServer.domain.Comment;
import org.websoso.WSSServer.domain.Feed;
import org.websoso.WSSServer.domain.User;
import org.websoso.WSSServer.domain.common.DiscordWebhookMessage;
import org.websoso.WSSServer.domain.common.ReportedType;
import org.websoso.WSSServer.dto.comment.CommentCreateRequest;
import org.websoso.WSSServer.dto.comment.CommentUpdateRequest;
import org.websoso.WSSServer.dto.comment.CommentsGetResponse;
import org.websoso.WSSServer.dto.feed.FeedCreateRequest;
import org.websoso.WSSServer.dto.feed.FeedGetResponse;
import org.websoso.WSSServer.dto.feed.FeedUpdateRequest;
import org.websoso.WSSServer.dto.feed.FeedsGetResponse;
import org.websoso.WSSServer.dto.feed.InterestFeedsGetResponse;
import org.websoso.WSSServer.dto.popularFeed.PopularFeedsGetResponse;
import org.websoso.WSSServer.exception.exception.CustomFeedException;
import org.websoso.WSSServer.service.BlockService;
import org.websoso.WSSServer.service.CommentService;
import org.websoso.WSSServer.service.FeedService;
import org.websoso.WSSServer.service.LikeService;
import org.websoso.WSSServer.service.MessageFormatter;
import org.websoso.WSSServer.service.MessageService;
import org.websoso.WSSServer.service.NotificationService;
import org.websoso.WSSServer.service.PopularFeedService;
import org.websoso.WSSServer.service.ReportService;
import org.websoso.WSSServer.service.UserService;

@Component
@RequiredArgsConstructor
public class FeedFacade {
private static final int POPULAR_FEED_LIKE_THRESHOLD = 5;
private final FeedService feedService;
private final PopularFeedService popularFeedService;
private final LikeService likeService;
private final CommentService commentService;
private final ReportService reportService;
private final MessageService messageService;
private final NotificationService notificationService;
private final UserService userService;
private final BlockService blockService;

public void createFeed(User user, FeedCreateRequest request) {
feedService.createFeed(user, request);
}

@Transactional(readOnly = true)
public FeedGetResponse getFeed(User user, Long feedId) {
return feedService.getFeedById(user, feedId);
}

@Transactional(readOnly = true)
public FeedsGetResponse getFeeds(User user, String category, Long lastFeedId, int size) {
return feedService.getFeeds(user, category, lastFeedId, size);
}

public void updateFeed(User user, Long feedId, FeedUpdateRequest request) {
feedService.updateFeed(user, feedId, request);
}

public void deleteFeed(User user, Long feedId) {
feedService.deleteFeed(user, feedId);
}

public void reportFeed(User user, Long feedId, ReportedType reportedType) {
Feed feed = getFeedOrException(feedId);

validate(feed, user.getUserId(), feed.getUser().getUserId());

int reportedCount = reportService.reportFeed(user, feed, reportedType);
messageService.sendDiscordWebhookMessage(DiscordWebhookMessage.of(
MessageFormatter.formatFeedReportMessage(user, feed, reportedType, reportedCount), REPORT));
}

public void reportComment(User reporteCreatedUser, Long feedId, Long commentId, ReportedType reportedType) {
Feed feed = getFeedOrException(feedId);
Comment comment = commentService.getCommentOrException(commentId);

validate(feed, reporteCreatedUser.getUserId(), comment.getUserId());

User commentCreatedUser = userService.getUserOrException(comment.getUserId());
int reportedCount = reportService.reportComment(reporteCreatedUser, getFeedOrException(feedId), comment,
commentCreatedUser, reportedType);
messageService.sendDiscordWebhookMessage(DiscordWebhookMessage.of(
MessageFormatter.formatCommentReportMessage(reporteCreatedUser, feed, comment, reportedType,
commentCreatedUser, reportedCount), REPORT));
}

@Transactional(readOnly = true)
public PopularFeedsGetResponse getPopularFeeds(User user) {
return popularFeedService.getPopularFeeds(user);
}

@Transactional(readOnly = true)
public InterestFeedsGetResponse getInterestFeeds(User user) {
return feedService.getInterestFeeds(user);
}

public void likeFeed(User user, Long feedId) {
Feed feed = getFeedOrException(feedId);
validate(feed, feed.getUser().getUserId(), user.getUserId());

likeService.createLike(user, feed);

if (feed.getLikes().size() == POPULAR_FEED_LIKE_THRESHOLD) {
popularFeedService.createPopularFeed(feed);
notificationService.sendPopularFeedPushMessage(feed);
}

if (blockService.isBlocked(user.getUserId(), feed.getFeedId())) {
return;
}
notificationService.sendLikePushMessage(user, feed);
}

public void unlikeFeed(User user, Long feedId) {
likeService.deleteLike(user, getFeedOrException(feedId));
}

public void createComment(User user, Long feedId, CommentCreateRequest request) {
Feed feed = getFeedOrException(feedId);
validate(feed, user.getUserId(), feed.getUser().getUserId());
commentService.createComment(user, getFeedOrException(feedId), request.commentContent());
sendCommentPushMessage(user, feed);
}

@Transactional(readOnly = true)
public CommentsGetResponse getComments(User user, Long feedId) {
Feed feed = getFeedOrException(feedId);
validate(feed, user.getUserId(), feed.getUser().getUserId());
return commentService.getComments(user, getFeedOrException(feedId));
}

public void updateComment(User user, Long feedId, Long commentId, CommentUpdateRequest request) {
Feed feed = getFeedOrException(feedId);
validate(feed, user.getUserId(), feed.getUser().getUserId());
commentService.updateComment(user.getUserId(), getFeedOrException(feedId), commentId, request.commentContent());
}

public void deleteComment(User user, Long feedId, Long commentId) {
Feed feed = getFeedOrException(feedId);
validate(feed, user.getUserId(), feed.getUser().getUserId());
commentService.deleteComment(user.getUserId(), getFeedOrException(feedId), commentId);
}

private Feed getFeedOrException(Long feedId) {
return feedService.getFeedOrException(feedId);
}

private void validate(Feed feed, Long blockingId, Long blockedId) {
checkHiddenFeed(feed);
checkBlocked(blockingId, blockedId);
}

private void checkHiddenFeed(Feed feed) {
if (feed.getIsHidden()) {
throw new CustomFeedException(HIDDEN_FEED_ACCESS, "Cannot access hidden feed.");
}
}

private void checkBlocked(Long blockingId, Long blockedId) {
if (blockService.isBlocked(blockingId, blockedId)) {
throw new CustomFeedException(BLOCKED_USER_ACCESS,
"cannot access this feed because either you or the feed author has blocked the other.");
}
}

private void sendCommentPushMessage(User user, Feed feed) {
User feedOwner = feed.getUser();
notificationService.sendCommentPushMessageToCommenters(user, feed);
if (isUserCommentOwner(user, feedOwner) || blockService.isBlocked(feedOwner.getUserId(), user.getUserId())) {
return;
}
notificationService.sendCommentPushMessageToFeedOwner(user, feed);
}

private boolean isUserCommentOwner(User createdUser, User user) {
return createdUser.equals(user);
}
}
Loading
Loading