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
33 changes: 26 additions & 7 deletions app/commands/feeds/import_from_opml.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
require_relative "../../models/feed"
require_relative "../../models/group"
require_relative "../../utils/opml_parser"

class ImportFromOpml
ONE_DAY = 24 * 60 * 60

def self.import(opml_contents)
feeds = OpmlParser.new.parse_feeds(opml_contents)
class << self
def import(opml_contents)
feeds_with_groups = OpmlParser.new.parse_feeds(opml_contents)

feeds.each do |feed|
Feed.create(name: feed[:name],
url: feed[:url],
last_fetched: Time.now - ONE_DAY)
# It considers a situation when feeds are already imported without groups,
# so it's possible to re-import the same subscriptions.xml just to set group_id
# for existing feeds. Feeds without groups are in 'Ungrouped' group, we don't
# create such group and create such feeds with group_id = nil.
#
feeds_with_groups.each do |group_name, parsed_feeds|
if parsed_feeds.size > 0
group = Group.where(name: group_name).first_or_create unless group_name == 'Ungrouped'

parsed_feeds.each { |parsed_feed| create_feed(parsed_feed, group) }
end
end
end

private

def create_feed(parsed_feed, group)
feed = Feed.where(name: parsed_feed[:name], url: parsed_feed[:url]).first_or_initialize
feed.last_fetched = Time.now - ONE_DAY if feed.new_record?
feed.group_id = group.id if group
feed.save
end
end
end
end
12 changes: 8 additions & 4 deletions app/commands/stories/mark_group_as_read.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
require_relative "../../repositories/story_repository"

class MarkGroupAsRead
KINDLING_GROUP_ID = 1
SPARKS_AND_KINDLING_GROUP_ID = 0
KINDLING_GROUP_ID = 0
SPARKS_GROUP_ID = -1

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about these constants. Are they needed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the Fever API docs, headings "Groups" and "Write". It turns out these constants are wrong, KINDLING_GROUP_ID should be 0 and SPARKS_GROUP_ID should be -1. Feel free to correct this.

As to your question if these constants are needed, I'd say that yes they are. Even though we don't have a notion of sparks for feeds, we should still handle requests using these IDs. My suggestion for handling these requests is to simply mark all feeds as read (which was the previous behavior).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so I changed constants and left the same behaviour for requests with such group_id.

def initialize(group_id, timestamp, repository = StoryRepository)
@group_id = group_id.to_i
@group_id = group_id
@repo = repository
@timestamp = timestamp
end

def mark_group_as_read
if [SPARKS_AND_KINDLING_GROUP_ID, KINDLING_GROUP_ID].include? @group_id
return unless @group_id

if [KINDLING_GROUP_ID, SPARKS_GROUP_ID].include?(@group_id.to_i)
@repo.fetch_unread_by_timestamp(@timestamp).update_all(is_read: true)
elsif @group_id.to_i > 0
@repo.fetch_unread_by_timestamp_and_group(@timestamp, @group_id).update_all(is_read: true)
end
end
end
Expand Down
12 changes: 4 additions & 8 deletions app/fever_api/read_feeds_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ def call(params = {})
private

def feeds_groups
[
@feed_repository.in_group.order('LOWER(name)').group_by(&:group_id).map do |group_id, feeds|
{
group_id: 1,
feed_ids: feeds.map{|f| f.id}.join(",")
group_id: group_id,
feed_ids: feeds.map(&:id).join(',')
}
]
end

def feeds
@feed_repository.list
end
end
end
end
13 changes: 7 additions & 6 deletions app/fever_api/read_groups.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require_relative "../repositories/group_repository"

module FeverAPI
class ReadGroups
def initialize(options = {})
@group_repository = options.fetch(:group_repository){ GroupRepository }
end

def call(params = {})
if params.keys.include?('groups')
{ groups: groups }
Expand All @@ -11,12 +17,7 @@ def call(params = {})
private

def groups
[
{
id: 1,
title: "All items"
}
]
@group_repository.list.map(&:as_fever_json)
end
end
end
1 change: 1 addition & 0 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Feed < ActiveRecord::Base
has_many :stories, -> {order "published desc"} , dependent: :delete_all
belongs_to :group

validates_uniqueness_of :url

Expand Down
7 changes: 7 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Group < ActiveRecord::Base
has_many :feeds

