import java.time.Clock;
import java.time.LocalDateTime;
import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
@Slf4j
@ToString(of = { "id", "name" })
public class ChatRoom
{
+ public final static Pattern VALID_USER = Pattern.compile("^[a-z0-9-]{2,}$");
@Getter
private final UUID id;
@Getter
private final int bufferSize;
private Sinks.Many<Message> sink;
+
public ChatRoom(
UUID id,
String name,
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
.getMessage(key)
: Mono.error(() -> new MessageMutationException(existing, text)))
.switchIfEmpty(
Mono
- .just(service.persistMessage(key, LocalDateTime.now(clock), text))
+ .fromSupplier(() ->service.persistMessage(key, LocalDateTime.now(clock), text))
.doOnNext(m ->
{
Sinks.EmitResult result = sink.tryEmitNext(m);