From 3991903b56855d4ce0478744b8429b8bfdf38bec Mon Sep 17 00:00:00 2001
From: Dennis Collinson <dennis.collective@gmail.com>
Date: Fri, 3 Feb 2012 14:31:11 -0800
Subject: [PATCH] MS DG DC Participate stream that has things bubble to the top

---
 Gemfile                                       |  2 -
 Gemfile.lock                                  |  2 -
 app/controllers/streams_controller.rb         |  1 +
 app/models/app_config.rb                      |  1 +
 app/models/post.rb                            |  9 ++++
 app/models/status_message.rb                  |  8 ----
 ...120203220932_add_interacted_at_to_posts.rb |  9 ++++
 db/schema.rb                                  |  7 +--
 lib/diaspora/relayable.rb                     |  5 +++
 lib/evil_query.rb                             |  7 ++-
 lib/stream/participate.rb                     |  5 ++-
 public/javascripts/app/collections/posts.js   |  4 --
 spec/lib/evil_query_spec.rb                   | 44 +++++++++++++++++++
 spec/shared_behaviors/relayable.rb            | 10 +++++
 spec/shared_behaviors/stream.rb               |  4 +-
 15 files changed, 92 insertions(+), 26 deletions(-)
 create mode 100644 db/migrate/20120203220932_add_interacted_at_to_posts.rb

diff --git a/Gemfile b/Gemfile
index dd6af5113f..5aed8adde3 100644
--- a/Gemfile
+++ b/Gemfile
@@ -152,8 +152,6 @@ group :development do
   gem 'ruby-debug', :platforms => :mri_18
   gem 'yard', :require => false
 
-  # speed up development requests (already pulled into rails 3.2)
-  gem 'active_reload'
 
   # for tracing AR object instantiation and memory usage per request
   gem 'oink'
diff --git a/Gemfile.lock b/Gemfile.lock
index b42fb741bb..845a64f4f0 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -61,7 +61,6 @@ GEM
       rack-mount (~> 0.6.14)
       rack-test (~> 0.5.7)
       tzinfo (~> 0.3.23)
-    active_reload (0.6.1)
     activemodel (3.0.11)
       activesupport (= 3.0.11)
       builder (~> 2.1.2)
@@ -431,7 +430,6 @@ PLATFORMS
 
 DEPENDENCIES
   SystemTimer (= 1.2.3)
-  active_reload
   activerecord-import
   acts-as-taggable-on!
   acts_as_api
diff --git a/app/controllers/streams_controller.rb b/app/controllers/streams_controller.rb
index f7f00f7725..73a40a751b 100644
--- a/app/controllers/streams_controller.rb
+++ b/app/controllers/streams_controller.rb
@@ -32,6 +32,7 @@ class StreamsController < ApplicationController
   end
 
   def participate
+    puts params.inspect
     stream_responder(Stream::Participate)
   end
 
diff --git a/app/models/app_config.rb b/app/models/app_config.rb
index 572c483aec..fa67acb259 100644
--- a/app/models/app_config.rb
+++ b/app/models/app_config.rb
@@ -70,6 +70,7 @@ Please do the following:
 HELP
       Process.exit(1)
     end
+    self.setup!
   end
 
   def self.setup!
diff --git a/app/models/post.rb b/app/models/post.rb
index b8855afeda..b2fdee258b 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -55,6 +55,15 @@ class Post < ActiveRecord::Base
   #scopes
   scope :includes_for_a_stream, includes(:o_embed_cache, {:author => :profile}, :mentions => {:person => :profile}) #note should include root and photos, but i think those are both on status_message
 
+
+  scope :commented_by, lambda { |person|
+    select('DISTINCT posts.*').joins(:comments).where(:comments => {:author_id => person.id})
+  }
+
+  scope :liked_by, lambda { |person|
+    joins(:likes).where(:likes => {:author_id => person.id})
+  }
+
   def post_type
     self.class.name
   end
diff --git a/app/models/status_message.rb b/app/models/status_message.rb
index cc71788d24..3e6611e719 100644
--- a/app/models/status_message.rb
+++ b/app/models/status_message.rb
@@ -32,14 +32,6 @@ class StatusMessage < Post
     joins(:mentions).where(:mentions => {:person_id => person.id})
   }
 
