Skip to content

Commit e8fef7a

Browse files
committed
Allow groups in fever to be marked as read
1 parent f3b297f commit e8fef7a

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require_relative "../../repositories/story_repository"
2+
3+
class MarkGroupAsRead
4+
def initialize(group_id, timestamp, repository = StoryRepository)
5+
@group_id = group_id.to_i
6+
@repo = repository
7+
@timestamp = timestamp
8+
end
9+
10+
def mark_group_as_read
11+
@repo.fetch_unread_by_timestamp(@timestamp).update_all(is_read: true) if @group_id == 1
12+
end
13+
end
14+

app/repositories/story_repository.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def self.fetch_by_ids(ids)
2020
Story.where(id: ids)
2121
end
2222

23+
def self.fetch_unread_by_timestamp(timestamp)
24+
timestamp = Time.at(timestamp.to_i)
25+
Story.where("created_at < ? AND is_read = ?", timestamp, false)
26+
end
27+
2328
def self.save(story)
2429
story.save
2530
end

fever_api.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
require_relative "app/commands/stories/mark_as_starred"
1212
require_relative "app/commands/stories/mark_as_unstarred"
13+
require_relative "app/commands/stories/mark_group_as_read"
1314

1415
class FeverAPI < Sinatra::Base
1516
configure do
@@ -68,7 +69,7 @@ def get_response(params, is_json = true)
6869
if keys.include?(:items)
6970
if keys.include?(:with_ids)
7071
response[:items] = stories_by_ids(params[:with_ids].split(",")).map{|s| s.as_fever_json}
71-
response[:total_items] = stories_by_ids(params[:with_ids].split(",")).count
72+
response[:total_items] = stories_by_ids(params[:with_ids].split(",")).count
7273
else
7374
response[:items] = unread_stories(params[:since_id]).map{|s| s.as_fever_json}
7475
response[:total_items] = unread_stories.count
@@ -98,6 +99,8 @@ def get_response(params, is_json = true)
9899
when "unsaved"
99100
MarkAsUnstarred.new(params[:id]).mark_as_unstarred
100101
end
102+
elsif params[:mark] == "group"
103+
MarkGroupAsRead.new(params[:id], params[:before]).mark_group_as_read
101104
end
102105

103106
response.to_json
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "spec_helper"
2+
3+
app_require "commands/stories/mark_group_as_read"
4+
5+
describe MarkGroupAsRead do
6+
describe "#mark_group_as_read" do
7+
let(:stories) { stub }
8+
let(:repo){ stub(fetch_unread_by_timestamp: stories) }
9+
10+
it "marks group 1 as read" do
11+
command = MarkGroupAsRead.new(1, Time.now.to_i, repo)
12+
stories.should_receive(:update_all).with(is_read: true)
13+
command.mark_group_as_read
14+
end
15+
16+
it "odes not mark other groups as read" do
17+
command = MarkGroupAsRead.new(2, Time.now.to_i, repo)
18+
stories.should_not_receive(:update_all).with(is_read: true)
19+
command.mark_group_as_read
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)