Skip to content

Commit

Permalink
#173 [Feat] 신고하기 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
sss4920 committed Sep 21, 2023
1 parent cafbdf9 commit 183da3d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/main/java/org/winey/server/controller/FeedController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
import org.winey.server.common.dto.ApiResponse;
import org.winey.server.config.resolver.UserId;
import org.winey.server.controller.request.CreateFeedRequestDto;
import org.winey.server.controller.response.feed.CreateFeedResponseDto;
import org.winey.server.controller.response.feed.DeleteFeedResponseDto;
import org.winey.server.controller.response.feed.GetAllFeedResponseDto;
import org.winey.server.controller.response.feed.GetFeedDetailResponseDto;
import org.winey.server.controller.response.feed.*;
import org.winey.server.exception.Error;
import org.winey.server.exception.Success;
import org.winey.server.external.client.aws.S3Service;
import org.winey.server.service.FeedService;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.io.IOException;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -83,4 +81,11 @@ public ApiResponse<GetAllFeedResponseDto> getMyFeed(@RequestParam int page, @Use
public ApiResponse<GetFeedDetailResponseDto> getFeedDetail(@UserId Long userId, @PathVariable Long feedId) {
return ApiResponse.success(Success.GET_DETAIL_SUCCESS, feedService.getFeedDetail(feedId, userId));
}

@PostMapping(value = "/report/{feedId}")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "피드 신고 API", description = "피드를 신고합니다.")
public ApiResponse<ReportFeedResponseDto> reportFeed(@UserId Long userId, @PathVariable Long feedId) throws IOException {
return ApiResponse.success(Success.REPORT_FEED_SUCCESS, feedService.reportFeed(feedId, userId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.winey.server.controller.response.feed;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ReportFeedResponseDto {
private Long reporterId;
private Long reportFeedId;

private String reportMessage;

public static ReportFeedResponseDto of(Long reporterId, Long reportFeedId, String reportMessage){
return new ReportFeedResponseDto(reporterId, reportFeedId, reportMessage);
}

}
1 change: 1 addition & 0 deletions src/main/java/org/winey/server/exception/Success.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum Success {
UPDATE_NICKNAME_SUCCESS(HttpStatus.OK, "닉네임 변경 성공"),
CHECK_NICKNAME_DUPLICATE_SUCCESS(HttpStatus.OK, "닉네임 중복 확인 성공"),
CHECK_NEW_NOTIFICATION_SUCCESS(HttpStatus.OK, "새 알림 여부 조회 성공"),
REPORT_FEED_SUCCESS(HttpStatus.OK, "피드 신고 완료"),

/**
* 201 CREATED
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/org/winey/server/service/FeedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.winey.server.common.dto.ApiResponse;
import org.winey.server.controller.request.CreateFeedRequestDto;
import org.winey.server.controller.response.PageResponseDto;
import org.winey.server.controller.response.comment.CommentResponseDto;
Expand All @@ -20,7 +21,9 @@
import org.winey.server.exception.model.NotFoundException;
import org.winey.server.exception.model.UnauthorizedException;
import org.winey.server.infrastructure.*;
import org.winey.server.slack.SlackApi;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
Expand All @@ -36,8 +39,8 @@ public class FeedService {
private final GoalRepository goalRepository;
private final FeedLikeRepository feedLikeRepository;
private final CommentRepository commentRepository;

private final NotiRepository notiRepository;
private final SlackApi slackApi;

@Transactional
public CreateFeedResponseDto createFeed(CreateFeedRequestDto request, Long userId, String imageUrl) {
Expand Down Expand Up @@ -292,4 +295,12 @@ private String getTimeAgo(LocalDateTime createdAt) {
}
return "지금";
}
@Transactional
public ReportFeedResponseDto reportFeed(Long userId, Long feedId) throws IOException {
Feed reportFeed = feedRepository.findByFeedId(feedId)
.orElseThrow(()-> new NotFoundException(Error.NOT_FOUND_FEED_EXCEPTION, Error.NOT_FOUND_FEED_EXCEPTION.getMessage()));
User reporter = userRepository.findByUserId(userId)
.orElseThrow(()-> new NotFoundException(Error.NOT_FOUND_USER_EXCEPTION, Error.NOT_FOUND_USER_EXCEPTION.getMessage()));
return ReportFeedResponseDto.of(userId,feedId,slackApi.sendReport(reporter, reportFeed));
}
}
30 changes: 30 additions & 0 deletions src/main/java/org/winey/server/slack/SlackApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.winey.server.domain.feed.Feed;
import org.winey.server.domain.user.User;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
Expand Down Expand Up @@ -69,6 +71,15 @@ private String generateErrorPointMessage(HttpServletRequest request) {

return sb.toString();
}
private String generateReportMessage(Feed feed){
sb.setLength(0);
sb.append("*[🔥 feed id]*" + NEW_LINE + feed.getFeedId() + DOUBLE_NEW_LINE);
sb.append("*[📩 feed content]*" + NEW_LINE + feed.getFeedTitle() + DOUBLE_NEW_LINE);
sb.append("*[🚨 feed image]*" + NEW_LINE + feed.getFeedImage() + DOUBLE_NEW_LINE);
return sb.toString();
}



private String readRootStackTrace(Exception error) {
return error.getStackTrace()[0].toString();
Expand All @@ -84,4 +95,23 @@ private LayoutBlock getSection(String message) {
return Blocks.section(s ->
s.text(BlockCompositions.markdownText(message)));
}
public String sendReport(User user, Feed feed) throws IOException{
List<LayoutBlock> layoutBlocks = generateReportBlock(feed);
Slack.getInstance().send(webhookUrl, WebhookPayloads
.payload(p ->
p.username("신고가 접수되었습니다. 🚨")
.iconUrl("https://yt3.googleusercontent.com/ytc/AGIKgqMVUzRrhoo1gDQcqvPo0PxaJz7e0gqDXT0D78R5VQ=s900-c-k-c0x00ffffff-no-rj")
.blocks(layoutBlocks)));
return "신고가 정상적으로 접수되었습니다.";
}

private List<LayoutBlock> generateReportBlock(Feed feed) {
return Blocks.asBlocks(
getHeader("신고가 들어온 게시물이 있습니다.🚨"+NEW_LINE+"정신적으로 해로운 사진일 수 있으니 주의하세요."),
Blocks.divider(),
getSection(generateReportMessage(feed)),
Blocks.divider(),
getSection("<https://github.com/team-winey/Winey-Server/issues|이슈 생성하러 가기>")
);
}
}

0 comments on commit 183da3d

Please sign in to comment.