From 33c1d5d483603682d7e45eaf9a0feddcbab53fa1 Mon Sep 17 00:00:00 2001 From: Silthus Date: Fri, 25 Feb 2022 05:44:13 +0100 Subject: [PATCH] fix(autojoin): new channels not auto joined after reloading --- .../features/AutoJoinChannelsFeature.java | 45 +++++++++++++---- .../features/AutoJoinChannelsFeatureTest.java | 48 +++++++++++++------ 2 files changed, 69 insertions(+), 24 deletions(-) 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 110db4ad4..4d3fc20d9 100644 --- a/features/src/main/java/net/silthus/schat/features/AutoJoinChannelsFeature.java +++ b/features/src/main/java/net/silthus/schat/features/AutoJoinChannelsFeature.java @@ -23,35 +23,62 @@ */ package net.silthus.schat.features; +import java.util.stream.Stream; +import net.silthus.schat.channel.Channel; import net.silthus.schat.channel.ChannelRepository; +import net.silthus.schat.chatter.Chatter; +import net.silthus.schat.chatter.ChatterRepository; import net.silthus.schat.eventbus.EventBus; import net.silthus.schat.eventbus.EventListener; import net.silthus.schat.events.chatter.ChatterJoinedServerEvent; +import net.silthus.schat.events.config.ConfigReloadedEvent; +import org.jetbrains.annotations.NotNull; import static net.silthus.schat.channel.ChannelSettings.AUTO_JOIN; import static net.silthus.schat.commands.JoinChannelCommand.joinChannel; import static net.silthus.schat.commands.SetActiveChannelCommand.setActiveChannel; public class AutoJoinChannelsFeature implements EventListener { + private final ChatterRepository chatterRepository; private final ChannelRepository channelRepository; - public AutoJoinChannelsFeature(ChannelRepository channelRepository) { + public AutoJoinChannelsFeature(ChatterRepository chatterRepository, ChannelRepository channelRepository) { + this.chatterRepository = chatterRepository; this.channelRepository = channelRepository; } @Override public void bind(EventBus bus) { bus.on(ChatterJoinedServerEvent.class, this::onChatterJoin); + bus.on(ConfigReloadedEvent.class, this::onConfigReload); } protected void onChatterJoin(ChatterJoinedServerEvent event) { - channelRepository.all().stream() - .filter(channel -> channel.is(AUTO_JOIN)) - .forEach(channel -> { - if (event.chatter().activeChannel().isEmpty()) - setActiveChannel(event.chatter(), channel); - else - joinChannel(event.chatter(), channel); - }); + autoJoinChannels(event.chatter()); + } + + protected void onConfigReload(ConfigReloadedEvent event) { + autoJoinableChannels().forEach(channel -> { + for (final Chatter chatter : chatterRepository.all()) + autoJoinChannel(chatter, channel); + }); + } + + private void autoJoinChannels(Chatter chatter) { + autoJoinableChannels() + .forEach(channel -> autoJoinChannel(chatter, channel)); + } + + @NotNull + private Stream autoJoinableChannels() { + return channelRepository.all().stream() + .filter(channel -> channel.is(AUTO_JOIN)); + } + + private void autoJoinChannel(Chatter chatter, Channel channel) { + if (chatter.activeChannel().isEmpty()) + setActiveChannel(chatter, channel); + else + joinChannel(chatter, channel); } } diff --git a/features/src/test/java/net/silthus/schat/features/AutoJoinChannelsFeatureTest.java b/features/src/test/java/net/silthus/schat/features/AutoJoinChannelsFeatureTest.java index b0366c558..bff3ea13b 100644 --- a/features/src/test/java/net/silthus/schat/features/AutoJoinChannelsFeatureTest.java +++ b/features/src/test/java/net/silthus/schat/features/AutoJoinChannelsFeatureTest.java @@ -26,11 +26,11 @@ import net.silthus.schat.channel.Channel; import net.silthus.schat.channel.ChannelRepository; import net.silthus.schat.chatter.ChatterMock; -import net.silthus.schat.commands.JoinChannelCommand; -import net.silthus.schat.eventbus.EventBus; +import net.silthus.schat.chatter.ChatterRepository; import net.silthus.schat.eventbus.EventBusMock; import net.silthus.schat.events.chatter.ChatterJoinedServerEvent; -import org.jetbrains.annotations.NotNull; +import net.silthus.schat.events.config.ConfigReloadedEvent; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -41,35 +41,47 @@ import static net.silthus.schat.channel.ChannelSettings.AUTO_JOIN; import static net.silthus.schat.channel.ChannelSettings.PROTECTED; import static net.silthus.schat.chatter.ChatterMock.randomChatter; +import static net.silthus.schat.chatter.ChatterRepository.createInMemoryChatterRepository; import static org.assertj.core.api.Assertions.assertThat; class AutoJoinChannelsFeatureTest { - private EventBusMock events; - private Channel channel; - private @NotNull ChatterMock chatter; - private ChannelRepository channelRepository; + private final EventBusMock events = EventBusMock.eventBusMock(); + private final ChannelRepository channelRepository = createInMemoryChannelRepository(); + private final ChatterRepository chatterRepository = createInMemoryChatterRepository(); + + private final Channel channel = channelWith(AUTO_JOIN, true); + private final ChatterMock chatter = randomChatter(); @BeforeEach void setUp() { - channelRepository = createInMemoryChannelRepository(); - AutoJoinChannelsFeature feature = new AutoJoinChannelsFeature(channelRepository); - events = EventBusMock.eventBusMock(); - feature.bind(events); - channel = channelWith(AUTO_JOIN, true); channelRepository.add(channel); - chatter = randomChatter(); - JoinChannelCommand.prototype(builder -> builder.eventBus(EventBus.empty())); + chatterRepository.add(chatter); + + new AutoJoinChannelsFeature(chatterRepository, channelRepository).bind(events); + } + + @AfterEach + void tearDown() { + events.close(); + } + + private void assertAutoJoinedChannel() { + chatter.assertJoinedChannel(channel); } private void triggerJoinEvent() { events.post(new ChatterJoinedServerEvent(chatter)); } + private void triggerReloadEvent() { + events.post(new ConfigReloadedEvent()); + } + @Test void onJoin_auto_joins_channels() { triggerJoinEvent(); - chatter.assertJoinedChannel(channel); + assertAutoJoinedChannel(); } @Test @@ -95,4 +107,10 @@ void given_no_active_channel_sets_joined_channel_as_active() { triggerJoinEvent(); assertThat(chatter.activeChannel().isPresent()).isTrue(); } + + @Test + void onReload_triggers_auto_join() { + triggerReloadEvent(); + assertAutoJoinedChannel(); + } }