Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
umi0410 committed Dec 30, 2021
2 parents 7d21669 + cff0b0c commit a491e41
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.khumu.alimi.data.ResourceKind;
import com.khumu.alimi.data.dto.*;
import com.khumu.alimi.data.entity.ResourceNotificationSubscription;
import com.khumu.alimi.service.NotifyAnnouncementCrawledService;
import com.khumu.alimi.service.AnnouncementService;
import com.khumu.alimi.service.notification.NotificationService;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -23,7 +23,7 @@
@RestController
public class NotificationController {
private final NotificationService notificationService;
private final NotifyAnnouncementCrawledService announcementEventService;
private final AnnouncementService announcementEventService;

@RequestMapping(value="/ping", method=RequestMethod.GET)
public String ping() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/khumu/alimi/data/ResourceKind.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.khumu.alimi.data;

public enum ResourceKind {
comment, article, announcement, study_article, haksa_schedule
user, comment, article, announcement, study_article, haksa_schedule
}
41 changes: 41 additions & 0 deletions src/main/java/com/khumu/alimi/data/dto/KhumuUserDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.khumu.alimi.data.dto;

import lombok.*;

/**
* ์ด๋ฒคํŠธ ๋ฉ”์‹œ์ง€๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ์œ ์ € ์ •๋ณด. ์ด ์ค‘ ํ•„์š”ํ•œ ํ•„๋“œ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.
{
"last_login": null,
"is_active": true,
"date_joined": "2021-12-03T03:27:35.493",
"username": "jinsu.dev",
"kind": "guest",
"nickname": "\\uac1c\\ubc1c\\ud558\\ub294 \\ud638\\ub791\\uc774",
"student_number": "2000123123",
"department": "\\ud559\\uacfc \\ubbf8\\uc124\\uc815",
"created_at": "2021-12-03T03:27:35.493",
"status": "deleted",
"is_superuser": false,
"profile_image": null,
"info21_authenticated_at": null,
"groups": [],
"user_permissions": []
}
*/
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
public class KhumuUserDto {
private String username;

@Override
public String toString() {
return "KhumuUserDto{" +
"username='" + username + '\'' +
'}';
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"sub_link": "http://ce.khu.ac.kr/index.php?hCode=BOARD&page=view&idx=2330&bo_idx=1",
"date": "2021-08-06 11:15:38",
"author": {
"id": 1,
"author_name": "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ",
"followed": false
"id": 1,
"author_name": "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ",
"followed": false
}
},
"followers": [
"jinsu", "bo314"
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/khumu/alimi/data/entity/PushDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public class PushDevice {
@Id
String deviceToken;
String user;

// ํ•ด๋‹น ๊ธฐ๊ธฐ์—์„œ ์œ ์ € ์ •๋ณด๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
public void deleteUserInfo() {
user = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import com.khumu.alimi.data.dto.*;
import com.khumu.alimi.data.resource.ArticleResource;
import com.khumu.alimi.external.slack.SlackNotifier;
import com.khumu.alimi.service.NotifyAnnouncementCrawledService;
import com.khumu.alimi.service.notification.ArticleEventService;
import com.khumu.alimi.service.notification.CommentEventService;
import com.khumu.alimi.service.notification.HaksaScheduleEventService;
import com.khumu.alimi.service.*;
import io.awspring.cloud.messaging.listener.annotation.SqsListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,9 +16,10 @@
@Slf4j
@RequiredArgsConstructor
public class SqsMessageListener {
final NotifyAnnouncementCrawledService announcementEventService;
final ArticleEventService articleEventMessageService;
final CommentEventService commentEventMessageService;
final AnnouncementService announcementEventService;
final ArticleService articleEventMessageService;
final CommentService commentEventMessageService;
final DeleteUserService deleteUserService;
final HaksaScheduleEventService haksaScheduleEventService;
final ObjectMapper objectMapper;
final SlackNotifier slackNotifier;
Expand All @@ -38,25 +36,42 @@ public void receiveMessage(SqsMessageBodyDto body) {
// body.getMessage()๋Š” ๊ฐ๊ฐ์˜ ๋ฆฌ์†Œ์Šค๋“ค์˜ Dto๋ฅผ Json ๋ฌธ์ž์—ด๋กœ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค.
// ์˜ˆ๋ฅผ ๋“ค์–ด CommentDto์˜ Json ๋ฌธ์ž์—ด ๋ฒ„์ ผ.
switch (resourceKind){
case user: {
KhumuUserDto khumuUserDto = objectMapper.readValue(body.getMessage(), KhumuUserDto.class);
switch (eventMessageDto.getEventKind()) {
case delete:
deleteUserService.delete(khumuUserDto);
break;
default:
log.error("์ง€์›ํ•˜์ง€ ์•Š๋Š” resource_kind์™€ event_kind์˜ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค." + eventKind);
break;
}
} break;
case comment:{
CommentDto commentDto = objectMapper.readValue(body.getMessage(), CommentDto.class);
switch (eventMessageDto.getEventKind()) {
case create:
commentEventMessageService.subscribeArticle(commentDto);
commentEventMessageService.createNotificationsForNewComment(commentDto);
break;
default:
log.error("์ง€์›ํ•˜์ง€ ์•Š๋Š” resource_kind์™€ event_kind์˜ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค." + eventKind);
break;
}
} break;
case article: {
ArticleResource article = objectMapper.readValue(body.getMessage(), ArticleResource.class);
switch (eventKind) {
case create:{
articleEventMessageService.subscribeByNewArticle(article);
}break;
} break;

case new_hot_article:{
articleEventMessageService.notifyNewHotArticle(article);
}break;
} break;
default:
log.error("์ง€์›ํ•˜์ง€ ์•Š๋Š” resource_kind์™€ event_kind์˜ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค." + eventKind);
break;
}
} break;
case announcement:{
Expand All @@ -66,6 +81,9 @@ public void receiveMessage(SqsMessageBodyDto body) {
slackNotifier.sendSlack("์ƒˆ๋กœ์šด ๊ณต์ง€์‚ฌํ•ญ์— ๋Œ€ํ•œ ์•Œ๋ฆผ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.", event.getAnnouncement().getTitle());
announcementEventService.notifyNewAnnouncementCrawled(event);
} break;
default:
log.error("์ง€์›ํ•˜์ง€ ์•Š๋Š” resource_kind์™€ event_kind์˜ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค." + eventKind);
break;
}
} break;
case haksa_schedule:{
Expand All @@ -75,10 +93,14 @@ public void receiveMessage(SqsMessageBodyDto body) {
HaksaScheduleDto haksaScheduleDto = objectMapper.readValue(body.getMessage(), HaksaScheduleDto.class);
haksaScheduleEventService.createNotificationForHaksaScheduleStarts(haksaScheduleDto);
} break;
default:
log.error("์ง€์›ํ•˜์ง€ ์•Š๋Š” resource_kind์™€ event_kind์˜ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค." + eventKind);
break;
}
} break;
default:
System.out.println("default");
log.error("์ง€์›ํ•˜์ง€ ์•Š๋Š” resource_kind์™€ event_kind์˜ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค." + eventKind);
break;
}

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ public class FcmPushManager implements PushManager{
@Value("${khumu.notification.rootLink}")
String NOTIFICATION_ROOT_LINK;

public void notify(Notification n, String deviceToken) throws PushException {
public void notify(Notification n, String deviceToken) throws ExpiredPushTokenException {
try {
System.out.println("Execute push notify to " + n.getRecipient() + "(" + deviceToken + ")");
String result = firebaseMessaging.send(
createMessage(n, deviceToken)
);
System.out.println(result);
} catch (FirebaseMessagingException e) {
if (e.getMessage().contains("Requested entity was not found.")) {
throw new ExpiredPushTokenException(deviceToken);
}

throw new PushException(e.getMessage());
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/khumu/alimi/external/push/PushManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
* ์ด๊ฑด DB์˜ Entity๊ฐ€ ์•„๋‹ˆ๋ผ FCM Push๋ฅผ ์˜๋ฏธ.
*/
public interface PushManager {
void notify(Notification n, String deviceToken) throws PushException;
void notify(Notification n, String deviceToken) throws PushException, ExpiredPushTokenException;
// ๋ชจ๋‘์—๊ฒŒ Notify
/// void notify(Notication n);
public static class PushException extends Exception{
class PushException extends RuntimeException{
public PushException(String message) {
super(message);
}
}

class ExpiredPushTokenException extends Exception{
public ExpiredPushTokenException(String token){
super("๋งŒ๋ฃŒ๋œ ํ† ํฐ์ž…๋‹ˆ๋‹ค.: " + token);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ public PushDevice save(PushDevice subscription) {
public void delete(PushDevice device) {
pushDeviceRepository.delete(device);
}

public void deleteAll(Iterable<PushDevice> devices){
pushDeviceRepository.deleteAll(devices);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface ResourceNotificationSubscriptionRepository extends JpaRepositor
List<ResourceNotificationSubscription> findAllByResourceKindAndResourceId(ResourceKind resourceKind, Long resourceId);
List<ResourceNotificationSubscription> findAllBySubscriberAndResourceKindAndResourceId(String subscriberId, ResourceKind resourceKind, Long resourceId);

List<ResourceNotificationSubscription> findAllBySubscriber(String subscriberId);

// ํ•ด๋‹น ์œ ์ €์˜ ํ•ด๋‹น ResourceKind์˜ ResourceNotificationSubscription์„ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ๋งŒ๋“ค๊ณ  ์กฐํšŒํ•จ.
default ResourceNotificationSubscription getOrCreate(ResourceNotificationSubscription subscription){
// user์˜ ํ•ด๋‹น ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ๊ตฌ๋… ์ •๋ณด๋ฅผ ์กฐํšŒํ•ด์ฃผ๊ฑฐ๋‚˜
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@Slf4j
@RequiredArgsConstructor
@Service
public class NotifyAnnouncementCrawledService {
public class AnnouncementService {
private final NotificationRepository notificationRepository;
private final CustomPushDeviceRepository pushDeviceRepository;
private final CustomPushOptionRepository pushOptionRepository;
Expand All @@ -50,13 +50,9 @@ public void notifyNewAnnouncementCrawled(NewAnnouncementCrawledDto event) {
try {
pushManager.notify(n, device.getDeviceToken());
log.info("ํ‘ธ์‹œ๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. " + device.getUser());
} catch (PushManager.PushException e) {
if (e.getMessage().contains("Requested entity was not found.")) {
log.warn("๋” ์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š๋Š” device tokne์ด๋ฏ€๋กœ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค." + device.getDeviceToken());
pushDeviceRepository.delete(device);
} else{
e.printStackTrace();
}
} catch (PushManager.ExpiredPushTokenException e) {
log.warn("๋” ์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š๋Š” device tokne์ด๋ฏ€๋กœ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค." + device.getDeviceToken());
pushDeviceRepository.delete(device);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.khumu.alimi.service.notification;
package com.khumu.alimi.service;

import com.khumu.alimi.data.ResourceKind;
import com.khumu.alimi.data.dto.EventMessageDto;
Expand All @@ -9,6 +9,7 @@
import com.khumu.alimi.repository.CustomPushOptionRepository;
import com.khumu.alimi.repository.NotificationRepository;
import com.khumu.alimi.repository.PushDeviceRepository;
import com.khumu.alimi.service.notification.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -21,7 +22,7 @@
@Slf4j
@RequiredArgsConstructor
@Service
public class ArticleEventService {
public class ArticleService {
final NotificationRepository notificationRepository;
final NotificationService notificationService;
final PushDeviceRepository pushDeviceRepository;
Expand Down Expand Up @@ -59,10 +60,15 @@ public void notifyNewHotArticle(ArticleResource article) throws PushManager.Push

Notification n = notificationRepository.save(tmp);

List<PushDevice> subscriptions = pushDeviceRepository.findAllByUser(author);
for (PushDevice subscription : subscriptions) {
pushManager.notify(n, subscription.getDeviceToken());
log.info("ํ‘ธ์‹œ๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. " + subscription.getUser());
List<PushDevice> devices = pushDeviceRepository.findAllByUser(author);
for (PushDevice device : devices) {
try {
log.info("ํ‘ธ์‹œ๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. " + n.getRecipient());
pushManager.notify(n, device.getDeviceToken());
} catch (PushManager.ExpiredPushTokenException e) {
log.warn("๋” ์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š๋Š” device tokne์ด๋ฏ€๋กœ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค." + device.getDeviceToken());
pushDeviceRepository.delete(device);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.khumu.alimi.service.notification;
package com.khumu.alimi.service;

import com.google.gson.Gson;
import com.khumu.alimi.data.ResourceKind;
Expand All @@ -7,6 +7,7 @@
import com.khumu.alimi.data.entity.*;
import com.khumu.alimi.external.push.PushManager;
import com.khumu.alimi.repository.*;
import com.khumu.alimi.service.notification.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -25,7 +26,7 @@
@Service
@RequiredArgsConstructor
@Slf4j
public class CommentEventService {
public class CommentService {
final NotificationRepository notificationRepository;
final CustomPushDeviceRepository pushDeviceRepository;
final CustomPushOptionRepository pushOptionRepository;
Expand All @@ -51,10 +52,15 @@ public List<Notification> createNotificationsForNewComment(CommentDto commentDto

Notification n = notificationRepository.save(tmp);

List<PushDevice> subscriptions = pushDeviceRepository.findAllByUser(recipientId);
for (PushDevice subscription : subscriptions) {
pushManager.notify(n, subscription.getDeviceToken());
log.info("ํ‘ธ์‹œ๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. " + subscription.getUser());
List<PushDevice> devices = pushDeviceRepository.findAllByUser(recipientId);
for (PushDevice device : devices) {
try {
log.info("ํ‘ธ์‹œ๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. " + n.getRecipient());
pushManager.notify(n, device.getDeviceToken());
} catch (PushManager.ExpiredPushTokenException e) {
log.warn("๋” ์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š๋Š” device tokne์ด๋ฏ€๋กœ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค." + device.getDeviceToken());
pushDeviceRepository.delete(device);
}
}
results.add(n);
}
Expand Down
Loading

0 comments on commit a491e41

Please sign in to comment.