Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Extensions/HelpSprocket/Help.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public bool Handle(ChatMessage message, IBot bot)

return false;
}

public bool Enabled { get; set; }
}
}
12 changes: 12 additions & 0 deletions Jabbot.AspNetBotHost/Modules/BotHostModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ public BotHostModule(Bot bot, IEnumerable<ISprocket> sprockets, IEnumerable<ISpr
return Response.AsRedirect("/");
};

Post["/disable/{sprocket}"] = _ =>
{
_bot.DisableSprocket(_.Sprocket);
return Response.AsRedirect("/");
};

Post["/enable/{sprocket}"] = _ =>
{
_bot.EnableSprocket(_.Sprocket);
return Response.AsRedirect("/");
};

}


Expand Down
7 changes: 6 additions & 1 deletion Jabbot.AspNetBotHost/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
<ul>
@foreach (var m in Model.Sprockets)
{
<li>@m</li>
string enabledText = m.Enabled ? "Disable" : "Enable";
<form method="POST" action="/bot/@enabledText/@m">
<li>@m
<input type="submit" value="@enabledText"/>
</li>
</form>
}
</ul>

Expand Down
2 changes: 2 additions & 0 deletions Jabbot.CommandSprockets/CommandSprocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ public virtual bool Handle(ChatMessage message, IBot bot)
}
return false;
}

public bool Enabled { get; set; }
}
}
44 changes: 43 additions & 1 deletion Jabbot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Jabbot.Models;
using Jabbot.Sprockets.Core;
using SignalR.Client.Hubs;
using System.Linq;

namespace Jabbot
{
Expand Down Expand Up @@ -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)
Copy link
Member

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...

Copy link
Author

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).

Copy link
Member

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.

{
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>
Expand Down Expand Up @@ -112,6 +131,10 @@ public void PowerUp(IEnumerable<ISprocketInitializer> sprocketInitializers = nul

if (sprocketInitializers != null)
IntializeSprockets(sprocketInitializers);
foreach(var sprocket in _sprockets)
{
sprocket.Enabled = true;
}
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -462,7 +493,6 @@ private void ProcessRoomArrival(dynamic message, string room)

}


private void OnLeave(dynamic user)
{

Expand Down Expand Up @@ -502,5 +532,17 @@ private void Send(string command)
_chat.Invoke("send", command).Wait();
}

private void SetSprocketEnabled(string sprocketTypeName, bool enabled)
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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).

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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:

  • the Bot only tracks sprockets which are running
  • inactive sprockets require an identifier too - so that we can create the "enable" URLs too
  • the bot shouldn't care about the URL generation code

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;
}
}
}
}
2 changes: 1 addition & 1 deletion Jabbot/Sprockets/Core/ISprocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace Jabbot.Sprockets.Core
[InheritedExport]
public interface ISprocket : IMessageHandler
{

bool Enabled { get; set; }
}
}
2 changes: 2 additions & 0 deletions Jabbot/Sprockets/RegexSprocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ public bool Handle(ChatMessage message, IBot bot)
}

protected abstract void ProcessMatch(Match match, ChatMessage message, IBot bot);

public bool Enabled { get; set; }
}
}