-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from 2024-Iris/notification
SSE 및 알림 기능 구현중
- Loading branch information
Showing
41 changed files
with
1,128 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,4 @@ out/ | |
.vscode/ | ||
/config.yml | ||
/src/main/resources/logback.xml | ||
docker-compose.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package site.iris.issuefy.component; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Component | ||
@Getter | ||
@NoArgsConstructor | ||
public class LambdaKey { | ||
@Value("${jwt.lambdaKey}") | ||
private String key; | ||
|
||
public LambdaKey(String key) { | ||
this.key = key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,42 @@ | ||
package site.iris.issuefy.config; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.listener.ChannelTopic; | ||
import org.springframework.data.redis.listener.PatternTopic; | ||
import org.springframework.data.redis.listener.RedisMessageListenerContainer; | ||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; | ||
|
||
import site.iris.issuefy.service.NotificationService; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Bean | ||
public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) { | ||
RedisTemplate<String, Serializable> template = new RedisTemplate<>(); | ||
template.setConnectionFactory(connectionFactory); | ||
return template; | ||
public RedisMessageListenerContainer container(LettuceConnectionFactory connectionFactory, | ||
MessageListenerAdapter listenerAdapter) { | ||
RedisMessageListenerContainer container = new RedisMessageListenerContainer(); | ||
container.setConnectionFactory(connectionFactory); | ||
container.addMessageListener(listenerAdapter, sseMessagesTopic()); | ||
container.addMessageListener(listenerAdapter, repositoryUpdatesTopic()); | ||
return container; | ||
} | ||
|
||
@Bean | ||
public MessageListenerAdapter listenerAdapter(NotificationService notificationService) { | ||
return new MessageListenerAdapter(notificationService, "handleRedisMessage"); | ||
} | ||
|
||
@Bean | ||
public ChannelTopic sseMessagesTopic() { | ||
return new ChannelTopic("sse:messages"); | ||
} | ||
|
||
@Bean | ||
public PatternTopic repositoryUpdatesTopic() { | ||
return new PatternTopic("repository_updates"); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package site.iris.issuefy.config; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
@Configuration | ||
public class SseConfig { | ||
@Bean | ||
public ConcurrentHashMap<String, SseEmitter> SseConnectionMap() { | ||
return new ConcurrentHashMap<>(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/site/iris/issuefy/controller/NotificationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package site.iris.issuefy.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
import org.springframework.web.bind.annotation.RequestAttribute; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import site.iris.issuefy.model.dto.NotificationDto; | ||
import site.iris.issuefy.model.dto.NotificationReadDto; | ||
import site.iris.issuefy.service.NotificationService; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("api/notifications") | ||
@Slf4j | ||
public class NotificationController { | ||
|
||
private final NotificationService notificationService; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<NotificationDto>> getNotifications(@RequestAttribute String githubId) { | ||
List<NotificationDto> notificationDtos = notificationService.findNotifications(githubId); | ||
log.info(notificationDtos.toString()); | ||
return ResponseEntity.ok(notificationDtos); | ||
} | ||
|
||
@PatchMapping | ||
public ResponseEntity<Void> updateNotification(@RequestBody NotificationReadDto notificationReadDto) { | ||
notificationService.updateUserNotificationsAsRead(notificationReadDto); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/site/iris/issuefy/controller/SseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package site.iris.issuefy.controller; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestAttribute; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import site.iris.issuefy.model.dto.UpdateRepositoryDto; | ||
import site.iris.issuefy.service.NotificationService; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class SseController { | ||
private final NotificationService notificationService; | ||
|
||
@GetMapping(value = "/api/connect", produces = MediaType.TEXT_EVENT_STREAM_VALUE) | ||
public ResponseEntity<SseEmitter> connect(@RequestAttribute String githubId) { | ||
log.info("New connection for user: {}", githubId); | ||
SseEmitter emitter = new SseEmitter(60000L); | ||
notificationService.addEmitter(githubId, emitter); | ||
notificationService.sendInitialNotification(githubId, emitter); | ||
|
||
emitter.onTimeout(() -> { | ||
log.info("SSE connection timed out for user: {}", githubId); | ||
notificationService.removeEmitter(githubId); | ||
try { | ||
emitter.send(SseEmitter.event().name("error").data("Connection timed out")); | ||
} catch (IOException e) { | ||
log.error("Error sending timeout message", e); | ||
} finally { | ||
emitter.complete(); | ||
} | ||
}); | ||
|
||
return ResponseEntity.ok(emitter); | ||
} | ||
|
||
@PostMapping("/api/receive") | ||
public void receive(@RequestBody UpdateRepositoryDto updateRepositoryDto) { | ||
log.info("lambda request receive"); | ||
notificationService.handleRedisMessage(updateRepositoryDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package site.iris.issuefy.entity; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Table(name = "notification") | ||
public class Notification { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "repository_id", nullable = false) | ||
private Repository repository; | ||
|
||
@Column(name = "repository_name", nullable = false) | ||
private String repositoryName; | ||
|
||
@Column(name = "push_time", nullable = false) | ||
private LocalDateTime pushTime; | ||
|
||
public Notification(Repository repository, String repositoryName, LocalDateTime pushTime) { | ||
this.repository = repository; | ||
this.repositoryName = repositoryName; | ||
this.pushTime = pushTime; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Notification{" + | ||
"id=" + id + | ||
", repository=" + repository + | ||
", message='" + repositoryName + '\'' + | ||
", pushTime=" + pushTime + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.