Skip to content

Commit

Permalink
chore: cleanup unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Mar 4, 2022
1 parent 310031c commit 66f2564
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,16 @@
*/
package net.silthus.schat.features;

import com.google.gson.InstanceCreator;
import java.lang.reflect.Type;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.eventbus.EventBus;
import net.silthus.schat.eventbus.Subscribe;
import net.silthus.schat.events.channel.ChannelRegisteredEvent;
import net.silthus.schat.events.channel.ChannelSettingChangedEvent;
import net.silthus.schat.events.channel.ChannelSettingsChanged;
import net.silthus.schat.events.channel.ChannelTargetAdded;
import net.silthus.schat.events.channel.ChannelTargetRemoved;
import net.silthus.schat.events.channel.ChatterJoinedChannelEvent;
import net.silthus.schat.events.message.SendChannelMessageEvent;
import net.silthus.schat.events.message.SendGlobalMessageEvent;
Expand All @@ -55,17 +46,13 @@ public class GlobalChatFeature {

private final EventBus eventBus;
private final Messenger messenger;
private final ChannelRepository channelRepository;

public GlobalChatFeature(EventBus eventBus, Messenger messenger, ChannelRepository channelRepository) {
public GlobalChatFeature(EventBus eventBus, Messenger messenger) {
this.eventBus = eventBus;
this.messenger = messenger;
this.channelRepository = channelRepository;

messenger.registerMessageType(SendGlobalMessage.class);
messenger.registerMessageType(ChatterJoinedChannel.class);
messenger.registerMessageType(UpdateGlobalChannel.class);
messenger.registerTypeAdapter(UpdateGlobalChannel.class, new MessageCreator());
eventBus.register(this);
}

Expand All @@ -80,46 +67,12 @@ protected void onChatterJoined(ChatterJoinedChannelEvent event) {
messenger.sendPluginMessage(new ChatterJoinedChannel(event.chatter(), event.channel()));
}

@Subscribe
protected void onChannelTargetAdded(ChannelTargetAdded event) {
updateChannel(event.channel());
}

@Subscribe
protected void onChannelTargetRemoved(ChannelTargetRemoved event) {
updateChannel(event.channel());
}

@Subscribe
protected void onChannelSettingsChanged(ChannelSettingsChanged event) {
updateChannel(event.channel());
}

@Subscribe
protected void onChannelSettingChange(ChannelSettingChangedEvent<?> event) {
updateChannel(event.channel());
}

@Subscribe
protected void onChannelRegistered(ChannelRegisteredEvent event) {
updateChannel(event.channel());
}

private void updateChannel(Channel event) {
if (event.is(GLOBAL))
sendUpdateChannelMessage(event);
}

private void sendGlobalMessage(Channel channel, Message message) {
final SendGlobalMessageEvent event = eventBus.post(new SendGlobalMessageEvent(channel, message));
if (event.isNotCancelled())
messenger.sendPluginMessage(new SendGlobalMessage(event.message()));
}

private void sendUpdateChannelMessage(Channel channel) {
// messenger.sendPluginMessage(new UpdateGlobalChannel(channel));
}

@Getter
@Accessors(fluent = true)
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
Expand Down Expand Up @@ -152,32 +105,4 @@ public void process() {
chatter.join(channel);
}
}

@Getter
@Setter(AccessLevel.PROTECTED)
@Accessors(fluent = true)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
public static final class UpdateGlobalChannel extends PluginMessage {
private ChannelRepository channelRepository;
private Channel channel;

public UpdateGlobalChannel(Channel channel) {
this.channel = channel;
}

@Override
public void process() {
final Channel existing = channelRepository.findOrCreate(channel.key(), Channel::createChannel);
existing.settings(channel.settings()).targets(channel.targets());
}
}

private final class MessageCreator implements InstanceCreator<UpdateGlobalChannel> {
@Override
public UpdateGlobalChannel createInstance(Type type) {
return new UpdateGlobalChannel()
.channelRepository(channelRepository);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.lang.reflect.Type;
import lombok.NonNull;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelHelper;
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.channel.ChannelSettings;
import net.silthus.schat.chatter.ChatterMock;
Expand All @@ -35,7 +34,6 @@
import net.silthus.schat.events.message.SendChannelMessageEvent;
import net.silthus.schat.events.message.SendGlobalMessageEvent;
import net.silthus.schat.message.Message;
import net.silthus.schat.message.Targets;
import net.silthus.schat.messenger.GsonPluginMessageSerializer;
import net.silthus.schat.messenger.Messenger;
import net.silthus.schat.messenger.PluginMessage;
Expand All @@ -50,7 +48,6 @@

import static net.silthus.schat.channel.ChannelHelper.channelWith;
import static net.silthus.schat.channel.ChannelSettings.GLOBAL;
import static net.silthus.schat.channel.ChannelSettings.PRIVATE;
import static net.silthus.schat.chatter.ChatterMock.randomChatter;
import static net.silthus.schat.message.Message.message;
import static net.silthus.schat.message.MessageHelper.randomMessage;
Expand All @@ -74,7 +71,7 @@ void setUp() {
channelRepository
);
serializer = PluginMessageSerializer.gsonSerializer(gsonProvider);
feature = new GlobalChatFeature(events, this, channelRepository);
feature = new GlobalChatFeature(events, this);
}

@AfterEach
Expand Down Expand Up @@ -166,28 +163,5 @@ void when_chatter_joins_channel_then_update_is_sent() {
chatter.join(channel);
assertMessageDispatched();
}

@Test
void global_channel_update_is_serializable() {
assertPluginMessageIsSerializable(new GlobalChatFeature.UpdateGlobalChannel(ChannelHelper.randomChannel()));
}

@Test
void when_global_update_message_is_received_then_channel_is_updated() {
final Channel channel = channelWith("test", GLOBAL, true);
channelRepository.add(channel);
final ChatterMock chatter = randomChatter();
chatterRepository.add(chatter);
final GlobalChatFeature.UpdateGlobalChannel message = new GlobalChatFeature.UpdateGlobalChannel(channelWith("test", builder -> builder
.set(GLOBAL, true)
.set(PRIVATE, true)
.targets(Targets.of(chatter))
)).channelRepository(channelRepository);

message.process();
assertThat(channel.is(PRIVATE)).isTrue();
assertThat(channel.is(GLOBAL)).isTrue();
assertThat(channel.targets()).contains(chatter);
}
}
}

This file was deleted.

0 comments on commit 66f2564

Please sign in to comment.