-  scope :commented_by, lambda { |person|
-    select('DISTINCT posts.*').joins(:comments).where(:comments => {:author_id => person.id})
-  }
-
-  scope :liked_by, lambda { |person|
-    joins(:likes).where(:likes => {:author_id => person.id})
-  }
-
   def self.guids_for_author(person)
     Post.connection.select_values(Post.where(:author_id => person.id).select('posts.guid').to_sql)
   end
diff --git a/db/migrate/20120203220932_add_interacted_at_to_posts.rb b/db/migrate/20120203220932_add_interacted_at_to_posts.rb
new file mode 100644
index 0000000000..e8df0e5792
--- /dev/null
+++ b/db/migrate/20120203220932_add_interacted_at_to_posts.rb
@@ -0,0 +1,9 @@
+class AddInteractedAtToPosts < ActiveRecord::Migration
+  def self.up
+    add_column :posts, :interacted_at, :datetime
+  end
+
+  def self.down
+    remove_column :posts, :interacted_at
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 48dd8527f2..268630e55a 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20120202190701) do
+ActiveRecord::Schema.define(:version => 20120203220932) do
 
   create_table "account_deletions", :force => true do |t|
     t.string  "diaspora_handle"
@@ -316,6 +316,7 @@ ActiveRecord::Schema.define(:version => 20120202190701) do
     t.integer  "comments_count",                      :default => 0
     t.integer  "o_embed_cache_id"
     t.integer  "reshares_count",                      :default => 0
+    t.datetime "interacted_at"
   end
 
   add_index "posts", ["author_id", "root_guid"], :name => "index_posts_on_author_id_and_root_guid", :unique => true
@@ -336,8 +337,8 @@ ActiveRecord::Schema.define(:version => 20120202190701) do
     t.date     "birthday"
     t.string   "gender"
     t.text     "bio"
-    t.boolean  "searchable",                      :default => true, :null => false
-    t.integer  "person_id",                                         :null => false
+    t.boolean  "searchable",                      :default => true,  :null => false
+    t.integer  "person_id",                                          :null => false
     t.datetime "created_at"
     t.datetime "updated_at"
     t.string   "location"
diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb
index f8ef08f67f..d98b244c3a 100644
--- a/lib/diaspora/relayable.rb
+++ b/lib/diaspora/relayable.rb
@@ -18,6 +18,11 @@ module Diaspora
         validate :author_is_not_ignored
 
         delegate :public?, :to => :parent
+
+        after_create do
+          parent.touch(:interacted_at) if parent.respond_to?(:interacted_at)
+        end
+
       end
     end
 
diff --git a/lib/evil_query.rb b/lib/evil_query.rb
index 6d98b49a04..e507c78ad0 100644
--- a/lib/evil_query.rb
+++ b/lib/evil_query.rb
@@ -19,8 +19,7 @@ module EvilQuery
     def posts
       liked_post_ids = fetch_ids!(LikedPosts.new(@user).posts, "posts.id")
       commented_post_ids = fetch_ids!(CommentedPosts.new(@user).posts, "posts.id")
-
-      Post.where(:id => liked_post_ids + commented_post_ids)
+      Post.where(:id => liked_post_ids + commented_post_ids).order("posts.interacted_at DESC")
     end
   end
 
@@ -30,7 +29,7 @@ module EvilQuery
     end
 
     def posts
-      StatusMessage.liked_by(@user.person)
+      Post.liked_by(@user.person)
     end
   end
 
@@ -40,7 +39,7 @@ module EvilQuery
     end
 
     def posts
-      StatusMessage.commented_by(@user.person)
+      Post.commented_by(@user.person)
     end
   end
 
diff --git a/lib/stream/participate.rb b/lib/stream/participate.rb
index f942858ea2..4a5f269e13 100644
--- a/lib/stream/participate.rb
+++ b/lib/stream/participate.rb
@@ -3,6 +3,10 @@ class Stream::Participate < Stream::Base
     Rails.application.routes.url_helpers.participate_stream_path(opts)
   end
 
+  def order
+    "interacted_at"
+  end
+
   def title
     I18n.translate("streams.participate.title")
   end
