From 41a8eaefc2b3cfd15a0741996eb93beff5a28c22 Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Thu, 21 Apr 2022 14:53:25 +0300 Subject: [PATCH] [rss] Unsubscriber service cleanup. --- .../RichSiteSummary/Service/Unsubscriber.cs | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/modules/RichSiteSummary/Service/Unsubscriber.cs b/modules/RichSiteSummary/Service/Unsubscriber.cs index 3de8acc..2b74353 100644 --- a/modules/RichSiteSummary/Service/Unsubscriber.cs +++ b/modules/RichSiteSummary/Service/Unsubscriber.cs @@ -15,26 +15,20 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service { public class Unsubscriber : AbstractTimedHostedService { - protected readonly ConcurrentQueue _queue; - protected readonly IServiceScopeFactory _scopeFactory; + protected readonly ConcurrentQueue Queue; + protected readonly IServiceScopeFactory ScopeFactory; protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(1); public Unsubscriber(ILogger logger, IBotInstance bot, ConcurrentQueue queue, IServiceScopeFactory scopeFactory) : base(logger, bot) { - _scopeFactory = scopeFactory; - _queue = queue; + ScopeFactory = scopeFactory; + Queue = queue; } protected override async Task OnRun() { - if (_queue.Count == 0) - { - return; - } - - UserUnsubscribe user; - if (!_queue.TryDequeue(out user)) + if (Queue.IsEmpty || !Queue.TryDequeue(out var user)) { return; } @@ -44,10 +38,13 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service private async Task UnsubscribeUser(ChatId chatId) { - using var scope = _scopeFactory.CreateScope(); + using var scope = ScopeFactory.CreateScope(); var dbContext = scope.ServiceProvider.GetRequiredService(); - - dbContext.Subscriptions.RemoveRange(dbContext.Subscriptions.Where(s => s.SubscriberId == chatId.Identifier)); + + var subscriptions = dbContext.Subscriptions; + subscriptions.RemoveRange( + subscriptions.Where(s => s.SubscriberId == chatId.Identifier) + ); await dbContext.SaveChangesAsync(); } }