From 55ffc44a42961c6088582eb05165ec730e8ff68a Mon Sep 17 00:00:00 2001
From: Your Name <maxwell@joindiaspora.com>
Date: Thu, 6 Oct 2011 15:49:04 -0700
Subject: [PATCH] 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 9b3d8e60ce..5a9854f99f 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 9b5ffff39d..66c030b78b 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 16c1ea16db..d546c9684f 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 997fe55f27..d0ee9370d7 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 e9e83a9575..923868f269 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 1ec4d11ed7..55945abbaf 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 c8b0f3e930..b02cfcf055 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 d466aae6b7..178167438e 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 774e1b29ac..e7999fa46e 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 2b00f555ea..fd90b25c7c 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 9df52ea420..e615acc029 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 1425ebb7dc..878bd182dc 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<String>] 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 69ab65092e..d12f59d43b 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<Integer>] Ids of aspects for given stream
diff --git a/lib/stream/featured_users_stream.rb b/lib/stream/featured_users_stream.rb
index d90857276c..b58ad08164 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 0000000000..59bc1d0d67
--- /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<Post>] AR association of posts
+  def posts
+    @posts ||= Post.all_public.for_a_stream(max_time, order) 
+  end
+
+  # @return [ActiveRecord::Association<Person>] 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 0000000000..90b8ca09a2
--- /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");
+  });
+};
-- 
GitLab