Skip to content

Commit

Permalink
Merge pull request #377 from greboid/dev4
Browse files Browse the repository at this point in the history
Add settings to the channel who plugin.
  • Loading branch information
csmith committed Jan 19, 2015
2 parents d79c4b5 + c14149e commit c181364
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
7 changes: 6 additions & 1 deletion channelwho/plugin.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ keysections:
metadata
updates
version
defaults

metadata:
author=Greg <greg@dmdirc.com>
Expand All @@ -19,4 +20,8 @@ updates:
id=77

version:
friendly=0.1
friendly=0.1

defaults:
sendwho=false
whointerval=60000
29 changes: 29 additions & 0 deletions channelwho/src/com/dmdirc/addons/channelwho/ChannelWhoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
package com.dmdirc.addons.channelwho;

import com.dmdirc.DMDircMBassador;
import com.dmdirc.config.prefs.PreferencesCategory;
import com.dmdirc.config.prefs.PreferencesSetting;
import com.dmdirc.config.prefs.PreferencesType;
import com.dmdirc.events.ClientPrefsOpenedEvent;
import com.dmdirc.events.GroupChatPrefsRequestedEvent;
import com.dmdirc.events.ServerConnectingEvent;
import com.dmdirc.events.ServerDisconnectedEvent;
import com.dmdirc.interfaces.Connection;
import com.dmdirc.interfaces.ConnectionManager;
import com.dmdirc.plugins.PluginDomain;
import com.dmdirc.util.validators.NumericalValidator;

import com.google.common.annotations.VisibleForTesting;

Expand All @@ -42,16 +49,19 @@
*/
public class ChannelWhoManager {

private final String domain;
private final ConnectionHandlerFactory connectionHandlerFactory;
private final ConnectionManager connectionManager;
private final DMDircMBassador eventBus;
private final Map<Connection, ConnectionHandler> connectionHandlers;

@Inject
public ChannelWhoManager(
@PluginDomain(ChannelWhoPlugin.class) final String domain,
final ConnectionHandlerFactory connectionHandlerFactory,
final ConnectionManager connectionManager,
final DMDircMBassador eventBus) {
this.domain = domain;
this.connectionHandlerFactory = connectionHandlerFactory;
this.connectionManager = connectionManager;
this.eventBus = eventBus;
Expand Down Expand Up @@ -79,6 +89,25 @@ private void removeConnectionHandler(final Connection connection) {
}
}

@VisibleForTesting
@Handler
void handleGroupChatPrefsRequestedEvent(final GroupChatPrefsRequestedEvent event) {
event.getCategory().addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, domain,
"sendWho", "Send Who Requests", "Should we send who requests to the channel?",
event.getConfig(), event.getIdentity()));
}

@VisibleForTesting
@Handler
void handlePrefsDialog(final ClientPrefsOpenedEvent event) {
final PreferencesCategory category = new PreferencesCategory("Channel Who", "Provides " +
"support for sending WHO requests to channels at regular intervals");
category.addSetting(new PreferencesSetting(PreferencesType.DURATION,
new NumericalValidator(0, Integer.MAX_VALUE), domain, "whointerval",
"Who Interval", "The interval WHO requests will be sent to channels",
event.getModel().getConfigManager(), event.getModel().getIdentity()));
}

@VisibleForTesting
@Handler
void handleServerConnectingEvent(final ServerConnectingEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,27 @@ public void unload() {
executorService.shutdown();
connection.getWindowModel().getEventBus().unsubscribe(this);
if (future != null) {
future.cancel(true);
future.cancel(false);
}
}

@VisibleForTesting
void checkWho() {
connectionManager.getConnections().forEach(connection ->
connection.getGroupChatManager().getChannels().forEach(channel -> {
if (channel.getWindowModel().getConfigManager().getOptionBool(domain, "sendWho")) {
channel.requestUsersInfo();
}
}));
if (channel.getWindowModel().getConfigManager().getOptionBool(domain, "sendwho")) {
channel.requestUsersInfo();
}}));
}

@VisibleForTesting
@ConfigBinding(key="whoInterval")
@ConfigBinding(key="whointerval")
void handleWhoInterval(final int value) {
if (future != null) {
future.cancel(true);
future.cancel(false);
}
future = executorService.schedule(this::checkWho, value, TimeUnit.MILLISECONDS);
future = executorService.scheduleAtFixedRate(this::checkWho, value, value,
TimeUnit.MILLISECONDS);
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public class ChannelWhoManagerTest {
public void setUp() throws Exception {
when(connectionManager.getConnections()).thenReturn(Lists.newArrayList(connection));
when(connectionHandlerFactory.get(connection)).thenReturn(connectionHandler);
instance = new ChannelWhoManager(connectionHandlerFactory, connectionManager, eventBus);
instance = new ChannelWhoManager("domain", connectionHandlerFactory, connectionManager,
eventBus);
instance.load();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public class ConnectionHandlerTest {

@Before
public void setUp() throws Exception {
when(scheduledExecutorService.schedule(any(Runnable.class), anyLong(), any()))
.thenReturn(scheduledFuture);
when(scheduledExecutorService.scheduleAtFixedRate(any(Runnable.class), anyLong(), anyLong(),
any())).thenReturn(scheduledFuture);
when(windowModel.getEventBus()).thenReturn(eventBus);
when(connection.getWindowModel()).thenReturn(windowModel);
when(config.getBinder()).thenReturn(configBinder);
Expand All @@ -107,7 +107,7 @@ public void testLoad() throws Exception {
instance.load();
verify(configBinder).bind(instance, ConnectionHandler.class);
verify(eventBus).subscribe(instance);
verify(scheduledExecutorService).schedule(any(Runnable.class), eq(5l),
verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(5l), eq(5l),
eq(TimeUnit.MILLISECONDS));
}

Expand All @@ -122,26 +122,26 @@ public void testUnload() throws Exception {
@Test
public void testHandleWhoInterval() throws Exception {
instance.handleWhoInterval(10);
verify(scheduledFuture).cancel(true);
verify(scheduledExecutorService).schedule(any(Runnable.class), eq(5l),
verify(scheduledFuture).cancel(false);
verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(5l), eq(5l),
eq(TimeUnit.MILLISECONDS));
verify(scheduledExecutorService).schedule(any(Runnable.class), eq(10l),
verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(10l), eq(10l),
eq(TimeUnit.MILLISECONDS));
}

@Test
public void testCheckWho_True() throws Exception {
when(config.getOptionBool("domain", "sendWho")).thenReturn(true);
when(config.getOptionBool("domain", "sendwho")).thenReturn(true);
instance.checkWho();
verify(config).getOptionBool("domain", "sendWho");
verify(config).getOptionBool("domain", "sendwho");
verify(groupChat).requestUsersInfo();
}

@Test
public void testCheckWho_False() throws Exception {
when(config.getOptionBool("domain", "sendWho")).thenReturn(false);
when(config.getOptionBool("domain", "sendwho")).thenReturn(false);
instance.checkWho();
verify(config).getOptionBool("domain", "sendWho");
verify(config).getOptionBool("domain", "sendwho");
verify(groupChat, never()).requestUsersInfo();
}

Expand Down

0 comments on commit c181364

Please sign in to comment.