Skip to content

Commit cbcdcb4

Browse files
authored
Allow empty name during import and use FeedDiscovery to try to figure the name out if possible (#1266)
While testing stringer locally, I tried to import my feeds from The Old Reader. The OPML generated by The Old Reader doesn't contain the feed names, so the import failed. I made a change to allow nils and the import went through, but then the feeds had no name and it was hard to manage the feeds. So I replicated the logic from `Feed::Create` to try to get a proper title for the Feed. This will make the import slower in the case there's nameless entries, but the result seems to be better. I'm falling back to the url if the call to the feed failed to avoid retrying on dead feeds.
1 parent 84fe822 commit cbcdcb4

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

app/commands/feed/import_from_opml.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ def find_or_create_group(group_name, user)
3030
def create_feed(parsed_feed, group, user)
3131
feed = user.feeds.where(**parsed_feed.slice(:name, :url))
3232
.first_or_initialize
33+
find_feed_name(feed, parsed_feed)
3334
feed.last_fetched = 1.day.ago if feed.new_record?
3435
feed.group_id = group.id if group
3536
feed.save
3637
end
38+
39+
def find_feed_name(feed, parsed_feed)
40+
return if feed.name?
41+
42+
result = FeedDiscovery.call(parsed_feed[:url])
43+
title = result.title if result
44+
feed.name = ContentSanitizer.call(title.presence || parsed_feed[:url])
45+
end
3746
end
3847
end

app/utils/opml_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def extract_name(attributes)
4040

4141
def feed_to_hash(feed)
4242
{
43-
name: extract_name(feed.attributes).value,
43+
name: extract_name(feed.attributes)&.value,
4444
url: feed.attributes["xmlUrl"].value
4545
}
4646
end

spec/commands/feed/import_from_opml_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
autoblog = { name: "Autoblog", url: "http://feeds.autoblog.com/weblogsinc/autoblog/" }
1515
city_guide = { name: "City Guide News", url: "http://www.probki.net/news/RSS_news_feed.asp" }
16+
macrumors = { name: "MacRumors: Mac News and Rumors - Front Page", url: "http://feeds.macrumors.com/MacRumors-Front" }
17+
dead_feed = { name: "http://deadfeed.example.com/feed.rss", url: "http://deadfeed.example.com/feed.rss" }
1618

1719
def read_subscriptions
1820
File.open(
@@ -28,6 +30,37 @@ def find_feed(feed_details)
2830
Feed.where(feed_details)
2931
end
3032

33+
before do
34+
stub_request(:get, "http://feeds.macrumors.com/MacRumors-Front").to_return(
35+
status: 200,
36+
body: File.read("spec/sample_data/feeds/feed01_valid_feed/feed.xml")
37+
)
38+
stub_request(
39+
:get,
40+
"http://deadfeed.example.com/feed.rss"
41+
).to_return(status: 404)
42+
end
43+
44+
context "adds title for existing feed" do
45+
it "changes existing feed if name is nil" do
46+
create_feed({ name: nil, url: macrumors[:url] })
47+
48+
described_class.call(read_subscriptions, user: default_user)
49+
50+
expect(find_feed(macrumors)).to exist
51+
end
52+
53+
it "keeps existing feed name if name is something else" do
54+
feed1 = create_feed({ name: "MacRumors", url: macrumors[:url] })
55+
56+
described_class.call(read_subscriptions, user: default_user)
57+
58+
expect(find_feed(macrumors)).not_to exist
59+
feed1.reload
60+
expect(feed1.name).to eq("MacRumors")
61+
end
62+
end
63+
3164
context "adding group_id for existing feeds" do
3265
it "retains exising feeds" do
3366
feed1 = create_feed(tmw_football)
@@ -69,6 +102,8 @@ def find_feed(feed_details)
69102

70103
expect(find_feed(tmw_football)).to exist
71104
expect(find_feed(giant_robots)).to exist
105+
expect(find_feed(macrumors)).to exist
106+
expect(find_feed(dead_feed)).to exist
72107
end
73108

74109
it "sets group" do

spec/support/files/subscriptions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@
2525
</outline>
2626
<outline title="Empty Group" text="Empty Group">
2727
</outline>
28+
<outline type="rss" xmlUrl="http://feeds.macrumors.com/MacRumors-Front"/>
29+
<outline type="rss" xmlUrl="http://deadfeed.example.com/feed.rss"/>
2830
</body>
2931
</opml>

0 commit comments

Comments
 (0)