diff --git a/app/commands/stories/mark_feed_as_read.rb b/app/commands/stories/mark_feed_as_read.rb new file mode 100644 index 000000000..bf664fb7a --- /dev/null +++ b/app/commands/stories/mark_feed_as_read.rb @@ -0,0 +1,14 @@ +require_relative "../../repositories/story_repository" + +class MarkFeedAsRead + def initialize(feed_id, timestamp, repository = StoryRepository) + @feed_id = feed_id.to_i + @repo = repository + @timestamp = timestamp + end + + def mark_feed_as_read + @repo.fetch_unread_for_feed_by_timestamp(@feed_id, @timestamp).update_all(is_read: true) + end +end + diff --git a/app/repositories/story_repository.rb b/app/repositories/story_repository.rb index 867a4396e..8692b6e58 100644 --- a/app/repositories/story_repository.rb +++ b/app/repositories/story_repository.rb @@ -25,6 +25,11 @@ def self.fetch_unread_by_timestamp(timestamp) Story.where("created_at < ? AND is_read = ?", timestamp, false) end + def self.fetch_unread_for_feed_by_timestamp(feed_id, timestamp) + timestamp = Time.at(timestamp.to_i) + Story.where(feed_id: feed_id).where("created_at < ? AND is_read = ?", timestamp, false) + end + def self.save(story) story.save end diff --git a/fever_api.rb b/fever_api.rb index d08357df2..76fe92cad 100644 --- a/fever_api.rb +++ b/fever_api.rb @@ -10,6 +10,7 @@ require_relative "app/commands/stories/mark_as_starred" require_relative "app/commands/stories/mark_as_unstarred" +require_relative "app/commands/stories/mark_feed_as_read" require_relative "app/commands/stories/mark_group_as_read" class FeverAPI < Sinatra::Base @@ -90,7 +91,8 @@ def get_response(params, is_json = true) response[:saved_item_ids] = all_starred_stories.map{|s| s.id}.join(",") end - if params[:mark] == "item" + case params[:mark] + when "item" case params[:as] when "read" MarkAsRead.new(params[:id]).mark_as_read @@ -101,7 +103,9 @@ def get_response(params, is_json = true) when "unsaved" MarkAsUnstarred.new(params[:id]).mark_as_unstarred end - elsif params[:mark] == "group" + when "feed" + MarkFeedAsRead.new(params[:id], params[:before]).mark_feed_as_read + when "group" MarkGroupAsRead.new(params[:id], params[:before]).mark_group_as_read end diff --git a/spec/commands/stories/mark_feed_as_read_spec.rb b/spec/commands/stories/mark_feed_as_read_spec.rb new file mode 100644 index 000000000..9eadbef7e --- /dev/null +++ b/spec/commands/stories/mark_feed_as_read_spec.rb @@ -0,0 +1,16 @@ +require "spec_helper" + +app_require "commands/stories/mark_feed_as_read" + +describe MarkFeedAsRead do + describe "#mark_feed_as_read" do + let(:stories) { stub } + let(:repo){ stub(fetch_unread_for_feed_by_timestamp: stories) } + + it "marks feed 1 as read" do + command = MarkFeedAsRead.new(1, Time.now.to_i, repo) + stories.should_receive(:update_all).with(is_read: true) + command.mark_feed_as_read + end + end +end