def as_fever_json
{ id: self.id, title: self.name }
end
end
4 changes: 4 additions & 0 deletions app/repositories/feed_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def self.list
Feed.order('lower(name)')
end

def self.in_group
Feed.where('group_id IS NOT NULL')
end

private

def self.valid_timestamp?(new_timestamp, current_timestamp)
Expand Down
7 changes: 7 additions & 0 deletions app/repositories/group_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative "../models/group"

class GroupRepository
def self.list
Group.order('LOWER(name)')
end
end
6 changes: 5 additions & 1 deletion app/repositories/story_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def self.fetch_by_ids(ids)

def self.fetch_unread_by_timestamp(timestamp)
timestamp = Time.at(timestamp.to_i)
Story.where("created_at < ? AND is_read = ?", timestamp, false)
Story.where('stories.created_at < ?', timestamp).where(is_read: false)
end

def self.fetch_unread_by_timestamp_and_group(timestamp, group_id)
fetch_unread_by_timestamp(timestamp).joins(:feed).where(feeds: { group_id: group_id })
end

def self.fetch_unread_for_feed_by_timestamp(feed_id, timestamp)
Expand Down
30 changes: 18 additions & 12 deletions app/utils/opml_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@ class OpmlParser
def parse_feeds(contents)
doc = Nokogiri.XML(contents)

doc.xpath("//body//outline").inject([]) do |feeds, outline|
next feeds if missing_fields? outline.attributes
feeds_with_groups = Hash.new { |h,k| h[k] = [] }

feeds << {
name: extract_name(outline.attributes).value,
url: outline.attributes["xmlUrl"].value
}
doc.xpath('//body/outline').each do |outline|

if outline.attributes['xmlUrl'].nil? # it's a group!
group_name = extract_name(outline.attributes).value
feeds = outline.xpath('./outline')
else # it's a top-level feed, which means it's a feed without group
group_name = 'Ungrouped'
feeds = [outline]
end

feeds.each do |feed|
feeds_with_groups[group_name] << { name: extract_name(feed.attributes).value,
url: feed.attributes['xmlUrl'].value }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the group names are unique, lines 17-21 could be simplified as follows.

feeds_with_groups[group_name] = feeds.map do |feed|
  { name: extract_name(feed.attributes).value, url: feed.attributes['xmlUrl'].value }
end

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such assumption makes sense to me, changed code according to your suggestion.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted this because when subscriptions.xml has at least two ungrouped feeds, then group names become not unique(two feeds will have the same name – Ungrouped).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, that's unfortunate. Thanks for trying!

end
end
feeds_with_groups
end

private
def missing_fields?(attributes)
attributes["xmlUrl"].nil? ||
(attributes["title"].nil? && attributes["text"].nil?)
end

def extract_name(attributes)
attributes["title"] || attributes["text"]
attributes['title'] || attributes['text']
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddGroupsTableAndForeignKeysToFeeds < ActiveRecord::Migration
def up
create_table :groups do |t|
t.string :name, null: false
t.timestamps null: false
end
add_column :feeds, :group_id, :integer
end

def down
drop_table :groups
remove_column :feeds, :group_id
end
end
7 changes: 7 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status"
t.integer "group_id"
end

add_index "feeds", ["url"], name: "index_feeds_on_url", unique: true, using: :btree

create_table "groups", force: true do |t|
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "stories", force: true do |t|
t.text "title"
t.text "permalink"
Expand Down
2 changes: 1 addition & 1 deletion spec/commands/feeds/add_new_feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
let(:discoverer) { double(discover: feed_result) }
let(:feed) { FeedFactory.build }
let(:repo) { double }

it "parses and creates the feed if discovered" do
repo.should_receive(:create).and_return(feed)

Expand Down
82 changes: 82 additions & 0 deletions spec/commands/feeds/import_from_opml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'spec_helper'

app_require "commands/feeds/import_from_opml"

describe ImportFromOpml do
let(:subscriptions) { File.open(File.expand_path('../../../support/files/subscriptions.xml', __FILE__)) }

def import
described_class.import(subscriptions)
end

after do
Feed.delete_all
Group.delete_all
end

let(:group_1 ) { Group.find_by_name('Football News') }
let(:group_2 ) { Group.find_by_name('RoR') }

