From 1ca3335fc0ace8198ebc9a76dde6c0ee54d494f9 Mon Sep 17 00:00:00 2001 From: Antonio Zhu Date: Fri, 2 Dec 2022 15:06:38 +0100 Subject: [PATCH] Added NLog and updated msg type for RocketMessage to changed --- .../Rocket.Chat.Net.Bot.csproj | 6 +- src/Rocket.Chat.Net.Bot/RocketChatBot.cs | 5 +- .../HelloWorldResponse.cs | 25 ++++++ .../Rocket.Chat.Net.Example.csproj | 6 ++ src/Rocket.Chat.Net.Example/packages.config | 1 + src/Rocket.Chat.Net/Driver/DdpClient.cs | 21 ++++- .../Driver/RocketChatDriver.cs | 15 ++-- src/Rocket.Chat.Net/Interfaces/ILogger.cs | 15 ---- src/Rocket.Chat.Net/Loggers/DummyLogger.cs | 27 ------ src/Rocket.Chat.Net/Rocket.Chat.Net.csproj | 10 +++ .../Rocket.Chat.Net.Bot.Tests.csproj | 5 ++ .../RocketChatBotFacts.cs | 2 +- .../Rocket.Chat.Net.Bot.Tests/packages.config | 1 + test/Rocket.Chat.Net.Tests/Driver/DdpFacts.cs | 4 +- .../Driver/MessagingFacts.cs | 2 +- test/Rocket.Chat.Net.Tests/Helpers/ILogger.cs | 6 ++ .../Helpers/XUnitLogger.cs | 1 + .../Rocket.Chat.Net.Tests.csproj | 85 ++++++++++++++++++- test/Rocket.Chat.Net.Tests/packages.config | 20 ++++- 19 files changed, 191 insertions(+), 66 deletions(-) create mode 100644 src/Rocket.Chat.Net.Example/HelloWorldResponse.cs delete mode 100644 src/Rocket.Chat.Net/Interfaces/ILogger.cs delete mode 100644 src/Rocket.Chat.Net/Loggers/DummyLogger.cs create mode 100644 test/Rocket.Chat.Net.Tests/Helpers/ILogger.cs diff --git a/src/Rocket.Chat.Net.Bot/Rocket.Chat.Net.Bot.csproj b/src/Rocket.Chat.Net.Bot/Rocket.Chat.Net.Bot.csproj index 7bae097..a393af5 100644 --- a/src/Rocket.Chat.Net.Bot/Rocket.Chat.Net.Bot.csproj +++ b/src/Rocket.Chat.Net.Bot/Rocket.Chat.Net.Bot.csproj @@ -1,9 +1,13 @@ - + netstandard2.0 + + + + diff --git a/src/Rocket.Chat.Net.Bot/RocketChatBot.cs b/src/Rocket.Chat.Net.Bot/RocketChatBot.cs index 84945e1..b7a888f 100644 --- a/src/Rocket.Chat.Net.Bot/RocketChatBot.cs +++ b/src/Rocket.Chat.Net.Bot/RocketChatBot.cs @@ -3,12 +3,11 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; - + using NLog; using Rocket.Chat.Net.Bot.Interfaces; using Rocket.Chat.Net.Bot.Models; using Rocket.Chat.Net.Driver; using Rocket.Chat.Net.Interfaces; - using Rocket.Chat.Net.Loggers; using Rocket.Chat.Net.Models; public class RocketChatBot : IDisposable @@ -23,7 +22,7 @@ public class RocketChatBot : IDisposable public RocketChatBot(IRocketChatDriver driver, ILogger logger) { Driver = driver; - _logger = logger ?? new DummyLogger(); + _logger = logger ?? NLog.LogManager.CreateNullLogger(); Driver.MessageReceived += DriverOnMessageReceived; Driver.DdpReconnect += DriverOnDdpReconnect; diff --git a/src/Rocket.Chat.Net.Example/HelloWorldResponse.cs b/src/Rocket.Chat.Net.Example/HelloWorldResponse.cs new file mode 100644 index 0000000..4ab171e --- /dev/null +++ b/src/Rocket.Chat.Net.Example/HelloWorldResponse.cs @@ -0,0 +1,25 @@ +using Rocket.Chat.Net.Bot; +using Rocket.Chat.Net.Bot.Interfaces; +using Rocket.Chat.Net.Bot.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rocket.Chat.Net.Example +{ + public class HelloWorldResponse : IBotResponse + { + public bool CanRespond(ResponseContext context) + { + return ! context.Message.IsFromMyself && context.Message.Message.ToLower().StartsWith("hello"); + } + + public IEnumerable GetResponse(ResponseContext context, RocketChatBot caller) + { + var message = context.Message; + yield return new BasicResponse("Hello world!", context.Message.RoomId); + } + } +} diff --git a/src/Rocket.Chat.Net.Example/Rocket.Chat.Net.Example.csproj b/src/Rocket.Chat.Net.Example/Rocket.Chat.Net.Example.csproj index b662280..d910d6f 100644 --- a/src/Rocket.Chat.Net.Example/Rocket.Chat.Net.Example.csproj +++ b/src/Rocket.Chat.Net.Example/Rocket.Chat.Net.Example.csproj @@ -51,6 +51,9 @@ ..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + ..\..\packages\NLog.5.1.0\lib\net46\NLog.dll + ..\..\packages\RestSharp.105.2.3\lib\net45\RestSharp.dll True @@ -65,12 +68,14 @@ True + ..\..\packages\System.IO.4.3.0\lib\net462\System.IO.dll True True + ..\..\packages\System.Linq.4.3.0\lib\net463\System.Linq.dll True @@ -144,6 +149,7 @@ + diff --git a/src/Rocket.Chat.Net.Example/packages.config b/src/Rocket.Chat.Net.Example/packages.config index ba90b54..ea358ee 100644 --- a/src/Rocket.Chat.Net.Example/packages.config +++ b/src/Rocket.Chat.Net.Example/packages.config @@ -2,6 +2,7 @@ + diff --git a/src/Rocket.Chat.Net/Driver/DdpClient.cs b/src/Rocket.Chat.Net/Driver/DdpClient.cs index dd4d958..e79a24a 100644 --- a/src/Rocket.Chat.Net/Driver/DdpClient.cs +++ b/src/Rocket.Chat.Net/Driver/DdpClient.cs @@ -15,6 +15,7 @@ using WebSocket4Net; using SuperSocket.ClientEngine; using System.Security.Authentication; + using NLog; public class DdpClient : IDdpClient { @@ -61,7 +62,15 @@ private void SocketOnClosed(object sender, EventArgs eventArgs) _logger.Debug("CLOSE"); if (SessionId != null && !IsDisposed) { - ConnectAsync(CancellationToken.None).Wait(); + // TODO: Fix reconnect + try + { + ConnectAsync(CancellationToken.None).Wait(); + } + catch (Exception ex) + { + _logger.Error(ex); + } } } @@ -70,10 +79,14 @@ private void SocketOnError(object sender, ErrorEventArgs errorEventArgs) _logger.Info("ERROR: " + errorEventArgs?.Exception?.Message); } - private void SocketOnOpened(object sender, EventArgs eventArgs) + private async void SocketOnOpened(object sender, EventArgs eventArgs) { _logger.Debug("OPEN"); + await SendConnectRequest().ConfigureAwait(false); + } + public async Task SendConnectRequest() + { _logger.Debug("Sending connection request"); const string ddpVersion = "1"; var request = new @@ -87,7 +100,7 @@ private void SocketOnOpened(object sender, EventArgs eventArgs) } }; - SendObjectAsync(request, CancellationToken.None).Wait(); + await SendObjectAsync(request, CancellationToken.None).ConfigureAwait(false); } // TODO: Real time API implementieren @@ -198,7 +211,7 @@ public async Task SubscribeAndWaitAsync(string name, CancellationToken t }; await SendObjectAsync(request, token).ConfigureAwait(false); - await WaitForIdOrReadyAsync(id, token).ConfigureAwait(false); + JObject result = await WaitForIdOrReadyAsync(id, token).ConfigureAwait(false); return id; } diff --git a/src/Rocket.Chat.Net/Driver/RocketChatDriver.cs b/src/Rocket.Chat.Net/Driver/RocketChatDriver.cs index 1db476c..a29a268 100644 --- a/src/Rocket.Chat.Net/Driver/RocketChatDriver.cs +++ b/src/Rocket.Chat.Net/Driver/RocketChatDriver.cs @@ -12,11 +12,10 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; - + using NLog; using Rocket.Chat.Net.Collections; using Rocket.Chat.Net.Helpers; using Rocket.Chat.Net.Interfaces; - using Rocket.Chat.Net.Loggers; using Rocket.Chat.Net.Models; using Rocket.Chat.Net.Models.Collections; using Rocket.Chat.Net.Models.LoginOptions; @@ -25,7 +24,7 @@ public class RocketChatDriver : IRocketChatDriver { - private const string MessageTopic = "stream-messages"; + private const string MessageTopic = "stream-room-messages"; private const int MessageSubscriptionLimit = 10; private readonly IStreamCollectionDatabase _collectionDatabase; @@ -50,7 +49,7 @@ public class RocketChatDriver : IRocketChatDriver public RocketChatDriver(string url, bool useSsl, ILogger logger = null, bool isBot = true, JsonSerializerSettings jsonSerializerSettings = null) { IsBot = isBot; - _logger = logger ?? new DummyLogger(); + _logger = logger ?? NLog.LogManager.CreateNullLogger(); _collectionDatabase = new StreamCollectionDatabase(); _logger.Info("Creating client..."); @@ -119,13 +118,13 @@ private void HandleStreamingCollections(string type, JObject data) private void HandleRocketMessage(string type, JObject data) { var o = data.ToObject>(JsonSerializer); - var isMessage = type == "added" && o.Collection == MessageTopic && o.Fields["args"] != null; + var isMessage = type == "changed" && o.Collection == MessageTopic && o.Fields["args"] != null; if (!isMessage) { return; } - var messageRaw = o.Fields["args"][1]; + var messageRaw = o.Fields["args"][0]; var message = messageRaw.ToObject(JsonSerializer); message.IsBotMentioned = message.Mentions.Any(x => x.Id == UserId); message.IsFromMyself = message.CreatedBy.Id == UserId; @@ -198,7 +197,7 @@ public async Task SubscribeToRoomAsync(string roomId = null) foreach (string id in ids) { _logger.Info($"Subscribing to Room: #{id}"); - Task task = new Task(() => _client.SubscribeAsync(MessageTopic, TimeoutToken, id, MessageSubscriptionLimit.ToString())); + Task task = new Task(() => _client.SubscribeAndWaitAsync(MessageTopic, TimeoutToken, id, false)); tasks.Add(task); task.Start(); } @@ -667,7 +666,7 @@ public TypedStreamCollection GetRoomInfoCollection() public async Task>> GetAvailableRoomInfoCollection() { - JObject result = await _client.CallAsync("rooms/get", CancellationToken.None, new object[] { 0 }).ConfigureAwait(false); + JObject result = await _client.CallAsync("rooms/get", CancellationToken.None, 0).ConfigureAwait(false); return result.ToObject>>(JsonSerializer); } diff --git a/src/Rocket.Chat.Net/Interfaces/ILogger.cs b/src/Rocket.Chat.Net/Interfaces/ILogger.cs deleted file mode 100644 index eaf2aa8..0000000 --- a/src/Rocket.Chat.Net/Interfaces/ILogger.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Rocket.Chat.Net.Interfaces -{ - public interface ILogger - { - void Debug(string message); - - void Info(string message); - - void Warn(string message); - - void Error(string message); - - void Fatal(string message); - } -} \ No newline at end of file diff --git a/src/Rocket.Chat.Net/Loggers/DummyLogger.cs b/src/Rocket.Chat.Net/Loggers/DummyLogger.cs deleted file mode 100644 index 6a6c56d..0000000 --- a/src/Rocket.Chat.Net/Loggers/DummyLogger.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace Rocket.Chat.Net.Loggers -{ - using Rocket.Chat.Net.Interfaces; - - public class DummyLogger : ILogger - { - public void Debug(string message) - { - } - - public void Info(string message) - { - } - - public void Warn(string message) - { - } - - public void Error(string message) - { - } - - public void Fatal(string message) - { - } - } -} \ No newline at end of file diff --git a/src/Rocket.Chat.Net/Rocket.Chat.Net.csproj b/src/Rocket.Chat.Net/Rocket.Chat.Net.csproj index e51ea90..986703d 100644 --- a/src/Rocket.Chat.Net/Rocket.Chat.Net.csproj +++ b/src/Rocket.Chat.Net/Rocket.Chat.Net.csproj @@ -4,10 +4,20 @@ netstandard2.0 + + + + + + + + + + diff --git a/test/Rocket.Chat.Net.Bot.Tests/Rocket.Chat.Net.Bot.Tests.csproj b/test/Rocket.Chat.Net.Bot.Tests/Rocket.Chat.Net.Bot.Tests.csproj index 20c4dbb..66d9750 100644 --- a/test/Rocket.Chat.Net.Bot.Tests/Rocket.Chat.Net.Bot.Tests.csproj +++ b/test/Rocket.Chat.Net.Bot.Tests/Rocket.Chat.Net.Bot.Tests.csproj @@ -47,6 +47,9 @@ ..\..\packages\FluentAssertions.4.9.1\lib\net45\FluentAssertions.Core.dll True + + ..\..\packages\NLog.5.1.0\lib\net46\NLog.dll + ..\..\packages\NSubstitute.1.10.0.0\lib\net45\NSubstitute.dll True @@ -56,6 +59,8 @@ True + + diff --git a/test/Rocket.Chat.Net.Bot.Tests/RocketChatBotFacts.cs b/test/Rocket.Chat.Net.Bot.Tests/RocketChatBotFacts.cs index 1059e95..e0cd2ff 100644 --- a/test/Rocket.Chat.Net.Bot.Tests/RocketChatBotFacts.cs +++ b/test/Rocket.Chat.Net.Bot.Tests/RocketChatBotFacts.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using FluentAssertions; - + using NLog; using NSubstitute; using NSubstitute.ExceptionExtensions; diff --git a/test/Rocket.Chat.Net.Bot.Tests/packages.config b/test/Rocket.Chat.Net.Bot.Tests/packages.config index 042c86a..210b858 100644 --- a/test/Rocket.Chat.Net.Bot.Tests/packages.config +++ b/test/Rocket.Chat.Net.Bot.Tests/packages.config @@ -4,6 +4,7 @@ + diff --git a/test/Rocket.Chat.Net.Tests/Driver/DdpFacts.cs b/test/Rocket.Chat.Net.Tests/Driver/DdpFacts.cs index c90453a..56d4ef6 100644 --- a/test/Rocket.Chat.Net.Tests/Driver/DdpFacts.cs +++ b/test/Rocket.Chat.Net.Tests/Driver/DdpFacts.cs @@ -24,7 +24,7 @@ public class DdpFacts : IDisposable private static readonly Fixture AutoFixture = new Fixture(); private readonly XUnitLogger _helper; - private readonly IWebSocketWrapper _socket = Substitute.For(); + private readonly WebSocket _socket = Substitute.For(); private CancellationToken TimeoutToken => CreateTimeoutToken(); @@ -116,7 +116,7 @@ public void On_error_log() var exception = new Exception(message); // Act - _socket.Error += Raise.Event>(new object(), new ErrorEventArgs(exception)); + _socket.Error += Raise.Event>(new object(), new SuperSocket.ClientEngine.ErrorEventArgs(exception)); // Assert logger.Received().Info($"ERROR: {message}"); diff --git a/test/Rocket.Chat.Net.Tests/Driver/MessagingFacts.cs b/test/Rocket.Chat.Net.Tests/Driver/MessagingFacts.cs index c094b48..08f0df1 100644 --- a/test/Rocket.Chat.Net.Tests/Driver/MessagingFacts.cs +++ b/test/Rocket.Chat.Net.Tests/Driver/MessagingFacts.cs @@ -3,7 +3,7 @@ using FluentAssertions; using Newtonsoft.Json.Linq; - + using NLog; using NSubstitute; using Ploeh.AutoFixture; diff --git a/test/Rocket.Chat.Net.Tests/Helpers/ILogger.cs b/test/Rocket.Chat.Net.Tests/Helpers/ILogger.cs new file mode 100644 index 0000000..6209474 --- /dev/null +++ b/test/Rocket.Chat.Net.Tests/Helpers/ILogger.cs @@ -0,0 +1,6 @@ +namespace Rocket.Chat.Net.Tests.Helpers +{ + public interface ILogger + { + } +} \ No newline at end of file diff --git a/test/Rocket.Chat.Net.Tests/Helpers/XUnitLogger.cs b/test/Rocket.Chat.Net.Tests/Helpers/XUnitLogger.cs index 2f723f9..604a83b 100644 --- a/test/Rocket.Chat.Net.Tests/Helpers/XUnitLogger.cs +++ b/test/Rocket.Chat.Net.Tests/Helpers/XUnitLogger.cs @@ -3,6 +3,7 @@ using System; using Rocket.Chat.Net.Interfaces; + using NLog; using Xunit.Abstractions; diff --git a/test/Rocket.Chat.Net.Tests/Rocket.Chat.Net.Tests.csproj b/test/Rocket.Chat.Net.Tests/Rocket.Chat.Net.Tests.csproj index adae783..5be1538 100644 --- a/test/Rocket.Chat.Net.Tests/Rocket.Chat.Net.Tests.csproj +++ b/test/Rocket.Chat.Net.Tests/Rocket.Chat.Net.Tests.csproj @@ -61,6 +61,9 @@ ..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + ..\..\packages\NLog.5.1.0\lib\net46\NLog.dll + ..\..\packages\NSubstitute.1.10.0.0\lib\net45\NSubstitute.dll True @@ -92,16 +95,91 @@ ..\..\packages\AutoFixture.3.47.8\lib\net40\Ploeh.AutoFixture.dll True + + ..\..\packages\SuperSocket.ClientEngine.Core.0.10.0\lib\net45\SuperSocket.ClientEngine.dll + + + ..\..\packages\System.Collections.Specialized.4.3.0\lib\net46\System.Collections.Specialized.dll + True + True + + + + + ..\..\packages\System.IO.4.3.0\lib\net462\System.IO.dll + True + True + + + + ..\..\packages\System.Linq.4.3.0\lib\net463\System.Linq.dll + True + True + + + ..\..\packages\System.Net.NameResolution.4.3.0\lib\net46\System.Net.NameResolution.dll + True + True + + + ..\..\packages\System.Net.Security.4.3.0\lib\net46\System.Net.Security.dll + True + True + + + ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + True + True + + + ..\..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll + True + True + + + ..\..\packages\System.Runtime.Extensions.4.3.0\lib\net462\System.Runtime.Extensions.dll + True + True + + + ..\..\packages\System.Runtime.InteropServices.4.3.0\lib\net463\System.Runtime.InteropServices.dll + True + True + + + ..\..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll + True + True + + + ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + True + True + + + ..\..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + True + True + + + ..\..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + True + True + + + ..\..\packages\System.Text.RegularExpressions.4.3.0\lib\net463\System.Text.RegularExpressions.dll + True + True + ..\..\packages\Validation.2.2.8\lib\dotnet\Validation.dll True - - ..\..\packages\WebSocket4Net.0.14.1\lib\net45\WebSocket4Net.dll - True + + ..\..\packages\WebSocket4Net.0.15.2\lib\net45\WebSocket4Net.dll ..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll @@ -133,6 +211,7 @@ + diff --git a/test/Rocket.Chat.Net.Tests/packages.config b/test/Rocket.Chat.Net.Tests/packages.config index 647a6dc..5341ad5 100644 --- a/test/Rocket.Chat.Net.Tests/packages.config +++ b/test/Rocket.Chat.Net.Tests/packages.config @@ -5,6 +5,7 @@ + @@ -13,8 +14,25 @@ + + + + + + + + + + + + + + + + + - +