From 9a4c478a7bf946e731f3b3d6152ce73ce19b5afa Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg <maxwell@joindiaspora.com> Date: Fri, 30 Sep 2011 19:10:01 -0700 Subject: [PATCH] these spes are really green. too bad we need #moar --- app/controllers/application_controller.rb | 4 ++++ app/controllers/tags_controller.rb | 19 +++++-------------- app/helpers/stream_helper.rb | 6 +++--- app/models/post.rb | 5 +++-- app/models/status_message.rb | 11 +++++++++++ features/step_definitions/custom_web_steps.rb | 1 + features/step_definitions/session_steps.rb | 1 + lib/aspect_stream.rb | 10 +++++----- lib/base_stream.rb | 2 -- lib/tag_stream.rb | 2 +- spec/lib/aspect_stream_spec.rb | 4 ++-- spec/models/post_spec.rb | 14 ++++++++++++++ spec/models/status_message_spec.rb | 10 ++++++++++ 13 files changed, 60 insertions(+), 29 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index adf460372d..4860a53b7c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -150,4 +150,8 @@ class ApplicationController < ActionController::Base def sort_order is_mobile_device? ? 'created_at' : session[:sort_order] end + + def max_time + params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now + end end diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index df448d52b2..bff20bb072 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -47,27 +47,17 @@ class TagsController < ApplicationController def show params[:name].downcase! @aspect = :tag + if current_user - @posts = StatusMessage. - joins("LEFT OUTER JOIN post_visibilities ON post_visibilities.post_id = posts.id"). - joins("LEFT OUTER JOIN contacts ON contacts.id = post_visibilities.contact_id"). - where(Contact.arel_table[:user_id].eq(current_user.id).or( - StatusMessage.arel_table[:public].eq(true).or( - StatusMessage.arel_table[:author_id].eq(current_user.person.id) - ) - )).select('DISTINCT posts.*') + @posts = StatusMessage.owned_or_visible_by_user(current_user) else @posts = StatusMessage.all_public end - params[:prefill] = "##{params[:name]} " - @posts = @posts.tagged_with(params[:name]) - - max_time = params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now - @posts = @posts.where(StatusMessage.arel_table[:created_at].lt(max_time)) - @posts = @posts.includes({:author => :profile}, :comments, :photos).order('posts.created_at DESC').limit(15) + @posts = @posts.tagged_with(params[:name]).for_a_stream(max_time) @commenting_disabled = true + params[:prefill] = "##{params[:name]} " if params[:only_posts] render :partial => 'shared/stream', :locals => {:posts => @posts} @@ -84,4 +74,5 @@ class TagsController < ApplicationController end @tag_followed end + end diff --git a/app/helpers/stream_helper.rb b/app/helpers/stream_helper.rb index 33228ac2d3..68ef837cd6 100644 --- a/app/helpers/stream_helper.rb +++ b/app/helpers/stream_helper.rb @@ -11,11 +11,11 @@ module StreamHelper elsif controller.instance_of?(PeopleController) person_path(@person, :max_time => @posts.last.created_at.to_i) elsif controller.instance_of?(TagFollowingsController) - tag_followings_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream)) + tag_followings_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream), :sort_order => session[:sort_order]) elsif controller.instance_of?(MentionsController) - mentions_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream)) + mentions_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream), :sort_order => session[:sort_order]) elsif controller.instance_of?(AspectsController) - aspects_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream), :a_ids => @stream.aspect_ids) + aspects_path(:max_time => time_for_scroll(opts[:ajax_stream], @stream), :a_ids => @stream.aspect_ids, :sort_order => session[:sort_order]) else raise 'in order to use pagination for this new controller, update next_page_path in stream helper' end diff --git a/app/models/post.rb b/app/models/post.rb index 5464120208..baeb3f9f02 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -33,10 +33,11 @@ class Post < ActiveRecord::Base validates :guid, :uniqueness => true scope :all_public, where(:public => true, :pending => false) + scope :includes_for_a_stream, includes({:author => :profile}, :mentions => {:person => :profile}) #note should include root and photos, but i think those are both on status_message - def self.for_a_stream(max_time, order) + def self.for_a_stream(max_time, order='created_at') where("posts.#{order} < ?", max_time).order("posts.#{order} desc"). - includes({:author => :profile}, :mentions => {:person => :profile}). + includes_for_a_stream. limit(15) end diff --git a/app/models/status_message.rb b/app/models/status_message.rb index c580f5733e..1c0f6b0be0 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -26,8 +26,19 @@ class StatusMessage < Post after_create :create_mentions + #scopes scope :where_person_is_mentioned, lambda{|person| joins(:mentions).where(:mentions => {:person_id => person.id})} + def self.owned_or_visible_by_user(user) + joins("LEFT OUTER JOIN post_visibilities ON post_visibilities.post_id = posts.id"). + joins("LEFT OUTER JOIN contacts ON contacts.id = post_visibilities.contact_id"). + where(Contact.arel_table[:user_id].eq(user.id).or( + StatusMessage.arel_table[:public].eq(true).or( + StatusMessage.arel_table[:author_id].eq(user.person.id) + ) + )).select('DISTINCT posts.*') + end + def text(opts = {}) self.formatted_message(opts) end diff --git a/features/step_definitions/custom_web_steps.rb b/features/step_definitions/custom_web_steps.rb index e37235364b..17b87b19bb 100644 --- a/features/step_definitions/custom_web_steps.rb +++ b/features/step_definitions/custom_web_steps.rb @@ -181,6 +181,7 @@ And /^I scroll down$/ do evaluate_script("window.scrollBy(0,3000000)") sleep 1 wait_until(30) { evaluate_script('$("#infscr-loading:visible").length') == 0 } + And "I wait for the ajax to finish" end Then /^the notification dropdown should be visible$/ do diff --git a/features/step_definitions/session_steps.rb b/features/step_definitions/session_steps.rb index df84adf9b3..6ca42600ac 100644 --- a/features/step_definitions/session_steps.rb +++ b/features/step_definitions/session_steps.rb @@ -25,6 +25,7 @@ When /^I sign in as "([^"]*)"$/ do |email| @me = User.find_by_email(email) @me.password ||= 'password' Given 'I am signed in' + And 'I wait for the ajax to finish' end When /^I sign in with password "([^"]*)"$/ do |password| diff --git a/lib/aspect_stream.rb b/lib/aspect_stream.rb index 371d0658ca..8dc22b1e1e 100644 --- a/lib/aspect_stream.rb +++ b/lib/aspect_stream.rb @@ -1,7 +1,7 @@ # Copyright (c) 2010-2011, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. - +require 'base_stream' class AspectStream < BaseStream TYPES_OF_POST_IN_STREAM = ['StatusMessage', 'Reshare', 'ActivityStreams::Photo'] @@ -41,14 +41,14 @@ class AspectStream < BaseStream # NOTE(this should be something like Post.all_for_stream(@user, aspect_ids, {}) that calls visible_posts @posts ||= @user.visible_posts(:by_members_of => aspect_ids, :type => TYPES_OF_POST_IN_STREAM, - :order => "#{@order} DESC", - :max_time => @max_time + :order => "#{order} DESC", + :max_time => max_time ).for_a_stream(max_time, order) end # @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects def people - @people ||= Person.all_from_aspects(aspect_ids, @user).includes(:profile) + @people ||= Person.all_from_aspects(aspect_ids, user).includes(:profile) end def link(opts={}) @@ -96,7 +96,7 @@ class AspectStream < BaseStream if for_all_aspects? || aspect_ids.size > 1 Rails.application.routes.url_helpers.contacts_path else - Rails.application.routes.url_helpers.contacts_path(:a_id => @stream.aspect.id) + Rails.application.routes.url_helpers.contacts_path(:a_id => aspect.id) end end end diff --git a/lib/base_stream.rb b/lib/base_stream.rb index c2eb82118a..d8c0cefcf5 100644 --- a/lib/base_stream.rb +++ b/lib/base_stream.rb @@ -1,6 +1,4 @@ class BaseStream - - attr_accessor :max_time, :order, :user def initialize(user, opts={}) diff --git a/lib/tag_stream.rb b/lib/tag_stream.rb index 594be8a1f5..26a0c1a8a1 100644 --- a/lib/tag_stream.rb +++ b/lib/tag_stream.rb @@ -17,7 +17,7 @@ class TagStream < BaseStream if tag_string.empty? [] else - @posts ||= StatusMessage.tagged_with([@tag_string], :any => true).where(:public => true).for_a_stream(@max_time, @order) + @posts ||= StatusMessage.owned_or_visible_by_user(user).tagged_with([@tag_string], :any => true).where(:public => true).for_a_stream(@max_time, @order) end end diff --git a/spec/lib/aspect_stream_spec.rb b/spec/lib/aspect_stream_spec.rb index f08acdc47d..d3fc8612ea 100644 --- a/spec/lib/aspect_stream_spec.rb +++ b/spec/lib/aspect_stream_spec.rb @@ -2,7 +2,7 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -require 'aspect_stream' +require 'spec_helper' describe AspectStream do describe '#aspects' do @@ -70,7 +70,7 @@ describe AspectStream do it 'respects max_time' do stream = AspectStream.new(@alice, [1,2], :max_time => 123) - @alice.should_receive(:visible_posts).with(hash_including(:max_time => 123)).and_return(stub.as_null_object) + @alice.should_receive(:visible_posts).with(hash_including(:max_time => instance_of(Time))).and_return(stub.as_null_object) stream.posts end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 07701aeb15..30c06b11de 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -10,6 +10,20 @@ describe Post do @aspect = @user.aspects.create(:name => "winners") end + describe 'scopes' do + describe '.for_a_stream' do + it 'returns the posts ordered and limited by unix time' + it 'includes everything in .includes_for_a_stream' + it 'is limited to 15 posts' + end + + describe 'includes for a stream' do + it 'inclues author profile and mentions' + it 'should include photos and root of reshares(but does not)' + end + end + + describe 'validations' do it 'validates uniqueness of guid and does not throw a db error' do message = Factory(:status_message) diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 826dcd4e80..66e685b7f8 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -17,6 +17,16 @@ describe StatusMessage do @aspect = @user.aspects.first end + describe 'scopes' do + describe '.where_person_is_mentioned' do + it 'returns status messages where the given person is mentioned' + end + + describe '.owned_or_visible_by_user' do + it 'scopes status_messages based on posts visisble via contacts or public' + end + end + describe '.before_create' do it 'calls build_tags' do status = Factory.build(:status_message) -- GitLab