diff options
author | Jacob Vosmaer <contact@jacobvosmaer.nl> | 2014-04-28 14:13:29 +0400 |
---|---|---|
committer | Jacob Vosmaer <contact@jacobvosmaer.nl> | 2014-04-28 14:13:29 +0400 |
commit | 0b615eb0e2b5cca7685360c0cae72484741d672e (patch) | |
tree | f5f2540b9de83311b01146b6f0c2ba30f3dbda36 | |
parent | 7339464e7701c0778cca12c12ace83ebd8ffe2f7 (diff) |
Filter out old notes in NotesFinder
-rw-r--r-- | app/finders/notes_finder.rb | 8 | ||||
-rw-r--r-- | spec/finders/notes_finder_spec.rb | 7 |
2 files changed, 14 insertions, 1 deletions
diff --git a/app/finders/notes_finder.rb b/app/finders/notes_finder.rb index 4e80bd81757..38d78f3a2d5 100644 --- a/app/finders/notes_finder.rb +++ b/app/finders/notes_finder.rb @@ -1,9 +1,12 @@ class NotesFinder + FETCH_OVERLAP = 5.seconds + def execute(project, current_user, params) target_type = params[:target_type] target_id = params[:target_id] + last_fetched_at = params.fetch(:last_fetched_at) - case target_type + notes = case target_type when "commit" project.notes.for_commit_id(target_id).not_inline.fresh when "issue" @@ -15,5 +18,8 @@ class NotesFinder else raise 'invalid target_type' end + + # Use overlapping intervals to avoid worrying about race conditions + notes.where('updated_at > ?', last_fetched_at - FETCH_OVERLAP) end end diff --git a/spec/finders/notes_finder_spec.rb b/spec/finders/notes_finder_spec.rb index 27eaba8dfa1..ffd3f5db815 100644 --- a/spec/finders/notes_finder_spec.rb +++ b/spec/finders/notes_finder_spec.rb @@ -27,5 +27,12 @@ describe NotesFinder do params = { target_id: commit.id, target_type: 'invalid' } expect { NotesFinder.new.execute(project, user, params) }.to raise_error('invalid target_type') end + + it 'filters out old notes' do + note2.update_attribute(:updated_at, 2.hours.ago) + params = { target_id: commit.id, target_type: 'commit', last_fetched_at: 1.hour.ago } + notes = NotesFinder.new.execute(project, user, params) + notes.should eq([note1]) + end end end |