diff --git a/core/src/test/java/net/silthus/schat/commands/SendPrivateMessageCommandTests.java b/core/src/test/java/net/silthus/schat/commands/SendPrivateMessageCommandTests.java index a822cb780..bd3c8020f 100644 --- a/core/src/test/java/net/silthus/schat/commands/SendPrivateMessageCommandTests.java +++ b/core/src/test/java/net/silthus/schat/commands/SendPrivateMessageCommandTests.java @@ -24,6 +24,7 @@ package net.silthus.schat.commands; +import java.util.Set; import net.silthus.schat.channel.Channel; import net.silthus.schat.channel.ChannelRepository; import net.silthus.schat.chatter.Chatter; @@ -139,7 +140,7 @@ class given_private_channel_exists { @BeforeEach void setUp() { - channel = channelWith(PRIVATE, true); + channel = channelWith(Set.of(source, target).hashCode() + "", PRIVATE, true); channel.addTarget(source); channel.addTarget(target); repository.add(channel); diff --git a/core/src/test/java/net/silthus/schat/util/gson/types/ChannelSerializerTest.java b/core/src/test/java/net/silthus/schat/util/gson/types/ChannelSerializerTest.java index 6051ff4fb..b35f95d21 100644 --- a/core/src/test/java/net/silthus/schat/util/gson/types/ChannelSerializerTest.java +++ b/core/src/test/java/net/silthus/schat/util/gson/types/ChannelSerializerTest.java @@ -32,6 +32,7 @@ import java.lang.reflect.Type; 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 org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.BeforeEach; @@ -106,7 +107,11 @@ private static class SettingsDeserializationContextStub implements JsonDeseriali @Override @SuppressWarnings("unchecked") public T deserialize(JsonElement jsonElement, Type type) throws JsonParseException { - return (T) Settings.createSettings(); + if (type.equals(Settings.class)) + return (T) Settings.createSettings(); + if (type.equals(Targets.class)) + return (T) new Targets(); + return null; } } } diff --git a/ui/src/main/java/net/silthus/schat/ui/format/ChannelFormat.java b/ui/src/main/java/net/silthus/schat/ui/format/ChannelFormat.java index 7dda69743..6fe66387d 100644 --- a/ui/src/main/java/net/silthus/schat/ui/format/ChannelFormat.java +++ b/ui/src/main/java/net/silthus/schat/ui/format/ChannelFormat.java @@ -31,13 +31,17 @@ import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; import net.silthus.schat.channel.Channel; +import net.silthus.schat.chatter.Chatter; +import net.silthus.schat.identity.Identified; import net.silthus.schat.pointer.Configured; import net.silthus.schat.pointer.Setting; import net.silthus.schat.pointer.Settings; import net.silthus.schat.ui.Click; +import org.jetbrains.annotations.NotNull; import static net.kyori.adventure.text.Component.text; import static net.kyori.adventure.text.format.NamedTextColor.WHITE; +import static net.silthus.schat.channel.ChannelSettings.PRIVATE; @Getter @Accessors(fluent = true) @@ -47,14 +51,16 @@ public class ChannelFormat implements Format, Configured { public static final Setting DECORATION = Setting.setting(TextDecoration.class, "style", null); public static final Setting ON_CLICK = Setting.setting(Click.Channel.class, "on_click", null); + private final Chatter chatter; private final Settings settings; - public ChannelFormat(Settings settings) { + public ChannelFormat(Chatter chatter, Settings settings) { + this.chatter = chatter; this.settings = settings; } public Component format(Channel channel) { - TextComponent.Builder builder = text().append(channel.displayName()); + TextComponent.Builder builder = text().append(channelName(channel)); if (get(COLOR) != null) builder.color(get(COLOR)); if (get(DECORATION) != null) @@ -63,4 +69,20 @@ public Component format(Channel channel) { builder.clickEvent(get(ON_CLICK).onClick(channel)); return builder.build(); } + + @NotNull + private Component channelName(Channel channel) { + return channel.is(PRIVATE) ? privateChannelName(channel) : channel.displayName(); + } + + @NotNull + private Component privateChannelName(Channel channel) { + return channel.targets().stream() + .filter(target -> target instanceof Chatter) + .filter(target -> !target.equals(chatter)) + .findFirst() + .map(target -> (Chatter) target) + .map(Identified::displayName) + .orElse(channel.displayName()); + } } diff --git a/ui/src/main/java/net/silthus/schat/ui/views/TabbedChannelsView.java b/ui/src/main/java/net/silthus/schat/ui/views/TabbedChannelsView.java index 096c595ce..41f4474c2 100644 --- a/ui/src/main/java/net/silthus/schat/ui/views/TabbedChannelsView.java +++ b/ui/src/main/java/net/silthus/schat/ui/views/TabbedChannelsView.java @@ -144,9 +144,9 @@ private List getRenderedChannels() { final ArrayList channels = new ArrayList<>(); for (final Channel channel : viewModel.channels()) { if (viewModel.isActiveChannel(channel)) - channels.add(new ChannelFormat(get(ACTIVE_CHANNEL)).format(channel)); + channels.add(new ChannelFormat(viewModel.chatter(), get(ACTIVE_CHANNEL)).format(channel)); else - channels.add(new ChannelFormat(get(INACTIVE_CHANNEL)).format(channel)); + channels.add(new ChannelFormat(viewModel.chatter(), get(INACTIVE_CHANNEL)).format(channel)); } return channels; } diff --git a/ui/src/test/java/net/silthus/schat/ui/format/ChannelFormatTests.java b/ui/src/test/java/net/silthus/schat/ui/format/ChannelFormatTests.java deleted file mode 100644 index f6566d5a4..000000000 --- a/ui/src/test/java/net/silthus/schat/ui/format/ChannelFormatTests.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of sChat, licensed under the MIT License. - * Copyright (C) Silthus - * Copyright (C) sChat team and contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package net.silthus.schat.ui.format; - -import org.junit.jupiter.api.Test; - -class ChannelFormatTests { - @Test - void name_is_rendered() { - - } -} diff --git a/ui/src/test/java/net/silthus/schat/ui/views/TabbedChannelsViewTests.java b/ui/src/test/java/net/silthus/schat/ui/views/TabbedChannelsViewTests.java index 14e75284f..b768f3940 100644 --- a/ui/src/test/java/net/silthus/schat/ui/views/TabbedChannelsViewTests.java +++ b/ui/src/test/java/net/silthus/schat/ui/views/TabbedChannelsViewTests.java @@ -32,6 +32,7 @@ import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.silthus.schat.channel.Channel; import net.silthus.schat.chatter.Chatter; +import net.silthus.schat.chatter.ChatterMock; import net.silthus.schat.identity.Identity; import net.silthus.schat.message.Message; import net.silthus.schat.ui.view.View; @@ -42,10 +43,12 @@ import static net.kyori.adventure.text.format.NamedTextColor.RED; import static net.silthus.schat.AssertionHelper.assertNPE; -import static net.silthus.schat.channel.ChannelSettings.PRIORITY; import static net.silthus.schat.channel.Channel.createChannel; import static net.silthus.schat.channel.ChannelHelper.ConfiguredSetting.set; import static net.silthus.schat.channel.ChannelHelper.channelWith; +import static net.silthus.schat.channel.ChannelSettings.PRIORITY; +import static net.silthus.schat.channel.ChannelSettings.PRIVATE; +import static net.silthus.schat.chatter.ChatterMock.chatterMock; import static net.silthus.schat.chatter.ChatterMock.randomChatter; import static net.silthus.schat.identity.Identity.identity; import static net.silthus.schat.message.Message.message; @@ -178,7 +181,6 @@ void renders_both_messages() { } @Nested class given_single_channel { - private Channel channel; @BeforeEach @@ -225,6 +227,22 @@ void then_channel_click_executes_join_command() { } } + @Nested class given_private_channel { + + @BeforeEach + void setUp() { + ChatterMock target = chatterMock(Identity.identity("target")); + Channel channel = channelWith(PRIVATE, true); + chatter.join(channel); + target.join(channel); + } + + @Test + void renders_partner_name() { + assertTextRenders("| target |"); + } + } + @Nested class given_two_channels { private @NotNull Channel channelOne;