diff --git a/modules/RichSiteSummary/Data/Feed.cs b/modules/RichSiteSummary/Data/Feed.cs index e63dc17..a7b28cd 100644 --- a/modules/RichSiteSummary/Data/Feed.cs +++ b/modules/RichSiteSummary/Data/Feed.cs @@ -80,5 +80,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Data /// All exists subscriber for this feed. /// public virtual ICollection Subscribers { get; set; } + + public override string ToString() => Title; } } \ No newline at end of file diff --git a/modules/RichSiteSummary/Service/RssFetch.cs b/modules/RichSiteSummary/Service/RssFetch.cs index 4dbc914..aae7319 100644 --- a/modules/RichSiteSummary/Service/RssFetch.cs +++ b/modules/RichSiteSummary/Service/RssFetch.cs @@ -39,7 +39,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service private readonly IServiceScopeFactory _scopeFactory; private readonly ConcurrentQueue _queue; - private TimeSpan timerPeriod => TimeSpan.FromSeconds(45); + private static TimeSpan TimerPeriod => TimeSpan.FromSeconds(45); public RssFetch(ILogger logger, ConcurrentQueue 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(); var period = scope.ServiceProvider.GetRequiredService() - .GetValue("rssFetchPeriod"); + .GetValue("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 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 /// True, if Telegram maybe can "use" this image, false if not. private static bool IsValidImage(string contentType) { - return new string[] + return new[] { "image/jpeg", "image/bmp",