Skip to content

Commit

Permalink
Merge pull request #764 from Walkhub/763-fcm-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lyutvs authored Jul 4, 2022
2 parents 01019f7 + 41ed6cc commit c14201f
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ out/
### VS Code ###
.vscode/

src/main/resources/walkhub-7bfde-firebase-adminsdk-gsqm9-f0c32d0607.json
src/main/resources/walkhub-7bfde-firebase-adminsdk-gsqm9-bf7ec039c2.json
**.DS_STORE
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package com.walkhub.walkhub.domain.notification.domain;


import com.walkhub.walkhub.domain.notification.domain.type.NotificationType;
import com.walkhub.walkhub.domain.user.domain.User;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@DynamicInsert
@Entity
public class Topic {

Expand All @@ -29,17 +24,8 @@ public class Topic {
@Length(max = 20)
private NotificationType type;

@ColumnDefault("0")
private Boolean isSubscribe;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Builder
public Topic(NotificationType type, Boolean isSubscribe, User user) {
public Topic(NotificationType type) {
this.type = type;
this.isSubscribe = isSubscribe;
this.user = user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.walkhub.walkhub.domain.notification.domain;

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

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class TopicList {

@EmbeddedId
private TopicListId id;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.walkhub.walkhub.domain.notification.domain;

import com.walkhub.walkhub.domain.user.domain.User;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;

import javax.persistence.*;
import java.io.Serializable;

@Getter
@Builder
@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@DynamicInsert
@EqualsAndHashCode
public class TopicListId implements Serializable {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "topic_id", nullable = false)
private Topic topic;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@ColumnDefault("0")
@Column(nullable = false)
private Boolean isSubscribe;

public void subscribeTopic() {
this.isSubscribe = true;
}

public void snSubscribeTopic() {
this.isSubscribe = false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.walkhub.walkhub.domain.notification.domain.repository;

import com.walkhub.walkhub.domain.notification.domain.TopicList;
import com.walkhub.walkhub.domain.notification.domain.TopicListId;
import com.walkhub.walkhub.domain.user.domain.User;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface TopicListRepository extends CrudRepository<TopicList, TopicListId> {

@Query("select tl from TopicList tl join fetch tl.id.user")
List<TopicList> findAllByIdUser(User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import com.walkhub.walkhub.domain.notification.domain.Topic;
import com.walkhub.walkhub.domain.notification.domain.type.NotificationType;
import com.walkhub.walkhub.domain.user.domain.User;
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Optional;

public interface TopicRepository extends CrudRepository<Topic, Long> {
List<Topic> findAllByUser(User user);

Optional<Topic> findByType(NotificationType type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public QueryNotificationListResponse notificationList(Pageable pageable) {
}

@PatchMapping
public void unSubscribeTopic(@RequestBody @Valid SubscribeRequest request) {
public void unSubscribeTopic(@RequestBody @Valid SubscribeRequest request) {
fcmUtil.unSubscribeTopic(request);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.walkhub.walkhub.domain.notification.service;

import com.walkhub.walkhub.domain.notification.domain.Topic;
import com.walkhub.walkhub.domain.notification.domain.repository.TopicRepository;
import com.walkhub.walkhub.domain.notification.domain.TopicList;
import com.walkhub.walkhub.domain.notification.domain.repository.TopicListRepository;
import com.walkhub.walkhub.domain.notification.presentation.dto.response.NotificationStatusResponse;
import com.walkhub.walkhub.domain.notification.presentation.dto.response.NotificationStatusResponse.StatusResponse;
import com.walkhub.walkhub.domain.user.domain.User;
Expand All @@ -17,24 +18,26 @@
public class QueryNotificationStatusListService {

private final UserFacade userFacade;
private final TopicRepository topicRepository;
private final TopicListRepository topicListRepository;

public NotificationStatusResponse execute() {
User user = userFacade.getCurrentUser();

List<StatusResponse> whetherList = topicRepository.findAllByUser(user)
List<StatusResponse> whetherList = topicListRepository.findAllByIdUser(user)
.stream()
.map(this::topicWhetherBuilder)
.map(this::topicStatusBuilder)
.collect(Collectors.toList());

return new NotificationStatusResponse(whetherList);
}

private StatusResponse topicWhetherBuilder(Topic topic) {
private StatusResponse topicStatusBuilder(TopicList topicList) {

Topic topic = topicList.getId().getTopic();
return StatusResponse.builder()
.id(topic.getId())
.type(topic.getType())
.isSubscribe(topic.getIsSubscribe())
.isSubscribe(topicList.getId().getIsSubscribe())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
import com.walkhub.walkhub.domain.notice.domain.Notice;
import com.walkhub.walkhub.domain.notification.domain.NotificationEntity;
import com.walkhub.walkhub.domain.notification.domain.Topic;
import com.walkhub.walkhub.domain.notification.domain.TopicList;
import com.walkhub.walkhub.domain.notification.domain.TopicListId;
import com.walkhub.walkhub.domain.notification.domain.repository.NotificationRepository;
import com.walkhub.walkhub.domain.notification.domain.repository.TopicListRepository;
import com.walkhub.walkhub.domain.notification.exception.TopicNotFoundException;
import com.walkhub.walkhub.domain.notification.facade.TopicFacade;
import com.walkhub.walkhub.domain.notification.presentation.dto.request.SubscribeRequest;
import com.walkhub.walkhub.domain.user.domain.User;
import com.walkhub.walkhub.domain.user.domain.repository.UserRepository;
import com.walkhub.walkhub.domain.user.facade.UserFacade;
import com.walkhub.walkhub.infrastructure.fcm.dto.request.NotificationInformation;
import com.walkhub.walkhub.infrastructure.fcm.dto.request.NotificationRequest;
import com.walkhub.walkhub.infrastructure.fcm.type.ContentType;
Expand All @@ -26,6 +31,7 @@
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.transaction.Transactional;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Arrays;
Expand All @@ -40,7 +46,9 @@ public class FirebaseNotification implements FcmUtil {

private final UserRepository userRepository;
private final NotificationRepository notificationRepository;
private final TopicListRepository topicListRepository;
private final TopicFacade topicFacade;
private final UserFacade userFacade;

@Value("${firebase.path}")
private String path;
Expand Down Expand Up @@ -99,45 +107,63 @@ public void sendNotification(NotificationRequest request) {
FirebaseMessaging.getInstance().sendAsync(message);
}

@Transactional
@Override
public void subscribeTopic(SubscribeRequest request) {
List<User> userList = userRepository.findAllByIdIn(request.getUserIdList());
Topic topic = topicFacade.getTopicByType(request.getType());

try {
for (int i = 0; i < userList.size() / 1000; i++) {
List<String> deviceTokenListToSubscribe = userList.subList(i * 1000, 1000 * i + 1000)
.stream().map(User::getDeviceToken)
.collect(Collectors.toList());

FirebaseMessaging.getInstance(FirebaseApp.getInstance())
.subscribeToTopic(
deviceTokenListToSubscribe, topic.toString()
);
}
} catch (FirebaseMessagingException e) {
log.error(e.getMessage());
}
subscribeToQueriedTopics(request, true);
}

@Transactional
@Override
public void unSubscribeTopic(SubscribeRequest request) {
subscribeToQueriedTopics(request, false);
}

private void subscribeToQueriedTopics(SubscribeRequest request, boolean isSubscribing) {
List<User> userList = userRepository.findAllByIdIn(request.getUserIdList());
Topic topic = topicFacade.getTopicByType(request.getType());

try {
for (int i = 0; i < userList.size() / 1000; i++) {
List<String> deviceTokenListToSubscribe = userList.subList(i * 1000, 1000 * i + 1000)
.stream().map(User::getDeviceToken)
.collect(Collectors.toList());

FirebaseMessaging.getInstance(FirebaseApp.getInstance())
.unsubscribeFromTopic(
deviceTokenListToSubscribe, topic.toString()
);
subscribeTopicInFirebase(userList, topic, isSubscribing);
}

private void subscribeTopicInFirebase(List<User> userList, Topic topic, boolean isSubscribing) {
for (int i = 0; i < userList.size() / 1000; i++) {
List<String> deviceTokenListToSubscribe = userList.subList(i * 1000, 1000 * i + 1000)
.stream().map(User::getDeviceToken)
.collect(Collectors.toList());

FirebaseMessaging instance = FirebaseMessaging.getInstance(FirebaseApp.getInstance());

try {
if (isSubscribing) {
instance.subscribeToTopic(deviceTokenListToSubscribe, topic.toString());
subscribeTopicEntity(topic, isSubscribing);
} else {
instance.unsubscribeFromTopic(deviceTokenListToSubscribe, topic.toString());
subscribeTopicEntity(topic, isSubscribing);
}
} catch (FirebaseMessagingException e) {
log.error(e.getMessage());
}
} catch (FirebaseMessagingException e) {
log.error(e.getMessage());

}
}

private void subscribeTopicEntity(Topic topic, boolean isSubscribing) {
User user = userFacade.getCurrentUser();

TopicListId topicListId = TopicListId.builder()
.topic(topic)
.user(user)
.build();

TopicList topicList = topicListRepository.findById(topicListId)
.orElseThrow(() -> TopicNotFoundException.EXCEPTION);

if (isSubscribing) {
topicList.getId().subscribeTopic();
} else {
topicList.getId().snSubscribeTopic();
}
}

Expand Down

0 comments on commit c14201f

Please sign in to comment.