Commit 0b615eb0e2b5cca7685360c0cae72484741d672e

Authored by Jacob Vosmaer
1 parent 7339464e

Filter out old notes in NotesFinder

app/finders/notes_finder.rb
1 class NotesFinder 1 class NotesFinder
  2 + FETCH_OVERLAP = 5.seconds
  3 +
2 def execute(project, current_user, params) 4 def execute(project, current_user, params)
3 target_type = params[:target_type] 5 target_type = params[:target_type]
4 target_id = params[:target_id] 6 target_id = params[:target_id]
  7 + last_fetched_at = params.fetch(:last_fetched_at)
5 8
6 - case target_type 9 + notes = case target_type
7 when "commit" 10 when "commit"
8 project.notes.for_commit_id(target_id).not_inline.fresh 11 project.notes.for_commit_id(target_id).not_inline.fresh
9 when "issue" 12 when "issue"
@@ -15,5 +18,8 @@ class NotesFinder @@ -15,5 +18,8 @@ class NotesFinder
15 else 18 else
16 raise 'invalid target_type' 19 raise 'invalid target_type'
17 end 20 end
  21 +
  22 + # Use overlapping intervals to avoid worrying about race conditions
  23 + notes.where('updated_at > ?', last_fetched_at - FETCH_OVERLAP)
18 end 24 end
19 end 25 end
spec/finders/notes_finder_spec.rb
@@ -27,5 +27,12 @@ describe NotesFinder do @@ -27,5 +27,12 @@ describe NotesFinder do
27 params = { target_id: commit.id, target_type: 'invalid' } 27 params = { target_id: commit.id, target_type: 'invalid' }
28 expect { NotesFinder.new.execute(project, user, params) }.to raise_error('invalid target_type') 28 expect { NotesFinder.new.execute(project, user, params) }.to raise_error('invalid target_type')
29 end 29 end
  30 +
  31 + it 'filters out old notes' do
  32 + note2.update_attribute(:updated_at, 2.hours.ago)
  33 + params = { target_id: commit.id, target_type: 'commit', last_fetched_at: 1.hour.ago }
  34 + notes = NotesFinder.new.execute(project, user, params)
  35 + notes.should eq([note1])
  36 + end
30 end 37 end
31 end 38 end