From dfba3a93094f5cb7e151a75682d7bdce8a858ef7 Mon Sep 17 00:00:00 2001 From: jimmyp Date: Sat, 20 Oct 2012 15:27:53 +1100 Subject: [PATCH 01/12] Created 'Will notify new users of voicemails' test --- Tests/ExtensionTests/ExtensionTests.csproj | 1 + Tests/ExtensionTests/VoicemailSprocketTest.cs | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 Tests/ExtensionTests/VoicemailSprocketTest.cs diff --git a/Tests/ExtensionTests/ExtensionTests.csproj b/Tests/ExtensionTests/ExtensionTests.csproj index eb3ae8d..bf6a203 100644 --- a/Tests/ExtensionTests/ExtensionTests.csproj +++ b/Tests/ExtensionTests/ExtensionTests.csproj @@ -51,6 +51,7 @@ + diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs new file mode 100644 index 0000000..8e395c3 --- /dev/null +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Jabbot; +using Jabbot.Models; +using Jabbot.Sprockets.Core; +using Moq; +using Xunit; + +namespace ExtensionTests +{ + public class VoicemailSprocketTest + { + [Fact] + public void WillNotifyNewlyArrivedUsersOfVoicemails() + { + //Setup + const string newlyArrivedUser = "James"; + const string room = "TestRoom"; + const string contents = "Some message"; + + + var voicemailSprocket = new VoicemailSprocket(); + var mockBot = new Mock(); + var bot = mockBot.Object; + + //Act + voicemailSprocket.Handle(new ChatMessage(string.Format(@"record ""{0}""", contents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage("[JABBR] - " + newlyArrivedUser + " just entered " + room, newlyArrivedUser, bot.Name), bot); + + //Test + mockBot.Verify(b => b.PrivateReply(It.IsAny(), It.Is(what => what == string.Format(@"@James said ""{0}""",contents)))); + } + } + + public class VoicemailSprocket : ISprocket + { + public bool Handle(ChatMessage message, IBot bot) + { + throw new NotImplementedException(); + } + } +} From 22948aadc8c82bc47d06fb33acc221f8dc5daf6d Mon Sep 17 00:00:00 2001 From: jimmyp Date: Sat, 20 Oct 2012 18:43:59 +1100 Subject: [PATCH 02/12] Implemented notifying new users to all existing voicemails --- Tests/ExtensionTests/VoicemailSprocketTest.cs | 100 ++++++++++++++++-- 1 file changed, 91 insertions(+), 9 deletions(-) diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index 8e395c3..9f3627c 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -3,11 +3,15 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using Jabbot; +using Jabbot.CommandSprockets; using Jabbot.Models; +using Jabbot.Sprockets; using Jabbot.Sprockets.Core; using Moq; using Xunit; +using Match = System.Text.RegularExpressions.Match; namespace ExtensionTests { @@ -17,29 +21,107 @@ public class VoicemailSprocketTest public void WillNotifyNewlyArrivedUsersOfVoicemails() { //Setup + const string recordingUser = "Jim"; const string newlyArrivedUser = "James"; - const string room = "TestRoom"; + const string roomName = "TestRoom"; + dynamic room = new {Name = roomName}; const string contents = "Some message"; - + IList usersInRoom = new List(); - var voicemailSprocket = new VoicemailSprocket(); var mockBot = new Mock(); var bot = mockBot.Object; + mockBot.Setup(b => b.GetRooms()).Returns(new [] { room }); + mockBot.Setup(b => b.GetUsers(It.Is(r => r ==roomName))).Returns(()=>usersInRoom); + var voicemailSprocket = new VoicemailSprocket(); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", contents), recordingUser, bot.Name), bot); + + //Act - voicemailSprocket.Handle(new ChatMessage(string.Format(@"record ""{0}""", contents), "Jim", bot.Name), bot); - voicemailSprocket.Handle(new ChatMessage("[JABBR] - " + newlyArrivedUser + " just entered " + room, newlyArrivedUser, bot.Name), bot); + usersInRoom.Add(newlyArrivedUser); + voicemailSprocket.Handle(new ChatMessage(VoicemailSprocket.SystemCommand + newlyArrivedUser + " just entered " + roomName, newlyArrivedUser, bot.Name), bot); //Test - mockBot.Verify(b => b.PrivateReply(It.IsAny(), It.Is(what => what == string.Format(@"@James said ""{0}""",contents)))); + mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("@{0} said '{1}'", newlyArrivedUser, contents)))); } } - public class VoicemailSprocket : ISprocket + public class VoicemailSprocket : RegexSprocket { - public bool Handle(ChatMessage message, IBot bot) + internal const string SystemCommand = "[JABBR] - "; + + public VoicemailSprocket() { - throw new NotImplementedException(); + RecordVoicemailSprocket = new RecordVoicemailSprocket(); + _newlyArrivedUser = new NewlyArrivedUser(RecordVoicemailSprocket); + } + + private RecordVoicemailSprocket RecordVoicemailSprocket; + private NewlyArrivedUser _newlyArrivedUser; + + public override Regex Pattern + { + get { return new Regex(string.Format("({0})|({1})", RecordVoicemailSprocket.RecordCommand, NewlyArrivedUser.UserArrivedNotification), RegexOptions.IgnoreCase); } + } + + protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) + { + _newlyArrivedUser.Handle(message, bot); + RecordVoicemailSprocket.Handle(message, bot); + } + + protected IList Users { get; set; } + } + + public class RecordVoicemailSprocket : RegexSprocket + { + public IList Voicemails = new List(); + internal const string RecordCommand = "record \'.*\'"; + + public override Regex Pattern + { + get { return new Regex(RecordCommand);} + } + + protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) + { + Voicemails.Add(message.Content.Split('\'')[1]); + } + } + + + internal class NewlyArrivedUser : RegexSprocket + { + private readonly RecordVoicemailSprocket _recordVoicemailSprocket; + private readonly IList _userNames = new List(); + + public NewlyArrivedUser(RecordVoicemailSprocket recordVoicemailSprocket) + { + if (recordVoicemailSprocket == null) throw new ArgumentNullException("recordVoicemailSprocket"); + _recordVoicemailSprocket = recordVoicemailSprocket; + } + + public const string UserArrivedNotification = @"\[JABBR\] - .* just entered .*"; + + private IEnumerable GetRoomNames(IBot bot) + { + foreach (var room in bot.GetRooms()) + yield return room.Name; + } + + public override Regex Pattern + { + get { return new Regex(UserArrivedNotification, RegexOptions.IgnoreCase); } + } + + protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) + { + var indexOfUserName = VoicemailSprocket.SystemCommand.Length; + string username = message.Content.Substring(indexOfUserName, message.Content.IndexOf(' ', indexOfUserName) - indexOfUserName); + _userNames.Add(username); + + foreach (var voicemail in _recordVoicemailSprocket.Voicemails) + bot.PrivateReply(username, string.Format("@{0} said '{1}'", username, voicemail)); } } } From 579287a069f759ffe6d8715bb75191d5b7100682 Mon Sep 17 00:00:00 2001 From: jimmyp Date: Sat, 20 Oct 2012 19:09:14 +1100 Subject: [PATCH 03/12] Refactored classes for notifying new users of existing voicemails --- Jabbot.sln | 57 +++++---- Tests/ExtensionTests/ExtensionTests.csproj | 4 + Tests/ExtensionTests/VoicemailSprocketTest.cs | 108 ++---------------- VoicemailSprocket/Properties/AssemblyInfo.cs | 36 ++++++ VoicemailSprocket/UserRegistry.cs | 43 +++++++ VoicemailSprocket/VoiceMailbox.cs | 30 +++++ VoicemailSprocket/VoicemailSprocket.cs | 24 ++++ VoicemailSprocket/VoicemailSprocket.csproj | 62 ++++++++++ 8 files changed, 243 insertions(+), 121 deletions(-) create mode 100644 VoicemailSprocket/Properties/AssemblyInfo.cs create mode 100644 VoicemailSprocket/UserRegistry.cs create mode 100644 VoicemailSprocket/VoiceMailbox.cs create mode 100644 VoicemailSprocket/VoicemailSprocket.cs create mode 100644 VoicemailSprocket/VoicemailSprocket.csproj diff --git a/Jabbot.sln b/Jabbot.sln index 311df2d..cf28fe2 100644 --- a/Jabbot.sln +++ b/Jabbot.sln @@ -81,6 +81,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeamCityAnnouncer", "Extens EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VotingSprocket", "Extensions\VotingSprocket\VotingSprocket.csproj", "{23F45EDA-A95D-4041-8B78-6F691A98B0CB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VoicemailSprocket", "VoicemailSprocket\VoicemailSprocket.csproj", "{FC6FF62D-C831-43EC-9843-01D658074C07}" +EndProject Global GlobalSection(TestCaseManagementSettings) = postSolution CategoryFile = Jabbot.vsmdi @@ -235,16 +237,6 @@ Global {93DB29BE-BC75-4D59-9313-891A1245940D}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {93DB29BE-BC75-4D59-9313-891A1245940D}.Release|Mixed Platforms.Build.0 = Release|Any CPU {93DB29BE-BC75-4D59-9313-891A1245940D}.Release|x86.ActiveCfg = Release|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|x86.ActiveCfg = Debug|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Any CPU.Build.0 = Release|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|x86.ActiveCfg = Release|Any CPU {1BCEA8B3-23C9-4087-9DC1-70EAC94D0787}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1BCEA8B3-23C9-4087-9DC1-70EAC94D0787}.Debug|Any CPU.Build.0 = Debug|Any CPU {1BCEA8B3-23C9-4087-9DC1-70EAC94D0787}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -265,6 +257,16 @@ Global {4D94EB55-9204-4579-A79C-4D42F4ED85BB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {4D94EB55-9204-4579-A79C-4D42F4ED85BB}.Release|Mixed Platforms.Build.0 = Release|Any CPU {4D94EB55-9204-4579-A79C-4D42F4ED85BB}.Release|x86.ActiveCfg = Release|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|x86.ActiveCfg = Debug|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Any CPU.Build.0 = Release|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|x86.ActiveCfg = Release|Any CPU {6416F073-97F1-4DBB-B8D6-2011BE448D98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {6416F073-97F1-4DBB-B8D6-2011BE448D98}.Debug|Any CPU.Build.0 = Debug|Any CPU {6416F073-97F1-4DBB-B8D6-2011BE448D98}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -295,16 +297,6 @@ Global {9731A150-94A1-4B46-A9AB-5B74B5D196AC}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {9731A150-94A1-4B46-A9AB-5B74B5D196AC}.Release|Mixed Platforms.Build.0 = Release|Any CPU {9731A150-94A1-4B46-A9AB-5B74B5D196AC}.Release|x86.ActiveCfg = Release|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|x86.ActiveCfg = Debug|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Any CPU.Build.0 = Release|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|x86.ActiveCfg = Release|Any CPU {577B79CF-B210-4ED1-8D1C-AF51BDAA2344}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {577B79CF-B210-4ED1-8D1C-AF51BDAA2344}.Debug|Any CPU.Build.0 = Debug|Any CPU {577B79CF-B210-4ED1-8D1C-AF51BDAA2344}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -315,6 +307,26 @@ Global {577B79CF-B210-4ED1-8D1C-AF51BDAA2344}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {577B79CF-B210-4ED1-8D1C-AF51BDAA2344}.Release|Mixed Platforms.Build.0 = Release|Any CPU {577B79CF-B210-4ED1-8D1C-AF51BDAA2344}.Release|x86.ActiveCfg = Release|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|x86.ActiveCfg = Debug|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Any CPU.Build.0 = Release|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|x86.ActiveCfg = Release|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Debug|x86.ActiveCfg = Debug|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Release|Any CPU.Build.0 = Release|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {FC6FF62D-C831-43EC-9843-01D658074C07}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -329,14 +341,15 @@ Global {ED3BAD8A-2972-4D52-BE1E-9B73EE43E53C} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} {D05250A1-6F48-49F1-967C-BCB25213EA06} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} {93DB29BE-BC75-4D59-9313-891A1245940D} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} - {87E3E12D-F46E-4425-8375-6882A6DF243A} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} {1BCEA8B3-23C9-4087-9DC1-70EAC94D0787} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} {4D94EB55-9204-4579-A79C-4D42F4ED85BB} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} + {87E3E12D-F46E-4425-8375-6882A6DF243A} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} {6416F073-97F1-4DBB-B8D6-2011BE448D98} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} {9518C000-0F98-41B0-81E9-5E065CF5387E} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} {9731A150-94A1-4B46-A9AB-5B74B5D196AC} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} - {23F45EDA-A95D-4041-8B78-6F691A98B0CB} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} {577B79CF-B210-4ED1-8D1C-AF51BDAA2344} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} + {23F45EDA-A95D-4041-8B78-6F691A98B0CB} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} + {FC6FF62D-C831-43EC-9843-01D658074C07} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7} {3E8A88F3-737F-4F93-92EA-151B1FE3A44B} = {A3A6EC44-1F10-4427-98DC-CF13D11F8C79} EndGlobalSection EndGlobal diff --git a/Tests/ExtensionTests/ExtensionTests.csproj b/Tests/ExtensionTests/ExtensionTests.csproj index bf6a203..d8969e1 100644 --- a/Tests/ExtensionTests/ExtensionTests.csproj +++ b/Tests/ExtensionTests/ExtensionTests.csproj @@ -83,6 +83,10 @@ {478BFCF7-9397-49A7-AFD4-060B6B749E77} Jabbot + + {FC6FF62D-C831-43EC-9843-01D658074C07} + VoicemailSprocket + diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index 9f3627c..c772184 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -1,17 +1,12 @@ -using System; -using System.Collections; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; -using System.Text.RegularExpressions; using Jabbot; using Jabbot.CommandSprockets; using Jabbot.Models; -using Jabbot.Sprockets; -using Jabbot.Sprockets.Core; using Moq; using Xunit; -using Match = System.Text.RegularExpressions.Match; namespace ExtensionTests { @@ -21,107 +16,22 @@ public class VoicemailSprocketTest public void WillNotifyNewlyArrivedUsersOfVoicemails() { //Setup - const string recordingUser = "Jim"; const string newlyArrivedUser = "James"; - const string roomName = "TestRoom"; - dynamic room = new {Name = roomName}; - const string contents = "Some message"; - IList usersInRoom = new List(); + const string voicemailContents = "Some message"; var mockBot = new Mock(); var bot = mockBot.Object; - mockBot.Setup(b => b.GetRooms()).Returns(new [] { room }); - mockBot.Setup(b => b.GetUsers(It.Is(r => r ==roomName))).Returns(()=>usersInRoom); - var voicemailSprocket = new VoicemailSprocket(); - voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", contents), recordingUser, bot.Name), bot); - - + var voicemailSprocket = new VoicemailSprocket.VoicemailSprocket(); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", voicemailContents), "Jim", bot.Name), bot); + //Act - usersInRoom.Add(newlyArrivedUser); - voicemailSprocket.Handle(new ChatMessage(VoicemailSprocket.SystemCommand + newlyArrivedUser + " just entered " + roomName, newlyArrivedUser, bot.Name), bot); + voicemailSprocket.Handle( + new ChatMessage("[JABBR] - " + newlyArrivedUser + " just entered " + "TestRoom", newlyArrivedUser, + bot.Name), bot); //Test - mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("@{0} said '{1}'", newlyArrivedUser, contents)))); - } - } - - public class VoicemailSprocket : RegexSprocket - { - internal const string SystemCommand = "[JABBR] - "; - - public VoicemailSprocket() - { - RecordVoicemailSprocket = new RecordVoicemailSprocket(); - _newlyArrivedUser = new NewlyArrivedUser(RecordVoicemailSprocket); - } - - private RecordVoicemailSprocket RecordVoicemailSprocket; - private NewlyArrivedUser _newlyArrivedUser; - - public override Regex Pattern - { - get { return new Regex(string.Format("({0})|({1})", RecordVoicemailSprocket.RecordCommand, NewlyArrivedUser.UserArrivedNotification), RegexOptions.IgnoreCase); } - } - - protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) - { - _newlyArrivedUser.Handle(message, bot); - RecordVoicemailSprocket.Handle(message, bot); - } - - protected IList Users { get; set; } - } - - public class RecordVoicemailSprocket : RegexSprocket - { - public IList Voicemails = new List(); - internal const string RecordCommand = "record \'.*\'"; - - public override Regex Pattern - { - get { return new Regex(RecordCommand);} - } - - protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) - { - Voicemails.Add(message.Content.Split('\'')[1]); - } - } - - - internal class NewlyArrivedUser : RegexSprocket - { - private readonly RecordVoicemailSprocket _recordVoicemailSprocket; - private readonly IList _userNames = new List(); - - public NewlyArrivedUser(RecordVoicemailSprocket recordVoicemailSprocket) - { - if (recordVoicemailSprocket == null) throw new ArgumentNullException("recordVoicemailSprocket"); - _recordVoicemailSprocket = recordVoicemailSprocket; - } - - public const string UserArrivedNotification = @"\[JABBR\] - .* just entered .*"; - - private IEnumerable GetRoomNames(IBot bot) - { - foreach (var room in bot.GetRooms()) - yield return room.Name; - } - - public override Regex Pattern - { - get { return new Regex(UserArrivedNotification, RegexOptions.IgnoreCase); } - } - - protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) - { - var indexOfUserName = VoicemailSprocket.SystemCommand.Length; - string username = message.Content.Substring(indexOfUserName, message.Content.IndexOf(' ', indexOfUserName) - indexOfUserName); - _userNames.Add(username); - - foreach (var voicemail in _recordVoicemailSprocket.Voicemails) - bot.PrivateReply(username, string.Format("@{0} said '{1}'", username, voicemail)); + mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("@{0} said '{1}'", newlyArrivedUser, voicemailContents)))); } } } diff --git a/VoicemailSprocket/Properties/AssemblyInfo.cs b/VoicemailSprocket/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d5ecdbd --- /dev/null +++ b/VoicemailSprocket/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("VoicemailSprocket")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("VoicemailSprocket")] +[assembly: AssemblyCopyright("Copyright © 2012")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("56742ee0-3bd2-4b4b-b216-1c6d0af2282f")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/VoicemailSprocket/UserRegistry.cs b/VoicemailSprocket/UserRegistry.cs new file mode 100644 index 0000000..7d0bde9 --- /dev/null +++ b/VoicemailSprocket/UserRegistry.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using Jabbot; +using Jabbot.Models; +using Jabbot.Sprockets; + +namespace VoicemailSprocket +{ + internal class UserRegistry : RegexSprocket + { + public const string UserArrivedNotification = @"\[JABBR\] - .* just entered .*"; + private readonly VoiceMailbox voiceMailbox; + private readonly IList userNames = new List(); + + public override Regex Pattern + { + get { return new Regex(UserArrivedNotification, RegexOptions.IgnoreCase); } + } + + public UserRegistry(VoiceMailbox voiceMailbox) + { + if (voiceMailbox == null) throw new ArgumentNullException("voiceMailbox"); + this.voiceMailbox = voiceMailbox; + } + + protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) + { + var username = ExtractUsernameFromMessage(message); + userNames.Add(username); + + voiceMailbox.SendVoiceMails(username, bot); + + } + + private static string ExtractUsernameFromMessage(ChatMessage message) + { + var indexOfUserName = "[JABBR] - ".Length; + var username = message.Content.Substring(indexOfUserName, message.Content.IndexOf(' ', indexOfUserName) - indexOfUserName); + return username; + } + } +} \ No newline at end of file diff --git a/VoicemailSprocket/VoiceMailbox.cs b/VoicemailSprocket/VoiceMailbox.cs new file mode 100644 index 0000000..8ebfeb5 --- /dev/null +++ b/VoicemailSprocket/VoiceMailbox.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; +using System.Text.RegularExpressions; +using Jabbot; +using Jabbot.Models; +using Jabbot.Sprockets; + +namespace VoicemailSprocket +{ + internal class VoiceMailbox : RegexSprocket + { + internal const string RecordCommand = "record \'.*\'"; + public IList Voicemails = new List(); + + public override Regex Pattern + { + get { return new Regex(RecordCommand);} + } + + protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) + { + Voicemails.Add(message.Content.Split('\'')[1]); + } + + public void SendVoiceMails(string recipient, IBot bot) + { + foreach (var voicemail in Voicemails) + bot.PrivateReply(recipient, string.Format("@{0} said '{1}'", recipient, voicemail)); + } + } +} \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailSprocket.cs b/VoicemailSprocket/VoicemailSprocket.cs new file mode 100644 index 0000000..3d43d5c --- /dev/null +++ b/VoicemailSprocket/VoicemailSprocket.cs @@ -0,0 +1,24 @@ +using Jabbot; +using Jabbot.Models; +using Jabbot.Sprockets.Core; + +namespace VoicemailSprocket +{ + public class VoicemailSprocket : ISprocket + { + public VoicemailSprocket() + { + voiceMailbox = new VoiceMailbox(); + userRegistry = new UserRegistry(voiceMailbox); + } + + private readonly VoiceMailbox voiceMailbox; + private readonly UserRegistry userRegistry; + + public bool Handle(ChatMessage message, IBot bot) + { + return userRegistry.Handle(message, bot) || + voiceMailbox.Handle(message, bot); + } + } +} \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailSprocket.csproj b/VoicemailSprocket/VoicemailSprocket.csproj new file mode 100644 index 0000000..56cb87e --- /dev/null +++ b/VoicemailSprocket/VoicemailSprocket.csproj @@ -0,0 +1,62 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {FC6FF62D-C831-43EC-9843-01D658074C07} + Library + Properties + VoicemailSprocket + VoicemailSprocket + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + {478BFCF7-9397-49A7-AFD4-060B6B749E77} + Jabbot + + + + + \ No newline at end of file From 2586b3cffb5a0a236ab4a54db52d93fe73c8cd88 Mon Sep 17 00:00:00 2001 From: jimmyp Date: Sun, 21 Oct 2012 15:23:29 +1100 Subject: [PATCH 04/12] Changes so new users are not given thier voicemails upon arrival but notified of thier existance --- Tests/ExtensionTests/VoicemailSprocketTest.cs | 8 ++++---- VoicemailSprocket/UserRegistry.cs | 15 ++++++++++----- .../{VoiceMailbox.cs => VoicemailRecorder.cs} | 6 ++++-- VoicemailSprocket/VoicemailSprocket.cs | 8 ++++---- VoicemailSprocket/VoicemailSprocket.csproj | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) rename VoicemailSprocket/{VoiceMailbox.cs => VoicemailRecorder.cs} (80%) diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index c772184..65733d4 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -18,20 +18,20 @@ public void WillNotifyNewlyArrivedUsersOfVoicemails() //Setup const string newlyArrivedUser = "James"; const string voicemailContents = "Some message"; + const string jibbr = "jibbr"; var mockBot = new Mock(); + mockBot.Setup(b => b.Name).Returns(jibbr); var bot = mockBot.Object; var voicemailSprocket = new VoicemailSprocket.VoicemailSprocket(); voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", voicemailContents), "Jim", bot.Name), bot); //Act - voicemailSprocket.Handle( - new ChatMessage("[JABBR] - " + newlyArrivedUser + " just entered " + "TestRoom", newlyArrivedUser, - bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage("[JABBR] - " + newlyArrivedUser + " just entered " + "TestRoom", newlyArrivedUser, bot.Name), bot); //Test - mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("@{0} said '{1}'", newlyArrivedUser, voicemailContents)))); + mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("{0} has {1} new voicemail for you", jibbr, "1")))); } } } diff --git a/VoicemailSprocket/UserRegistry.cs b/VoicemailSprocket/UserRegistry.cs index 7d0bde9..f9d002d 100644 --- a/VoicemailSprocket/UserRegistry.cs +++ b/VoicemailSprocket/UserRegistry.cs @@ -10,7 +10,7 @@ namespace VoicemailSprocket internal class UserRegistry : RegexSprocket { public const string UserArrivedNotification = @"\[JABBR\] - .* just entered .*"; - private readonly VoiceMailbox voiceMailbox; + private readonly VoicemailRecorder voicemailRecorder; private readonly IList userNames = new List(); public override Regex Pattern @@ -18,20 +18,25 @@ public override Regex Pattern get { return new Regex(UserArrivedNotification, RegexOptions.IgnoreCase); } } - public UserRegistry(VoiceMailbox voiceMailbox) + public UserRegistry(VoicemailRecorder voicemailRecorder) { - if (voiceMailbox == null) throw new ArgumentNullException("voiceMailbox"); - this.voiceMailbox = voiceMailbox; + if (voicemailRecorder == null) throw new ArgumentNullException("voicemailRecorder"); + this.voicemailRecorder = voicemailRecorder; } protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) { var username = ExtractUsernameFromMessage(message); + userNames.Add(username); - voiceMailbox.SendVoiceMails(username, bot); + NotifyOfWaitingVoiceMails(username, bot); } + private void NotifyOfWaitingVoiceMails(string username, IBot bot) + { + bot.PrivateReply(username, string.Format("{0} has {1} new voicemail for you", bot.Name, voicemailRecorder.MessageCount)); + } private static string ExtractUsernameFromMessage(ChatMessage message) { diff --git a/VoicemailSprocket/VoiceMailbox.cs b/VoicemailSprocket/VoicemailRecorder.cs similarity index 80% rename from VoicemailSprocket/VoiceMailbox.cs rename to VoicemailSprocket/VoicemailRecorder.cs index 8ebfeb5..119e14c 100644 --- a/VoicemailSprocket/VoiceMailbox.cs +++ b/VoicemailSprocket/VoicemailRecorder.cs @@ -6,7 +6,7 @@ namespace VoicemailSprocket { - internal class VoiceMailbox : RegexSprocket + internal class VoicemailRecorder : RegexSprocket { internal const string RecordCommand = "record \'.*\'"; public IList Voicemails = new List(); @@ -16,12 +16,14 @@ public override Regex Pattern get { return new Regex(RecordCommand);} } + public int MessageCount { get { return Voicemails.Count; } } + protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) { Voicemails.Add(message.Content.Split('\'')[1]); } - public void SendVoiceMails(string recipient, IBot bot) + private void SendVoiceMail(string recipient, IBot bot) { foreach (var voicemail in Voicemails) bot.PrivateReply(recipient, string.Format("@{0} said '{1}'", recipient, voicemail)); diff --git a/VoicemailSprocket/VoicemailSprocket.cs b/VoicemailSprocket/VoicemailSprocket.cs index 3d43d5c..303a5dc 100644 --- a/VoicemailSprocket/VoicemailSprocket.cs +++ b/VoicemailSprocket/VoicemailSprocket.cs @@ -8,17 +8,17 @@ public class VoicemailSprocket : ISprocket { public VoicemailSprocket() { - voiceMailbox = new VoiceMailbox(); - userRegistry = new UserRegistry(voiceMailbox); + voicemailRecorder = new VoicemailRecorder(); + userRegistry = new UserRegistry(voicemailRecorder); } - private readonly VoiceMailbox voiceMailbox; + private readonly VoicemailRecorder voicemailRecorder; private readonly UserRegistry userRegistry; public bool Handle(ChatMessage message, IBot bot) { return userRegistry.Handle(message, bot) || - voiceMailbox.Handle(message, bot); + voicemailRecorder.Handle(message, bot); } } } \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailSprocket.csproj b/VoicemailSprocket/VoicemailSprocket.csproj index 56cb87e..5320693 100644 --- a/VoicemailSprocket/VoicemailSprocket.csproj +++ b/VoicemailSprocket/VoicemailSprocket.csproj @@ -42,7 +42,7 @@ - + From 3bec8131da3d48e94fa668b7a19c3a60ff1e1b1f Mon Sep 17 00:00:00 2001 From: jimmyp Date: Sun, 21 Oct 2012 16:00:26 +1100 Subject: [PATCH 05/12] Added broken tests for notifying current users of new voicwmails --- Tests/ExtensionTests/VoicemailSprocketTest.cs | 47 +++++++++++++++---- VoicemailSprocket/UserRegistry.cs | 2 +- VoicemailSprocket/VoicemailRecorder.cs | 2 +- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index 65733d4..3d02c79 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -12,26 +12,55 @@ namespace ExtensionTests { public class VoicemailSprocketTest { + //Mocks, Stubs & Constants + private readonly Mock mockBot; + private readonly IBot bot; + private const string Jibbr = "jibbr"; + private const string ExampleContents = "Some message"; + + //SUT + private VoicemailSprocket.VoicemailSprocket voicemailSprocket; + + public VoicemailSprocketTest() + { + mockBot = new Mock(); + mockBot.Setup(b => b.Name).Returns(Jibbr); + bot = mockBot.Object; + } + + [Fact] + public void CanNotifyAllUsersOfNewVoicemails() + { + //Setup + mockBot.Setup(b => b.GetRooms()).Returns(new List() + { + new {Name = "room1", Users = new {Name = "claire, mads"}}, + new {Name = "room2", Users = new {Name = "claire, bryce, vicky"}} + }); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + + //Act + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + + //Test + mockBot.Verify(b => b.PrivateReply(It.IsAny(), It.Is(what => what == string.Format("{0} has a new voicemail for you. There are {1} in total", Jibbr, 3)))); + } + [Fact] public void WillNotifyNewlyArrivedUsersOfVoicemails() { //Setup const string newlyArrivedUser = "James"; - const string voicemailContents = "Some message"; - const string jibbr = "jibbr"; - - var mockBot = new Mock(); - mockBot.Setup(b => b.Name).Returns(jibbr); - var bot = mockBot.Object; - var voicemailSprocket = new VoicemailSprocket.VoicemailSprocket(); - voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", voicemailContents), "Jim", bot.Name), bot); + voicemailSprocket = new VoicemailSprocket.VoicemailSprocket(); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); //Act voicemailSprocket.Handle(new ChatMessage("[JABBR] - " + newlyArrivedUser + " just entered " + "TestRoom", newlyArrivedUser, bot.Name), bot); //Test - mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("{0} has {1} new voicemail for you", jibbr, "1")))); + mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("{0} has {1} new voicemail for you", Jibbr, "1")))); } } } diff --git a/VoicemailSprocket/UserRegistry.cs b/VoicemailSprocket/UserRegistry.cs index f9d002d..76925ef 100644 --- a/VoicemailSprocket/UserRegistry.cs +++ b/VoicemailSprocket/UserRegistry.cs @@ -35,7 +35,7 @@ protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) } private void NotifyOfWaitingVoiceMails(string username, IBot bot) { - bot.PrivateReply(username, string.Format("{0} has {1} new voicemail for you", bot.Name, voicemailRecorder.MessageCount)); + bot.PrivateReply(username, string.Format("{0} has {1} new voicemail for you", bot.Name, voicemailRecorder.VoicemailCount)); } private static string ExtractUsernameFromMessage(ChatMessage message) diff --git a/VoicemailSprocket/VoicemailRecorder.cs b/VoicemailSprocket/VoicemailRecorder.cs index 119e14c..00bc84f 100644 --- a/VoicemailSprocket/VoicemailRecorder.cs +++ b/VoicemailSprocket/VoicemailRecorder.cs @@ -16,7 +16,7 @@ public override Regex Pattern get { return new Regex(RecordCommand);} } - public int MessageCount { get { return Voicemails.Count; } } + public int VoicemailCount { get { return Voicemails.Count; } } protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) { From 16b8a25bd0739f3685394a2dcc1e3f1f2ac97642 Mon Sep 17 00:00:00 2001 From: jimmyp Date: Sun, 21 Oct 2012 17:23:08 +1100 Subject: [PATCH 06/12] Implemented notification of all current users of new voicemails --- Tests/ExtensionTests/VoicemailSprocketTest.cs | 26 ++++++++++++++----- VoicemailSprocket/VoicemailRecorder.cs | 23 ++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index 3d02c79..e465370 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Dynamic; using System.Linq; using System.Text; using Jabbot; @@ -26,17 +27,18 @@ public VoicemailSprocketTest() mockBot = new Mock(); mockBot.Setup(b => b.Name).Returns(Jibbr); bot = mockBot.Object; + + dynamic room1 = new DynamicRoom() { Name = "room1", Users = new List { new DynamicUser() { Name = "claire" }, new DynamicUser() { Name = "mads" } } }; + dynamic room2 = new DynamicRoom() { Name = "room2", Users = new List { new DynamicUser() { Name = "claire" }, new DynamicUser() { Name = "bryce" }, new DynamicUser() { Name = "vicky" } } }; + mockBot.Setup(b => b.GetRooms()).Returns(new List() { room1, room2 }); + + voicemailSprocket = new VoicemailSprocket.VoicemailSprocket(); } [Fact] public void CanNotifyAllUsersOfNewVoicemails() { //Setup - mockBot.Setup(b => b.GetRooms()).Returns(new List() - { - new {Name = "room1", Users = new {Name = "claire, mads"}}, - new {Name = "room2", Users = new {Name = "claire, bryce, vicky"}} - }); voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); @@ -44,7 +46,7 @@ public void CanNotifyAllUsersOfNewVoicemails() voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); //Test - mockBot.Verify(b => b.PrivateReply(It.IsAny(), It.Is(what => what == string.Format("{0} has a new voicemail for you. There are {1} in total", Jibbr, 3)))); + mockBot.Verify(b => b.PrivateReply(It.IsAny(), It.Is(what => what == string.Format("{0} has a new voicemail for you. There are {1} in total", Jibbr, 3))), Times.Exactly(4)); } [Fact] @@ -63,4 +65,16 @@ public void WillNotifyNewlyArrivedUsersOfVoicemails() mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("{0} has {1} new voicemail for you", Jibbr, "1")))); } } + + public class DynamicUser : DynamicObject + { + public string Name { get; set; } + } + + public class DynamicRoom : DynamicObject + { + public List Users { get; set; } + + public string Name { get; set; } + } } diff --git a/VoicemailSprocket/VoicemailRecorder.cs b/VoicemailSprocket/VoicemailRecorder.cs index 00bc84f..0a7be82 100644 --- a/VoicemailSprocket/VoicemailRecorder.cs +++ b/VoicemailSprocket/VoicemailRecorder.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Text.RegularExpressions; using Jabbot; using Jabbot.Models; @@ -21,6 +22,28 @@ public override Regex Pattern protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) { Voicemails.Add(message.Content.Split('\'')[1]); + NotifyAllUsers(bot); + } + + private void NotifyAllUsers(IBot bot) + { + var allUsersInSameRoomsAsJibber = GetAllUsersInSameRoomsAsJibber(bot); + foreach (var user in allUsersInSameRoomsAsJibber) + bot.PrivateReply(user, string.Format("{0} has a new voicemail for you. There are {1} in total", bot.Name, Voicemails.Count)); + } + + private static IEnumerable GetAllUsersInSameRoomsAsJibber(IBot bot) + { + var rooms = GetRooms(bot); + var users = rooms.SelectMany(r => r.Users); + var userNames = users.Select(u => u.Name); + var distinctUserNames = userNames.Distinct().Cast(); + return distinctUserNames; + } + + private static IEnumerable GetRooms(IBot bot) + { + return bot.GetRooms(); } private void SendVoiceMail(string recipient, IBot bot) From 4146d8c9679519bd346684440040704b17195c1c Mon Sep 17 00:00:00 2001 From: jimmyp Date: Sun, 21 Oct 2012 17:42:53 +1100 Subject: [PATCH 07/12] Add failing test for retriving voicemails --- Tests/ExtensionTests/VoicemailSprocketTest.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index e465370..54f44a7 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -64,6 +64,23 @@ public void WillNotifyNewlyArrivedUsersOfVoicemails() //Test mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("{0} has {1} new voicemail for you", Jibbr, "1")))); } + + [Fact] + public void CanRetrieveVoicemails() + { + //Setup + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + + //Act + voicemailSprocket.Handle(new ChatMessage("retrieve", "Jim", bot.Name), bot); + + //Test + mockBot.Verify(b => b.PrivateReply(It.Is(s => s == "Jim"), string.Format( + @"@Jim said: {0}", ExampleContents)), Times.Exactly(3)); + + } } public class DynamicUser : DynamicObject From 3d45f8bee4c24218dfe7a30cd0b9341335be95c6 Mon Sep 17 00:00:00 2001 From: jimmyp Date: Sun, 21 Oct 2012 17:42:53 +1100 Subject: [PATCH 08/12] Add failing test for retriving voicemails --- Tests/ExtensionTests/VoicemailSprocketTest.cs | 16 +++++++++ VoicemailSprocket/VoicemailRecorder.cs | 6 ---- VoicemailSprocket/VoicemailRetriever.cs | 33 +++++++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 VoicemailSprocket/VoicemailRetriever.cs diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index e465370..796cb5e 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -64,6 +64,22 @@ public void WillNotifyNewlyArrivedUsersOfVoicemails() //Test mockBot.Verify(b => b.PrivateReply(newlyArrivedUser, It.Is(what => what == string.Format("{0} has {1} new voicemail for you", Jibbr, "1")))); } + + [Fact] + public void CanRetrieveVoicemails() + { + //Setup + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + + //Act + voicemailSprocket.Handle(new ChatMessage("retrieve", "Jim", bot.Name), bot); + + //Test + mockBot.Verify(b => b.PrivateReply(It.Is(s => s == "Jim"), string.Format(@"@Jim said: {0}", ExampleContents)), Times.Exactly(3)); + + } } public class DynamicUser : DynamicObject diff --git a/VoicemailSprocket/VoicemailRecorder.cs b/VoicemailSprocket/VoicemailRecorder.cs index 0a7be82..d1e122d 100644 --- a/VoicemailSprocket/VoicemailRecorder.cs +++ b/VoicemailSprocket/VoicemailRecorder.cs @@ -45,11 +45,5 @@ private static IEnumerable GetRooms(IBot bot) { return bot.GetRooms(); } - - private void SendVoiceMail(string recipient, IBot bot) - { - foreach (var voicemail in Voicemails) - bot.PrivateReply(recipient, string.Format("@{0} said '{1}'", recipient, voicemail)); - } } } \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailRetriever.cs b/VoicemailSprocket/VoicemailRetriever.cs new file mode 100644 index 0000000..c5cac13 --- /dev/null +++ b/VoicemailSprocket/VoicemailRetriever.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using Jabbot; +using Jabbot.Models; +using Jabbot.Sprockets; + +namespace VoicemailSprocket +{ + internal class VoicemailRetriever : RegexSprocket + { + internal const string RetrieveCommand = "retrieve"; + public IList Voicemails = new List(); + + public override Regex Pattern + { + get { return new Regex(RetrieveCommand);} + } + + public int VoicemailCount { get { return Voicemails.Count; } } + + protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) + { + SendAllVoicemailsToUser() + } + + private void SendAllVoicemailsToUser(string recipient, IBot bot) + { + foreach (var voicemail in Voicemails) + bot.PrivateReply(recipient, string.Format("@{0} said '{1}'", recipient, voicemail)); + } + } +} \ No newline at end of file From 96b53b643c53d04183dc22eebde93653d42e5a51 Mon Sep 17 00:00:00 2001 From: jimmyp Date: Sun, 21 Oct 2012 18:35:36 +1100 Subject: [PATCH 09/12] Implemented retrieving voicemails --- Tests/ExtensionTests/VoicemailSprocketTest.cs | 4 ++-- VoicemailSprocket/Voicemail.cs | 9 +++++++++ VoicemailSprocket/VoicemailRecorder.cs | 4 ++-- VoicemailSprocket/VoicemailRetriever.cs | 14 +++++++++++--- VoicemailSprocket/VoicemailSprocket.cs | 5 ++++- VoicemailSprocket/VoicemailSprocket.csproj | 2 ++ 6 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 VoicemailSprocket/Voicemail.cs diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index 796cb5e..30c49bb 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -74,10 +74,10 @@ public void CanRetrieveVoicemails() voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); //Act - voicemailSprocket.Handle(new ChatMessage("retrieve", "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage("retrieve", "claire", bot.Name), bot); //Test - mockBot.Verify(b => b.PrivateReply(It.Is(s => s == "Jim"), string.Format(@"@Jim said: {0}", ExampleContents)), Times.Exactly(3)); + mockBot.Verify(b => b.PrivateReply(It.Is(s => s == "claire"), string.Format(@"Jim said '{0}'", ExampleContents)), Times.Exactly(3)); } } diff --git a/VoicemailSprocket/Voicemail.cs b/VoicemailSprocket/Voicemail.cs new file mode 100644 index 0000000..22a8300 --- /dev/null +++ b/VoicemailSprocket/Voicemail.cs @@ -0,0 +1,9 @@ +namespace VoicemailSprocket +{ + internal class Voicemail + { + public string Sender { get; set; } + + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailRecorder.cs b/VoicemailSprocket/VoicemailRecorder.cs index d1e122d..a1dd657 100644 --- a/VoicemailSprocket/VoicemailRecorder.cs +++ b/VoicemailSprocket/VoicemailRecorder.cs @@ -10,7 +10,7 @@ namespace VoicemailSprocket internal class VoicemailRecorder : RegexSprocket { internal const string RecordCommand = "record \'.*\'"; - public IList Voicemails = new List(); + public IList Voicemails = new List(); public override Regex Pattern { @@ -21,7 +21,7 @@ public override Regex Pattern protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) { - Voicemails.Add(message.Content.Split('\'')[1]); + Voicemails.Add(new Voicemail(){Sender = message.Sender, Message = message.Content.Split('\'')[1]}); NotifyAllUsers(bot); } diff --git a/VoicemailSprocket/VoicemailRetriever.cs b/VoicemailSprocket/VoicemailRetriever.cs index c5cac13..6fb597d 100644 --- a/VoicemailSprocket/VoicemailRetriever.cs +++ b/VoicemailSprocket/VoicemailRetriever.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; @@ -9,9 +10,16 @@ namespace VoicemailSprocket { internal class VoicemailRetriever : RegexSprocket { + private readonly VoicemailRecorder voicemailRecorder; internal const string RetrieveCommand = "retrieve"; public IList Voicemails = new List(); + public VoicemailRetriever(VoicemailRecorder voicemailRecorder) + { + if (voicemailRecorder == null) throw new ArgumentNullException("voicemailRecorder"); + this.voicemailRecorder = voicemailRecorder; + } + public override Regex Pattern { get { return new Regex(RetrieveCommand);} @@ -21,13 +29,13 @@ public override Regex Pattern protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) { - SendAllVoicemailsToUser() + SendAllVoicemailsToUser(message.Sender, bot); } private void SendAllVoicemailsToUser(string recipient, IBot bot) { - foreach (var voicemail in Voicemails) - bot.PrivateReply(recipient, string.Format("@{0} said '{1}'", recipient, voicemail)); + foreach (var voicemail in voicemailRecorder.Voicemails) + bot.PrivateReply(recipient, string.Format("{0} said '{1}'", voicemail.Sender, voicemail.Message)); } } } \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailSprocket.cs b/VoicemailSprocket/VoicemailSprocket.cs index 303a5dc..5b0f868 100644 --- a/VoicemailSprocket/VoicemailSprocket.cs +++ b/VoicemailSprocket/VoicemailSprocket.cs @@ -9,16 +9,19 @@ public class VoicemailSprocket : ISprocket public VoicemailSprocket() { voicemailRecorder = new VoicemailRecorder(); + voicemailRetriever = new VoicemailRetriever(voicemailRecorder); userRegistry = new UserRegistry(voicemailRecorder); } private readonly VoicemailRecorder voicemailRecorder; private readonly UserRegistry userRegistry; + private readonly VoicemailRetriever voicemailRetriever; public bool Handle(ChatMessage message, IBot bot) { return userRegistry.Handle(message, bot) || - voicemailRecorder.Handle(message, bot); + voicemailRecorder.Handle(message, bot) || + voicemailRetriever.Handle(message, bot); } } } \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailSprocket.csproj b/VoicemailSprocket/VoicemailSprocket.csproj index 5320693..7ed6b64 100644 --- a/VoicemailSprocket/VoicemailSprocket.csproj +++ b/VoicemailSprocket/VoicemailSprocket.csproj @@ -40,6 +40,8 @@ + + From 0e8be93c6425a674e3c830508ba4e0b7f6958a6b Mon Sep 17 00:00:00 2001 From: jimmyp Date: Tue, 23 Oct 2012 22:25:24 +1100 Subject: [PATCH 10/12] Added test and implementation for clearing voicemails --- Tests/ExtensionTests/VoicemailSprocketTest.cs | 37 +++++++++---- VoicemailSprocket/VoicemailRecorder.cs | 54 ++++++++++++++----- VoicemailSprocket/VoicemailSprocket.csproj | 4 ++ 3 files changed, 72 insertions(+), 23 deletions(-) diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index 30c49bb..13ce242 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -28,8 +28,8 @@ public VoicemailSprocketTest() mockBot.Setup(b => b.Name).Returns(Jibbr); bot = mockBot.Object; - dynamic room1 = new DynamicRoom() { Name = "room1", Users = new List { new DynamicUser() { Name = "claire" }, new DynamicUser() { Name = "mads" } } }; - dynamic room2 = new DynamicRoom() { Name = "room2", Users = new List { new DynamicUser() { Name = "claire" }, new DynamicUser() { Name = "bryce" }, new DynamicUser() { Name = "vicky" } } }; + dynamic room1 = new DynamicRoom() { Name = "room1", Users = new List { new DynamicUser() { Name = "Claire" }, new DynamicUser() { Name = "mads" } } }; + dynamic room2 = new DynamicRoom() { Name = "room2", Users = new List { new DynamicUser() { Name = "Claire" }, new DynamicUser() { Name = "bryce" }, new DynamicUser() { Name = "vicky" } } }; mockBot.Setup(b => b.GetRooms()).Returns(new List() { room1, room2 }); voicemailSprocket = new VoicemailSprocket.VoicemailSprocket(); @@ -39,11 +39,11 @@ public VoicemailSprocketTest() public void CanNotifyAllUsersOfNewVoicemails() { //Setup - voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); - voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); //Act - voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); //Test mockBot.Verify(b => b.PrivateReply(It.IsAny(), It.Is(what => what == string.Format("{0} has a new voicemail for you. There are {1} in total", Jibbr, 3))), Times.Exactly(4)); @@ -56,7 +56,7 @@ public void WillNotifyNewlyArrivedUsersOfVoicemails() const string newlyArrivedUser = "James"; voicemailSprocket = new VoicemailSprocket.VoicemailSprocket(); - voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); //Act voicemailSprocket.Handle(new ChatMessage("[JABBR] - " + newlyArrivedUser + " just entered " + "TestRoom", newlyArrivedUser, bot.Name), bot); @@ -69,16 +69,31 @@ public void WillNotifyNewlyArrivedUsersOfVoicemails() public void CanRetrieveVoicemails() { //Setup - voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); - voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); - voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); //Act - voicemailSprocket.Handle(new ChatMessage("retrieve", "claire", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage("retrieve", "Claire", bot.Name), bot); //Test - mockBot.Verify(b => b.PrivateReply(It.Is(s => s == "claire"), string.Format(@"Jim said '{0}'", ExampleContents)), Times.Exactly(3)); + mockBot.Verify(b => b.PrivateReply(It.Is(s => s == "Claire"), string.Format(@"Jim said '{0}'", ExampleContents)), Times.Exactly(3)); + } + + [Fact] + public void CanClearYourOwnVoicemails() + { + //Setup + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Claire", bot.Name), bot); + + //Act + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail clear", ExampleContents), "Jim", bot.Name), bot); + //Test + voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail retrieve", ExampleContents), "Giselle", bot.Name), bot); + mockBot.Verify(b => b.PrivateReply(It.Is(s => s == "Giselle"), string.Format(@"Claire said '{0}'", ExampleContents)), Times.Exactly(1)); } } diff --git a/VoicemailSprocket/VoicemailRecorder.cs b/VoicemailSprocket/VoicemailRecorder.cs index a1dd657..666fb16 100644 --- a/VoicemailSprocket/VoicemailRecorder.cs +++ b/VoicemailSprocket/VoicemailRecorder.cs @@ -1,28 +1,58 @@ using System.Collections.Generic; using System.Linq; -using System.Text.RegularExpressions; using Jabbot; -using Jabbot.Models; -using Jabbot.Sprockets; +using Jabbot.CommandSprockets; namespace VoicemailSprocket { - internal class VoicemailRecorder : RegexSprocket + internal class VoicemailRecorder : CommandSprocket { - internal const string RecordCommand = "record \'.*\'"; public IList Voicemails = new List(); + + public int VoicemailCount { get { return Voicemails.Count; } } + + public override IEnumerable SupportedInitiators + { + get { yield return "voicemail"; } + } - public override Regex Pattern + public override IEnumerable SupportedCommands { - get { return new Regex(RecordCommand);} + get + { + yield return "record"; + yield return "clear"; + } } - public int VoicemailCount { get { return Voicemails.Count; } } + public override bool ExecuteCommand() + { + switch (Command) + { + case "clear": + return ClearAllMessages(); + break; + case "record": + return RecordMessage(); + break; + default: + return false; + } + } - protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) + private bool ClearAllMessages() { - Voicemails.Add(new Voicemail(){Sender = message.Sender, Message = message.Content.Split('\'')[1]}); - NotifyAllUsers(bot); + Voicemails = Voicemails.Where(v => v.Sender != Message.Sender).ToList(); + + return true; + } + + private bool RecordMessage() + { + Voicemails.Add(new Voicemail() { Sender = Message.Sender, Message = Message.Content.Split('\'')[1] }); + NotifyAllUsers(Bot); + + return true; } private void NotifyAllUsers(IBot bot) @@ -39,7 +69,7 @@ private static IEnumerable GetAllUsersInSameRoomsAsJibber(IBot bot) var userNames = users.Select(u => u.Name); var distinctUserNames = userNames.Distinct().Cast(); return distinctUserNames; - } + } private static IEnumerable GetRooms(IBot bot) { diff --git a/VoicemailSprocket/VoicemailSprocket.csproj b/VoicemailSprocket/VoicemailSprocket.csproj index 7ed6b64..14897c9 100644 --- a/VoicemailSprocket/VoicemailSprocket.csproj +++ b/VoicemailSprocket/VoicemailSprocket.csproj @@ -48,6 +48,10 @@ + + {FB5CE3F1-1575-440B-A6E9-4E5AFED35D8B} + Jabbot.CommandSprockets + {478BFCF7-9397-49A7-AFD4-060B6B749E77} Jabbot From 76a04aee0ad731a3325979c6e10cc9c4d00afb56 Mon Sep 17 00:00:00 2001 From: jimmyp Date: Tue, 23 Oct 2012 22:42:42 +1100 Subject: [PATCH 11/12] Just a few final touches --- Tests/ExtensionTests/VoicemailSprocketTest.cs | 8 +--- VoicemailSprocket/UserRegistry.cs | 2 +- VoicemailSprocket/Voicemail.cs | 4 +- VoicemailSprocket/VoicemailRecorder.cs | 23 +++++++---- VoicemailSprocket/VoicemailRetriever.cs | 41 ------------------- VoicemailSprocket/VoicemailSprocket.cs | 5 +-- VoicemailSprocket/VoicemailSprocket.csproj | 1 - 7 files changed, 22 insertions(+), 62 deletions(-) delete mode 100644 VoicemailSprocket/VoicemailRetriever.cs diff --git a/Tests/ExtensionTests/VoicemailSprocketTest.cs b/Tests/ExtensionTests/VoicemailSprocketTest.cs index 13ce242..58a8e0d 100644 --- a/Tests/ExtensionTests/VoicemailSprocketTest.cs +++ b/Tests/ExtensionTests/VoicemailSprocketTest.cs @@ -1,10 +1,6 @@ -using System.Collections; -using System.Collections.Generic; +using System.Collections.Generic; using System.Dynamic; -using System.Linq; -using System.Text; using Jabbot; -using Jabbot.CommandSprockets; using Jabbot.Models; using Moq; using Xunit; @@ -74,7 +70,7 @@ public void CanRetrieveVoicemails() voicemailSprocket.Handle(new ChatMessage(string.Format("{0} '{1}'", "voicemail record", ExampleContents), "Jim", bot.Name), bot); //Act - voicemailSprocket.Handle(new ChatMessage("retrieve", "Claire", bot.Name), bot); + voicemailSprocket.Handle(new ChatMessage("voicemail retrieve", "Claire", bot.Name), bot); //Test mockBot.Verify(b => b.PrivateReply(It.Is(s => s == "Claire"), string.Format(@"Jim said '{0}'", ExampleContents)), Times.Exactly(3)); diff --git a/VoicemailSprocket/UserRegistry.cs b/VoicemailSprocket/UserRegistry.cs index 76925ef..f96e27a 100644 --- a/VoicemailSprocket/UserRegistry.cs +++ b/VoicemailSprocket/UserRegistry.cs @@ -9,7 +9,7 @@ namespace VoicemailSprocket { internal class UserRegistry : RegexSprocket { - public const string UserArrivedNotification = @"\[JABBR\] - .* just entered .*"; + private const string UserArrivedNotification = @"\[JABBR\] - .* just entered .*"; private readonly VoicemailRecorder voicemailRecorder; private readonly IList userNames = new List(); diff --git a/VoicemailSprocket/Voicemail.cs b/VoicemailSprocket/Voicemail.cs index 22a8300..0d7fe55 100644 --- a/VoicemailSprocket/Voicemail.cs +++ b/VoicemailSprocket/Voicemail.cs @@ -2,8 +2,8 @@ namespace VoicemailSprocket { internal class Voicemail { - public string Sender { get; set; } + internal string Sender { get; set; } - public string Message { get; set; } + internal string Message { get; set; } } } \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailRecorder.cs b/VoicemailSprocket/VoicemailRecorder.cs index 666fb16..a92b868 100644 --- a/VoicemailSprocket/VoicemailRecorder.cs +++ b/VoicemailSprocket/VoicemailRecorder.cs @@ -7,9 +7,9 @@ namespace VoicemailSprocket { internal class VoicemailRecorder : CommandSprocket { - public IList Voicemails = new List(); + private IList voicemails = new List(); - public int VoicemailCount { get { return Voicemails.Count; } } + public int VoicemailCount { get { return voicemails.Count; } } public override IEnumerable SupportedInitiators { @@ -22,6 +22,7 @@ public override IEnumerable SupportedCommands { yield return "record"; yield return "clear"; + yield return "retrieve"; } } @@ -31,25 +32,33 @@ public override bool ExecuteCommand() { case "clear": return ClearAllMessages(); - break; case "record": return RecordMessage(); - break; + case "retrieve": + return Retrieve(); default: return false; } } + private bool Retrieve() + { + foreach (var voicemail in voicemails) + Bot.PrivateReply(Message.Sender, string.Format("{0} said '{1}'", voicemail.Sender, voicemail.Message)); + + return false; + } + private bool ClearAllMessages() { - Voicemails = Voicemails.Where(v => v.Sender != Message.Sender).ToList(); + voicemails = voicemails.Where(v => v.Sender != Message.Sender).ToList(); return true; } private bool RecordMessage() { - Voicemails.Add(new Voicemail() { Sender = Message.Sender, Message = Message.Content.Split('\'')[1] }); + voicemails.Add(new Voicemail() { Sender = Message.Sender, Message = Message.Content.Split('\'')[1] }); NotifyAllUsers(Bot); return true; @@ -59,7 +68,7 @@ private void NotifyAllUsers(IBot bot) { var allUsersInSameRoomsAsJibber = GetAllUsersInSameRoomsAsJibber(bot); foreach (var user in allUsersInSameRoomsAsJibber) - bot.PrivateReply(user, string.Format("{0} has a new voicemail for you. There are {1} in total", bot.Name, Voicemails.Count)); + bot.PrivateReply(user, string.Format("{0} has a new voicemail for you. There are {1} in total", bot.Name, voicemails.Count)); } private static IEnumerable GetAllUsersInSameRoomsAsJibber(IBot bot) diff --git a/VoicemailSprocket/VoicemailRetriever.cs b/VoicemailSprocket/VoicemailRetriever.cs deleted file mode 100644 index 6fb597d..0000000 --- a/VoicemailSprocket/VoicemailRetriever.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text.RegularExpressions; -using Jabbot; -using Jabbot.Models; -using Jabbot.Sprockets; - -namespace VoicemailSprocket -{ - internal class VoicemailRetriever : RegexSprocket - { - private readonly VoicemailRecorder voicemailRecorder; - internal const string RetrieveCommand = "retrieve"; - public IList Voicemails = new List(); - - public VoicemailRetriever(VoicemailRecorder voicemailRecorder) - { - if (voicemailRecorder == null) throw new ArgumentNullException("voicemailRecorder"); - this.voicemailRecorder = voicemailRecorder; - } - - public override Regex Pattern - { - get { return new Regex(RetrieveCommand);} - } - - public int VoicemailCount { get { return Voicemails.Count; } } - - protected override void ProcessMatch(Match match, ChatMessage message, IBot bot) - { - SendAllVoicemailsToUser(message.Sender, bot); - } - - private void SendAllVoicemailsToUser(string recipient, IBot bot) - { - foreach (var voicemail in voicemailRecorder.Voicemails) - bot.PrivateReply(recipient, string.Format("{0} said '{1}'", voicemail.Sender, voicemail.Message)); - } - } -} \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailSprocket.cs b/VoicemailSprocket/VoicemailSprocket.cs index 5b0f868..303a5dc 100644 --- a/VoicemailSprocket/VoicemailSprocket.cs +++ b/VoicemailSprocket/VoicemailSprocket.cs @@ -9,19 +9,16 @@ public class VoicemailSprocket : ISprocket public VoicemailSprocket() { voicemailRecorder = new VoicemailRecorder(); - voicemailRetriever = new VoicemailRetriever(voicemailRecorder); userRegistry = new UserRegistry(voicemailRecorder); } private readonly VoicemailRecorder voicemailRecorder; private readonly UserRegistry userRegistry; - private readonly VoicemailRetriever voicemailRetriever; public bool Handle(ChatMessage message, IBot bot) { return userRegistry.Handle(message, bot) || - voicemailRecorder.Handle(message, bot) || - voicemailRetriever.Handle(message, bot); + voicemailRecorder.Handle(message, bot); } } } \ No newline at end of file diff --git a/VoicemailSprocket/VoicemailSprocket.csproj b/VoicemailSprocket/VoicemailSprocket.csproj index 14897c9..248b63f 100644 --- a/VoicemailSprocket/VoicemailSprocket.csproj +++ b/VoicemailSprocket/VoicemailSprocket.csproj @@ -41,7 +41,6 @@ - From e0e744eb5e9b89bd21b12fdc3eed9b3e84bc22a8 Mon Sep 17 00:00:00 2001 From: jimmyp Date: Tue, 23 Oct 2012 23:00:45 +1100 Subject: [PATCH 12/12] Added help/info to voicemails --- VoicemailSprocket/VoicemailRecorder.cs | 32 +++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/VoicemailSprocket/VoicemailRecorder.cs b/VoicemailSprocket/VoicemailRecorder.cs index a92b868..f4fea60 100644 --- a/VoicemailSprocket/VoicemailRecorder.cs +++ b/VoicemailSprocket/VoicemailRecorder.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using Jabbot; @@ -8,7 +9,19 @@ namespace VoicemailSprocket internal class VoicemailRecorder : CommandSprocket { private IList voicemails = new List(); - + + private readonly string _helpInfo; + + public VoicemailRecorder() + { + _helpInfo = "Hi {0}," + Environment.NewLine + Environment.NewLine + + "I accept the following commands:" + Environment.NewLine + + "info/help:\t" + "this message" + Environment.NewLine + + "record:\t" + "accepts a message to record" + Environment.NewLine + + "clear:\t" + "clears all voicemails you have recorded" + Environment.NewLine + + "retrieve:\t" + "retrieves any voicemails left by any other users" + Environment.NewLine; + } + public int VoicemailCount { get { return voicemails.Count; } } public override IEnumerable SupportedInitiators @@ -18,8 +31,10 @@ public override IEnumerable SupportedInitiators public override IEnumerable SupportedCommands { - get - { + get + { + yield return "info"; + yield return "help"; yield return "record"; yield return "clear"; yield return "retrieve"; @@ -30,6 +45,9 @@ public override bool ExecuteCommand() { switch (Command) { + case "info": + case "help": + return ShowInfo(); case "clear": return ClearAllMessages(); case "record": @@ -41,6 +59,14 @@ public override bool ExecuteCommand() } } + private bool ShowInfo() + { + Bot.PrivateReply(Message.Sender, string.Format(_helpInfo, Message.Sender)); + + return true; + + } + private bool Retrieve() { foreach (var voicemail in voicemails)