mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Use file-scoped namespaces
This commit is contained in:
@@ -9,41 +9,40 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.Service;
|
||||
|
||||
public class Unsubscriber : AbstractTimedHostedService
|
||||
{
|
||||
public class Unsubscriber : AbstractTimedHostedService
|
||||
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)
|
||||
{
|
||||
protected readonly ConcurrentQueue<UserUnsubscribe> Queue;
|
||||
protected readonly IServiceScopeFactory ScopeFactory;
|
||||
ScopeFactory = scopeFactory;
|
||||
Queue = queue;
|
||||
}
|
||||
|
||||
protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(1);
|
||||
|
||||
public Unsubscriber(ILogger<Unsubscriber> logger, IBotInstance bot, ConcurrentQueue<UserUnsubscribe> queue, IServiceScopeFactory scopeFactory) : base(logger, bot)
|
||||
protected override async Task OnRun()
|
||||
{
|
||||
if (Queue.IsEmpty || !Queue.TryDequeue(out var user))
|
||||
{
|
||||
ScopeFactory = scopeFactory;
|
||||
Queue = queue;
|
||||
return;
|
||||
}
|
||||
|
||||
protected override async Task OnRun()
|
||||
{
|
||||
if (Queue.IsEmpty || !Queue.TryDequeue(out var user))
|
||||
{
|
||||
return;
|
||||
}
|
||||
await UnsubscribeUser(user.ChatId);
|
||||
}
|
||||
|
||||
await UnsubscribeUser(user.ChatId);
|
||||
}
|
||||
private async Task UnsubscribeUser(ChatId chatId)
|
||||
{
|
||||
using var scope = ScopeFactory.CreateScope();
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<RichSiteSummaryContext>();
|
||||
|
||||
private async Task UnsubscribeUser(ChatId chatId)
|
||||
{
|
||||
using var scope = ScopeFactory.CreateScope();
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<RichSiteSummaryContext>();
|
||||
|
||||
var subscriptions = dbContext.Subscriptions;
|
||||
subscriptions.RemoveRange(
|
||||
subscriptions.Where(s => s.SubscriberId == chatId.Identifier)
|
||||
);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
var subscriptions = dbContext.Subscriptions;
|
||||
subscriptions.RemoveRange(
|
||||
subscriptions.Where(s => s.SubscriberId == chatId.Identifier)
|
||||
);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user