@@ -11,5 +15,4 @@ class Stream::Participate < Stream::Base
   def posts
     @posts ||= EvilQuery::Participation.new(user).posts
   end
-
 end
\ No newline at end of file
diff --git a/public/javascripts/app/collections/posts.js b/public/javascripts/app/collections/posts.js
index 496d5a7545..37fb1a2069 100644
--- a/public/javascripts/app/collections/posts.js
+++ b/public/javascripts/app/collections/posts.js
@@ -8,9 +8,5 @@ app.collections.Posts = Backbone.Collection.extend({
 
   parse: function(resp){
     return resp.posts;
-  },
-
-  comparator : function(post) {
-    return -post.createdAt();
   }
 });
diff --git a/spec/lib/evil_query_spec.rb b/spec/lib/evil_query_spec.rb
index 8b75f07642..10ee9ffe45 100644
--- a/spec/lib/evil_query_spec.rb
+++ b/spec/lib/evil_query_spec.rb
@@ -14,4 +14,48 @@ describe EvilQuery::Participation do
     alice.comment!(@status_message, "hey")
     EvilQuery::Participation.new(alice).posts.should include(@status_message)
   end
+
+  it "should include your statusMessages" do
+    pending
+  end
+
+  describe "ordering" do
+    before do
+      @status_messageA = Factory(:status_message, :author => bob.person)
+      @status_messageB = Factory(:status_message, :author => bob.person)
+      @photoC = Factory(:activity_streams_photo, :author => bob.person)
+      @status_messageD = Factory(:status_message, :author => bob.person)
+      @status_messageE = Factory(:status_message, :author => bob.person)
+
+      time = Time.now
+
+      Timecop.freeze do
+        Timecop.travel time += 1.month
+
+        alice.comment!(@status_messageB, "party")
+        Timecop.travel time += 1.month
+
+        Factory(:like, :target => @status_messageA, :author => alice.person)
+        Timecop.travel time += 1.month
+
+        alice.comment!(@photoC, "party")
+        Timecop.travel time += 1.month
+
+        alice.comment!(@status_messageE, "party")
+      end
+
+      Timecop.return
+    end
+
+    let(:posts) {EvilQuery::Participation.new(alice).posts}
+
+    it "doesn't include Posts that aren't acted on" do
+      posts.map(&:id).should_not include(@status_messageD.id)
+      posts.map(&:id).should =~ [@status_messageA.id, @status_messageB.id, @photoC.id, @status_messageE.id]
+    end
+
+    it "returns the posts that the user has commented on or liked with the most recently acted on ones first" do
+      posts.map(&:id).should == [@status_messageE.id, @photoC.id, @status_messageA.id, @status_messageB.id]
+    end
+  end
 end
\ No newline at end of file
diff --git a/spec/shared_behaviors/relayable.rb b/spec/shared_behaviors/relayable.rb
index c3d414ca34..aa3168699f 100644
--- a/spec/shared_behaviors/relayable.rb
+++ b/spec/shared_behaviors/relayable.rb
@@ -7,6 +7,16 @@ require 'spec_helper'
 describe Diaspora::Relayable do
   shared_examples_for "it is relayable" do
 
+    describe 'interacted_at' do
+      it 'sets the interacted at of the parent to the created at of the relayable post' do
+        relayable = build_object
+        relayable.save
+        if relayable.parent.respond_to?(:interacted_at) #I'm sorry.
+          relayable.parent.interacted_at.to_i.should == relayable.created_at.to_i
+        end
+      end
+    end
+
     describe 'validations' do
       describe 'on :author_id' do
         context "the author is on the parent object author's ignore list when object is created" do
diff --git a/spec/shared_behaviors/stream.rb b/spec/shared_behaviors/stream.rb
index d2aea4785f..9ff20b3957 100644
--- a/spec/shared_behaviors/stream.rb
+++ b/spec/shared_behaviors/stream.rb
@@ -32,9 +32,9 @@ describe 'Streams' do
         @stream.max_time.should be_a(Time)
       end
 
-      it 'should default order to created_at' do
+      it 'should always have an order (default created_at)' do
         @stream.order=nil
-        @stream.order.should == 'created_at'
+        @stream.order.should_not be_nil
       end
 
       it 'initializes a publisher' do
-- 
GitLab