From bdc4b9f746eca12b50b5d37fd329eeb0bfa75cc5 Mon Sep 17 00:00:00 2001
From: danielgrippi <danielgrippi@gmail.com>
Date: Thu, 14 Jul 2011 15:32:04 -0700
Subject: [PATCH] marsharing reshares now grabs the post from the original
 poster, also fetcheer that person if they don't exist locally still need to
 ping for xml and give the right xml back

---
 app/controllers/publics_controller.rb       |  7 ++++-
 app/models/reshare.rb                       | 23 ++++++++--------
 app/views/shared/_stream_element.html.haml  |  2 +-
 features/posts.feature                      |  1 +
 spec/controllers/publics_controller_spec.rb | 27 ++++++++++++++++---
 spec/models/reshare_spec.rb                 | 30 +++++++++++++--------
 6 files changed, 63 insertions(+), 27 deletions(-)

diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb
index 1b59bd1efc..c58b7f917c 100644
--- a/app/controllers/publics_controller.rb
+++ b/app/controllers/publics_controller.rb
@@ -66,7 +66,12 @@ class PublicsController < ApplicationController
   end
 
   def post
-    @post = Post.where(:guid => params[:guid], :public => true).includes(:author, :comments => :author).first
+
+    if params[:guid].to_s.length <= 8
+      @post = Post.where(:id => params[:guid], :public => true).includes(:author, :comments => :author).first
+    else
+      @post = Post.where(:guid => params[:guid], :public => true).includes(:author, :comments => :author).first
+    end
 
     #hax to upgrade logged in users who can comment
     if @post
diff --git a/app/models/reshare.rb b/app/models/reshare.rb
index fb1c59542e..8eb0303d78 100644
--- a/app/models/reshare.rb
+++ b/app/models/reshare.rb
@@ -1,4 +1,5 @@
 class Reshare < Post 
+
   belongs_to :root, :class_name => 'Post'
   validate :root_must_be_public
   attr_accessible :root_id, :public
@@ -6,8 +7,6 @@ class Reshare < Post
   xml_attr :root_diaspora_id
   xml_attr :root_guid
 
-  attr_accessible :root_diaspora_id, :root_guid
-
   before_validation do 
     self.public = true
   end
@@ -15,19 +14,10 @@ class Reshare < Post
   def root_guid
     self.root.guid  
   end
-  def root_guid= rg
-    #self.root = Post.where(:guid => rg).first
-    debugger
-    person = Person.where(:diaspora_handle => self[:root_diaspora_id]).first
-    Faraday.get(person.url + public_post_path(:guid => rg))
-  end
 
   def root_diaspora_id
     self.root.author.diaspora_handle
   end
-  def root_diaspora_id= id
-    Webfinger.new(id).fetch
-  end
 
   def receive(user, person)
     local_reshare = Reshare.where(:guid => self.guid).first
@@ -45,6 +35,17 @@ class Reshare < Post
 
   private
 
+  def after_parse
+    root_author = Webfinger.new(@root_diaspora_id).fetch
+    root_author.save!
+
+    unless self.root = Post.where(:guid => @root_guid).first
+      self.root = Diaspora::Parser.from_xml(Faraday.get(root_author.url + "/p/#{@root_guid}").body)
+      self.root.save!
+    end
+    
+  end
+
   def root_must_be_public
     if self.root.nil? || !self.root.public
       errors[:base] << "you must reshare public posts"
diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml
index a4a1f735fa..0a4a3ef13c 100644
--- a/app/views/shared/_stream_element.html.haml
+++ b/app/views/shared/_stream_element.html.haml
@@ -9,7 +9,7 @@
 
   - else
     .right.controls
-      = link_to image_tag('deletelabel.png'), post_path(post), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete stream_element_delete", :title => t('delete')
+      = link_to image_tag('deletelabel.png'), post_visibility_path(:id => "42", :post_id => post.id), :method => :put, :remote => true, :class => "delete stream_element_delete", :title => t('hide')
 
   .undo_text.hidden
     = t('post_visibilites.update.post_hidden', :name => post.author.name)
diff --git a/features/posts.feature b/features/posts.feature
index 74b9f024e0..ed6b226c74 100644
--- a/features/posts.feature
+++ b/features/posts.feature
@@ -64,6 +64,7 @@ Feature: posting
         And I am on "bob@bob.bob"'s page
 
         And I hover over the ".stream_element"
