diff --git a/Changelog.md b/Changelog.md index 0f739596a..6033f8556 100644 --- a/Changelog.md +++ b/Changelog.md @@ -166,6 +166,7 @@ The command will report queues that still have jobs and launch sidekiq process f * Correctly filter mentions on the server side [#6902](https://github.com/diaspora/diaspora/pull/6902) * Add aspects to the aspect membership dropdown when creating them on the getting started page [#6864](https://github.com/diaspora/diaspora/pull/6864) * Strip markdown from message preview in conversations list [#6923](https://github.com/diaspora/diaspora/pull/6923) +* Improve tag stream performance [#6903](https://github.com/diaspora/diaspora/pull/6903) ## Features * Support color themes [#6033](https://github.com/diaspora/diaspora/pull/6033) diff --git a/lib/stream/tag.rb b/lib/stream/tag.rb index d8f3ba06b..53372ba57 100644 --- a/lib/stream/tag.rb +++ b/lib/stream/tag.rb @@ -29,7 +29,16 @@ class Stream::Tag < Stream::Base end def posts - @posts ||= construct_post_query + @posts ||= if user + StatusMessage.user_tag_stream(user, tag.id) + else + StatusMessage.public_tag_stream(tag.id) + end + end + + def stream_posts + return [] unless tag + super end def tag_name=(tag_name) @@ -42,13 +51,4 @@ class Stream::Tag < Stream::Base def publisher_opts {:open => true} end - - def construct_post_query - posts = if user.present? - StatusMessage.owned_or_visible_by_user(user) - else - StatusMessage.all_public - end - posts.tagged_with(tag_name, :any => true) - end end diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index ebcf401ff..663251a10 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -107,6 +107,18 @@ describe TagsController, :type => :controller do get :show, :name => 'foo', :format => :mobile expect(response).to be_success end + + it "returns the post with the correct age" do + post2 = eve.post( + :status_message, + text: "#what #yes #hellyes #foo tagged second post", + public: true, + created_at: @post.created_at - 1.day + ) + get :show, name: "what", max_time: @post.created_at, format: :json + expect(JSON.parse(response.body).size).to be(1) + expect(JSON.parse(response.body).first["guid"]).to eq(post2.guid) + end end end end diff --git a/spec/lib/stream/tag_spec.rb b/spec/lib/stream/tag_spec.rb index 5e1205c83..75952680a 100644 --- a/spec/lib/stream/tag_spec.rb +++ b/spec/lib/stream/tag_spec.rb @@ -80,11 +80,30 @@ describe Stream::Tag do describe 'shared behaviors' do before do - @stream = Stream::Tag.new(FactoryGirl.create(:user), "test") + @stream = Stream::Tag.new(FactoryGirl.create(:user), FactoryGirl.create(:tag).name) end it_should_behave_like 'it is a stream' end + describe '#stream_posts' do + it "returns an empty array if the tag does not exist" do + stream = Stream::Tag.new(FactoryGirl.create(:user), "test") + expect(stream.stream_posts).to eq([]) + end + + it "returns an empty array if there are no visible posts for the tag" do + alice.post(:status_message, text: "#what", public: false, to: "all") + stream = Stream::Tag.new(nil, "what") + expect(stream.stream_posts).to eq([]) + end + + it "returns the post containing the tag" do + post = alice.post(:status_message, text: "#what", public: true) + stream = Stream::Tag.new(FactoryGirl.create(:user), "what") + expect(stream.stream_posts).to eq([post]) + end + end + describe '#tag_name=' do it 'downcases the tag' do stream = Stream::Tag.new(nil, "WHAT")