Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Jobs/RssFeedJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public async Task Execute(IJobExecutionContext context)

// If nothing from this feed has ever been seen, this is an initial run: mark
// everything seen and only post the latest entry to avoid backfilling history.
bool initialSeed = !entries.Any(e => seen.Contains(e.EntryId));
// Check all history for the feed because older seen entries may have rolled out of
// the feed's current response.
bool initialSeed = !await HasFeedHistoryAsync(db, feedUrl);
if (initialSeed)
{
RssFeedService.FeedEntry latest = entries.OrderByDescending(e => e.Published).First();
Expand Down Expand Up @@ -97,6 +99,9 @@ internal static async Task<bool> DispatchAsync(
return allSucceeded;
}

internal static Task<bool> HasFeedHistoryAsync(DB db, string feedUrl) =>
db.RssSeenEntries.AnyAsync(entry => entry.FeedUrl == feedUrl);

private async Task<bool> SendAsync(RssSubscription sub, string content)
{
if (sub.Webhook == null)
Expand Down
25 changes: 25 additions & 0 deletions Morpheus.Tests/RssFeedJobTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Morpheus.Database;
using Morpheus.Database.Models;
using Morpheus.Jobs;
using Morpheus.Services;
Expand All @@ -6,6 +9,28 @@ namespace Morpheus.Tests;

public class RssFeedJobTests
{
[Fact]
public async Task HasFeedHistoryAsync_WhenOlderEntryRolledOutOfFeed_ReturnsTrue()
{
await using SqliteConnection connection = new("Data Source=:memory:");
await connection.OpenAsync();
DbContextOptions<DB> options = new DbContextOptionsBuilder<DB>()
.UseSqlite(connection)
.Options;
await using DB db = new(options);
await db.Database.EnsureCreatedAsync();
db.RssSeenEntries.Add(new RssSeenEntry
{
FeedUrl = "https://example.com/feed",
EntryId = "entry-that-is-no-longer-in-the-feed"
});
await db.SaveChangesAsync();

bool hasHistory = await RssFeedJob.HasFeedHistoryAsync(db, "https://example.com/feed");

Assert.True(hasHistory);
}

[Fact]
public async Task DispatchAsync_WhenOneDeliveryFails_ReportsFailureAndAttemptsEverySubscriber()
{
Expand Down