Skip to content

Commit

Permalink
Merge pull request #247 from depromeet/feature/246-not-retrieve-block…
Browse files Browse the repository at this point in the history
…-users-notification
  • Loading branch information
mybloom authored Dec 26, 2022
2 parents 8e81941 + d1abe54 commit 5451f04
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ public class GroupBriefInfoDto {

private GroupType groupType;

public GroupBriefInfoDto(GroupBaseInfoVo groupBaseInfoVo) {
groupId = groupBaseInfoVo.getGroupId();
title = groupBaseInfoVo.getTitle();
description = groupBaseInfoVo.getDescription();
publicAccess = groupBaseInfoVo.getPublicAccess();
thumbnailPath = groupBaseInfoVo.getThumbnailPath();
groupType = groupBaseInfoVo.getGroupType();
category = new CategoryDto(groupBaseInfoVo.getCategory());
}

public GroupBriefInfoDto(GroupBaseInfoVo groupBaseInfoVo, Integer memberCount) {
groupId = groupBaseInfoVo.getGroupId();
title = groupBaseInfoVo.getTitle();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.depromeet.knockknockbackend.domain.group.presentation.dto.response;


import io.github.depromeet.knockknockbackend.domain.group.domain.GroupType;
import io.github.depromeet.knockknockbackend.domain.group.domain.vo.GroupBaseInfoVo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class GroupInfoForNotificationDto {
private Long groupId;

private String title;

private String description;

private String thumbnailPath;

@Schema(description = "공개 그룹 여부 ture 면 공개임")
private Boolean publicAccess;

private GroupType groupType;

public GroupInfoForNotificationDto(GroupBaseInfoVo groupBaseInfoVo) {
groupId = groupBaseInfoVo.getGroupId();
title = groupBaseInfoVo.getTitle();
description = groupBaseInfoVo.getDescription();
publicAccess = groupBaseInfoVo.getPublicAccess();
thumbnailPath = groupBaseInfoVo.getThumbnailPath();
groupType = groupBaseInfoVo.getGroupType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ List<DeviceToken> findTokenByGroupAndOptionAndNonBlock(
Long userId, Long groupId, Boolean nightOption);

List<Notification> findSliceLatestByReceiver(Long receiveUserId);

Slice<Notification> findSliceByGroupId(
Long userId, Long groupId, boolean deleted, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.depromeet.knockknockbackend.domain.notification.domain.repository;

import static io.github.depromeet.knockknockbackend.domain.group.domain.QGroup.group;
import static io.github.depromeet.knockknockbackend.domain.group.domain.QGroupUser.groupUser;
import static io.github.depromeet.knockknockbackend.domain.notification.domain.QDeviceToken.deviceToken;
import static io.github.depromeet.knockknockbackend.domain.notification.domain.QNotification.notification;
Expand Down Expand Up @@ -33,15 +34,39 @@ public class CustomNotificationRepositoryImpl implements CustomNotificationRepos
private static final int NUMBER_OF_LATEST_NOTIFICATIONS = 10;
private final JPAQueryFactory queryFactory;

private boolean hasNext(List<Notification> notifications, Pageable pageable) {
private <T> boolean hasNext(List<T> list, Pageable pageable) {
boolean hasNext = false;
if (notifications.size() > pageable.getPageSize()) {
notifications.remove(pageable.getPageSize());
if (list.size() > pageable.getPageSize()) {
list.remove(pageable.getPageSize());
hasNext = true;
}
return hasNext;
}

@Override
public Slice<Notification> findSliceByGroupId(
Long userId, Long groupId, boolean deleted, Pageable pageable) {
List<Notification> notifications =
queryFactory
.selectFrom(notification)
.join(notification.group, group)
.fetchJoin()
.where(
group.id.eq(groupId),
notification.deleted.eq(deleted),
JPAExpressions.selectFrom(blockUser)
.where(
blockUser.blockedUser.eq(notification.sendUser),
blockUser.user.id.eq(userId))
.notExists())
.orderBy(sort("notification", pageable))
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + NEXT_SLICE_CHECK)
.fetch();

return new SliceImpl<>(notifications, pageable, hasNext(notifications, pageable));
}

@Override
public Slice<Notification> findSliceFromStorage(
Long userId, List<Group> groups, Integer periodOfMonth, Pageable pageable) {
Expand Down Expand Up @@ -89,6 +114,8 @@ public List<DeviceToken> findTokenByGroupAndOptionAndNonBlock(
public List<Notification> findSliceLatestByReceiver(Long receiveUserId) {
return queryFactory
.selectFrom(notification)
.join(notification.group, group)
.fetchJoin()
.where(
notification.deleted.eq(false),
JPAExpressions.selectFrom(notificationReceiver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@

import io.github.depromeet.knockknockbackend.domain.notification.domain.Notification;
import java.util.Optional;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;

public interface NotificationRepository
extends JpaRepository<Notification, Long>, CustomNotificationRepository {

@EntityGraph(attributePaths = {"group"})
Slice<Notification> findAllByGroupIdAndDeleted(
Long groupId, boolean deleted, Pageable pageable);

@EntityGraph(attributePaths = {"group"})
Optional<Notification> findById(Long notificationId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.depromeet.knockknockbackend.domain.notification.presentation.dto.response;


import io.github.depromeet.knockknockbackend.domain.group.presentation.dto.response.GroupBriefInfoDto;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -11,7 +10,6 @@
@AllArgsConstructor
public class QueryNotificationListResponse {

private final GroupBriefInfoDto groups;
private final List<QueryReservationListResponseElement> reservations;
private final Slice<QueryNotificationListResponseElement> notifications;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.depromeet.knockknockbackend.domain.notification.presentation.dto.response;


import io.github.depromeet.knockknockbackend.domain.group.presentation.dto.response.GroupInfoForNotificationDto;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -14,5 +15,6 @@ public class QueryNotificationListResponseElement {
private String imageUrl;
private LocalDateTime createdDate;
private Long sendUserId;
private GroupInfoForNotificationDto groups;
private QueryNotificationReactionResponseElement reactions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import io.github.depromeet.knockknockbackend.domain.group.domain.Group;
import io.github.depromeet.knockknockbackend.domain.group.presentation.dto.response.GroupBriefInfoDto;
import io.github.depromeet.knockknockbackend.domain.group.presentation.dto.response.GroupInfoForNotificationDto;
import io.github.depromeet.knockknockbackend.domain.notification.domain.*;
import io.github.depromeet.knockknockbackend.domain.notification.domain.Notification;
import io.github.depromeet.knockknockbackend.domain.notification.domain.repository.DeviceTokenRepository;
Expand Down Expand Up @@ -63,18 +63,13 @@ public QueryNotificationListLatestResponse queryListLatest() {

@Transactional(readOnly = true)
public QueryNotificationListResponse queryListByGroupId(Pageable pageable, Long groupId) {
Slice<Notification> notifications =
notificationRepository.findAllByGroupIdAndDeleted(
groupId, CREATED_DELETED_STATUS, pageable);

GroupBriefInfoDto groupBriefInfoDto =
notifications.stream()
.findFirst()
.map(
notification ->
new GroupBriefInfoDto(
notification.getGroup().getGroupBaseInfoVo()))
.orElse(null);
Slice<Notification> notifications =
notificationRepository.findSliceByGroupId(
SecurityUtils.getCurrentUserId(),
groupId,
CREATED_DELETED_STATUS,
pageable);

List<NotificationReaction> myNotificationReactions =
retrieveMyReactions(notifications.getContent());
Expand All @@ -101,9 +96,7 @@ public QueryNotificationListResponse queryListByGroupId(Pageable pageable, Long
.collect(Collectors.toList());

return new QueryNotificationListResponse(
groupBriefInfoDto,
queryReservationListResponseElements,
queryNotificationListResponseElements);
queryReservationListResponseElements, queryNotificationListResponseElements);
}

public QueryNotificationListResponseElement getQueryNotificationListResponseElements(
Expand Down Expand Up @@ -141,6 +134,9 @@ public QueryNotificationListResponseElement getQueryNotificationListResponseElem
.imageUrl(notification.getImageUrl())
.createdDate(notification.getCreatedDate())
.sendUserId(notification.getSendUser().getId())
.groups(
new GroupInfoForNotificationDto(
notification.getGroup().getGroupBaseInfoVo()))
.reactions(notificationReactionResponseElement)
.build();
}
Expand Down Expand Up @@ -232,8 +228,8 @@ public void recordNotification(
}

public List<NotificationReaction> retrieveMyReactions(List<Notification> notifications) {
return notificationReactionRepository.findByUserIdAndNotificationIn(
SecurityUtils.getCurrentUserId(), notifications);
return notificationReactionRepository.findByUserAndNotificationIn(
User.of(SecurityUtils.getCurrentUserId()), notifications);
}

private void validateDeletePermission(Notification notification) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.depromeet.knockknockbackend.domain.notification.domain.Notification;
import io.github.depromeet.knockknockbackend.domain.notification.domain.vo.NotificationReactionCountInfoVo;
import io.github.depromeet.knockknockbackend.domain.reaction.domain.NotificationReaction;
import io.github.depromeet.knockknockbackend.domain.user.domain.User;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
Expand All @@ -17,6 +18,6 @@ public interface NotificationReactionRepository extends CrudRepository<Notificat
+ "group by NR.reaction.id")
List<NotificationReactionCountInfoVo> findAllCountByNotification(Notification notification);

List<NotificationReaction> findByUserIdAndNotificationIn(
Long userId, List<Notification> notifications);
List<NotificationReaction> findByUserAndNotificationIn(
User user, List<Notification> notifications);
}

0 comments on commit 5451f04

Please sign in to comment.