dev: feed code cleaned up a bit

This commit is contained in:
Kwoth
2024-06-14 20:44:07 +00:00
parent 30f3ae1ade
commit 8fae6e621d

View File

@@ -79,6 +79,15 @@ public class FeedsService : INService
} }
} }
private DateTime? GetPubDate(FeedItem item)
{
if (item.PublishingDate is not null)
return item.PublishingDate;
if (item.SpecificItem is AtomFeedItem atomItem)
return atomItem.UpdatedDate;
return null;
}
public async Task<EmbedBuilder> TrackFeeds() public async Task<EmbedBuilder> TrackFeeds()
{ {
while (true) while (true)
@@ -94,24 +103,32 @@ public class FeedsService : INService
{ {
var feed = await FeedReader.ReadAsync(rssUrl); var feed = await FeedReader.ReadAsync(rssUrl);
var items = feed var items = new List<(FeedItem Item, DateTime LastUpdate)>();
.Items.Select(item => (Item: item, foreach (var item in feed.Items)
LastUpdate: item.PublishingDate?.ToUniversalTime() {
?? (item.SpecificItem as AtomFeedItem)?.UpdatedDate?.ToUniversalTime())) var pubDate = GetPubDate(item);
.Where(data => data.LastUpdate is not null)
.Select(data => (data.Item, LastUpdate: (DateTime)data.LastUpdate)) if (pubDate is null)
.OrderByDescending(data => data.LastUpdate) continue;
.Reverse() // start from the oldest
.ToList(); items.Add((item, pubDate.Value.ToUniversalTime()));
// show at most 3 items if you're behind
if (items.Count > 2)
break;
}
if (items.Count == 0)
continue;
if (!_lastPosts.TryGetValue(kvp.Key, out var lastFeedUpdate)) if (!_lastPosts.TryGetValue(kvp.Key, out var lastFeedUpdate))
{ {
lastFeedUpdate = _lastPosts[kvp.Key] = lastFeedUpdate = _lastPosts[kvp.Key] = items[0].LastUpdate;
items.Any() ? items[items.Count - 1].LastUpdate : DateTime.UtcNow;
} }
foreach (var (feedItem, itemUpdateDate) in items) for (var index = 1; index <= items.Count; index++)
{ {
var (feedItem, itemUpdateDate) = items[^index];
if (itemUpdateDate <= lastFeedUpdate) if (itemUpdateDate <= lastFeedUpdate)
continue; continue;
@@ -168,27 +185,26 @@ public class FeedsService : INService
if (!string.IsNullOrWhiteSpace(feedItem.Description)) if (!string.IsNullOrWhiteSpace(feedItem.Description))
embed.WithDescription(desc.TrimTo(2048)); embed.WithDescription(desc.TrimTo(2048));
//send the created embed to all subscribed channels
var feedSendTasks = kvp.Value
.Where(x => x.GuildConfig is not null)
.Select(x =>
{
var ch = _client.GetGuild(x.GuildConfig.GuildId)
?.GetTextChannel(x.ChannelId);
if (ch is null) var tasks = new List<Task>();
return null;
return _sender.Response(ch) foreach (var val in kvp.Value)
.Embed(embed) {
.Text(string.IsNullOrWhiteSpace(x.Message) var ch = _client.GetGuild(val.GuildConfig.GuildId).GetTextChannel(val.ChannelId);
? string.Empty
: x.Message)
.SendAsync();
})
.Where(x => x is not null);
allSendTasks.Add(feedSendTasks.WhenAll()); if (ch is null)
continue;
var sendTask = _sender.Response(ch)
.Embed(embed)
.Text(string.IsNullOrWhiteSpace(val.Message)
? string.Empty
: val.Message)
.SendAsync();
tasks.Add(sendTask);
}
allSendTasks.Add(tasks.WhenAll());
// as data retrieval was successful, reset error counter // as data retrieval was successful, reset error counter
ClearErrors(rssUrl); ClearErrors(rssUrl);