diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb index dc1a338fb..1b59bd1ef 100644 --- a/app/controllers/publics_controller.rb +++ b/app/controllers/publics_controller.rb @@ -66,7 +66,7 @@ class PublicsController < ApplicationController end def post - @post = Post.where(:id => params[:id], :public => true).includes(:author, :comments => :author).first + @post = Post.where(:guid => params[:guid], :public => true).includes(:author, :comments => :author).first #hax to upgrade logged in users who can comment if @post diff --git a/app/models/reshare.rb b/app/models/reshare.rb index 08853dee9..fb1c59542 100644 --- a/app/models/reshare.rb +++ b/app/models/reshare.rb @@ -3,10 +3,32 @@ class Reshare < Post validate :root_must_be_public attr_accessible :root_id, :public + xml_attr :root_diaspora_id + xml_attr :root_guid + + attr_accessible :root_diaspora_id, :root_guid + before_validation do self.public = true end + 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 if local_reshare.root.author_id == user.person.id diff --git a/config/routes.rb b/config/routes.rb index dca33bae5..42ff03d3f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,7 +18,7 @@ Diaspora::Application.routes.draw do resources :likes, :only => [:create, :destroy, :index] resources :comments, :only => [:create, :destroy, :index] end - get 'p/:id' => 'publics#post', :as => 'public_post' + get 'p/:guid' => 'publics#post', :as => 'public_post' # roll up likes into a nested resource above resources :comments, :only => [:create, :destroy] do diff --git a/spec/models/reshare_spec.rb b/spec/models/reshare_spec.rb index 523f6cc78..10b93d261 100644 --- a/spec/models/reshare_spec.rb +++ b/spec/models/reshare_spec.rb @@ -1,6 +1,13 @@ require 'spec_helper' describe Reshare do + include ActionView::Helpers::UrlHelper + include Rails.application.routes.url_helpers + def controller + mock() + end + + it 'has a valid Factory' do Factory(:reshare).should be_valid end @@ -33,4 +40,64 @@ describe Reshare do @root.resharers.should include(@reshare.author) end end + + describe "XML" do + before do + @reshare = Factory(:reshare) + @xml = @reshare.to_xml.to_s + pp @xml + end + + context 'serialization' do + it 'serializes root_diaspora_id' do + @xml.should include("root_diaspora_id") + end + + it 'serializes root_guid' do + @xml.should include("root_guid") + end + end + + context 'marshalling' do + context 'local' do + before do + @original_author = @reshare.root.author.dup + @root_object = @reshare.root.dup + end + + it 'fetches the root post from root_guid' do + Reshare.from_xml(@xml).root.should == @root_object + end + + it 'fetches the root author from root_diaspora_id' do + Reshare.from_xml(@xml).root.author.should == @original_author + end + end + + 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 + end + + it 'fetches the root author from root_diaspora_id' do + person = Factory.build(:person) + wf_prof_mock = mock + wf_prof_mock.should_receive(:fetch).and_return(person) + Webfinger.should_receive(:new).and_return(wf_prof_mock) + Reshare.from_xml(@xml) + end + end + end + end end