Skip to content

Commit

Permalink
fix: finally fix global chatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Mar 4, 2022
1 parent 22d5522 commit d14823e
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 44 deletions.
3 changes: 1 addition & 2 deletions acceptance/src/test/resources/features/private-chat.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Then player4 receives the message
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,34 @@
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;
import net.kyori.adventure.text.Component;
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<Message> {
public final class MessageSerializer implements JsonSerializer<Message>, JsonDeserializer<Message> {

@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 {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit d14823e

Please sign in to comment.