Added Post system. Notification system

This commit is contained in:
redtea committed 2022-09-01 17:04:25 +03:00
1 parent cc068fc3a8
commit 3ac444931c
29 files changed
+639 -86

No files matched your search

@@ -0,0 +1,12 @@
package site.easystartup.web.notification.event;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface EventHandler {
}
@@ -0,0 +1,9 @@
package site.easystartup.web.notification.event;
public interface NotificationEvent {
void invoke();
String getRecipient();
String getMessage();
}
@@ -0,0 +1,36 @@
package site.easystartup.web.notification.event;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
@Scope("singleton")
public class NotificationEventManager {
private List<NotificationListener> listeners = new ArrayList<>();
public void registerListener(NotificationListener listener) {
listeners.add(listener);
}
public void removeListener(NotificationListener listener) {
listeners.remove(listener);
}
public void notifyListeners(Class<? extends NotificationEvent> event) {
listeners.forEach(listener -> Arrays.stream(listener.getClass().getMethods()).
filter(m -> m.isAnnotationPresent(EventHandler.class)).forEach(method -> {
if (method.getParameterTypes()[0].equals(event.getClass())) {
try {
method.invoke(listener, event);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}));
}
}
@@ -0,0 +1,4 @@
package site.easystartup.web.notification.event;
public interface NotificationListener {
}
@@ -0,0 +1,26 @@
package site.easystartup.web.notification.event.realtime;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import site.easystartup.web.notification.domian.Notification;
import site.easystartup.web.notification.event.EventHandler;
import site.easystartup.web.notification.event.NotificationEvent;
import site.easystartup.web.notification.event.NotificationListener;
import site.easystartup.web.notification.repo.NotificationRepo;
import site.easystartup.web.notification.service.NotificationService;
import site.easystartup.web.post.domain.event.RepostEvent;
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class RealTimeListener implements NotificationListener {
private final NotificationService notificationService;
private final NotificationRepo repo;
@EventHandler
public void onEvent(NotificationEvent event) {
Notification notification = new Notification();
notification.setText(event.getMessage());
notification.setRecipient(event.getRecipient());
notificationService.sendNotification(repo.save(notification));
}
}