From 90eeb7f85128ab5d9d4de2d8ac9d149917724937 Mon Sep 17 00:00:00 2001 From: Sergey Nazarov Date: Fri, 22 Mar 2024 20:46:17 +0400 Subject: [PATCH] Add chat username to TextMatchMatched --- Directory.Packages.props | 2 +- .../TextMatchers/MessageReceivedHandler.cs | 6 ++++-- .../InMemoryTextMatchRepository.cs | 20 +++++++++++++++++-- .../TextMatches/Services/TextMatchService.cs | 3 ++- .../TextMatchMatched.cs | 4 +++- .../Nocr.TextMatcher.Host.csproj | 1 - 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 0193467..373385f 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -9,7 +9,7 @@ - + diff --git a/src/Nocr.TextMatcher.AppServices/TextMatchers/MessageReceivedHandler.cs b/src/Nocr.TextMatcher.AppServices/TextMatchers/MessageReceivedHandler.cs index a2f39ec..e3f634b 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatchers/MessageReceivedHandler.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatchers/MessageReceivedHandler.cs @@ -34,19 +34,21 @@ public sealed class MessageReceivedHandler : IHandleMessages foreach (var match in matches) { - if (match.IsMatches(message.ChatId, message.ChatUserName, message.Text)) + if (match.IsMatches(message.ChatId, message.ChatUsername, message.Text)) { _logger.LogInformation("Message {@Message} matched {@Match}", message, match); var @event = new TextMatchMatched { ChatId = message.ChatId, + ChatUsername = message.ChatUsername, Text = message.Text, UserId = message.From, OccuredDateTime = message.OccuredDateTime, PublishedDateTime = _dateProvider.UtcNow }; - await _bus.Advanced.Topics.Publish("nocr.text.matcher", @event); + // TODO: + await _bus.Advanced.Topics.Publish("nocr.text.matcher.matched", @event); } } } diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/InMemoryTextMatchRepository.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/InMemoryTextMatchRepository.cs index 81f6284..f9f2779 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/InMemoryTextMatchRepository.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Repositories/InMemoryTextMatchRepository.cs @@ -1,3 +1,5 @@ +using Nocr.TextMatcher.AppServices.Contracts.TextMatches; + namespace Nocr.TextMatcher.AppServices.TextMatches.Repositories; public sealed class InMemoryTextMatchRepository : ITextMatchRepository @@ -6,12 +8,26 @@ public sealed class InMemoryTextMatchRepository : ITextMatchRepository private List _textMatches = new List(); + public InMemoryTextMatchRepository() + { + var seed = new[] + { + TextMatch.Initialize(1, "baraholka_tbi", "телевизор", TextMatchRule.Full, DateTimeOffset.UtcNow), + TextMatch.Initialize(1, "baraholka_tbi", "macbook mac", TextMatchRule.AnyWord, DateTimeOffset.UtcNow), + TextMatch.Initialize(1, "baraholka_tbi", "гитар", TextMatchRule.Full, DateTimeOffset.UtcNow), + TextMatch.Initialize(1, "baraholka_tbi", "обувь ботинки туфли", TextMatchRule.AnyWord, DateTimeOffset.UtcNow), + TextMatch.Initialize(1, "baraholka_tbi", "одежда платья брюки рубашка рубашки", TextMatchRule.AnyWord, DateTimeOffset.UtcNow), + }; + + _textMatches.AddRange(seed); + } + public Task Create(TextMatch textMatch, CancellationToken cancellationToken = default) { var id = Interlocked.Increment(ref _id); textMatch.Id = id; _textMatches.Add(textMatch); - + return Task.FromResult(id); } @@ -19,7 +35,7 @@ public sealed class InMemoryTextMatchRepository : ITextMatchRepository { var index = _textMatches.FindIndex(x => x.Id == id); _textMatches.RemoveAt(index); - + return Task.CompletedTask; } diff --git a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs index 63a116d..6cd8d7b 100644 --- a/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs +++ b/src/Nocr.TextMatcher.AppServices/TextMatches/Services/TextMatchService.cs @@ -31,7 +31,8 @@ public sealed class TextMatchService : ITextMatchService ChatUsername = textMatch.ChatUsername }; - await _bus.Advanced.Topics.Publish("nocr.text.matcher", @event); + // TODO: + await _bus.Advanced.Topics.Publish("nocr.text.matcher.matches", @event); return textMatch.Id; } diff --git a/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchMatched.cs b/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchMatched.cs index 4a58507..949f400 100644 --- a/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchMatched.cs +++ b/src/Nocr.TextMatcher.Async.Api.Contracts/TextMatchMatched.cs @@ -6,7 +6,9 @@ public class TextMatchMatched : IEvent public long UserId { get; set; } - public long ChatId { get; set; } + public long? ChatId { get; set; } + + public string ChatUsername { get; set; } = null!; public string Text { get; set; } diff --git a/src/Nocr.TextMatcher.Host/Nocr.TextMatcher.Host.csproj b/src/Nocr.TextMatcher.Host/Nocr.TextMatcher.Host.csproj index 84806b2..ba69f55 100644 --- a/src/Nocr.TextMatcher.Host/Nocr.TextMatcher.Host.csproj +++ b/src/Nocr.TextMatcher.Host/Nocr.TextMatcher.Host.csproj @@ -12,7 +12,6 @@ -