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

This commit is contained in:
Maxwell Salzberg 2011-10-14 11:50:38 -07:00
parent 01d7b9507b
commit 5a8024a71a
6 changed files with 75 additions and 39 deletions

View file

@ -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

View file

@ -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<Post>]
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<Contact>]
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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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