From 55ffc44a42961c6088582eb05165ec730e8ff68a Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 6 Oct 2011 15:49:04 -0700 Subject: [PATCH 1/2] wip --- app/controllers/application_controller.rb | 13 ++++++++ app/controllers/featured_users_controller.rb | 10 +----- app/controllers/mentions_controller.rb | 11 +------ app/controllers/posts_controller.rb | 6 ++++ app/controllers/publics_controller.rb | 2 ++ app/controllers/tag_followings_controller.rb | 8 +---- app/helpers/stream_helper.rb | 2 ++ app/models/post.rb | 1 + config/locales/diaspora/en.yml | 3 +- config/routes.rb | 3 +- lib/base_stream.rb | 2 +- lib/diaspora/redis_cache.rb | 2 +- lib/stream/aspect_stream.rb | 1 - lib/stream/featured_users_stream.rb | 2 +- lib/stream/public_stream.rb | 33 ++++++++++++++++++++ public/javascripts/pages/posts-index.js | 10 ++++++ 16 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 lib/stream/public_stream.rb create mode 100644 public/javascripts/pages/posts-index.js diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9b3d8e60c..5a9854f99 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -147,6 +147,19 @@ class ApplicationController < ActionController::Base end end + def default_stream_action(stream_klass) + puts "yah" + authenticate_user! + save_sort_order + @stream = stream_klass.new(current_user, :max_time => params[:max_time], :order => sort_order) + + if params[:only_posts] + render :partial => 'shared/stream', :locals => {:posts => @stream.posts} + else + render 'aspects/index' + end + end + def sort_order is_mobile_device? ? 'created_at' : session[:sort_order] end diff --git a/app/controllers/featured_users_controller.rb b/app/controllers/featured_users_controller.rb index 9b5ffff39..66c030b78 100644 --- a/app/controllers/featured_users_controller.rb +++ b/app/controllers/featured_users_controller.rb @@ -1,15 +1,7 @@ require File.join(Rails.root, 'lib', 'stream', 'featured_users_stream') class FeaturedUsersController < ApplicationController - before_filter :authenticate_user! - before_filter :save_sort_order, :only => :index - def index - @stream = FeaturedUsersStream.new(current_user, :max_time => params[:max_time], :order => sort_order) - if params[:only_posts] - render :partial => 'shared/stream', :locals => {:posts => @stream.posts} - else - render 'aspects/index' - end + default_stream_action(FeaturedUsersStream) end end diff --git a/app/controllers/mentions_controller.rb b/app/controllers/mentions_controller.rb index 16c1ea16d..d546c9684 100644 --- a/app/controllers/mentions_controller.rb +++ b/app/controllers/mentions_controller.rb @@ -5,16 +5,7 @@ require File.join(Rails.root, 'lib','stream', 'mention_stream') class MentionsController < ApplicationController - before_filter :authenticate_user! - before_filter :save_sort_order, :only => :index - def index - @stream = MentionStream.new(current_user, :max_time => params[:max_time], :order => sort_order) - - if params[:only_posts] - render :partial => 'shared/stream', :locals => {:posts => @stream.posts} - else - render 'aspects/index' - end + default_stream_action(MentionStream) end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 997fe55f2..d0ee9370d 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -2,6 +2,8 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. +require File.join(Rails.root, 'lib', 'stream', 'public_stream') + class PostsController < ApplicationController before_filter :authenticate_user!, :except => :show before_filter :set_format_if_malformed_from_status_net, :only => :show @@ -59,6 +61,10 @@ class PostsController < ApplicationController end end + def index + default_stream_action(PublicStream) + end + def set_format_if_malformed_from_status_net request.format = :html if request.format == 'application/html+xml' end diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb index e9e83a957..923868f26 100644 --- a/app/controllers/publics_controller.rb +++ b/app/controllers/publics_controller.rb @@ -1,6 +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 File.join(Rails.root, 'lib', 'stream', 'public_stream') class PublicsController < ApplicationController require File.join(Rails.root, '/lib/diaspora/parser') @@ -13,6 +14,7 @@ class PublicsController < ApplicationController skip_before_filter :set_grammatical_gender before_filter :allow_cross_origin, :only => [:hcard, :host_meta, :webfinger] before_filter :check_for_xml, :only => [:receive, :receive_public] + before_filter :authenticate_user!, :only => [:index] respond_to :html respond_to :xml, :only => :post diff --git a/app/controllers/tag_followings_controller.rb b/app/controllers/tag_followings_controller.rb index 1ec4d11ed..55945abba 100644 --- a/app/controllers/tag_followings_controller.rb +++ b/app/controllers/tag_followings_controller.rb @@ -6,15 +6,9 @@ require File.join(Rails.root, 'lib', 'stream', 'tag_stream') class TagFollowingsController < ApplicationController before_filter :authenticate_user! - before_filter :save_sort_order, :only => :index def index - @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} - else - render 'aspects/index' - end + default_stream_action(TagStream) end # POST /tag_followings diff --git a/app/helpers/stream_helper.rb b/app/helpers/stream_helper.rb index c8b0f3e93..b02cfcf05 100644 --- a/app/helpers/stream_helper.rb +++ b/app/helpers/stream_helper.rb @@ -16,6 +16,8 @@ module StreamHelper featured_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), :sort_order => session[:sort_order]) + elsif controller.instance_of?(PostsController) + public_stream_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, :sort_order => session[:sort_order]) else diff --git a/app/models/post.rb b/app/models/post.rb index d466aae6b..178167438 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -41,6 +41,7 @@ class Post < ActiveRecord::Base def self.for_a_stream(max_time, order) by_max_time(max_time, order). includes_for_a_stream. + where(:type => BaseStream::TYPES_OF_POST_IN_STREAM). limit(15) end diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 774e1b29a..e7999fa46 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -839,7 +839,8 @@ en: tags: title: "Posts tagged: %{tags}" contacts_title: "People who dig these tags" - + public: + title: "Public Activity" users: logged_out: diff --git a/config/routes.rb b/config/routes.rb index 2b00f555e..fd90b25c7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,7 @@ Diaspora::Application.routes.draw do resources :comments, :only => [:new, :create, :destroy, :index] end get 'p/:id' => 'posts#show', :as => 'short_post' - + get 'public_stream' => 'posts#index', :as => 'public_stream' # roll up likes into a nested resource above resources :comments, :only => [:create, :destroy] do resources :likes, :only => [:create, :destroy, :index] @@ -137,6 +137,7 @@ Diaspora::Application.routes.draw do end + # External resources :authorizations, :only => [:index, :destroy] diff --git a/lib/base_stream.rb b/lib/base_stream.rb index 9df52ea42..e615acc02 100644 --- a/lib/base_stream.rb +++ b/lib/base_stream.rb @@ -1,4 +1,5 @@ class BaseStream + TYPES_OF_POST_IN_STREAM = ['StatusMessage', 'Reshare', 'ActivityStreams::Photo'] attr_accessor :max_time, :order, :user def initialize(user, opts={}) @@ -7,7 +8,6 @@ class BaseStream self.order = opts[:order] end - def random_featured_user Person.find_by_diaspora_handle(featured_diaspora_id) end diff --git a/lib/diaspora/redis_cache.rb b/lib/diaspora/redis_cache.rb index 1425ebb7d..878bd182d 100644 --- a/lib/diaspora/redis_cache.rb +++ b/lib/diaspora/redis_cache.rb @@ -78,7 +78,7 @@ class RedisCache # exposing the need to tie cache to a stream # @return [Array] Acceptable Post types for the given cache def self.acceptable_types - ::AspectStream::TYPES_OF_POST_IN_STREAM + BaseStream::TYPES_OF_POST_IN_STREAM end # Instantiate a redis connection diff --git a/lib/stream/aspect_stream.rb b/lib/stream/aspect_stream.rb index 69ab65092..d12f59d43 100644 --- a/lib/stream/aspect_stream.rb +++ b/lib/stream/aspect_stream.rb @@ -3,7 +3,6 @@ # the COPYRIGHT file. class AspectStream < BaseStream - TYPES_OF_POST_IN_STREAM = ['StatusMessage', 'Reshare', 'ActivityStreams::Photo'] # @param user [User] # @param inputted_aspect_ids [Array] Ids of aspects for given stream diff --git a/lib/stream/featured_users_stream.rb b/lib/stream/featured_users_stream.rb index d90857276..b58ad0816 100644 --- a/lib/stream/featured_users_stream.rb +++ b/lib/stream/featured_users_stream.rb @@ -20,7 +20,7 @@ class FeaturedUsersStream < BaseStream end def posts - Post.all_public.where(:author_id => people.map{|x| x.id}, :type => AspectStream::TYPES_OF_POST_IN_STREAM).for_a_stream(max_time, order) + Post.all_public.where(:author_id => people.map{|x| x.id}).for_a_stream(max_time, order) end def people diff --git a/lib/stream/public_stream.rb b/lib/stream/public_stream.rb new file mode 100644 index 000000000..59bc1d0d6 --- /dev/null +++ b/lib/stream/public_stream.rb @@ -0,0 +1,33 @@ +# Copyright (c) 2010-2011, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +class PublicStream < BaseStream + + def link(opts={}) + Rails.application.routes.url_helpers.public_stream_path(opts) + end + + def title + I18n.translate("streams.public.title") + end + + + # @return [ActiveRecord::Association] AR association of posts + def posts + @posts ||= Post.all_public.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 + + def contacts_title + "The last 15 people in this stream" + end + + def can_comment?(post) + post.author.local? + end +end diff --git a/public/javascripts/pages/posts-index.js b/public/javascripts/pages/posts-index.js new file mode 100644 index 000000000..90b8ca09a --- /dev/null +++ b/public/javascripts/pages/posts-index.js @@ -0,0 +1,10 @@ +Diaspora.Pages.PostsIndex = function() { + var self = this; + + this.subscribe("page/ready", function(evt, document) { + + self.aspectNavigation = self.instantiate("AspectNavigation", document.find("ul#aspect_nav")); + self.stream = self.instantiate("Stream", document.find("#aspect_stream_container")); + self.infiniteScroll = self.instantiate("InfiniteScroll"); + }); +}; From 5b9fbd19ed69dd5594743b39df71c9c0c7cb2d1b Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 6 Oct 2011 16:36:40 -0700 Subject: [PATCH 2/2] moar specs, a little perf upgrade for base_stream#person --- app/controllers/application_controller.rb | 1 - app/controllers/posts_controller.rb | 1 + app/helpers/publisher_helper.rb | 2 +- config/locales/diaspora/en.yml | 1 + lib/base_stream.rb | 16 +++++++++------- lib/stream/mention_stream.rb | 5 ----- lib/stream/public_stream.rb | 8 +------- lib/stream/tag_stream.rb | 4 ---- spec/controllers/posts_controller_spec.rb | 19 +++++++++++++++++++ spec/lib/stream/public_stream_spec.rb | 11 +++++++++++ 10 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 spec/lib/stream/public_stream_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5a9854f99..8fe708d97 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -148,7 +148,6 @@ class ApplicationController < ActionController::Base end def default_stream_action(stream_klass) - puts "yah" authenticate_user! save_sort_order @stream = stream_klass.new(current_user, :max_time => params[:max_time], :order => sort_order) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index d0ee9370d..cb36b646c 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -7,6 +7,7 @@ require File.join(Rails.root, 'lib', 'stream', 'public_stream') class PostsController < ApplicationController before_filter :authenticate_user!, :except => :show before_filter :set_format_if_malformed_from_status_net, :only => :show + before_filter :redirect_unless_admin, :only => :index respond_to :html, :mobile, diff --git a/app/helpers/publisher_helper.rb b/app/helpers/publisher_helper.rb index 516a20b28..235fafbdb 100644 --- a/app/helpers/publisher_helper.rb +++ b/app/helpers/publisher_helper.rb @@ -4,7 +4,7 @@ module PublisherHelper def public_value - params[:controller] == "tags" + params[:controller] == "tags" || params[:controller] == "posts" end def remote? diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index e7999fa46..5665e1cea 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -841,6 +841,7 @@ en: contacts_title: "People who dig these tags" public: title: "Public Activity" + contacts_title: "Recent Posters" users: logged_out: diff --git a/lib/base_stream.rb b/lib/base_stream.rb index e615acc02..e07077fe8 100644 --- a/lib/base_stream.rb +++ b/lib/base_stream.rb @@ -9,16 +9,16 @@ class BaseStream end def random_featured_user - Person.find_by_diaspora_handle(featured_diaspora_id) + @random_featured_user ||= Person.find_by_diaspora_handle(featured_diaspora_id) end def has_featured_users? - featured_diaspora_id.present? + random_featured_user.present? end #requied to implement said stream def link(opts={}) - Rails.application.routes.url_helpers.mentions_path(opts) + 'change me in lib/base_stream.rb!' end def can_comment?(post) @@ -26,15 +26,17 @@ class BaseStream end def title - 'a title' + 'change me in lib/base_stream.rb!' end def posts [] end + # @return [ActiveRecord::Association] AR association of people within stream's given aspects def people - [] + people_ids = posts.map{|x| x.author_id} + Person.where(:id => people_ids).includes(:profile) end def contacts_link_title @@ -42,11 +44,11 @@ class BaseStream end def contacts_title - "title for a stream" + 'change me in lib/base_stream.rb!' end def contacts_link - '#' + 'change me in lib/base_stream.rb!' end #helpers diff --git a/lib/stream/mention_stream.rb b/lib/stream/mention_stream.rb index fbf954ebb..069bad02d 100644 --- a/lib/stream/mention_stream.rb +++ b/lib/stream/mention_stream.rb @@ -17,11 +17,6 @@ class MentionStream< BaseStream @posts ||= StatusMessage.where_person_is_mentioned(self.user.person).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 - def contacts_title I18n.translate('streams.mentions.contacts_title') end diff --git a/lib/stream/public_stream.rb b/lib/stream/public_stream.rb index 59bc1d0d6..dbb813aa2 100644 --- a/lib/stream/public_stream.rb +++ b/lib/stream/public_stream.rb @@ -3,7 +3,6 @@ # the COPYRIGHT file. class PublicStream < BaseStream - def link(opts={}) Rails.application.routes.url_helpers.public_stream_path(opts) end @@ -12,19 +11,14 @@ class PublicStream < BaseStream I18n.translate("streams.public.title") end - # @return [ActiveRecord::Association] AR association of posts def posts @posts ||= Post.all_public.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 def contacts_title - "The last 15 people in this stream" + I18n.translate("streams.public.contacts_title") end def can_comment?(post) diff --git a/lib/stream/tag_stream.rb b/lib/stream/tag_stream.rb index fcec4e473..47ccc5ec4 100644 --- a/lib/stream/tag_stream.rb +++ b/lib/stream/tag_stream.rb @@ -20,10 +20,6 @@ class TagStream < BaseStream for_a_stream(@max_time, @order) end - def people - @people ||= posts.map{|p| p.author}.uniq - end - def contacts_title I18n.translate('streams.tags.contacts_title') end diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 6e0da0ab4..df4ac9a0a 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -146,4 +146,23 @@ describe PostsController do StatusMessage.exists?(message.id).should be_true end end + + describe '#index' do + before do + sign_in alice + end + + it 'will succeed if admin' do + AppConfig[:admins] = [alice.username] + get :index + response.should be_success + end + + it 'will redirect if not' do + AppConfig[:admins] = [] + get :index + response.should be_redirect + end + + end end diff --git a/spec/lib/stream/public_stream_spec.rb b/spec/lib/stream/public_stream_spec.rb new file mode 100644 index 000000000..3d272ed09 --- /dev/null +++ b/spec/lib/stream/public_stream_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' +require File.join(Rails.root, 'spec', 'shared_behaviors', 'stream') +describe PublicStream do + before do + @stream = PublicStream.new(stub) + end + + describe 'shared behaviors' do + it_should_behave_like 'it is a stream' + end +end