From d14823eb4b0e11139d3b92e0114e5249f2c0ff80 Mon Sep 17 00:00:00 2001 From: Silthus Date: Fri, 4 Mar 2022 18:42:25 +0100 Subject: [PATCH] fix: finally fix global chatting --- .../resources/features/private-chat.feature | 3 +-- .../GsonPluginMessageSerializer.java | 6 +++--- .../silthus/schat/util/gson/GsonProvider.java | 2 +- .../gson/serializers/ChatterSerializer.java | 6 ++---- .../gson/serializers/MessageSerializer.java | 20 ++++++++++++++++++- .../serializers/IdentitySerializerTest.java | 4 ++-- .../serializers/TargetsSerializerTest.java | 11 ++++++---- .../features/AutoJoinChannelsFeature.java | 6 +++--- .../platform/chatter/ConnectionListener.java | 15 ++++++-------- .../platform/messaging/MessagingService.java | 6 ------ .../chatter/ConnectionListenerTests.java | 10 +++------- .../messaging/MessagingServiceMock.java | 4 ++-- .../schat/ui/views/tabbed/ChannelTab.java | 1 + 13 files changed, 50 insertions(+), 44 deletions(-) diff --git a/acceptance/src/test/resources/features/private-chat.feature b/acceptance/src/test/resources/features/private-chat.feature index 9fff08d04..e5433fdf2 100644 --- a/acceptance/src/test/resources/features/private-chat.feature +++ b/acceptance/src/test/resources/features/private-chat.feature @@ -18,5 +18,4 @@ Feature: Private Chats Scenario: Players can send private messages across servers When I send a private message to player4 - Then player4 receives the message -# And the view of player4 shows the message \ No newline at end of file + Then player4 receives the message \ No newline at end of file diff --git a/core/src/main/java/net/silthus/schat/messenger/GsonPluginMessageSerializer.java b/core/src/main/java/net/silthus/schat/messenger/GsonPluginMessageSerializer.java index f95c26625..888c5ecec 100644 --- a/core/src/main/java/net/silthus/schat/messenger/GsonPluginMessageSerializer.java +++ b/core/src/main/java/net/silthus/schat/messenger/GsonPluginMessageSerializer.java @@ -64,15 +64,15 @@ public boolean supports(PluginMessage message) { public @NotNull String encode(PluginMessage pluginMessage) { if (!supports(pluginMessage)) throw new IllegalArgumentException(pluginMessage.getClass().getCanonicalName() + " is not a supported PluginMessage type!"); - final Gson gson = this.gsonProvider.gson(); - final JsonObject json = gson.toJsonTree(pluginMessage).getAsJsonObject(); + final Gson gson = this.gsonProvider.prettyGson(); + final JsonObject json = gson.toJsonTree(pluginMessage, pluginMessage.getClass()).getAsJsonObject(); json.addProperty("type", pluginMessage.getClass().getTypeName()); return gson.toJson(json); } @Override public @NotNull PluginMessage decode(@NonNull String encodedString) { - final Gson gson = this.gsonProvider.gson(); + final Gson gson = this.gsonProvider.prettyGson(); final JsonObject json = gson.fromJson(encodedString, JsonObject.class); final String type = json.get("type").getAsString(); return gson.fromJson(json, typeMap.get(type)); diff --git a/core/src/main/java/net/silthus/schat/util/gson/GsonProvider.java b/core/src/main/java/net/silthus/schat/util/gson/GsonProvider.java index 4b612190c..48e33a677 100644 --- a/core/src/main/java/net/silthus/schat/util/gson/GsonProvider.java +++ b/core/src/main/java/net/silthus/schat/util/gson/GsonProvider.java @@ -68,7 +68,7 @@ public static GsonProvider gsonProvider() { .registerTypeHierarchyAdapter(Component.class, new ComponentSerializer()) .registerTypeHierarchyAdapter(Message.class, new MessageSerializer()) .registerTypeHierarchyAdapter(Settings.class, new SettingsSerializer()) - .registerTypeHierarchyAdapter(Identity.class, new IdentitySerializer()) + .registerTypeAdapter(Identity.class, new IdentitySerializer()) .registerTypeAdapter(Targets.class, new TargetsSerializer()) .registerTypeAdapter(MessageTarget.class, new MessageTargetSerializer()) .registerTypeAdapter(MessageSource.class, new MessageSourceSerializer()) diff --git a/core/src/main/java/net/silthus/schat/util/gson/serializers/ChatterSerializer.java b/core/src/main/java/net/silthus/schat/util/gson/serializers/ChatterSerializer.java index d8e17b407..715781d98 100644 --- a/core/src/main/java/net/silthus/schat/util/gson/serializers/ChatterSerializer.java +++ b/core/src/main/java/net/silthus/schat/util/gson/serializers/ChatterSerializer.java @@ -50,7 +50,7 @@ public ChatterSerializer(ChatterRepository chatters) { @Override public JsonElement serialize(Chatter src, Type typeOfSrc, JsonSerializationContext context) { return JObject.json() - .add("identity", context.serialize(src.identity())) + .add("identity", context.serialize(src.identity(), Identity.class)) .add("active_channel", src.activeChannel().map(Channel::key).orElse(null)) .add("channels", context.serialize(src.channels().stream().map(Channel::key).toList())) .create(); @@ -67,9 +67,7 @@ public Chatter deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo private Chatter deserializeChatter(JsonElement json, JsonDeserializationContext context) { final JsonObject object = json.getAsJsonObject(); final Identity identity = context.deserialize(object.get("identity"), Identity.class); - final Chatter chatter = chatters - .findOrCreate(identity.uniqueId(), uuid -> Chatter.chatter(identity)) - .activeChannel(context.deserialize(object.get("active_channel"), Channel.class)); + final Chatter chatter = chatters.findOrCreate(identity.uniqueId(), uuid -> Chatter.chatter(identity)); deserializeAndJoinChannels(chatter, object.getAsJsonArray("channels"), context); return chatter; } diff --git a/core/src/main/java/net/silthus/schat/util/gson/serializers/MessageSerializer.java b/core/src/main/java/net/silthus/schat/util/gson/serializers/MessageSerializer.java index 45db8bfed..4de6d9367 100644 --- a/core/src/main/java/net/silthus/schat/util/gson/serializers/MessageSerializer.java +++ b/core/src/main/java/net/silthus/schat/util/gson/serializers/MessageSerializer.java @@ -28,6 +28,8 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; import java.lang.reflect.Type; import java.time.Instant; import java.util.UUID; @@ -35,10 +37,25 @@ import net.silthus.schat.message.Message; import net.silthus.schat.message.MessageSource; import net.silthus.schat.message.Targets; +import net.silthus.schat.pointer.Settings; +import net.silthus.schat.util.gson.JObject; import static net.silthus.schat.message.Message.message; -public final class MessageSerializer implements JsonDeserializer { +public final class MessageSerializer implements JsonSerializer, JsonDeserializer { + + @Override + public JsonElement serialize(Message src, Type typeOfSrc, JsonSerializationContext context) { + return JObject.json() + .add("id", context.serialize(src.id())) + .add("timestamp", context.serialize(src.timestamp())) + .add("text", context.serialize(src.text(), Component.class)) + .add("type", context.serialize(src.type())) + .add("source", context.serialize(src.source(), MessageSource.class)) + .add("targets", context.serialize(src.targets(), Targets.class)) + .add("settings", context.serialize(src.settings(), Settings.class)) + .create(); + } @Override public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { @@ -50,6 +67,7 @@ public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo .type(context.deserialize(object.get("type"), Message.Type.class)) .source(context.deserialize(object.get("source"), MessageSource.class)) .targets(context.deserialize(object.get("targets"), Targets.class)) + .settings(context.deserialize(object.get("settings"), Settings.class)) .create(); } } diff --git a/core/src/test/java/net/silthus/schat/util/gson/serializers/IdentitySerializerTest.java b/core/src/test/java/net/silthus/schat/util/gson/serializers/IdentitySerializerTest.java index 0ec6bca95..a1dd53715 100644 --- a/core/src/test/java/net/silthus/schat/util/gson/serializers/IdentitySerializerTest.java +++ b/core/src/test/java/net/silthus/schat/util/gson/serializers/IdentitySerializerTest.java @@ -45,7 +45,7 @@ void setUp() { @Test void serialize_stores_identity_properties() { final Identity identity = Identity.identity("Bob", text("Bobby")); - final String json = gson.toJson(identity); + final String json = gson.toJson(identity, Identity.class); assertThat(json).isEqualTo(""" { "id": "%s", @@ -76,7 +76,7 @@ void serializes_chatter_as_chatter() { @Test void deserializes_identity() { final Identity origin = Identity.identity("Bob", text("Bobby")); - final String json = gson.toJson(origin); + final String json = gson.toJson(origin, Identity.class); final Identity identity = gson.fromJson(json, Identity.class); assertThat(identity).extracting( Identity::uniqueId, diff --git a/core/src/test/java/net/silthus/schat/util/gson/serializers/TargetsSerializerTest.java b/core/src/test/java/net/silthus/schat/util/gson/serializers/TargetsSerializerTest.java index 6704b5ced..11a578d22 100644 --- a/core/src/test/java/net/silthus/schat/util/gson/serializers/TargetsSerializerTest.java +++ b/core/src/test/java/net/silthus/schat/util/gson/serializers/TargetsSerializerTest.java @@ -32,14 +32,16 @@ import net.silthus.schat.chatter.Chatter; import net.silthus.schat.chatter.ChatterMock; import net.silthus.schat.chatter.ChatterRepository; +import net.silthus.schat.eventbus.EventBus; import net.silthus.schat.message.MessageTarget; import net.silthus.schat.message.Targets; -import net.silthus.schat.util.gson.GsonProvider; +import net.silthus.schat.util.gson.GsonProviderStub; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static java.util.UUID.randomUUID; +import static net.silthus.schat.channel.ChannelRepository.createInMemoryChannelRepository; import static net.silthus.schat.chatter.ChatterRepository.createInMemoryChatterRepository; import static org.assertj.core.api.Assertions.assertThat; @@ -51,9 +53,10 @@ class TargetsSerializerTest { @BeforeEach void setUp() { chatterRepository = createInMemoryChatterRepository(); - gson = GsonProvider.gsonProvider() - .registerChatterSerializer(chatterRepository) - .prettyGson(); + gson = GsonProviderStub.gsonProviderStub( + chatterRepository, + createInMemoryChannelRepository(EventBus.empty()) + ).prettyGson(); } @NotNull diff --git a/features/src/main/java/net/silthus/schat/features/AutoJoinChannelsFeature.java b/features/src/main/java/net/silthus/schat/features/AutoJoinChannelsFeature.java index 3d47727c2..bad85d654 100644 --- a/features/src/main/java/net/silthus/schat/features/AutoJoinChannelsFeature.java +++ b/features/src/main/java/net/silthus/schat/features/AutoJoinChannelsFeature.java @@ -60,17 +60,17 @@ protected void onChatterJoin(ChatterJoinedServerEvent event) { } protected void onConfigReload(ConfigReloadedEvent event) { - autoJoinableChannels().forEach(this::autojoinAllChattersToChannel); + autoJoinableChannels().forEach(this::autoJoinAllChattersToChannel); } - private void autojoinAllChattersToChannel(Channel channel) { + private void autoJoinAllChattersToChannel(Channel channel) { for (final Chatter chatter : chatterRepository.all()) autoJoinChannel(chatter, channel); } protected void onRegisteredChannel(ChannelRegisteredEvent event) { if (event.channel().is(AUTO_JOIN)) - autojoinAllChattersToChannel(event.channel()); + autoJoinAllChattersToChannel(event.channel()); } private void autoJoinChannels(Chatter chatter) { diff --git a/platform/src/main/java/net/silthus/schat/platform/chatter/ConnectionListener.java b/platform/src/main/java/net/silthus/schat/platform/chatter/ConnectionListener.java index 48f3330e3..9d705abe3 100644 --- a/platform/src/main/java/net/silthus/schat/platform/chatter/ConnectionListener.java +++ b/platform/src/main/java/net/silthus/schat/platform/chatter/ConnectionListener.java @@ -35,7 +35,6 @@ import net.silthus.schat.chatter.ChatterRepository; import net.silthus.schat.eventbus.EventBus; import net.silthus.schat.events.chatter.ChatterJoinedServerEvent; -import net.silthus.schat.identity.Identity; import net.silthus.schat.messenger.Messenger; import net.silthus.schat.messenger.PluginMessage; import net.silthus.schat.platform.sender.Sender; @@ -77,29 +76,27 @@ private void fireJoinServerEvent(Chatter chatter) { } protected void sendGlobalJoinPing(Chatter chatter) { - messenger.sendPluginMessage(new ChatterJoined(chatter.identity())); + messenger.sendPluginMessage(new ChatterJoined(chatter)); } @Getter @Setter @Accessors(fluent = true) @NoArgsConstructor - @EqualsAndHashCode(of = {"identity"}, callSuper = true) + @EqualsAndHashCode(of = {"chatter"}, callSuper = true) final static class ChatterJoined extends PluginMessage { - private Identity identity; + private Chatter chatter; private transient ChatterRepository repository; private transient ChatterFactory factory; private transient EventBus eventBus; - ChatterJoined(Identity identity) { - super(); - this.identity = identity; + ChatterJoined(Chatter chatter) { + this.chatter = chatter; } @Override public void process() { - if (!repository.contains(identity.uniqueId())) { - final Chatter chatter = factory.createChatter(identity.uniqueId()); + if (!repository.contains(chatter.uniqueId())) { repository.add(chatter); eventBus.post(new ChatterJoinedServerEvent(chatter)); } diff --git a/platform/src/main/java/net/silthus/schat/platform/messaging/MessagingService.java b/platform/src/main/java/net/silthus/schat/platform/messaging/MessagingService.java index f70269407..781f14af4 100644 --- a/platform/src/main/java/net/silthus/schat/platform/messaging/MessagingService.java +++ b/platform/src/main/java/net/silthus/schat/platform/messaging/MessagingService.java @@ -166,11 +166,5 @@ public boolean consumeIncomingMessage(@NonNull PluginMessage message) { log.info("PluginMessage(" + message + ") - NOT processed"); return processed; } - - @Override - public boolean consumeIncomingMessageAsString(@NonNull String encodedString) { - log.info("Decoding Incoming Message: " + encodedString); - return super.consumeIncomingMessageAsString(encodedString); - } } } diff --git a/platform/src/test/java/net/silthus/schat/platform/chatter/ConnectionListenerTests.java b/platform/src/test/java/net/silthus/schat/platform/chatter/ConnectionListenerTests.java index 40ca5e224..c49ab3589 100644 --- a/platform/src/test/java/net/silthus/schat/platform/chatter/ConnectionListenerTests.java +++ b/platform/src/test/java/net/silthus/schat/platform/chatter/ConnectionListenerTests.java @@ -36,7 +36,6 @@ import org.junit.jupiter.api.Test; import static net.silthus.schat.chatter.ChatterRepository.createInMemoryChatterRepository; -import static net.silthus.schat.identity.IdentityHelper.randomIdentity; import static net.silthus.schat.platform.sender.SenderMock.randomSender; import static org.assertj.core.api.Assertions.assertThat; @@ -64,7 +63,7 @@ void tearDown() { } private ConnectionListener.ChatterJoined createRandomPluginMessage() { - return new ConnectionListener.ChatterJoined(randomIdentity()) + return new ConnectionListener.ChatterJoined(ChatterMock.randomChatter()) .repository(chatterRepository) .factory(ChatterMock::randomChatter) .eventBus(eventBus); @@ -95,21 +94,18 @@ void loads_chatter_into_cache() { void sends_join_ping_to_all_servers() { join(); messenger.assertSentMessage(ConnectionListener.ChatterJoined.class); - messenger.assertLastReceivedMessage(ConnectionListener.ChatterJoined.class) - .extracting(ConnectionListener.ChatterJoined::identity) - .isEqualTo(sender.identity()); } @Test void when_ping_is_processed_then_chatter_is_created() { final ConnectionListener.ChatterJoined msg = consumeIncomingMessage(); - assertThat(chatterRepository.contains(msg.identity().uniqueId())).isTrue(); + assertThat(chatterRepository.contains(msg.chatter().uniqueId())).isTrue(); } @Test void when_ping_is_processed_then_joined_server_event_is_fired() { final ConnectionListener.ChatterJoined msg = consumeIncomingMessage(); - assertJoinEventFired(msg.identity().uniqueId()); + assertJoinEventFired(msg.chatter().uniqueId()); } @Test diff --git a/platform/src/testFixtures/java/net/silthus/schat/platform/messaging/MessagingServiceMock.java b/platform/src/testFixtures/java/net/silthus/schat/platform/messaging/MessagingServiceMock.java index 66dbccf6c..06dbeffa0 100644 --- a/platform/src/testFixtures/java/net/silthus/schat/platform/messaging/MessagingServiceMock.java +++ b/platform/src/testFixtures/java/net/silthus/schat/platform/messaging/MessagingServiceMock.java @@ -32,7 +32,7 @@ import org.jetbrains.annotations.NotNull; import static net.silthus.schat.messenger.PluginMessageSerializer.gsonSerializer; -import static net.silthus.schat.util.gson.GsonProvider.gsonProvider; +import static net.silthus.schat.util.gson.GsonProviderStub.gsonProviderStub; import static org.assertj.core.api.Assertions.assertThat; public class MessagingServiceMock extends MessagingService { @@ -47,7 +47,7 @@ public static MessagingServiceMock messengerMock() { private int processedMessageCount = 0; protected MessagingServiceMock() { - super(new MockMessagingGatewayProvider(), gsonSerializer(gsonProvider())); + super(new MockMessagingGatewayProvider(), gsonSerializer(gsonProviderStub())); registerMessageType(MockPluginMessage.class); } diff --git a/ui/src/main/java/net/silthus/schat/ui/views/tabbed/ChannelTab.java b/ui/src/main/java/net/silthus/schat/ui/views/tabbed/ChannelTab.java index c7168cb36..4a6a60bb4 100644 --- a/ui/src/main/java/net/silthus/schat/ui/views/tabbed/ChannelTab.java +++ b/ui/src/main/java/net/silthus/schat/ui/views/tabbed/ChannelTab.java @@ -86,6 +86,7 @@ protected ChannelTab(@NonNull TabbedChannelsView view, .copyFrom(channel.settings()); this.messages = new TreeMap<>(Stream.concat(channel.messages().stream(), view.chatter().messages().stream()) .filter(this::isMessageDisplayed) + .distinct() .collect(toMap(message -> message, this::renderMessage))); if (!isActive()) unreadCount(messages.size());