-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Enable/Disable hook for sprockets #73
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,7 @@ public bool Handle(ChatMessage message, IBot bot) | |
|
||
return false; | ||
} | ||
|
||
public bool Enabled { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
using Jabbot.Models; | ||
using Jabbot.Sprockets.Core; | ||
using SignalR.Client.Hubs; | ||
using System.Linq; | ||
|
||
namespace Jabbot | ||
{ | ||
|
@@ -60,6 +61,24 @@ public void RemoveSprocket(ISprocket sprocket) | |
_sprockets.Remove(sprocket); | ||
} | ||
|
||
/// <summary> | ||
/// Enable a specific sprocket specified by Type Name | ||
/// </summary> | ||
/// <param name="sprocketTypeName">The Type Name of the sprocket (what's returned by mySprocket.GetType().FullName</param> | ||
public void EnableSprocket(string sprocketTypeName) | ||
{ | ||
SetSprocketEnabled(sprocketTypeName,true); | ||
} | ||
|
||
/// <summary> | ||
/// Disable a specific sprocket specified by Type Name | ||
/// </summary> | ||
/// <param name="sprocketTypeName">The Type Name of the sprocket (what's returned by mySprocket.GetType().FullName</param> | ||
public void DisableSprocket(string sprocketTypeName) | ||
{ | ||
SetSprocketEnabled(sprocketTypeName, false); | ||
} | ||
|
||
/// <summary> | ||
/// Add a sprocket to the bot instance | ||
/// </summary> | ||
|
@@ -112,6 +131,10 @@ public void PowerUp(IEnumerable<ISprocketInitializer> sprocketInitializers = nul | |
|
||
if (sprocketInitializers != null) | ||
IntializeSprockets(sprocketInitializers); | ||
foreach(var sprocket in _sprockets) | ||
{ | ||
sprocket.Enabled = true; | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -387,6 +410,10 @@ private void ProcessChatMessages(ChatMessage message) | |
|
||
foreach (var handler in _sprockets) | ||
{ | ||
if(!handler.Enabled) | ||
{ | ||
continue; | ||
} | ||
if (handler.Handle(message, this)) | ||
{ | ||
handled = true; | ||
|
@@ -430,6 +457,10 @@ private void ProcessRoomArrival(dynamic message, string room) | |
|
||
foreach (var handler in _sprockets) | ||
{ | ||
if(!handler.Enabled) | ||
{ | ||
continue; | ||
} | ||
if (handler.Handle(new ChatMessage("[JABBR] - " + name + " just entered " + room, name, room), this)) | ||
{ | ||
handled = true; | ||
|
@@ -462,7 +493,6 @@ private void ProcessRoomArrival(dynamic message, string room) | |
|
||
} | ||
|
||
|
||
private void OnLeave(dynamic user) | ||
{ | ||
|
||
|
@@ -502,5 +532,17 @@ private void Send(string command) | |
_chat.Invoke("send", command).Wait(); | ||
} | ||
|
||
private void SetSprocketEnabled(string sprocketTypeName, bool enabled) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to use something simpler to identify sprockets than the full type? Someting like an int ID? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem with that is that where would this ID live? I don't want to add it to the ISprocket interface thus forcing all implementers to arbitrarily return some random int. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not return a random int, but the host/bot should assign the ID when it is set up. Idea: what if you wanted to create two sprockets in the same bot, but listening to different rooms. The architecture currently suggests only one will ever exist, but I'm trying to think of a use case where you might want to have multiple instances of the same sprocket being active... Configuration is another topic I need to think about too (unrelated to all this). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but the ISprocket interface would still need an ID prop like int ID {get; set;}, and we'd have to make it very clear in the documentation that this is something you do NOT need to set when implementing a custom sprocket. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After some more pondering, I don't think we need to clutter up the ISprocket interface with that detail. Given the Bot maintains a collection of sprockets internally, perhaps switching IList<T> to IDictionary<T,K> is an easier change to manage. Problem with heading down that path is that we need to handle sprockets and announcers in the same fashion. i'm just jotting down notes as these things come to mind There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That thought crossed my mind as well, but then you'd need a way of exposing these ID's to the outside world, since the WebApp needs a way to build the urls to enable/disable the sprockets. Right now, in the IoC container we just have a list of Sprockets and that gets passed along in the HomeModule to the view. When looping through this collection and building the list of sprockets, we need a way to build the enable/disable url for each sprocket e.g. /bot/enable/{id}. sorry to be a pain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To decouple the model from the view, we should be using viewmodels to represent the structure of data on the screen. This may help us cover multiple issues:
it's all good, i wish i had more time at the moment to code. is it friday yet? |
||
{ | ||
if (String.IsNullOrEmpty(sprocketTypeName)) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var sprocket in _sprockets.Where(s => s.GetType().FullName == sprocketTypeName)) | ||
{ | ||
sprocket.Enabled = enabled; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about our IAnnounce classes? Admins might want to switch them on/off too...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with that, and as far as I can tell, the only place we actually initiate announcements is in the Scheduler class which right now lives in the ConsoleBotHost application. Should I add that logic there? If so, how will the admin enable/disable these announcements, there's no UI for that, as the web app doesn't actually start the announcers (not as far as I can tell).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. Let me have a think about how this should hang together.