Skip to content
Merged
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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Feed operations are exposed as rake tasks: `rake fetch_feeds`, `rake fetch_feed[

**Background jobs** funnel through a single `CallableJob`, which just calls any callable: `CallableJob.perform_later(Feed::FetchOne, feed)` enqueues `Feed::FetchOne.call(feed)`. There is rarely a need to write a new `ApplicationJob` subclass — enqueue a command via `CallableJob` instead. Jobs run on GoodJob (Postgres-backed).

**Feed fetching flow:** `Feed::FetchAll` → enqueues `Feed::FetchOne` per feed → `HTTParty` fetches, `Feedjira` parses → `Feed::FindNewStories` diffs against the latest stored entry → `StoryRepository.add`. Fetch failures set the feed status to `:red` (vs `:green`) and log rather than raise.
**Feed fetching flow:** `Feed::FetchAll` → enqueues `Feed::FetchOne` per feed → `HTTParty` fetches, `Feedjira` parses → `Feed::FindNewStories` drops entries that are too old or already stored → `StoryRepository.add`. Fetch failures set the feed status to `:red` (vs `:green`) and log rather than raise.

**Admin routes** under `/admin` (GoodJob dashboard, settings, debug) are gated by `AdminConstraint` (`lib/admin_constraint.rb`). Stringer is single-user; auth is password-based (`bcrypt`).

Expand Down
6 changes: 1 addition & 5 deletions app/commands/feed/fetch_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ def fetch_raw_feed(feed)
end

def new_entries_from(feed, raw_feed)
Feed::FindNewStories.call(raw_feed, feed.id, latest_entry_id(feed))
end

def latest_entry_id(feed)
feed.stories.first.entry_id unless feed.stories.empty?
Feed::FindNewStories.call(raw_feed, feed.id)
end
end
end
19 changes: 5 additions & 14 deletions app/commands/feed/find_new_stories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,14 @@
module Feed::FindNewStories
STORY_AGE_THRESHOLD_DAYS = 3

def self.call(raw_feed, feed_id, latest_entry_id = nil)
stories = []
def self.call(raw_feed, feed_id)
recent = raw_feed.entries.reject { |story| too_old?(story) }
existing_ids = StoryRepository.existing_entry_ids(recent.map(&:id), feed_id)

raw_feed.entries.each do |story|
break if latest_entry_id && story.id == latest_entry_id
next if story_age_exceeds_threshold?(story) || StoryRepository.exists?(
story.id,
feed_id
)

stories << story
end

stories
recent.reject { |story| existing_ids.include?(story.id) }
end

def self.story_age_exceeds_threshold?(story)
def self.too_old?(story)
max_age = Time.now - STORY_AGE_THRESHOLD_DAYS.days
story.published && story.published < max_age
end
Expand Down
4 changes: 2 additions & 2 deletions app/repositories/story_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def self.fetch_unread_for_feed_by_timestamp(feed_id, timestamp)
.where("created_at < ? AND is_read = ?", timestamp, false)
end

def self.exists?(id, feed_id)
Story.exists?(entry_id: id, feed_id:)
def self.existing_entry_ids(entry_ids, feed_id)
Story.where(entry_id: entry_ids, feed_id:).pluck(:entry_id)
end

def self.unread(order: "desc")
Expand Down
12 changes: 6 additions & 6 deletions spec/commands/feed/find_new_stories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ def create_raw_feed(feed, entries: [])
end
end

it "scans until matching the last story id" do
new_entry = create_entry(published: Time.zone.now, url: "www.new.com")
old_entry = create_entry(title: "old entry", url: "www.old.com")
raw_feed = create_raw_feed(create(:feed), entries: [new_entry, old_entry])
result = described_class.call(raw_feed, 1, "www.old.com")
it "finds new stories listed after an existing story" do
story = create(:story, entry_id: "www.pinned.com")
entries = [story.entry_id, "www.new.com"].map { |url| create_entry(url:) }
raw_feed = create_raw_feed(story.feed, entries:)

expect(result).to eq([raw_feed.entries.first])
expect(described_class.call(raw_feed, story.feed_id))
.to eq([raw_feed.entries.last])
end

it "ignores stories older than 3 days" do
Expand Down
16 changes: 16 additions & 0 deletions spec/repositories/story_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ def create_feed(url: "http://blog.golang.org/feed.atom")
end
end

describe ".existing_entry_ids" do
it "returns the given entry ids that are stored for the feed" do
story = create(:story, entry_id: "stored")
ids = described_class.existing_entry_ids(["stored", "new"], story.feed_id)

expect(ids).to eq(["stored"])
end

it "ignores entry ids stored for other feeds" do
create(:story, entry_id: "stored")
ids = described_class.existing_entry_ids(["stored"], create(:feed).id)

expect(ids).to be_empty
end
end

describe ".unread" do
it "returns unread stories ordered by published date descending" do
story1 = create(:story, published: 5.minutes.ago)
Expand Down
Loading