1 package de.juplo.kafka.chat.backend.domain;
3 import de.juplo.kafka.chat.backend.domain.exceptions.ChatRoomInactiveException;
4 import de.juplo.kafka.chat.backend.domain.exceptions.InvalidUsernameException;
5 import de.juplo.kafka.chat.backend.domain.exceptions.MessageMutationException;
6 import lombok.extern.slf4j.Slf4j;
7 import reactor.core.publisher.Flux;
8 import reactor.core.publisher.Mono;
9 import reactor.core.publisher.Sinks;
10 import reactor.core.publisher.SynchronousSink;
12 import java.time.Clock;
13 import java.time.LocalDateTime;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
19 public class ChatRoomData
21 public final static Pattern VALID_USER = Pattern.compile("^[a-z0-9-]{2,}$");
23 private final ChatMessageService service;
24 private final Clock clock;
25 private final int historyLimit;
26 private Sinks.Many<Message> sink;
27 private volatile boolean active = false;
32 ChatMessageService service,
35 log.info("Created ChatRoom with history-limit {}", historyLimit);
37 this.service = service;
38 this.historyLimit = historyLimit;
39 // @RequiredArgsConstructor unfortunately not possible, because
40 // the `historyLimit` is not set, if `createSink()` is called
41 // from the variable declaration!
45 synchronized public Mono<Message> addMessage(
50 Matcher matcher = VALID_USER.matcher(user);
51 if (!matcher.matches())
52 throw new InvalidUsernameException(user);
54 Message.MessageKey key = Message.MessageKey.of(user, id);
57 .handle((Message existing, SynchronousSink<Message> sink) ->
59 if (existing.getMessageText().equals(text))
65 sink.error(new MessageMutationException(existing, text));
70 .defer(() -> service.persistMessage(key, LocalDateTime.now(clock), text))
73 Sinks.EmitResult result = sink.tryEmitNext(m);
74 if (result.isFailure())
76 log.warn("Emitting of message failed with {} for {}", result.name(), m);
79 : Mono.error(new ChatRoomInactiveException(service.getChatRoomId())));
83 public ChatMessageService getChatRoomService()
88 public Mono<Message> getMessage(String username, Long messageId)
90 Message.MessageKey key = Message.MessageKey.of(username, messageId);
91 return service.getMessage(key);
94 synchronized public Flux<Message> listen()
99 .doOnCancel(() -> sink = createSink()) // Sink hast to be recreated on auto-cancel!
101 .error(new ChatRoomInactiveException(service.getChatRoomId()));
105 public Flux<Message> getMessages()
107 return getMessages(0, Long.MAX_VALUE);
110 public Flux<Message> getMessages(long first, long last)
112 return service.getMessages(first, last);
115 public void activate()
119 log.info("{} is already active!", service.getChatRoomId());
123 log.info("{} is being activated", service.getChatRoomId());
124 this.sink = createSink();
128 public void deactivate()
130 log.info("{} is being deactivated", service.getChatRoomId());
132 sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
135 private Sinks.Many<Message> createSink()
140 .limit(historyLimit);