diff --git a/app/controllers/tag_followings_controller.rb b/app/controllers/tag_followings_controller.rb index 4992554e0..1ec4d11ed 100644 --- a/app/controllers/tag_followings_controller.rb +++ b/app/controllers/tag_followings_controller.rb @@ -9,7 +9,6 @@ class TagFollowingsController < ApplicationController before_filter :save_sort_order, :only => :index def index - @commenting_disabled = true @stream = TagStream.new(current_user, :max_time => params[:max_time], :order => sort_order) if params[:only_posts] render :partial => 'shared/stream', :locals => {:posts => @stream.posts} diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 1551aa41d..192b2891a 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -31,11 +31,13 @@ module CommentsHelper end end - def commenting_disabled? + def commenting_disabled?(post) return true unless user_signed_in? if defined?(@commenting_disabled) - return @commenting_disabled - else + @commenting_disabled + elsif defined?(@stream) + !@stream.can_comment?(post) + else false end end diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index 365f57c36..fd8a9834e 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -22,7 +22,7 @@ .likes_container = render "likes/likes_container", :target_id => comment.id, :likes_count => comment.likes_count, :target_type => "Comment" - - unless commenting_disabled? + - unless commenting_disabled?(post) %span.like_action = like_action(comment, current_user) diff --git a/app/views/comments/_comments.html.haml b/app/views/comments/_comments.html.haml index 857c8f796..b7df16c82 100644 --- a/app/views/comments/_comments.html.haml +++ b/app/views/comments/_comments.html.haml @@ -13,6 +13,6 @@ -else = render :partial => 'comments/comment', :collection => post.comments, :locals => {:post => post} - - unless commenting_disabled? + - unless commenting_disabled?(post) .new_comment_form_wrapper{:class => comment_form_wrapper_class(post)} = new_comment_form(post.id, current_user) diff --git a/app/views/comments/create.js.erb b/app/views/comments/create.js.erb index 96650cf8b..e9840a2f2 100644 --- a/app/views/comments/create.js.erb +++ b/app/views/comments/create.js.erb @@ -1,3 +1,3 @@ ContentUpdater.addCommentToPost("<%= @comment.post.guid %>", "<%= @comment.guid%>", - "<%= escape_javascript(render(:partial => 'comments/comment', :locals => { :comment => @comment, :person => current_user.person}))%>"); \ No newline at end of file + "<%= escape_javascript(render(:partial => 'comments/comment', :locals => { :comment => @comment, :person => current_user.person, :post => @comment.post}))%>"); diff --git a/app/views/people/show.html.haml b/app/views/people/show.html.haml index 27887dd22..10ccd9937 100644 --- a/app/views/people/show.html.haml +++ b/app/views/people/show.html.haml @@ -31,7 +31,7 @@ = render 'photos/index', :photos => @posts - else #main_stream.stream - = render 'shared/stream', :posts => @posts, :commenting_disabled => commenting_disabled? + = render 'shared/stream', :posts => @posts #pagination =link_to(t('more'), next_page_path, :class => 'paginate') diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml index bed2cdd68..effc52129 100644 --- a/app/views/posts/show.html.haml +++ b/app/views/posts/show.html.haml @@ -8,7 +8,7 @@ - if @post.is_a?(Photo) = render 'posts/photo', :post => @post - else - = render 'shared/stream_element', :post => @post, :commenting_disabled => commenting_disabled? + = render 'shared/stream_element', :post => @post, :commenting_disabled => commenting_disabled?(@post) %br %br %br diff --git a/app/views/posts/show.mobile.haml b/app/views/posts/show.mobile.haml index 767952d8d..84d886908 100644 --- a/app/views/posts/show.mobile.haml +++ b/app/views/posts/show.mobile.haml @@ -4,4 +4,4 @@ .stream = render :partial => 'shared/stream_element', - :locals => {:post => @post, :commenting_disabled => commenting_disabled?, :expanded_info => true} + :locals => {:post => @post, :commenting_disabled => commenting_disabled?(@post), :expanded_info => true} diff --git a/app/views/shared/_stream.haml b/app/views/shared/_stream.haml index ee849ca11..74a06e827 100644 --- a/app/views/shared/_stream.haml +++ b/app/views/shared/_stream.haml @@ -4,6 +4,4 @@ = render :partial => 'shared/stream_element', :collection => posts, - :as => :post, - :locals => { :commenting_disabled => commenting_disabled?} - + :as => :post diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml index 38ddc9079..e2f3dcb1c 100644 --- a/app/views/shared/_stream_element.html.haml +++ b/app/views/shared/_stream_element.html.haml @@ -68,7 +68,7 @@ – - - unless commenting_disabled? + - unless commenting_disabled?(post) %span.like_action = like_action(post, current_user) @@ -84,4 +84,4 @@ .likes_container = render "likes/likes_container", :target_id => post.id, :likes_count => post.likes_count, :current_user => current_user, :target_type => "Post" - = render "comments/comments", :post => post, :current_user => current_user, :commenting_disabled => commenting_disabled? + = render "comments/comments", :post => post, :current_user => current_user, :commenting_disabled => commenting_disabled?(post) diff --git a/diaspora_development b/diaspora_development new file mode 100644 index 000000000..ae9b55f67 Binary files /dev/null and b/diaspora_development differ diff --git a/diaspora_test b/diaspora_test new file mode 100644 index 000000000..9bfaabec2 Binary files /dev/null and b/diaspora_test differ diff --git a/lib/base_stream.rb b/lib/base_stream.rb index d8c0cefcf..780ed6e37 100644 --- a/lib/base_stream.rb +++ b/lib/base_stream.rb @@ -14,6 +14,10 @@ class BaseStream Rails.application.routes.url_helpers.mentions_path(opts) end + def can_comment?(post) + true + end + def title 'a title' end diff --git a/lib/stream/tag_stream.rb b/lib/stream/tag_stream.rb index 772a933a0..fcec4e473 100644 --- a/lib/stream/tag_stream.rb +++ b/lib/stream/tag_stream.rb @@ -14,16 +14,12 @@ class TagStream < BaseStream # @return [ActiveRecord::Association] AR association of posts def posts - if tag_string.empty? - [] - else - @posts ||= StatusMessage.owned_or_visible_by_user(user). - joins(:tags).where(:tags => {:name => tag_array}). - for_a_stream(@max_time, @order) - end + return [] if tag_string.empty? + @posts ||= StatusMessage.owned_or_visible_by_user(user). + joins(:tags).where(:tags => {:name => tag_array}). + for_a_stream(@max_time, @order) end - # @return [ActiveRecord::Association] AR association of people within stream's given aspects def people @people ||= posts.map{|p| p.author}.uniq end @@ -32,6 +28,17 @@ class TagStream < BaseStream 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 def tag_string diff --git a/public/javascripts/inbox.js b/public/javascripts/inbox.js index 8792dba63..1e4515aa1 100644 --- a/public/javascripts/inbox.js +++ b/public/javascripts/inbox.js @@ -26,6 +26,7 @@ $(document).ready(function(){ }); } + jQuery("abbr.timeago").timeago(); return false; }); diff --git a/spec/helpers/comments_helper_spec.rb b/spec/helpers/comments_helper_spec.rb index 7880ab4df..f784858f4 100644 --- a/spec/helpers/comments_helper_spec.rb +++ b/spec/helpers/comments_helper_spec.rb @@ -1,21 +1,42 @@ require 'spec_helper' describe CommentsHelper do - before do - @user = alice - @aspect = @user.aspects.first - @post = @user.post(:status_message, :text => "hi", :to => @aspect.id) - end - it 'renders a new comment form' do - new_comment_form(@post.id, @user).should == - @controller.render_to_string(:partial => 'comments/new_comment', - :locals => {:post_id => @post.id, :current_user => @user}) - end - it 'renders it fast the second time' do - new_comment_form(@post.id, @user) - time = Benchmark.realtime{ + describe '.new_comment_form' do + before do + @user = alice + @aspect = @user.aspects.first + @post = @user.post(:status_message, :text => "hi", :to => @aspect.id) + end + it 'renders a new comment form' do + new_comment_form(@post.id, @user).should == + @controller.render_to_string(:partial => 'comments/new_comment', + :locals => {:post_id => @post.id, :current_user => @user}) + end + it 'renders it fast the second time' do new_comment_form(@post.id, @user) - } - (time*1000).should < 1 + time = Benchmark.realtime{ + new_comment_form(@post.id, @user) + } + (time*1000).should < 1 + end + end + + describe 'commenting_disabled?' do + it 'returns true if @commenting_disabled is set' do + @commenting_disabled = true + commenting_disabled?(stub).should_be true + @commenting_disabled = false + commenting_disabled?(stub).should_be false + end + + it 'returns @stream.can_comment? if @stream is set' do + post = stub + @stream = stub + @stream.should_receive(:can_comment?).with(post).and_return(true) + commenting_disabled?(post).should_be true + + @stream.should_receive(:can_comment?).with(post).and_return(false) + commenting_disabled?(post).should_be false + end end end diff --git a/spec/lib/stream/tag_stream_spec.rb b/spec/lib/stream/tag_stream_spec.rb index 8b99e037c..deff89821 100644 --- a/spec/lib/stream/tag_stream_spec.rb +++ b/spec/lib/stream/tag_stream_spec.rb @@ -10,4 +10,26 @@ describe TagStream do describe 'shared behaviors' do it_should_behave_like 'it is a stream' end + + describe '.can_comment?' do + before do + @stream = TagStream.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 01af8b497..ea90a5513 100644 --- a/spec/shared_behaviors/stream.rb +++ b/spec/shared_behaviors/stream.rb @@ -40,6 +40,10 @@ 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