+        And I preemptively confirm the alert
         And I click to delete the first post
         And I wait for the ajax to finish
         And I go to "bob@bob.bob"'s page
diff --git a/spec/controllers/publics_controller_spec.rb b/spec/controllers/publics_controller_spec.rb
index 0e4b6d4651..8f55400fc3 100644
--- a/spec/controllers/publics_controller_spec.rb
+++ b/spec/controllers/publics_controller_spec.rb
@@ -65,22 +65,43 @@ describe PublicsController do
     it 'shows a public post' do
       status = alice.post(:status_message, :text => "hello", :public => true, :to => 'all')
 
-      get :post, :id => status.id
+      get :post, :guid => status.id
       response.status= 200
     end
 
     it 'does not show a private post' do
       status = alice.post(:status_message, :text => "hello", :public => false, :to => 'all')
-      get :post, :id => status.id
+      get :post, :guid => status.id
       response.status = 302
     end
 
     it 'redirects to the proper show page if the user has visibility of the post' do
       status = alice.post(:status_message, :text => "hello", :public => true, :to => 'all')
       sign_in bob
-      get :post, :id => status.id
+      get :post, :guid => status.id
       response.should be_redirect
     end
+
+    # We want to be using guids from now on for this post route, but do not want to break
+    # preexisiting permalinks.  We can assume a guid is 8 characters long as we have
+    # guids set to hex(8) since we started using them.
+    context 'id/guid switch' do
+      before do
+        @status = alice.post(:status_message, :text => "hello", :public => true, :to => 'all')
+      end
+
+      it 'assumes guids less than 8 chars are ids and not guids' do
+        Post.should_receive(:where).with(hash_including(:id => @status.id)).and_return(Post)
+        get :post, :guid => @status.id
+        response.status= 200
+      end
+
+      it 'assumes guids more than (or equal to) 8 chars are actually guids' do
+        Post.should_receive(:where).with(hash_including(:guid => @status.guid)).and_return(Post)
+        get :post, :guid => @status.guid
+        response.status= 200
+      end
+    end
   end
 
   describe '#hcard' do
diff --git a/spec/models/reshare_spec.rb b/spec/models/reshare_spec.rb
index 10b93d2613..c84890052b 100644
--- a/spec/models/reshare_spec.rb
+++ b/spec/models/reshare_spec.rb
@@ -45,7 +45,6 @@ describe Reshare do
     before do
       @reshare = Factory(:reshare)
       @xml = @reshare.to_xml.to_s
-      pp @xml
     end
 
     context 'serialization' do
@@ -76,25 +75,34 @@ describe Reshare do
 
       context 'remote' do
         before do
-          @original_profile = @reshare.root.author.profile
-          @original_author = @reshare.root.author.delete
           @root_object = @reshare.root.delete
         end
 
         it 'fetches the root post from root_guid' do
-          @original_profile.save!
-          pp @original_profile
-          @original_author.save!
-          pp @original_author
-          Faraday.should_receive(:get).with(@original_author.url + public_post_path(:guid => @reshare.guid)).and_return(@root_object.to_diaspora_xml)
-          Reshare.from_xml(@xml).root.should == @root_object
+          response = mock
+          response.stub(:body).and_return(@root_object.to_diaspora_xml)
+          Faraday.default_connection.should_receive(:get).with(@reshare.root.author.url + public_post_path(:guid => @root_object.guid)).and_return(response)
+
+          root = Reshare.from_xml(@xml).root
+
+          [:text, :guid, :diaspora_handle, :type].each do |attr|
+            root.send(attr).should == @reshare.root.send(attr)
+          end
         end
 
         it 'fetches the root author from root_diaspora_id' do
-          person = Factory.build(:person)
+          @original_profile = @reshare.root.author.profile
+          @original_author = @reshare.root.author.delete
+
           wf_prof_mock = mock
-          wf_prof_mock.should_receive(:fetch).and_return(person)
+          wf_prof_mock.should_receive(:fetch).and_return(@original_author)
           Webfinger.should_receive(:new).and_return(wf_prof_mock)
+
+          response = mock
+          response.stub(:body).and_return(@root_object.to_diaspora_xml)
+
+          Faraday.default_connection.should_receive(:get).with(@original_author.url + public_post_path(:guid => @root_object.guid)).and_return(response)
+
           Reshare.from_xml(@xml)
         end
       end
-- 
GitLab