refactor: The `ChatRoom` determines the timestamp of a `Message`
authorKai Moritz <kai@juplo.de>
Sun, 8 Jan 2023 09:57:25 +0000 (10:57 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:35:59 +0000 (19:35 +0100)
src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java
src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java
src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatHomeService.java

index 1b56829..05fc2cb 100644 (file)
@@ -39,9 +39,11 @@ public class ChatBackendConfiguration
   }
 
   @Bean
-  InMemoryChatHomeService chatHomeService(ChatBackendProperties properties)
+  InMemoryChatHomeService chatHomeService(
+      Clock clock,
+      ChatBackendProperties properties)
   {
-    return new InMemoryChatHomeService(properties.getChatroomBufferSize());
+    return new InMemoryChatHomeService(clock, properties.getChatroomBufferSize());
   }
 
   @Bean
index 68c53d5..cc6958f 100644 (file)
@@ -9,8 +9,6 @@ import org.springframework.web.bind.annotation.*;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
-import java.time.Clock;
-import java.time.LocalDateTime;
 import java.util.Optional;
 import java.util.UUID;
 import java.util.stream.Stream;
@@ -21,7 +19,6 @@ import java.util.stream.Stream;
 public class ChatBackendController
 {
   private final ChatHome chatHome;
-  private final Clock clock;
   private final StorageStrategy storageStrategy;
 
 
@@ -78,7 +75,6 @@ public class ChatBackendController
         chatroom
             .addMessage(
                 messageId,
-                LocalDateTime.now(clock),
                 username,
                 text)
             .switchIfEmpty(chatroom.getMessage(username, messageId))
index 0fdea33..63b5b36 100644 (file)
@@ -6,6 +6,7 @@ import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 import reactor.core.publisher.Sinks;
 
+import java.time.Clock;
 import java.time.LocalDateTime;
 import java.util.*;
 
@@ -17,6 +18,7 @@ public class ChatRoom
   private final UUID id;
   @Getter
   private final String name;
+  private final Clock clock;
   private final ChatRoomService service;
   private final int bufferSize;
   private Sinks.Many<Message> sink;
@@ -24,11 +26,13 @@ public class ChatRoom
   public ChatRoom(
       UUID id,
       String name,
+      Clock clock,
       ChatRoomService service,
       int bufferSize)
   {
     this.id = id;
     this.name = name;
+    this.clock = clock;
     this.service = service;
     this.bufferSize = bufferSize;
     this.sink = createSink();
@@ -37,12 +41,11 @@ public class ChatRoom
 
   synchronized public Mono<Message> addMessage(
       Long id,
-      LocalDateTime timestamp,
       String user,
       String text)
   {
     return service
-        .persistMessage(Message.MessageKey.of(user, id), timestamp, text)
+        .persistMessage(Message.MessageKey.of(user, id), LocalDateTime.now(clock), text)
         .doOnNext(message ->
         {
           Sinks.EmitResult result = sink.tryEmitNext(message);
index e57b99b..df0ecba 100644 (file)
@@ -4,6 +4,7 @@ import de.juplo.kafka.chat.backend.domain.ChatRoom;
 import de.juplo.kafka.chat.backend.domain.ChatHomeService;
 import lombok.RequiredArgsConstructor;
 
+import java.time.Clock;
 import java.util.LinkedHashMap;
 import java.util.UUID;
 
@@ -11,6 +12,7 @@ import java.util.UUID;
 @RequiredArgsConstructor
 public class InMemoryChatHomeService implements ChatHomeService<InMemoryChatRoomService>
 {
+  private final Clock clock;
   private final int bufferSize;
 
 
@@ -19,7 +21,7 @@ public class InMemoryChatHomeService implements ChatHomeService<InMemoryChatRoom
   {
     InMemoryChatRoomService service =
         new InMemoryChatRoomService(new LinkedHashMap<>());
-    return new ChatRoom(id, name, service, bufferSize);
+    return new ChatRoom(id, name, clock, service, bufferSize);
   }
 
   public ChatRoom restoreChatroom(
@@ -27,6 +29,6 @@ public class InMemoryChatHomeService implements ChatHomeService<InMemoryChatRoom
       String name,
       InMemoryChatRoomService service)
   {
-    return new ChatRoom(id, name, service, bufferSize);
+    return new ChatRoom(id, name, clock, service, bufferSize);
   }
 }