context 'adding group_id for existing feeds' do
let!(:feed_1) { Feed.create(name: 'TMW Football Transfer News',
url: 'http://www.transfermarketweb.com/rss') }
let!(:feed_2) { Feed.create(name: 'GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS - Home',
url: 'http://feeds.feedburner.com/GiantRobotsSmashingIntoOtherGiantRobots') }
before { import }

it 'retains exising feeds' do
feed_1.should be_valid
feed_2.should be_valid
end

it 'creates new groups' do
group_1.should be
group_2.should be
end

it 'sets group_id for existing feeds' do
feed_1.reload.group.should eq group_1
feed_2.reload.group.should eq group_2
end
end

context 'creates new feeds with groups' do
let(:feed_1) { Feed.where(name: 'TMW Football Transfer News',
url: 'http://www.transfermarketweb.com/rss') }

let(:feed_2) { Feed.where(name: 'GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS - Home',
url: 'http://feeds.feedburner.com/GiantRobotsSmashingIntoOtherGiantRobots') }
before { import }

it 'creates groups' do
group_1.should be
group_1.should be
end

it 'creates feeds' do
feed_1.should exist
feed_2.should exist
end

it 'sets group' do
feed_1.first.group.should eq group_1
feed_2.first.group.should eq group_2
end
end

context 'creates new feeds without group' do
let(:feed_1) { Feed.where(name: 'Autoblog', url: 'http://feeds.autoblog.com/weblogsinc/autoblog/').first }
let(:feed_2) { Feed.where(name: 'City Guide News', url: 'http://www.probki.net/news/RSS_news_feed.asp').first }

before { import }

it 'does not create any new group for feeds without group' do
Group.where('id NOT IN (?)', [group_1.id, group_2.id]).count.should eq 0
end

it 'creates feeds without group_id' do
feed_1.group_id.should be_nil
feed_2.group_id.should be_nil
end
end
end
40 changes: 29 additions & 11 deletions spec/commands/stories/mark_group_as_read_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,44 @@
app_require "commands/stories/mark_group_as_read"

describe MarkGroupAsRead do
describe "#mark_group_as_read" do
describe '#mark_group_as_read' do
let(:stories) { double }
let(:repo){ double(fetch_unread_by_timestamp: stories) }
let(:repo) { double }
let(:timestamp) { Time.now.to_i }

it "marks group 0 as read" do
command = MarkGroupAsRead.new(0, Time.now.to_i, repo)
stories.should_receive(:update_all).with(is_read: true)
command.mark_group_as_read
def run_command(group_id)
MarkGroupAsRead.new(group_id, timestamp, repo)
end

it "marks group 1 as read" do
command = MarkGroupAsRead.new(1, Time.now.to_i, repo)
it 'marks group as read' do
command = run_command(2)
stories.should_receive(:update_all).with(is_read: true)
repo.should_receive(:fetch_unread_by_timestamp_and_group).with(timestamp, 2).and_return(stories)
command.mark_group_as_read
end

it "does not mark other groups as read" do
command = MarkGroupAsRead.new(2, Time.now.to_i, repo)
stories.should_not_receive(:update_all).with(is_read: true)
it 'does not mark any group as read when group is not provided' do
command = run_command(nil)
repo.should_not_receive(:fetch_unread_by_timestamp_and_group)
repo.should_not_receive(:fetch_unread_by_timestamp)
command.mark_group_as_read
end

context 'SPARKS_GROUP_ID and KINDLING_GROUP_ID' do
before do
stories.should_receive(:update_all).with(is_read: true)
repo.should_receive(:fetch_unread_by_timestamp).and_return(stories)
end

it 'marks as read all feeds when group is 0' do
command = run_command(0)
command.mark_group_as_read
end

it 'marks as read all feeds when group is -1' do
command = run_command(-1)
command.mark_group_as_read
end
end
end
end
1 change: 1 addition & 0 deletions spec/factories/feed_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def as_fever_json
def self.build(params = {})
FakeFeed.new(
id: rand(100),
group_id: params[:group_id] || rand(100),
name: params[:name] || Faker::Name.name + " on Software",
url: params[:url] || Faker::Internet.url,
last_fetched: params[:last_fetched] || Time.now,
Expand Down
Loading