mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[rss] Unsubscriber service cleanup.
This commit is contained in:
@@ -15,26 +15,20 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
||||
{
|
||||
public class Unsubscriber : AbstractTimedHostedService
|
||||
{
|
||||
protected readonly ConcurrentQueue<UserUnsubscribe> _queue;
|
||||
protected readonly IServiceScopeFactory _scopeFactory;
|
||||
protected readonly ConcurrentQueue<UserUnsubscribe> Queue;
|
||||
protected readonly IServiceScopeFactory ScopeFactory;
|
||||
|
||||
protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(1);
|
||||
|
||||
public Unsubscriber(ILogger<Unsubscriber> logger, IBotInstance bot, ConcurrentQueue<UserUnsubscribe> 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<RichSiteSummaryContext>();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user