Merge pull request #11 from Bubuni-Team/refactor/rss-cleanup

RSS module code cleanup.
This commit is contained in:
Andriy
2022-04-21 15:12:11 +03:00
committed by GitHub
4 changed files with 40 additions and 43 deletions
+2
View File
@@ -80,5 +80,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Data
/// All exists subscriber for this feed.
/// </summary>
public virtual ICollection<Subscriber> Subscribers { get; set; }
public override string ToString() => Title;
}
}
@@ -2,7 +2,6 @@
using System.Collections.Concurrent;
using System.Text;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Abstractions;
using Kruzya.TelegramBot.Core.Service;
using Microsoft.Extensions.Logging;
@@ -16,41 +15,40 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
{
protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(1);
protected readonly ConcurrentQueue<UserMessage> _queue;
protected readonly ConcurrentQueue<UserUnsubscribe> _unsubscribeQueue;
protected readonly ConcurrentQueue<UserMessage> Queue;
protected readonly ConcurrentQueue<UserUnsubscribe> UnsubscribeQueue;
public MessageSender(ILogger<MessageSender> logger, IBotInstance bot, ConcurrentQueue<UserMessage> userMessageQueue, ConcurrentQueue<UserUnsubscribe> userUnsubscribeQueue) : base(logger, bot)
{
_unsubscribeQueue = userUnsubscribeQueue;
_queue = userMessageQueue;
UnsubscribeQueue = userUnsubscribeQueue;
Queue = userMessageQueue;
}
protected override async Task OnRun()
{
if (_queue.Count == 0)
if (Queue.IsEmpty || !Queue.TryDequeue(out var message))
{
return;
}
UserMessage message;
if (!_queue.TryDequeue(out message))
{
return;
}
_logger.LogDebug($"Message for {message.ChatId.Identifier} dequeued.");
_logger.LogDebug("Message for {ChatId} dequeued.", message.ChatId.Identifier);
try
{
if (string.IsNullOrWhiteSpace(message.ImageUrl))
{
await _bot.BotClient.SendTextMessageAsync(message.ChatId, message.Text, message.ParseMode, null,
message.DisableWebPagePreview);
}
else
{
await _bot.BotClient.SendPhotoAsync(message.ChatId, message.ImageUrl, message.Text,
message.ParseMode, null, message.DisableWebPagePreview);
}
}
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"))
{
ReEnqueue(message, e, message.ChatId); // looks like a network issue
@@ -59,14 +57,14 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
// User added bot to blacklist.
// Unsubscribe him.
_unsubscribeQueue.Enqueue(new UserUnsubscribe()
UnsubscribeQueue.Enqueue(new UserUnsubscribe
{
ChatId = message.ChatId
});
}
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);
}
}
@@ -85,7 +83,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
_logger.LogError(eMessage.ToString());
}
_queue.Enqueue(message);
Queue.Enqueue(message);
}
}
}
+12 -12
View File
@@ -39,7 +39,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
private readonly IServiceScopeFactory _scopeFactory;
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)
{
@@ -57,7 +57,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
public Task StartAsync(CancellationToken cancellationToken)
{
_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;
}
@@ -99,7 +99,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
using var scope = _scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<RichSiteSummaryContext>();
var period = scope.ServiceProvider.GetRequiredService<IConfiguration>()
.GetValue<UInt16>("rssFetchPeriod");
.GetValue<ushort>("rssFetchPeriod");
var feeds = await dbContext.Feeds.ForFetching(period);
_logger.LogDebug("Received {count} feeds for fetching", new {count = feeds.Length});
@@ -116,7 +116,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
}
finally
{
_timer.Change(timerPeriod, timerPeriod);
_timer.Change(TimerPeriod, TimerPeriod);
}
}
@@ -160,14 +160,14 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
var text = post.MessageText;
foreach (var subscriber in subscribers)
{
var message = new UserMessage()
var message = new UserMessage
{
ChatId = new ChatId(subscriber.SubscriberId), DisableWebPagePreview = true,
ParseMode = ParseMode.Html, Text = text, ImageUrl = postsImages.GetValueOrDefault(post)
};
_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
{
return await FeedReader.ReadAsync(feed.Url.ToString());
return await FeedReader.ReadAsync(feed.Url);
}
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;
}
}
@@ -193,9 +193,9 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
private async Task<string> FetchImageUrl(FeedItem item)
{
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))
{
return enclosure.Url;
@@ -237,7 +237,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
}
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>
private static bool IsValidImage(string contentType)
{
return new string[]
return new[]
{
"image/jpeg",
"image/bmp",
+10 -13
View File
@@ -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();
}
}