Skip to content

Commit

Permalink
Make it possible to mark feeds as read through Fever API
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenj committed Jul 26, 2013
1 parent f442704 commit 71baa13
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/commands/stories/mark_feed_as_read.rb
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions app/repositories/story_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions fever_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
16 changes: 16 additions & 0 deletions spec/commands/stories/mark_feed_as_read_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 71baa13

Please sign in to comment.