Skip to content

Commit

Permalink
fix(global): channel names not displayed correctly, e.g. random numbe…
Browse files Browse the repository at this point in the history
…rs only for private chats
  • Loading branch information
Silthus committed Feb 25, 2022
1 parent 6beba2c commit a0138df
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,33 @@
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import lombok.extern.java.Log;
import net.kyori.adventure.text.Component;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.message.Targets;
import net.silthus.schat.pointer.Settings;
import net.silthus.schat.util.gson.JObject;

/**
* A Gson serializer to serialize and deserialize a {@link Channel} into JSON format.
*
* <p>Used for sending packets across servers.</p>
*
* @since next
*/
public class ChannelSerializer implements JsonSerializer<Channel>, JsonDeserializer<Channel> {

public static final Type CHANNEL_TYPE = new TypeToken<Channel>() {
}.getType();

/**
* Creates a new channel serializer.
*
* @param channelRepository the channel repository to lookup existing channels
* @param debug set true to log the serialization process
* @return the created serializer
* @since next
*/
public static ChannelSerializer createChannelSerializer(ChannelRepository channelRepository, boolean debug) {
if (debug)
return new Logging(channelRepository);
Expand All @@ -61,6 +77,7 @@ private ChannelSerializer(ChannelRepository channelRepository) {
public JsonElement serialize(Channel src, Type typeOfSrc, JsonSerializationContext context) {
return JObject.json()
.add("key", src.key())
.add("name", context.serialize(src.displayName(), Component.class))
.add("settings", context.serialize(src.settings(), Settings.class))
.add("targets", context.serialize(src.targets(), Targets.class))
.create();
Expand All @@ -75,6 +92,7 @@ public Channel deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo

protected Channel createChannel(String key, JsonObject object, JsonDeserializationContext context) {
return Channel.channel(key)
.name(context.deserialize(object.get("name"), Component.class))
.settings(context.deserialize(object.get("settings"), Settings.class))
.targets(context.deserialize(object.get("targets"), Targets.class))
.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;

/**
* A Gson serializer to serialize and deserialize a {@link Component} into JSON format.
*
* <p>Used for sending packets across servers.</p>
*
* @since next
*/
public final class ComponentSerializer implements JsonSerializer<Component>, JsonDeserializer<Component> {

public static final Type COMPONENT_TYPE = new TypeToken<Component>() {
Expand All @@ -48,6 +55,8 @@ public JsonElement serialize(Component src, Type typeOfSrc, JsonSerializationCon

@Override
public Component deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json == null)
return Component.empty();
return componentSerializer.deserializeFromTree(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import java.lang.reflect.Type;
import net.kyori.adventure.text.Component;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.message.Targets;
Expand All @@ -37,6 +38,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static net.kyori.adventure.text.Component.text;
import static net.silthus.schat.channel.ChannelHelper.channelWith;
import static net.silthus.schat.channel.ChannelRepository.createInMemoryChannelRepository;
import static net.silthus.schat.util.gson.JObject.json;
Expand All @@ -47,6 +49,9 @@ class ChannelSerializerTest {
private static final SerializationContextStub SERIALIZATION_CONTEXT = new SerializationContextStub();
private static final SettingsDeserializationContextStub DESERIALIZATION_CONTEXT = new SettingsDeserializationContextStub();

private static final SettingsSerializer SETTINGS_SERIALIZER = new SettingsSerializer();
private static final ComponentSerializer COMPONENT_SERIALIZER = new ComponentSerializer();

private ChannelSerializer serializer;
private ChannelRepository repository;

Expand All @@ -70,6 +75,10 @@ private Channel deserialize(JsonElement json) {
return serializer.deserialize(json, Channel.class, DESERIALIZATION_CONTEXT);
}

private Channel serializeAndDeserialize(Channel channel) {
return deserialize(serialize(channel));
}

@Test
void serialization_writes_key() {
final JsonElement json = serialize(channelWith("test"));
Expand All @@ -90,16 +99,28 @@ void given_channel_does_not_exist_creates_and_adds_channel() {
assertThat(repository.all()).contains(channel);
}

@Test
void keeps_formatted_channel_name() {
final Channel channel = channelWith("test", builder -> builder.name(text("My Channel")));
final Channel result = serializeAndDeserialize(channel);
assertThat(result.displayName()).isEqualTo(channel.displayName());
}

private static class SerializationContextStub implements JsonSerializationContext {
@Override
public JsonElement serialize(Object o) {
if (o instanceof Component)
return serialize(o, o.getClass());
return new JsonObject();
}

@Override
public JsonElement serialize(Object o, Type type) {
if (o instanceof Component)
return COMPONENT_SERIALIZER.serialize((Component) o, type, SERIALIZATION_CONTEXT);
return new JsonObject();
}

}

private static class SettingsDeserializationContextStub implements JsonDeserializationContext {
Expand All @@ -110,6 +131,8 @@ public <T> T deserialize(JsonElement jsonElement, Type type) throws JsonParseExc
return (T) Settings.createSettings();
if (type.equals(Targets.class))
return (T) new Targets();
if (type.equals(Component.class))
return (T) COMPONENT_SERIALIZER.deserialize(jsonElement, type, DESERIALIZATION_CONTEXT);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import lombok.Getter;
import lombok.experimental.Accessors;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelSettings;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.eventbus.EventBus;
import net.silthus.schat.eventbus.EventListener;
Expand All @@ -37,6 +36,9 @@
import net.silthus.schat.messenger.Messenger;
import net.silthus.schat.messenger.PluginMessage;

import static net.silthus.schat.channel.ChannelSettings.GLOBAL;
import static net.silthus.schat.commands.JoinChannelCommand.joinChannel;

public class GlobalChatFeature implements EventListener {

private final Messenger messenger;
Expand All @@ -52,7 +54,7 @@ public void bind(EventBus bus) {
}

private void onChannelMessage(SendChannelMessageEvent event) {
if (event.channel().is(ChannelSettings.GLOBAL))
if (event.channel().is(GLOBAL))
messenger.sendPluginMessage(new GlobalChannelPluginMessage(event.channel(), event.message()));
}

Expand All @@ -78,7 +80,7 @@ public void process() {
private void updateChannelTargets() {
for (MessageTarget target : channel.targets())
if (target instanceof Chatter chatter)
chatter.join(channel);
joinChannel(chatter, channel);
}
}
}

0 comments on commit a0138df

Please sign in to comment.