Added filter on boardName for subscription on newly created posts
*/
package org.forum.server.specific_code;
+import javax.annotation.Resource;
+
+import org.forum.server.graphql.Board;
import org.forum.server.graphql.Post;
+import org.forum.server.graphql.Topic;
+import org.forum.server.jpa.BoardRepository;
+import org.forum.server.jpa.TopicRepository;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import io.reactivex.BackpressureStrategy;
+import io.reactivex.Flowable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.PublishSubject;
@@ -26,6 +33,11 @@ public class PostPublisher {
/** The logger for this instance */
protected Logger logger = LoggerFactory.getLogger(this.getClass());
+ @Resource
+ TopicRepository topicRepository;
+ @Resource
+ BoardRepository boardRepository;
+
PublishSubject<Post> subject = PublishSubject.create();
public PostPublisher() {
@@ -74,7 +86,18 @@ public class PostPublisher {
*/
Publisher<Post> getPublisher(String boardName) {
logger.debug("Executing Suscription for {}", boardName);
- return subject.toFlowable(BackpressureStrategy.BUFFER);
+
+ Flowable<Post> publisher = subject.toFlowable(BackpressureStrategy.BUFFER);
+
+ if (boardName != null) {
+ publisher.filter((post) -> {
+ Topic topic = topicRepository.findById(post.getTopicId()).get();
+ Board board = boardRepository.findById(topic.getBoardId()).get();
+ return board.getName().equals(boardName);
+ });
+ }
+
+ return publisher;
}
}
