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
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);