From 5a8024a71aa74289cf834ab7de0c6f7d71f94420 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Fri, 14 Oct 2011 11:50:38 -0700 Subject: [PATCH] MS DG; some stream refactoring; can comment now exists in base stream; default behavior is to allow you to comment on locals post, and then remote people if you are connected with them --- lib/stream/aspect.rb | 9 +++++++ lib/stream/base.rb | 35 +++++++++++++++++++++++++--- lib/stream/followed_tag.rb | 9 ------- spec/lib/stream/base_spec.rb | 35 +++++++++++++++++++++++++++- spec/lib/stream/followed_tag_spec.rb | 22 ----------------- spec/shared_behaviors/stream.rb | 4 ---- 6 files changed, 75 insertions(+), 39 deletions(-) diff --git a/lib/stream/aspect.rb b/lib/stream/aspect.rb index 86d9f817f..7f8888cfe 100644 --- a/lib/stream/aspect.rb +++ b/lib/stream/aspect.rb @@ -117,4 +117,13 @@ class Stream::Aspect< Stream::Base Rails.application.routes.url_helpers.contacts_path(:a_id => aspect.id) end end + + # This is perfomance optimization, as everyone in your aspect stream you have + # a contact. + # + # @param post [Post] + # @return [Boolean] + def can_comment?(post) + true + end end diff --git a/lib/stream/base.rb b/lib/stream/base.rb index b19d05fc3..0f8f31085 100644 --- a/lib/stream/base.rb +++ b/lib/stream/base.rb @@ -8,10 +8,12 @@ class Stream::Base self.order = opts[:order] end + # @return [Person] def random_featured_user @random_featured_user ||= Person.find_by_diaspora_handle(featured_diaspora_id) end + # @return [Boolean] def has_featured_users? random_featured_user.present? end @@ -21,14 +23,18 @@ class Stream::Base 'change me in lib/base_stream.rb!' end + # @return [Boolean] def can_comment?(post) - true + return true if post.author.local? + post_is_from_contact?(post) end + # @return [String] def title 'a title' end + # @return [ActiveRecord::Relation] def posts [] end @@ -39,23 +45,28 @@ class Stream::Base Person.where(:id => people_ids).includes(:profile) end + # @return [String] def contacts_link_title I18n.translate('aspects.selected_contacts.view_all_contacts') end + # @return [String] def contacts_title 'change me in lib/base_stream.rb!' end + # @return [String] def contacts_link '#' end #helpers + # @return [Boolean] def ajax_stream? false end - + + # @return [Boolean] def for_all_aspects? true end @@ -64,9 +75,10 @@ class Stream::Base #NOTE: MBS bad bad methods the fact we need these means our views are foobared. please kill them and make them #private methods on the streams that need them def aspects - @user.aspects + user.aspects end + # @return [Aspect] The first aspect in #aspects def aspect aspects.first end @@ -86,7 +98,24 @@ class Stream::Base end private + + # Memoizes all Contacts present in the Stream + # + # @return [Array] + def contacts_in_stream + @contacts_in_stream ||= Contact.where(:user_id => user.id, :person_id => people.map{|x| x.id}).all + end + def featured_diaspora_id @featured_diaspora_id ||= AppConfig[:featured_users].try(:sample, 1) end + + # @param post [Post] + # @return [Boolean] + def post_is_from_contact?(post) + @can_comment_cache ||= {} + @can_comment_cache[post.id] ||= contacts_in_stream.find{|contact| contact.person_id == post.author.id}.present? + @can_comment_cache[post.id] ||= user.person.id == post.author.id + @can_comment_cache[post.id] + end end diff --git a/lib/stream/followed_tag.rb b/lib/stream/followed_tag.rb index 5b7a5f4ab..40a69b817 100644 --- a/lib/stream/followed_tag.rb +++ b/lib/stream/followed_tag.rb @@ -22,16 +22,7 @@ class Stream::FollowedTag < Stream::Base I18n.translate('streams.tags.contacts_title') end - def can_comment?(post) - @can_comment_cache ||= {} - @can_comment_cache[post.id] ||= contacts_in_stream.find{|contact| contact.person_id == post.author.id}.present? - @can_comment_cache[post.id] ||= user.person.id == post.author.id - @can_comment_cache[post.id] - end - def contacts_in_stream - @contacts_in_stream ||= Contact.where(:user_id => user.id, :person_id => people.map{|x| x.id}).all - end private diff --git a/spec/lib/stream/base_spec.rb b/spec/lib/stream/base_spec.rb index f1f6f60ad..f901a8d2b 100644 --- a/spec/lib/stream/base_spec.rb +++ b/spec/lib/stream/base_spec.rb @@ -2,7 +2,40 @@ require 'spec_helper' require File.join(Rails.root, 'spec', 'shared_behaviors', 'stream') describe Stream::Base do before do - @stream = Stream::Base.new(stub) + @stream = Stream::Base.new(alice) + end + + describe '.can_comment?' do + before do + @person = Factory(:person) + @stream.stub(:people).and_return([bob.person, eve.person, @person]) + end + + it 'returns true if user is a contact of the post author' do + post = Factory(:status_message, :author => bob.person) + @stream.can_comment?(post).should be_true + end + + it 'returns true if a user is the author of the post' do + post = Factory(:status_message, :author => alice.person) + @stream.can_comment?(post).should be_true + end + + it 'returns true if the author of the post is local' do + post = Factory(:status_message, :author => eve.person) + @stream.can_comment?(post).should be_true + end + + it 'returns true if person is remote and is a contact' do + Contact.create!(:user => @stream.user, :person => @person) + post = Factory(:status_message, :author => @person) + @stream.can_comment?(post).should be_true + end + + it 'returns false if person is remote and not a contact' do + post = Factory(:status_message, :author => @person) + @stream.can_comment?(post).should be_false + end end describe 'shared behaviors' do diff --git a/spec/lib/stream/followed_tag_spec.rb b/spec/lib/stream/followed_tag_spec.rb index 2bc19a548..805915bff 100644 --- a/spec/lib/stream/followed_tag_spec.rb +++ b/spec/lib/stream/followed_tag_spec.rb @@ -10,26 +10,4 @@ describe Stream::FollowedTag do describe 'shared behaviors' do it_should_behave_like 'it is a stream' end - - describe '.can_comment?' do - before do - @stream = Stream::FollowedTag.new(alice) - @stream.stub(:people).and_return([bob.person]) - end - - it 'returns true if user is a contact of the post author' do - post = Factory(:status_message, :author => bob.person) - @stream.can_comment?(post).should be_true - end - - it 'returns true if a user is the author of the post' do - post = Factory(:status_message, :author => alice.person) - @stream.can_comment?(post).should be_true - end - - it 'returns false otherwise' do - post = Factory(:status_message, :author => eve.person) - @stream.can_comment?(post).should be_false - end - end end diff --git a/spec/shared_behaviors/stream.rb b/spec/shared_behaviors/stream.rb index ea90a5513..01af8b497 100644 --- a/spec/shared_behaviors/stream.rb +++ b/spec/shared_behaviors/stream.rb @@ -40,10 +40,6 @@ describe 'Streams' do @stream.order=nil @stream.order.should == 'created_at' end - - it 'should have can_comment?(post)' do - @stream.can_comment?(Factory(:status_message)).should_not be_nil - end end end end