|
| 1 | +package com.likelion.chak.controller; |
| 2 | + |
| 3 | +import com.likelion.chak.alert.MinigameRateLimiter; |
| 4 | +import com.likelion.chak.alert.MinigameRateLimiter.Action; |
| 5 | +import com.likelion.chak.config.AuthenticatedUser; |
| 6 | +import com.likelion.chak.dto.GuestSession; |
| 7 | +import com.likelion.chak.dto.PollDtos.PollList; |
| 8 | +import com.likelion.chak.dto.PollDtos.PollView; |
| 9 | +import com.likelion.chak.dto.PollDtos.VoteRequest; |
| 10 | +import com.likelion.chak.exception.CustomException; |
| 11 | +import com.likelion.chak.exception.ErrorCode; |
| 12 | +import com.likelion.chak.repository.GuestIdentityRepository; |
| 13 | +import com.likelion.chak.service.AnalyticsService; |
| 14 | +import com.likelion.chak.service.GuestIdentityService; |
| 15 | +import com.likelion.chak.service.PollService; |
| 16 | +import com.likelion.chak.service.RegionNames; |
| 17 | +import jakarta.servlet.http.HttpServletRequest; |
| 18 | +import jakarta.validation.Valid; |
| 19 | +import lombok.RequiredArgsConstructor; |
| 20 | +import org.springframework.beans.factory.annotation.Value; |
| 21 | +import org.springframework.http.HttpHeaders; |
| 22 | +import org.springframework.http.ResponseCookie; |
| 23 | +import org.springframework.http.ResponseEntity; |
| 24 | +import org.springframework.security.core.annotation.AuthenticationPrincipal; |
| 25 | +import org.springframework.web.bind.annotation.CookieValue; |
| 26 | +import org.springframework.web.bind.annotation.GetMapping; |
| 27 | +import org.springframework.web.bind.annotation.PathVariable; |
| 28 | +import org.springframework.web.bind.annotation.PostMapping; |
| 29 | +import org.springframework.web.bind.annotation.RequestBody; |
| 30 | +import org.springframework.web.bind.annotation.RequestHeader; |
| 31 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 32 | +import org.springframework.web.bind.annotation.RestController; |
| 33 | + |
| 34 | +import java.time.Duration; |
| 35 | +import java.time.Instant; |
| 36 | + |
| 37 | +/** 수능 여론 투표. 로그인 없이 쓰고, 회원이면 회원 번호로, 아니면 손님 쿠키로 한 사람을 구분한다. */ |
| 38 | +@RestController |
| 39 | +@RequestMapping("/api/public/polls") |
| 40 | +@RequiredArgsConstructor |
| 41 | +public class PublicPollController { |
| 42 | + |
| 43 | + private final PollService pollService; |
| 44 | + private final GuestIdentityService guestIdentityService; |
| 45 | + private final GuestIdentityRepository guestIdentityRepository; |
| 46 | + private final MinigameRateLimiter rateLimiter; |
| 47 | + |
| 48 | + @Value("${chak.guest-cookie.name}") |
| 49 | + private String guestCookieName; |
| 50 | + |
| 51 | + @Value("${chak.guest-cookie.max-age-seconds}") |
| 52 | + private long guestCookieMaxAgeSeconds; |
| 53 | + |
| 54 | + @Value("${chak.guest-cookie.secure}") |
| 55 | + private boolean guestCookieSecure; |
| 56 | + |
| 57 | + @Value("${chak.guest-cookie.same-site:Lax}") |
| 58 | + private String guestCookieSameSite; |
| 59 | + |
| 60 | + @GetMapping |
| 61 | + public ResponseEntity<PollList> list( |
| 62 | + @AuthenticationPrincipal AuthenticatedUser principal, |
| 63 | + @CookieValue(name = "${chak.guest-cookie.name}", required = false) String guestKey) { |
| 64 | + String voterKey; |
| 65 | + if (principal != null) { |
| 66 | + voterKey = "u:" + principal.userId(); |
| 67 | + } else if (guestKey != null && !guestKey.isBlank()) { |
| 68 | + // 목록을 볼 때는 손님을 새로 만들지 않는다. 이미 있는 손님이면 고른 답을 같이 보여 준다. |
| 69 | + voterKey = guestIdentityRepository.findByGuestKey(guestKey).map(g -> "g:" + g.getId()).orElse(null); |
| 70 | + } else { |
| 71 | + voterKey = null; |
| 72 | + } |
| 73 | + return ResponseEntity.ok(pollService.list(voterKey, Instant.now())); |
| 74 | + } |
| 75 | + |
| 76 | + @PostMapping("/{pollId}/votes") |
| 77 | + public ResponseEntity<PollView> vote( |
| 78 | + @PathVariable long pollId, |
| 79 | + @AuthenticationPrincipal AuthenticatedUser principal, |
| 80 | + @CookieValue(name = "${chak.guest-cookie.name}", required = false) String guestKey, |
| 81 | + @RequestHeader(name = "X-Chak-Geo-Country", required = false) String chakCountry, |
| 82 | + @RequestHeader(name = "X-Chak-Geo-Region", required = false) String chakRegion, |
| 83 | + @RequestHeader(name = "X-Vercel-IP-Country", required = false) String vercelCountry, |
| 84 | + @RequestHeader(name = "X-Vercel-IP-Country-Region", required = false) String vercelRegion, |
| 85 | + @Valid @RequestBody VoteRequest request, |
| 86 | + HttpServletRequest http) { |
| 87 | + String ip = ClientIp.of(http); |
| 88 | + if (!rateLimiter.tryAcquire(Action.POLL_VOTE, "ip" + ip)) { |
| 89 | + throw new CustomException(ErrorCode.TOO_MANY_VOTES); |
| 90 | + } |
| 91 | + GuestSession guest = null; |
| 92 | + String voterKey; |
| 93 | + if (principal != null) { |
| 94 | + voterKey = "u:" + principal.userId(); |
| 95 | + } else { |
| 96 | + guest = guestIdentityService.resolveOrCreateLimited(guestKey, ip); |
| 97 | + voterKey = "g:" + guest.getGuestIdentity().getId(); |
| 98 | + } |
| 99 | + AnalyticsService.Geo geo = AnalyticsService.Geo.fromHeaders( |
| 100 | + firstNonBlank(chakCountry, vercelCountry), firstNonBlank(chakRegion, vercelRegion)); |
| 101 | + if (geo == null) { |
| 102 | + geo = AnalyticsService.Geo.fromHeaders(request.geoCountry(), request.geoRegion()); |
| 103 | + } |
| 104 | + String region = geo == null || !"KR".equals(geo.country()) || geo.region() == null |
| 105 | + ? null : RegionNames.of(geo.country(), geo.region()); |
| 106 | + PollView view = pollService.vote(pollId, request.optionId(), voterKey, region, Instant.now()); |
| 107 | + |
| 108 | + ResponseEntity.BodyBuilder builder = ResponseEntity.ok(); |
| 109 | + if (guest != null && guest.isCreated()) { |
| 110 | + builder.header(HttpHeaders.SET_COOKIE, ResponseCookie.from(guestCookieName, guest.getGuestIdentity().getGuestKey()) |
| 111 | + .httpOnly(true).secure(guestCookieSecure).sameSite(guestCookieSameSite).path("/") |
| 112 | + .maxAge(Duration.ofSeconds(guestCookieMaxAgeSeconds)).build().toString()); |
| 113 | + } |
| 114 | + return builder.body(view); |
| 115 | + } |
| 116 | + |
| 117 | + private static String firstNonBlank(String preferred, String fallback) { |
| 118 | + return preferred != null && !preferred.isBlank() ? preferred : fallback; |
| 119 | + } |
| 120 | +} |
0 commit comments