Skip to content

Commit

Permalink
fix(autojoin): new channels not auto joined after reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Feb 25, 2022
1 parent 8713339 commit 33c1d5d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Channel> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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();
}
}

0 comments on commit 33c1d5d

Please sign in to comment.