Skip to content

Commit

Permalink
fix(global): private chats not shown on other server
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Mar 4, 2022
1 parent fa09535 commit 22d5522
Show file tree
Hide file tree
Showing 30 changed files with 391 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Channel channel(String key) {
}

private Channel createChannel(String key) {
return context.addChannelToAllServers(Channel.createChannel(key));
return context.primaryServer().addChannel(Channel.createChannel(key));
}

@ParameterType("[a-zA-Z]+")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
@ScenarioScoped
public class Context {
public static final String PRIMARY_SERVER = "server1";
public static final String SECOND_SERVER = "server2";

private final UserSteps userSteps;
private final ServerSteps serverSteps;
Expand All @@ -69,7 +70,7 @@ public Context() {
@Before(order = 10)
public void setup() {
servers.put(PRIMARY_SERVER, serverSteps.createServer());
servers.put("server2", serverSteps.createServer());
servers.put(SECOND_SERVER, serverSteps.createServer());
}

@Before(order = 20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ServerSteps(Context context) {

@ParameterType("[a-zA-Z0-9]+")
public Server server(String name) {
return context.servers().computeIfAbsent(name, n -> createServer());
return context.servers().get(name);
}

public Server createServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public void injectMessenger(StubMessengerGatewayProvider messenger) {
plugin().gatewayProviderRegistry().register(GATEWAY_TYPE, messenger);
}

public void addChannel(Channel channel) {
public Channel addChannel(Channel channel) {
plugin().channelRepository().add(channel);
return channel;
}

public void join(SenderMock sender) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,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
# And the view of player4 shows the message
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void setUp() {
server = mock(Server.class);
messenger = mock(Messenger.class);
when(server.getMessenger()).thenReturn(messenger);
consumer = new MessagingServiceMock();
consumer = MessagingServiceMock.messengerMock();
final SChatConfig config = mock(SChatConfig.class);
when(config.get(ConfigKeys.DEBUG)).thenReturn(false);
gateway = BukkitMessengerGateway.createBukkitMessengerGateway(mockPlugin, server, scheduler, consumer, config);
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/net/silthus/schat/channel/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ public sealed interface Channel extends Entity<String>, Configurable<Channel>, M
*/
@NotNull @Unmodifiable Targets targets();

/**
* Replaces all targets of the channel with the given targets.
*
* @param targets the targets to set
* @return this channel
* @since next
*/
@NotNull Channel targets(@NonNull Targets targets);

/**
* Adds a target to this channel without performing any checks.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ static ChannelImpl.Builder builder(String key) {
private static final String VALID_KEY_PATTERN = "^[a-zA-Z0-9_-]+$";

private final String key;
private final Targets targets;
private final transient Messages messages = new Messages();
private final transient EventBus eventBus;
private final transient Map<Class<? extends Policy>, Policy> policies;
private @NonNull Targets targets;
private @NonNull Settings settings;

private ChannelImpl(Builder builder) {
Expand Down Expand Up @@ -138,6 +138,12 @@ private ChannelImpl(Builder builder) {
return Targets.unmodifiable(targets);
}

@Override
public @NotNull Channel targets(@NonNull Targets targets) {
this.targets = Targets.copyOf(targets);
return this;
}

@Override
public void addTarget(@NonNull MessageTarget target) {
if (targets.add(target))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import lombok.extern.java.Log;
import net.silthus.schat.eventbus.EventBus;
import net.silthus.schat.events.channel.RegisteredChannelEvent;
import net.silthus.schat.events.channel.ChannelRegisteredEvent;
import net.silthus.schat.repository.InMemoryRepository;
import org.jetbrains.annotations.NotNull;

Expand All @@ -42,7 +42,7 @@ public void add(@NotNull Channel channel) {
if (contains(channel.key()))
throw new DuplicateChannel(channel);
super.add(channel);
eventBus.post(new RegisteredChannelEvent(channel));
eventBus.post(new ChannelRegisteredEvent(channel));
}

@Log(topic = "sChat:ChannelRepository")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
*
* @since next
*/
public record RegisteredChannelEvent(Channel channel) implements SChatEvent {
public record ChannelRegisteredEvent(Channel channel) implements SChatEvent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import lombok.NonNull;
import lombok.experimental.Accessors;
import net.silthus.schat.util.gson.GsonProvider;
import org.jetbrains.annotations.NotNull;

@Getter
@Accessors(fluent = true)
public final class GsonPluginMessageSerializer implements PluginMessageSerializer {

private final GsonProvider gsonProvider;
Expand All @@ -48,7 +52,7 @@ public void registerMessageType(Type type) {

@Override
public void registerTypeAdapter(Type type, Object adapter) {
gsonProvider.registerTypeAdapter(type, adapter);
gsonProvider.builder().registerTypeAdapter(type, adapter);
}

@Override
Expand All @@ -58,6 +62,8 @@ public boolean supports(PluginMessage message) {

@Override
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();
json.addProperty("type", pluginMessage.getClass().getTypeName());
Expand Down
21 changes: 11 additions & 10 deletions core/src/main/java/net/silthus/schat/util/gson/GsonProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import lombok.NonNull;
import lombok.experimental.Accessors;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.chatter.Chatter;
Expand Down Expand Up @@ -60,17 +61,18 @@ public static GsonProvider gsonProvider() {
return new GsonProvider();
}

private final GsonBuilder gson = new GsonBuilder()
private final GsonBuilder gson = GsonComponentSerializer.gson().populator().apply(
new GsonBuilder()
.disableHtmlEscaping()
.registerTypeAdapter(Instant.class, new InstantSerializer())
.registerTypeHierarchyAdapter(Component.class, new ComponentSerializer())
.registerTypeHierarchyAdapter(Message.class, new MessageSerializer())
.registerTypeHierarchyAdapter(Settings.class, new SettingsSerializer())
.registerTypeHierarchyAdapter(Identity.class, new IdentitySerializer())
.registerTypeAdapter(Targets.class, new TargetsSerializer())
.registerTypeAdapter(MessageTarget.class, new MessageTargetSerializer());

private final GsonBuilder prettyPrinting = gson.setPrettyPrinting();
.registerTypeAdapter(MessageTarget.class, new MessageTargetSerializer())
.registerTypeAdapter(MessageSource.class, new MessageSourceSerializer())
);

private GsonProvider() {
}
Expand All @@ -85,17 +87,16 @@ public GsonProvider registerChannelSerializer(ChannelRepository channelRepositor
return this;
}

public GsonProvider registerMessageSourceSerializer(ChatterRepository chatters) {
gson.registerTypeHierarchyAdapter(MessageSource.class, new MessageSourceSerializer(chatters));
return this;
}

public Gson gson() {
return gson.create();
}

public Gson prettyGson() {
return prettyPrinting.create();
return gson.setPrettyPrinting().create();
}

public GsonBuilder builder() {
return gson;
}

public void registerTypeAdapter(Type type, @NonNull Object adapter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public JsonElement serialize(Identity src, Type typeOfSrc, JsonSerializationCont
@Override
public Identity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
final JsonObject object = json.getAsJsonObject();
if (object.has("identity"))
return deserialize(object.get("identity"), typeOfT, context);
return Identity.identity(
context.deserialize(object.get("id"), UUID.class),
object.get("name").getAsString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,34 @@
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import net.silthus.schat.chatter.ChatterRepository;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.message.MessageSource;

public final class MessageSourceSerializer implements JsonSerializer<MessageSource>, JsonDeserializer<MessageSource> {

private final ChatterRepository chatters;

public MessageSourceSerializer(ChatterRepository chatters) {
this.chatters = chatters;
}

@Override
public JsonElement serialize(MessageSource src, Type typeOfSrc, JsonSerializationContext context) {
return null;
if (src instanceof Chatter chatter)
return new JsonPrimitive("chatter:" + chatter.uniqueId());
else
return context.serialize(src.identity(), Identity.class);
}

@Override
public MessageSource deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return null;
if (json.isJsonPrimitive()) {
final String[] split = json.getAsString().split(":");
return switch (split[0]) {
case "chatter" -> context.deserialize(new JsonPrimitive(split[1]), Chatter.class);
default -> MessageSource.nil();
};
} else {
return MessageSource.of(context.deserialize(json, Identity.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.commands.SendMessageResult;
import net.silthus.schat.message.MessageTarget;

public class MessageTargetSerializer implements JsonSerializer<MessageTarget>, JsonDeserializer<MessageTarget> {
Expand All @@ -43,7 +45,7 @@ public JsonElement serialize(MessageTarget src, Type typeOfSrc, JsonSerializatio
return new JsonPrimitive("chatter:" + chatter.uniqueId());
if (src instanceof Channel channel)
return new JsonPrimitive("channel:" + channel.key());
return null;
return JsonNull.INSTANCE;
}

@Override
Expand All @@ -52,7 +54,7 @@ public MessageTarget deserialize(JsonElement json, Type typeOfT, JsonDeserializa
return switch (split[0]) {
case "chatter" -> context.deserialize(new JsonPrimitive(split[1]), Chatter.class);
case "channel" -> context.deserialize(new JsonPrimitive(split[1]), Channel.class);
default -> null;
default -> SendMessageResult::failure;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package net.silthus.schat.channel;

import net.silthus.schat.eventbus.EventBusMock;
import net.silthus.schat.events.channel.RegisteredChannelEvent;
import net.silthus.schat.events.channel.ChannelRegisteredEvent;
import net.silthus.schat.repository.Repository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -83,7 +83,7 @@ void then_get_returns_the_channel_by_key() {

@Test
void RegisteredChannelEvent_is_fired() {
eventBus.assertEventFired(RegisteredChannelEvent.class);
eventBus.assertEventFired(ChannelRegisteredEvent.class);
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.eventbus.EventBus;
import net.silthus.schat.util.gson.GsonProvider;
import net.silthus.schat.util.gson.JObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static net.silthus.schat.channel.ChannelHelper.channelWith;
import static net.silthus.schat.channel.ChannelSettings.GLOBAL;
import static org.assertj.core.api.Assertions.assertThat;

class ChannelSerializerTest {
Expand Down Expand Up @@ -74,13 +72,4 @@ void deserializes_unknown_channel_to_null() {
final Channel channel = gson.fromJson(new JsonPrimitive("foobar"), Channel.class);
assertThat(channel).isNull();
}

@Test
void deserializes_full_channel_from_repository_if_existant() {
final Channel channel = channelWith("test").set(GLOBAL, true);
channelRepository.add(channel);

final Channel result = gson.fromJson(JObject.json().add("key", "test").create(), Channel.class);
assertThat(result).isSameAs(channel);
}
}
Loading

0 comments on commit 22d5522

Please sign in to comment.