package de.juplo.kafka.chat.backend.domain;
-import lombok.EqualsAndHashCode;
-import lombok.Getter;
-import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;
+import reactor.core.publisher.SynchronousSink;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
@Slf4j
-@EqualsAndHashCode(of = { "id" })
-@ToString(of = { "id", "name" })
-public class ChatRoom
+public class ChatRoom extends ChatRoomInfo
{
- @Getter
- private final UUID id;
- @Getter
- private final String name;
+ public final static Pattern VALID_USER = Pattern.compile("^[a-z0-9-]{2,}$");
private final Clock clock;
private final ChatRoomService service;
private final int bufferSize;
private Sinks.Many<Message> sink;
+
public ChatRoom(
UUID id,
String name,
+ int shard,
Clock clock,
ChatRoomService service,
int bufferSize)
{
- this.id = id;
- this.name = name;
+ super(id, name, shard);
+ log.info("Created ChatRoom {} with buffer-size {}", id, bufferSize);
this.clock = clock;
this.service = service;
this.bufferSize = bufferSize;
+ // @RequiredArgsConstructor unfortunately not possible, because
+ // the `bufferSize` is not set, if `createSink()` is called
+ // from the variable declaration!
this.sink = createSink();
}
String user,
String text)
{
+ Matcher matcher = VALID_USER.matcher(user);
+ if (!matcher.matches())
+ throw new InvalidUsernameException(user);
+
+ Message.MessageKey key = Message.MessageKey.of(user, id);
return service
- .persistMessage(Message.MessageKey.of(user, id), LocalDateTime.now(clock), text)
- .doOnNext(message ->
+ .getMessage(key)
+ .handle((Message existing, SynchronousSink<Message> sink) ->
{
- Sinks.EmitResult result = sink.tryEmitNext(message);
- if (result.isFailure())
+ if (existing.getMessageText().equals(text))
+ {
+ sink.next(existing);
+ }
+ else
{
- log.warn("Emitting of message failed with {} for {}", result.name(), message);
+ sink.error(new MessageMutationException(existing, text));
}
- });
+ })
+ .switchIfEmpty(
+ Mono
+ .defer(() -> service.persistMessage(key, LocalDateTime.now(clock), text))
+ .doOnNext(m ->
+ {
+ Sinks.EmitResult result = sink.tryEmitNext(m);
+ if (result.isFailure())
+ {
+ log.warn("Emitting of message failed with {} for {}", result.name(), m);
+ }
+ }));
}