Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

develop to main #167

Merged
merged 5 commits into from
Dec 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import postman.bottler.letter.dto.response.LetterDetailResponseDTO;
import postman.bottler.letter.dto.response.LetterRecommendHeadersResponseDTO;
import postman.bottler.letter.exception.LetterNotFoundException;
import postman.bottler.notification.dto.request.NotificationLabelRequestDTO;
import postman.bottler.user.service.UserService;

@Slf4j
Expand Down Expand Up @@ -76,4 +77,10 @@ public List<LetterRecommendHeadersResponseDTO> getRecommendHeaders(List<Long> le
public boolean checkLetterExists(Long letterId) {
return letterRepository.checkLetterExists(letterId);
}

public List<NotificationLabelRequestDTO> getLabels(List<Long> ids) {
return letterRepository.findAllByIds(ids).stream()
.map(find -> new NotificationLabelRequestDTO(find.getId(), find.getLabel()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,11 @@ public Page<FindReceivedMapLetterDTO> findActiveReceivedMapLettersByUserId(Long
public List<MapLetterAndDistance> guestFindLettersByUserLocation(BigDecimal latitude, BigDecimal longitude) {
return mapLetterJpaRepository.guestFindLettersByUserLocation(latitude, longitude);
}

@Override
public List<MapLetter> findAllByIds(List<Long> ids) {
return mapLetterJpaRepository.findAllById(ids).stream()
.map(MapLetterEntity::toDomain)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public interface MapLetterRepository {
Page<FindReceivedMapLetterDTO> findActiveReceivedMapLettersByUserId(Long userId, PageRequest pageRequest);

List<MapLetterAndDistance> guestFindLettersByUserLocation(BigDecimal latitude, BigDecimal longitude);

List<MapLetter> findAllByIds(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -40,6 +39,7 @@
import postman.bottler.mapletter.exception.LetterAlreadyReplyException;
import postman.bottler.mapletter.exception.MapLetterAlreadyArchivedException;
import postman.bottler.mapletter.exception.PageRequestException;
import postman.bottler.notification.dto.request.NotificationLabelRequestDTO;
import postman.bottler.reply.dto.ReplyType;
import postman.bottler.user.service.UserService;

Expand Down Expand Up @@ -387,4 +387,12 @@ public OneLetterResponseDTO guestFindOneMapLetter(Long letterId, BigDecimal lati
String profileImg = userService.getProfileImageUrlById(mapLetter.getCreateUserId());
return OneLetterResponseDTO.from(mapLetter, profileImg, false);
}

@Transactional(readOnly = true)
public List<NotificationLabelRequestDTO> getLabels(List<Long> ids) {
List<MapLetter> finds = mapLetterRepository.findAllByIds(ids);
return finds.stream()
.map(find -> new NotificationLabelRequestDTO(find.getId(), find.getLabel()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -48,7 +49,7 @@ public ApiResponse<NotificationResponseDTO> create(
}

@Operation(summary = "์•Œ๋ฆผ ์กฐํšŒ", description = "์‚ฌ์šฉ์ž์˜ ์•Œ๋ฆผ์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
@GetMapping
@PatchMapping
public ApiResponse<List<NotificationResponseDTO>> getNotifications(
@AuthenticationPrincipal CustomUserDetails customUserDetails) {
List<NotificationResponseDTO> userNotifications = notificationService.getUserNotifications(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.Getter;
import lombok.Setter;
import postman.bottler.notification.exception.NoLetterIdException;

@Getter
public class LetterNotification extends Notification {
private final long letterId;

@Setter
private String label;

protected LetterNotification(NotificationType type, long receiver, Long letterId, Boolean isRead) {
super(type, receiver, isRead);
validateLetterId(letterId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public Boolean isLetterNotification() {
return type.isLetterNotification();
}

public Boolean isKeywordLetterNotification() {
return type.equals(NotificationType.NEW_LETTER);
}

public Boolean isMapLetterNotification() {
return type.equals(NotificationType.TARGET_LETTER);
}

public Notification read() {
return Notification.of(id, type, receiver,
isLetterNotification() ? ((LetterNotification) this).getLetterId() : null, createdAt, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import postman.bottler.notification.dto.request.NotificationLabelRequestDTO;
import postman.bottler.notification.dto.response.NotificationResponseDTO;

@Builder
Expand Down Expand Up @@ -32,6 +33,55 @@ public Notifications markAsRead() {
return new Notifications(changed);
}

public List<Long> extractMapIds() {
List<Long> ids = new ArrayList<>();
for (Notification notification : notifications) {
if (notification.isMapLetterNotification()) {
ids.add(((LetterNotification) notification).getLetterId());
}
}
return ids;
}

public List<Long> extractKeywordIds() {
List<Long> ids = new ArrayList<>();
for (Notification notification : notifications) {
if (notification.isKeywordLetterNotification()) {
ids.add(((LetterNotification) notification).getLetterId());
}
}
return ids;
}

public void setMapLabels(List<NotificationLabelRequestDTO> mapLabels) {
for (Notification notification : notifications) {
if (!notification.isMapLetterNotification()) {
continue;
}
mapLabels.stream()
.filter(label -> label.letterId() == ((LetterNotification) notification).getLetterId())
.forEach(label -> ((LetterNotification) notification).setLabel(label.label()));
}
}

public void setKeywordLabels(List<NotificationLabelRequestDTO> keywordLabels) {
for (Notification notification : notifications) {
if (!notification.isKeywordLetterNotification()) {
continue;
}
keywordLabels.stream()
.filter(label -> label.letterId() == ((LetterNotification) notification).getLetterId())
.forEach(label -> ((LetterNotification) notification).setLabel(label.label()));
}
}

public List<Long> getLetterIds() {
return notifications.stream()
.filter(Notification::isLetterNotification)
.map(notification -> ((LetterNotification) notification).getLetterId())
.toList();
}

public List<NotificationResponseDTO> createDTO() {
return notifications.stream()
.map(NotificationResponseDTO::from)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package postman.bottler.notification.dto.request;

public record NotificationLabelRequestDTO(
Long letterId,
String label
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public record NotificationResponseDTO(
Long receiver,
LocalDateTime createdAt,
Long letterId,
Boolean isRead
Boolean isRead,
String label
) {
public static NotificationResponseDTO from(final Notification notification) {
return new NotificationResponseDTO(
Expand All @@ -21,6 +22,7 @@ public static NotificationResponseDTO from(final Notification notification) {
notification.getReceiver(),
notification.getCreatedAt(),
notification instanceof LetterNotification ? ((LetterNotification) notification).getLetterId() : null,
notification.getIsRead());
notification.getIsRead(),
notification instanceof LetterNotification ? ((LetterNotification) notification).getLabel() : null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import postman.bottler.letter.service.LetterRepository;
import postman.bottler.mapletter.service.MapLetterRepository;
import postman.bottler.notification.domain.Notification;
import postman.bottler.notification.domain.NotificationType;
import postman.bottler.notification.domain.Notifications;
import postman.bottler.notification.domain.PushMessages;
import postman.bottler.notification.domain.Subscriptions;
import postman.bottler.notification.dto.request.NotificationLabelRequestDTO;
import postman.bottler.notification.dto.request.RecommendNotificationRequestDTO;
import postman.bottler.notification.dto.response.NotificationResponseDTO;

Expand All @@ -21,6 +24,9 @@ public class NotificationService {
private final SubscriptionRepository subscriptionRepository;
private final PushNotificationProvider pushNotificationProvider;

private final MapLetterRepository mapLetterRepository;
private final LetterRepository keywordLetterRepository;

@Transactional
public NotificationResponseDTO sendNotification(NotificationType type, Long userId, Long letterId) {
Notification notification = Notification.create(type, userId, letterId);
Expand All @@ -36,10 +42,17 @@ public NotificationResponseDTO sendNotification(NotificationType type, Long user
@Transactional
public List<NotificationResponseDTO> getUserNotifications(Long userId) {
Notifications notifications = notificationRepository.findByReceiver(userId);
List<Long> mapIds = notifications.extractMapIds();
List<NotificationLabelRequestDTO> mapLabels = mapLetterRepository.findAllByIds(mapIds).stream()
.map(letter -> new NotificationLabelRequestDTO(letter.getId(), letter.getLabel())).toList();
notifications.setMapLabels(mapLabels);
List<Long> keywordIds = notifications.extractKeywordIds();
List<NotificationLabelRequestDTO> keywordLabels = keywordLetterRepository.findAllByIds(keywordIds)
.stream().map(letter -> new NotificationLabelRequestDTO(letter.getId(), letter.getLabel())).toList();
notifications.setKeywordLabels(keywordLabels);
notifications.orderByCreatedAt();
List<NotificationResponseDTO> result = notifications.createDTO();
Notifications changed = notifications.markAsRead();
notificationRepository.updateNotifications(changed);
notificationRepository.updateNotifications(notifications.markAsRead());
return result;
}

Expand Down
26 changes: 13 additions & 13 deletions src/main/java/postman/bottler/user/auth/JwtTokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void afterPropertiesSet() throws Exception {
this.key = Keys.hmacShaKeyFor(keyBytes);
}

public String createToken(Authentication authentication) {
private String createToken(Authentication authentication, String type) {
String email = authentication.getName();

String authorities = authentication.getAuthorities().stream()
Expand All @@ -62,7 +62,13 @@ public String createToken(Authentication authentication) {
.collect(Collectors.joining(","));

Date now = new Date();
Date expiryDate = new Date(now.getTime() + accessTokenExpirationTime);
Date expiryDate = null;

if (type.equals("access")) {
expiryDate = new Date(now.getTime() + accessTokenExpirationTime);
} else if (type.equals("refresh")) {
expiryDate = new Date(now.getTime() + accessTokenExpirationTime);
}

return Jwts.builder()
.setSubject(email)
Expand All @@ -73,18 +79,12 @@ public String createToken(Authentication authentication) {
.compact();
}

public String createRefreshToken(Authentication authentication) {
String email = authentication.getName();

Date now = new Date();
Date expiryDate = new Date(now.getTime() + refreshTokenExpirationTime);
public String createAccessToken(Authentication authentication) {
return createToken(authentication, "access");
}

return Jwts.builder()
.setSubject(email)
.setIssuedAt(now)
.setExpiration(expiryDate)
.signWith(key, SignatureAlgorithm.HS512)
.compact();
public String createRefreshToken(Authentication authentication) {
return createToken(authentication, "refresh");
}

public Authentication getAuthentication(String token) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/postman/bottler/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public AccessTokenResponseDTO validateRefreshToken(String refreshToken) {
}

Authentication authentication = jwtTokenProvider.getAuthentication(refreshToken);
String newAccessToken = jwtTokenProvider.createToken(authentication);
String newAccessToken = jwtTokenProvider.createAccessToken(authentication);
return new AccessTokenResponseDTO(newAccessToken);
}

Expand Down Expand Up @@ -201,7 +201,7 @@ private SignInDTO authenticateAndGenerateTokens(String email, String password) {
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);

String accessToken = jwtTokenProvider.createToken(authentication);
String accessToken = jwtTokenProvider.createAccessToken(authentication);
String refreshToken = jwtTokenProvider.createRefreshToken(authentication);

refreshTokenRepository.createRefreshToken(RefreshToken.createRefreshToken(email, refreshToken));
Expand Down
Loading