mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Merge pull request #11 from Bubuni-Team/refactor/rss-cleanup
RSS module code cleanup.
This commit is contained in:
@@ -80,5 +80,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Data
|
|||||||
/// All exists subscriber for this feed.
|
/// All exists subscriber for this feed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual ICollection<Subscriber> Subscribers { get; set; }
|
public virtual ICollection<Subscriber> Subscribers { get; set; }
|
||||||
|
|
||||||
|
public override string ToString() => Title;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BotFramework;
|
|
||||||
using BotFramework.Abstractions;
|
using BotFramework.Abstractions;
|
||||||
using Kruzya.TelegramBot.Core.Service;
|
using Kruzya.TelegramBot.Core.Service;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -16,41 +15,40 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
{
|
{
|
||||||
protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(1);
|
protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(1);
|
||||||
|
|
||||||
protected readonly ConcurrentQueue<UserMessage> _queue;
|
protected readonly ConcurrentQueue<UserMessage> Queue;
|
||||||
protected readonly ConcurrentQueue<UserUnsubscribe> _unsubscribeQueue;
|
protected readonly ConcurrentQueue<UserUnsubscribe> UnsubscribeQueue;
|
||||||
|
|
||||||
public MessageSender(ILogger<MessageSender> logger, IBotInstance bot, ConcurrentQueue<UserMessage> userMessageQueue, ConcurrentQueue<UserUnsubscribe> userUnsubscribeQueue) : base(logger, bot)
|
public MessageSender(ILogger<MessageSender> logger, IBotInstance bot, ConcurrentQueue<UserMessage> userMessageQueue, ConcurrentQueue<UserUnsubscribe> userUnsubscribeQueue) : base(logger, bot)
|
||||||
{
|
{
|
||||||
_unsubscribeQueue = userUnsubscribeQueue;
|
UnsubscribeQueue = userUnsubscribeQueue;
|
||||||
_queue = userMessageQueue;
|
Queue = userMessageQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task OnRun()
|
protected override async Task OnRun()
|
||||||
{
|
{
|
||||||
if (_queue.Count == 0)
|
if (Queue.IsEmpty || !Queue.TryDequeue(out var message))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserMessage message;
|
_logger.LogDebug("Message for {ChatId} dequeued.", message.ChatId.Identifier);
|
||||||
if (!_queue.TryDequeue(out message))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogDebug($"Message for {message.ChatId.Identifier} dequeued.");
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(message.ImageUrl))
|
if (string.IsNullOrWhiteSpace(message.ImageUrl))
|
||||||
|
{
|
||||||
await _bot.BotClient.SendTextMessageAsync(message.ChatId, message.Text, message.ParseMode, null,
|
await _bot.BotClient.SendTextMessageAsync(message.ChatId, message.Text, message.ParseMode, null,
|
||||||
message.DisableWebPagePreview);
|
message.DisableWebPagePreview);
|
||||||
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
|
{
|
||||||
await _bot.BotClient.SendPhotoAsync(message.ChatId, message.ImageUrl, message.Text,
|
await _bot.BotClient.SendPhotoAsync(message.ChatId, message.ImageUrl, message.Text,
|
||||||
message.ParseMode, null, message.DisableWebPagePreview);
|
message.ParseMode, null, message.DisableWebPagePreview);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (ApiRequestException e)
|
catch (ApiRequestException e)
|
||||||
{
|
{
|
||||||
_logger.LogDebug($"Message for {message.ChatId.Identifier} is failed: {e.Message}.");
|
_logger.LogDebug("Message for {ChatId} is failed: {error}.", message.ChatId.Identifier, e.Message);
|
||||||
if (!e.Message.Contains("bot was blocked by user"))
|
if (!e.Message.Contains("bot was blocked by user"))
|
||||||
{
|
{
|
||||||
ReEnqueue(message, e, message.ChatId); // looks like a network issue
|
ReEnqueue(message, e, message.ChatId); // looks like a network issue
|
||||||
@@ -59,14 +57,14 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
|
|
||||||
// User added bot to blacklist.
|
// User added bot to blacklist.
|
||||||
// Unsubscribe him.
|
// Unsubscribe him.
|
||||||
_unsubscribeQueue.Enqueue(new UserUnsubscribe()
|
UnsubscribeQueue.Enqueue(new UserUnsubscribe
|
||||||
{
|
{
|
||||||
ChatId = message.ChatId
|
ChatId = message.ChatId
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_logger.LogDebug($"Message for {message.ChatId.Identifier} is failed: {e.Message}.");
|
_logger.LogDebug("Message for {ChatId} is failed: {error}.", message.ChatId.Identifier, e.Message);
|
||||||
ReEnqueue(message, e);
|
ReEnqueue(message, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,7 +83,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
_logger.LogError(eMessage.ToString());
|
_logger.LogError(eMessage.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
_queue.Enqueue(message);
|
Queue.Enqueue(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
private readonly IServiceScopeFactory _scopeFactory;
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
private readonly ConcurrentQueue<UserMessage> _queue;
|
private readonly ConcurrentQueue<UserMessage> _queue;
|
||||||
|
|
||||||
private TimeSpan timerPeriod => TimeSpan.FromSeconds(45);
|
private static TimeSpan TimerPeriod => TimeSpan.FromSeconds(45);
|
||||||
|
|
||||||
public RssFetch(ILogger<RssFetch> logger, ConcurrentQueue<UserMessage> queue, IServiceScopeFactory scopeFactory)
|
public RssFetch(ILogger<RssFetch> logger, ConcurrentQueue<UserMessage> queue, IServiceScopeFactory scopeFactory)
|
||||||
{
|
{
|
||||||
@@ -57,7 +57,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("RSS fetcher service is starting.");
|
_logger.LogInformation("RSS fetcher service is starting.");
|
||||||
_timer = new Timer(DoFetch, null, TimeSpan.Zero, timerPeriod);
|
_timer = new Timer(DoFetch, null, TimeSpan.Zero, TimerPeriod);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
using var scope = _scopeFactory.CreateScope();
|
using var scope = _scopeFactory.CreateScope();
|
||||||
var dbContext = scope.ServiceProvider.GetRequiredService<RichSiteSummaryContext>();
|
var dbContext = scope.ServiceProvider.GetRequiredService<RichSiteSummaryContext>();
|
||||||
var period = scope.ServiceProvider.GetRequiredService<IConfiguration>()
|
var period = scope.ServiceProvider.GetRequiredService<IConfiguration>()
|
||||||
.GetValue<UInt16>("rssFetchPeriod");
|
.GetValue<ushort>("rssFetchPeriod");
|
||||||
|
|
||||||
var feeds = await dbContext.Feeds.ForFetching(period);
|
var feeds = await dbContext.Feeds.ForFetching(period);
|
||||||
_logger.LogDebug("Received {count} feeds for fetching", new {count = feeds.Length});
|
_logger.LogDebug("Received {count} feeds for fetching", new {count = feeds.Length});
|
||||||
@@ -116,7 +116,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
_timer.Change(timerPeriod, timerPeriod);
|
_timer.Change(TimerPeriod, TimerPeriod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,14 +160,14 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
var text = post.MessageText;
|
var text = post.MessageText;
|
||||||
foreach (var subscriber in subscribers)
|
foreach (var subscriber in subscribers)
|
||||||
{
|
{
|
||||||
var message = new UserMessage()
|
var message = new UserMessage
|
||||||
{
|
{
|
||||||
ChatId = new ChatId(subscriber.SubscriberId), DisableWebPagePreview = true,
|
ChatId = new ChatId(subscriber.SubscriberId), DisableWebPagePreview = true,
|
||||||
ParseMode = ParseMode.Html, Text = text, ImageUrl = postsImages.GetValueOrDefault(post)
|
ParseMode = ParseMode.Html, Text = text, ImageUrl = postsImages.GetValueOrDefault(post)
|
||||||
};
|
};
|
||||||
|
|
||||||
_queue.Enqueue(message);
|
_queue.Enqueue(message);
|
||||||
_logger.LogDebug($"Enqueued message for {subscriber.SubscriberId}");
|
_logger.LogDebug("Queued message for {SubscriberId}", subscriber.SubscriberId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -177,11 +177,11 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return await FeedReader.ReadAsync(feed.Url.ToString());
|
return await FeedReader.ReadAsync(feed.Url);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_logger.LogError($"Feed {feed} can't be fetched: {e.Message}");
|
_logger.LogError("Feed {feed} can't be fetched: {error}", feed, e.Message);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -193,9 +193,9 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
private async Task<string> FetchImageUrl(FeedItem item)
|
private async Task<string> FetchImageUrl(FeedItem item)
|
||||||
{
|
{
|
||||||
var feedItem = item.SpecificItem;
|
var feedItem = item.SpecificItem;
|
||||||
if (feedItem is Rss20FeedItem)
|
if (feedItem is Rss20FeedItem rss20FeedItem)
|
||||||
{
|
{
|
||||||
var enclosure = ((Rss20FeedItem)feedItem).Enclosure;
|
var enclosure = rss20FeedItem.Enclosure;
|
||||||
if (enclosure != null && IsValidImage(enclosure.MediaType))
|
if (enclosure != null && IsValidImage(enclosure.MediaType))
|
||||||
{
|
{
|
||||||
return enclosure.Url;
|
return enclosure.Url;
|
||||||
@@ -237,7 +237,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_logger.LogError($"Caused error when fetching image for RSS post: {e.Message}");
|
_logger.LogError("Caused error when fetching image for RSS post: {error}", e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,7 +251,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
/// <returns>True, if Telegram maybe can "use" this image, false if not.</returns>
|
/// <returns>True, if Telegram maybe can "use" this image, false if not.</returns>
|
||||||
private static bool IsValidImage(string contentType)
|
private static bool IsValidImage(string contentType)
|
||||||
{
|
{
|
||||||
return new string[]
|
return new[]
|
||||||
{
|
{
|
||||||
"image/jpeg",
|
"image/jpeg",
|
||||||
"image/bmp",
|
"image/bmp",
|
||||||
|
|||||||
@@ -15,26 +15,20 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
{
|
{
|
||||||
public class Unsubscriber : AbstractTimedHostedService
|
public class Unsubscriber : AbstractTimedHostedService
|
||||||
{
|
{
|
||||||
protected readonly ConcurrentQueue<UserUnsubscribe> _queue;
|
protected readonly ConcurrentQueue<UserUnsubscribe> Queue;
|
||||||
protected readonly IServiceScopeFactory _scopeFactory;
|
protected readonly IServiceScopeFactory ScopeFactory;
|
||||||
|
|
||||||
protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(1);
|
protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(1);
|
||||||
|
|
||||||
public Unsubscriber(ILogger<Unsubscriber> logger, IBotInstance bot, ConcurrentQueue<UserUnsubscribe> queue, IServiceScopeFactory scopeFactory) : base(logger, bot)
|
public Unsubscriber(ILogger<Unsubscriber> logger, IBotInstance bot, ConcurrentQueue<UserUnsubscribe> queue, IServiceScopeFactory scopeFactory) : base(logger, bot)
|
||||||
{
|
{
|
||||||
_scopeFactory = scopeFactory;
|
ScopeFactory = scopeFactory;
|
||||||
_queue = queue;
|
Queue = queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task OnRun()
|
protected override async Task OnRun()
|
||||||
{
|
{
|
||||||
if (_queue.Count == 0)
|
if (Queue.IsEmpty || !Queue.TryDequeue(out var user))
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
UserUnsubscribe user;
|
|
||||||
if (!_queue.TryDequeue(out user))
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -44,10 +38,13 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
|
|
||||||
private async Task UnsubscribeUser(ChatId chatId)
|
private async Task UnsubscribeUser(ChatId chatId)
|
||||||
{
|
{
|
||||||
using var scope = _scopeFactory.CreateScope();
|
using var scope = ScopeFactory.CreateScope();
|
||||||
var dbContext = scope.ServiceProvider.GetRequiredService<RichSiteSummaryContext>();
|
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();
|
await dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user