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:
@@ -5,70 +5,69 @@ using BotFramework.Abstractions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Service
|
||||
namespace Kruzya.TelegramBot.Core.Service;
|
||||
|
||||
public abstract class AbstractTimedHostedService : IHostedService, IDisposable
|
||||
{
|
||||
public abstract class AbstractTimedHostedService : IHostedService, IDisposable
|
||||
private Timer _timer;
|
||||
|
||||
protected readonly ILogger _logger;
|
||||
protected readonly IBotInstance _bot;
|
||||
|
||||
protected virtual TimeSpan TimerPeriod => TimeSpan.FromSeconds(45);
|
||||
|
||||
protected AbstractTimedHostedService(ILogger logger, IBotInstance bot)
|
||||
{
|
||||
private Timer _timer;
|
||||
_bot = bot;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
#region IHostedService
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Message sender service is starting.");
|
||||
_timer = new Timer(Run, null, TimeSpan.Zero, TimerPeriod);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Message sender service is stopping.");
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
protected readonly ILogger _logger;
|
||||
protected readonly IBotInstance _bot;
|
||||
|
||||
protected virtual TimeSpan TimerPeriod => TimeSpan.FromSeconds(45);
|
||||
#endregion
|
||||
#region IDisposable
|
||||
/// <summary>
|
||||
/// Disposes the timer.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected AbstractTimedHostedService(ILogger logger, IBotInstance bot)
|
||||
private async void Run(object state)
|
||||
{
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
try
|
||||
{
|
||||
_bot = bot;
|
||||
_logger = logger;
|
||||
await OnRun();
|
||||
}
|
||||
|
||||
#region IHostedService
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation("Message sender service is starting.");
|
||||
_timer = new Timer(Run, null, TimeSpan.Zero, TimerPeriod);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Message sender service is stopping.");
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region IDisposable
|
||||
/// <summary>
|
||||
/// Disposes the timer.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
}
|
||||
#endregion
|
||||
|
||||
private async void Run(object state)
|
||||
{
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
try
|
||||
{
|
||||
await OnRun();
|
||||
}
|
||||
finally
|
||||
{
|
||||
var timerPeriod = TimerPeriod;
|
||||
_timer?.Change(timerPeriod, timerPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual async Task OnRun()
|
||||
{
|
||||
await Task.Delay(500);
|
||||
var timerPeriod = TimerPeriod;
|
||||
_timer?.Change(timerPeriod, timerPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual async Task OnRun()
|
||||
{
|
||||
await Task.Delay(500);
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,13 @@ using System.Threading.Tasks;
|
||||
using Kruzya.TelegramBot.Core.Data;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Service
|
||||
namespace Kruzya.TelegramBot.Core.Service;
|
||||
|
||||
public interface IReputation
|
||||
{
|
||||
public interface IReputation
|
||||
{
|
||||
public Task<double> GetUserReputationAsync(Chat chat, User user);
|
||||
public Task<double> SetUserReputationAsync(Chat chat, User user, double value);
|
||||
public Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver);
|
||||
public Task<double> IncrementReputationAsync(Chat chat, User user, double diff);
|
||||
public Task<IEnumerable<IReputationEntity>> GetChatRatingAsync(Chat chat, int limit = 10, int offset = 0);
|
||||
}
|
||||
}
|
||||
public Task<double> GetUserReputationAsync(Chat chat, User user);
|
||||
public Task<double> SetUserReputationAsync(Chat chat, User user, double value);
|
||||
public Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver);
|
||||
public Task<double> IncrementReputationAsync(Chat chat, User user, double diff);
|
||||
public Task<IEnumerable<IReputationEntity>> GetChatRatingAsync(Chat chat, int limit = 10, int offset = 0);
|
||||
}
|
||||
+62
-63
@@ -11,81 +11,80 @@ using Microsoft.Extensions.Logging;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Service
|
||||
namespace Kruzya.TelegramBot.Core.Service;
|
||||
|
||||
public class UserService
|
||||
{
|
||||
public class UserService
|
||||
private readonly List<long> _superUsers;
|
||||
private readonly Dictionary<long, string> _statusMap;
|
||||
private readonly ICacheStorage<long, User> _userCache;
|
||||
private readonly ITelegramBotClient _bot;
|
||||
private readonly ILogger<UserService> _logger;
|
||||
|
||||
|
||||
public UserService(
|
||||
IConfiguration configuration,
|
||||
IBotInstance bot,
|
||||
ICacheStorage<long, User> userCache,
|
||||
ILogger<UserService> logger)
|
||||
{
|
||||
private readonly List<long> _superUsers;
|
||||
private readonly Dictionary<long, string> _statusMap;
|
||||
private readonly ICacheStorage<long, User> _userCache;
|
||||
private readonly ITelegramBotClient _bot;
|
||||
private readonly ILogger<UserService> _logger;
|
||||
_superUsers = configuration.GetSection("SuperUsers")
|
||||
.GetChildren()
|
||||
.Select(q => Convert.ToInt64(q.Value))
|
||||
.ToList();
|
||||
|
||||
_statusMap = configuration.GetSection("CustomMemberStatus")
|
||||
.GetChildren()
|
||||
.ToDictionary(x => Convert.ToInt64(x.Key), x => x.Value);
|
||||
|
||||
public UserService(
|
||||
IConfiguration configuration,
|
||||
IBotInstance bot,
|
||||
ICacheStorage<long, User> userCache,
|
||||
ILogger<UserService> logger)
|
||||
_userCache = userCache;
|
||||
_bot = bot.BotClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<User> GetUser(long userId, long chatId, bool bypassCache = false)
|
||||
{
|
||||
if (bypassCache || !_userCache.TryGetValue(userId, out var user))
|
||||
{
|
||||
_superUsers = configuration.GetSection("SuperUsers")
|
||||
.GetChildren()
|
||||
.Select(q => Convert.ToInt64(q.Value))
|
||||
.ToList();
|
||||
|
||||
_statusMap = configuration.GetSection("CustomMemberStatus")
|
||||
.GetChildren()
|
||||
.ToDictionary(x => Convert.ToInt64(x.Key), x => x.Value);
|
||||
|
||||
_userCache = userCache;
|
||||
_bot = bot.BotClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<User> GetUser(long userId, long chatId, bool bypassCache = false)
|
||||
{
|
||||
if (bypassCache || !_userCache.TryGetValue(userId, out var user))
|
||||
if (bypassCache)
|
||||
{
|
||||
if (bypassCache)
|
||||
{
|
||||
_logger.LogDebug("Bypassing cache for {UserId}.", userId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("User {UserId} is not found in cache. Fetching from Telegram API.", userId);
|
||||
}
|
||||
|
||||
user = await GetUserByChat(chatId, userId);
|
||||
CacheUser(user);
|
||||
_logger.LogDebug("Bypassing cache for {UserId}.", userId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("User {UserId} is not found in cache. Fetching from Telegram API.", userId);
|
||||
}
|
||||
|
||||
user = await GetUserByChat(chatId, userId);
|
||||
CacheUser(user);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
private void CacheUser(User user)
|
||||
{
|
||||
// TODO: config entry for TTL
|
||||
_userCache.SetValue(user.Id, user, Convert.ToInt32(TimeSpan.FromHours(1).TotalSeconds));
|
||||
}
|
||||
private void CacheUser(User user)
|
||||
{
|
||||
// TODO: config entry for TTL
|
||||
_userCache.SetValue(user.Id, user, Convert.ToInt32(TimeSpan.FromHours(1).TotalSeconds));
|
||||
}
|
||||
|
||||
private async Task<User> GetUserByChat(long chatId, long userId)
|
||||
{
|
||||
return (await _bot.GetChatMemberAsync(chatId, userId)).User;
|
||||
}
|
||||
private async Task<User> GetUserByChat(long chatId, long userId)
|
||||
{
|
||||
return (await _bot.GetChatMemberAsync(chatId, userId)).User;
|
||||
}
|
||||
|
||||
public bool IsUserSuper(User user)
|
||||
{
|
||||
return IsUserSuper(user.Id);
|
||||
}
|
||||
public bool IsUserSuper(User user)
|
||||
{
|
||||
return IsUserSuper(user.Id);
|
||||
}
|
||||
|
||||
public bool IsUserSuper(long userId)
|
||||
{
|
||||
return _superUsers.Contains(userId);
|
||||
}
|
||||
public bool IsUserSuper(long userId)
|
||||
{
|
||||
return _superUsers.Contains(userId);
|
||||
}
|
||||
|
||||
public string? GetUserCustomStatus(long userId)
|
||||
{
|
||||
return _statusMap.ContainsKey(userId) ? _statusMap[userId] : null;
|
||||
}
|
||||
public string? GetUserCustomStatus(long userId)
|
||||
{
|
||||
return _statusMap.ContainsKey(userId) ? _statusMap[userId] : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user