add more "diaspora to federation entity" methods

move it to Diaspora::Federation::Entities and use it in some tests, but most of the tests can be removed later.
This commit is contained in:
Benjamin Neff 2016-01-10 21:14:30 +01:00
parent 88d91233d3
commit b1d30aa9cc
11 changed files with 220 additions and 141 deletions

View file

@ -25,7 +25,7 @@ class PostsController < ApplicationController
render locals: {post: post}
}
format.mobile { render locals: {post: post} }
format.xml { render xml: post.to_diaspora_xml }
format.xml { render xml: Diaspora::Federation.xml(Diaspora::Federation::Entities.post(post)) }
format.json { render json: PostPresenter.new(post, current_user) }
end
end

View file

@ -112,7 +112,7 @@ DiasporaFederation.configure do |config|
on :fetch_public_entity do |entity_type, guid|
entity = entity_type.constantize.find_by(guid: guid, public: true)
Diaspora::Federation.post(entity) if entity.is_a? Post
Diaspora::Federation::Entities.post(entity) if entity.is_a? Post
end
on :fetch_person_url_to do |diaspora_id, path|

View file

@ -1,78 +1,10 @@
module Diaspora
module Federation
def self.post(post)
case post
when StatusMessage
status_message(post)
when Reshare
reshare(post)
else
raise ArgumentError, "unknown post-class: #{post.class}"
end
end
def self.location(location)
DiasporaFederation::Entities::Location.new(
address: location.address,
lat: location.lat,
lng: location.lng
)
end
def self.photo(photo)
DiasporaFederation::Entities::Photo.new(
author: photo.diaspora_handle,
guid: photo.guid,
public: photo.public,
created_at: photo.created_at,
remote_photo_path: photo.remote_photo_path,
remote_photo_name: photo.remote_photo_name,
text: photo.text,
status_message_guid: photo.status_message_guid,
height: photo.height,
width: photo.width
)
end
def self.poll(poll)
DiasporaFederation::Entities::Poll.new(
guid: poll.guid,
question: poll.question,
poll_answers: poll.poll_answers.map {|answer| poll_answer(answer) }
)
end
def self.poll_answer(poll_answer)
DiasporaFederation::Entities::PollAnswer.new(
guid: poll_answer.guid,
answer: poll_answer.answer
)
end
def self.reshare(reshare)
DiasporaFederation::Entities::Reshare.new(
root_author: reshare.root_diaspora_id,
root_guid: reshare.root_guid,
author: reshare.diaspora_handle,
guid: reshare.guid,
public: reshare.public,
created_at: reshare.created_at,
provider_display_name: reshare.provider_display_name
)
end
def self.status_message(status_message)
DiasporaFederation::Entities::StatusMessage.new(
author: status_message.diaspora_handle,
guid: status_message.guid,
raw_message: status_message.raw_message,
photos: status_message.photos.map {|photo| photo(photo) },
location: status_message.location ? location(status_message.location) : nil,
poll: status_message.poll ? poll(status_message.poll) : nil,
public: status_message.public,
created_at: status_message.created_at,
provider_display_name: status_message.provider_display_name
)
# @deprecated
def self.xml(entity)
DiasporaFederation::Salmon::XmlPayload.pack(entity)
end
end
end
require "diaspora/federation/entities"

View file

@ -0,0 +1,133 @@
module Diaspora
module Federation
module Entities
def self.post(post)
case post
when StatusMessage
status_message(post)
when Reshare
reshare(post)
else
raise ArgumentError, "unknown post-class: #{post.class}"
end
end
def self.comment(comment)
DiasporaFederation::Entities::Comment.new(
author: comment.diaspora_handle,
guid: comment.guid,
parent_guid: comment.parent_guid,
text: comment.text
)
end
def self.location(location)
DiasporaFederation::Entities::Location.new(
address: location.address,
lat: location.lat,
lng: location.lng
)
end
def self.photo(photo)
DiasporaFederation::Entities::Photo.new(
author: photo.diaspora_handle,
guid: photo.guid,
public: photo.public,
created_at: photo.created_at,
remote_photo_path: photo.remote_photo_path,
remote_photo_name: photo.remote_photo_name,
text: photo.text,
status_message_guid: photo.status_message_guid,
height: photo.height,
width: photo.width
)
end
def self.poll(poll)
DiasporaFederation::Entities::Poll.new(
guid: poll.guid,
question: poll.question,
poll_answers: poll.poll_answers.map {|answer| poll_answer(answer) }
)
end
def self.poll_answer(poll_answer)
DiasporaFederation::Entities::PollAnswer.new(
guid: poll_answer.guid,
answer: poll_answer.answer
)
end
def self.profile(profile)
DiasporaFederation::Entities::Profile.new(
diaspora_id: profile.diaspora_handle,
first_name: profile.first_name,
last_name: profile.last_name,
image_url: profile.image_url,
image_url_medium: profile.image_url_medium,
image_url_small: profile.image_url_small,
birthday: profile.birthday,
gender: profile.gender,
bio: profile.bio,
location: profile.location,
searchable: profile.searchable,
nsfw: profile.nsfw,
tag_string: profile.tag_string
)
end
# @deprecated
def self.relayable_retraction(target, sender)
DiasporaFederation::Entities::RelayableRetraction.new(
target_guid: target.guid,
target_type: target.class.to_s,
author: sender.diaspora_handle
)
end
def self.reshare(reshare)
DiasporaFederation::Entities::Reshare.new(
root_author: reshare.root_diaspora_id,
root_guid: reshare.root_guid,
author: reshare.diaspora_handle,
guid: reshare.guid,
public: reshare.public,
created_at: reshare.created_at,
provider_display_name: reshare.provider_display_name
)
end
def self.retraction(target)
DiasporaFederation::Entities::Retraction.new(
target_guid: target.is_a?(User) ? target.person.guid : target.guid,
target_type: target.is_a?(User) ? Person.to_s : target.class.to_s,
author: target.diaspora_handle
)
end
# @deprecated
def self.signed_retraction(target, sender)
DiasporaFederation::Entities::SignedRetraction.new(
target_guid: target.guid,
target_type: target.class.to_s,
author: sender.diaspora_handle
)
end
def self.status_message(status_message)
DiasporaFederation::Entities::StatusMessage.new(
author: status_message.diaspora_handle,
guid: status_message.guid,
raw_message: status_message.raw_message,
photos: status_message.photos.map {|photo| photo(photo) },
location: status_message.location ? location(status_message.location) : nil,
poll: status_message.poll ? poll(status_message.poll) : nil,
public: status_message.public,
created_at: status_message.created_at,
provider_display_name: status_message.provider_display_name
)
end
end
end
end

View file

@ -78,7 +78,7 @@ describe PostsController, type: :controller do
it "responds with diaspora xml if format is xml" do
get :show, id: public.guid, format: :xml
expect(response.body).to eq(public.to_diaspora_xml)
expect(response.body).to eq(Diaspora::Federation.xml(Diaspora::Federation::Entities.post(public)).to_xml)
end
end

View file

@ -20,7 +20,7 @@ describe 'a user receives a post', :type => :request do
it 'should be able to parse and store a status message from xml' do
status_message = bob.post :status_message, :text => 'store this!', :to => @bobs_aspect.id
xml = status_message.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(status_message)).to_xml
bob.delete
status_message.destroy
@ -97,7 +97,7 @@ describe 'a user receives a post', :type => :request do
it 'does not update posts not marked as mutable' do
status = alice.post :status_message, :text => "store this!", :to => @alices_aspect.id
status.text = 'foo'
xml = status.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(status)).to_xml
receive_with_zord(bob, alice.person, xml)
@ -105,9 +105,16 @@ describe 'a user receives a post', :type => :request do
end
it 'updates posts marked as mutable' do
photo = alice.post(:photo, :user_file => uploaded_photo, :text => "Original", :to => @alices_aspect.id)
photo = alice.post(
:photo,
user_file: uploaded_photo,
text: "Original",
to: @alices_aspect.id
)
photo.text = 'foo'
xml = photo.to_diaspora_xml
photo.height = 42
photo.width = 23
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.photo(photo)).to_xml
bob.reload
receive_with_zord(bob, alice.person, xml)
@ -154,7 +161,7 @@ describe 'a user receives a post', :type => :request do
connect_users(alice, @alices_aspect, eve, @eves_aspect)
@post = alice.post(:status_message, :text => "hello", :to => @alices_aspect.id)
xml = @post.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(@post)).to_xml
receive_with_zord(bob, alice.person, xml)
receive_with_zord(eve, alice.person, xml)
@ -164,12 +171,13 @@ describe 'a user receives a post', :type => :request do
# After Eve creates her comment, it gets sent to Alice, who signs it with her private key
# before relaying it out to the contacts on the top-level post
comment.parent_author_signature = comment.sign_with_key(alice.encryption_key)
@xml = comment.to_diaspora_xml
@xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.comment(comment)).to_xml
comment.delete
comment_with_whitespace = alice.comment!(@post, ' I cannot lift my thumb from the spacebar ')
queue.drain_all
@xml_with_whitespace = comment_with_whitespace.to_diaspora_xml
comment_entity = Diaspora::Federation::Entities.comment(comment_with_whitespace)
@xml_with_whitespace = Diaspora::Federation.xml(comment_entity).to_xml
@guid_with_whitespace = comment_with_whitespace.guid
comment_with_whitespace.delete
end
@ -220,7 +228,7 @@ describe 'a user receives a post', :type => :request do
before do
@post = alice.post :status_message, :text => "hello", :to => @alices_aspect.id
xml = @post.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(@post)).to_xml
alice.share_with(eve.person, alice.aspects.first)
@ -231,7 +239,7 @@ describe 'a user receives a post', :type => :request do
it 'does not raise a `Mysql2::Error: Duplicate entry...` exception on save' do
inlined_jobs do
@comment = bob.comment!(@post, 'tada')
@xml = @comment.to_diaspora_xml
@xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.comment(@comment)).to_xml
expect {
receive_with_zord(alice, bob.person, @xml)
@ -245,27 +253,41 @@ describe 'a user receives a post', :type => :request do
describe 'receiving mulitple versions of the same post from a remote pod' do
before do
@local_luke, @local_leia, @remote_raphael = set_up_friends
@post = FactoryGirl.create(:status_message, :text => 'hey', :guid => '12313123', :author=> @remote_raphael, :created_at => 5.days.ago, :updated_at => 5.days.ago)
@post = FactoryGirl.build(
:status_message,
text: "hey",
guid: UUID.generate(:compact),
author: @remote_raphael,
created_at: 5.days.ago,
updated_at: 5.days.ago
)
end
it 'does not update created_at or updated_at when two people save the same post' do
@post = FactoryGirl.build(:status_message, :text => 'hey', :guid => '12313123', :author=> @remote_raphael, :created_at => 5.days.ago, :updated_at => 5.days.ago)
xml = @post.to_diaspora_xml
it "allows two people saving the same post" do
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(@post)).to_xml
receive_with_zord(@local_luke, @remote_raphael, xml)
old_time = Time.now+1
receive_with_zord(@local_leia, @remote_raphael, xml)
expect((Post.find_by_guid @post.guid).updated_at).to be < old_time
expect((Post.find_by_guid @post.guid).created_at).to be < old_time
expect(Post.find_by_guid(@post.guid).updated_at).to be < Time.now.utc + 1
expect(Post.find_by_guid(@post.guid).created_at.day).to eq(@post.created_at.day)
end
it 'does not update the post if a new one is sent with a new created_at' do
@post = FactoryGirl.build(:status_message, :text => 'hey', :guid => '12313123', :author => @remote_raphael, :created_at => 5.days.ago)
old_time = @post.created_at
xml = @post.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(@post)).to_xml
receive_with_zord(@local_luke, @remote_raphael, xml)
@post = FactoryGirl.build(:status_message, :text => 'hey', :guid => '12313123', :author => @remote_raphael, :created_at => 2.days.ago)
@post = FactoryGirl.build(
:status_message,
text: "hey",
guid: @post.guid,
author: @remote_raphael,
created_at: 2.days.ago
)
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(@post)).to_xml
receive_with_zord(@local_luke, @remote_raphael, xml)
expect((Post.find_by_guid @post.guid).created_at.day).to eq(old_time.day)
expect(Post.find_by_guid(@post.guid).created_at.day).to eq(old_time.day)
end
end
@ -290,28 +312,17 @@ describe 'a user receives a post', :type => :request do
let(:zord) { Postzord::Receiver::Private.new(alice, person: bob.person) }
it 'should accept retractions' do
retraction = Retraction.for(message)
xml = retraction.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.retraction(message)).to_xml
expect {
zord.parse_and_receive(xml)
}.to change(StatusMessage, :count).by(-1)
end
it 'should accept relayable retractions' do
comment = bob.comment! message, "and dogs"
retraction = RelayableRetraction.build(bob, comment)
xml = retraction.to_diaspora_xml
expect {
zord.parse_and_receive xml
}.to change(Comment, :count).by(-1)
end
it 'should accept signed retractions for public posts' do
message = bob.post(:status_message, text: "cats", public: true)
retraction = SignedRetraction.build(bob, message)
salmon = Postzord::Dispatcher::Public.salmon bob, retraction.to_diaspora_xml
retraction = Diaspora::Federation.xml(Diaspora::Federation::Entities.signed_retraction(message, bob)).to_xml
salmon = Postzord::Dispatcher::Public.salmon(bob, retraction)
xml = salmon.xml_for alice.person
zord = Postzord::Receiver::Public.new xml
@ -324,9 +335,13 @@ describe 'a user receives a post', :type => :request do
it 'should marshal a profile for a person' do
#Create person
person = bob.person
id = person.id
person.profile.delete
person.profile = Profile.new(:first_name => 'bob', :last_name => 'billytown', :image_url => "http://clown.com", :person_id => person.id)
person.profile = Profile.new(
first_name: "bob",
last_name: "billytown",
image_url: "http://clown.com/image.png",
person_id: person.id
)
person.save
#Cache profile for checking against marshaled profile
@ -334,7 +349,7 @@ describe 'a user receives a post', :type => :request do
new_profile.first_name = 'boo!!!'
#Build xml for profile
xml = new_profile.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.profile(new_profile)).to_xml
#Marshal profile
zord = Postzord::Receiver::Private.new(alice, :person => person)
zord.parse_and_receive(xml)

View file

@ -2,33 +2,26 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
require "spec_helper"
describe Diaspora::Parser do
before do
@user1 = alice
@user2 = bob
@user3 = eve
@aspect1 = @user1.aspects.first
@aspect2 = @user2.aspects.first
@aspect3 = @user3.aspects.first
@person = FactoryGirl.create(:person)
end
describe "parsing compliant XML object" do
it 'should be able to correctly parse comment fields' do
post = @user1.post :status_message, :text => "hello", :to => @aspect1.id
comment = FactoryGirl.create(:comment, :post => post, :author => @person, :diaspora_handle => @person.diaspora_handle, :text => "Freedom!")
comment.delete
xml = comment.to_diaspora_xml
it "should be able to correctly parse comment fields" do
user = FactoryGirl.create(:user)
post = alice.post :status_message, text: "hello", to: alice.aspects.first.id
comment = FactoryGirl.build(
:comment_entity,
parent_guid: post.guid,
author: user.diaspora_handle,
text: "Freedom!"
)
xml = Diaspora::Federation.xml(comment).to_xml
comment_from_xml = Diaspora::Parser.from_xml(xml)
expect(comment_from_xml.diaspora_handle).to eq(@person.diaspora_handle)
expect(comment_from_xml.diaspora_handle).to eq(user.diaspora_handle)
expect(comment_from_xml.post).to eq(post)
expect(comment_from_xml.text).to eq("Freedom!")
expect(comment_from_xml).not_to be comment
end
end
end

View file

@ -226,7 +226,9 @@ describe Photo, :type => :model do
url = @saved_photo.url
thumb_url = @saved_photo.url :thumb_medium
xml = @saved_photo.to_diaspora_xml
@saved_photo.height = 42
@saved_photo.width = 23
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.photo(@saved_photo)).to_xml
@saved_photo.destroy
zord = Postzord::Receiver::Private.new(user2, :person => @photo.author)

View file

@ -195,7 +195,7 @@ describe Post, :type => :model do
describe 'serialization' do
it 'should serialize the handle and not the sender' do
post = @user.post :status_message, :text => "hello", :to => @aspect.id
xml = post.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(post)).to_xml
expect(xml.include?("person_id")).to be false
expect(xml.include?(@user.person.diaspora_handle)).to be true

View file

@ -174,10 +174,10 @@ describe Profile, :type => :model do
end
describe 'serialization' do
let(:person) {FactoryGirl.build(:person,:diaspora_handle => "foobar" )}
let(:person) { FactoryGirl.build(:person, diaspora_handle: "foobar@localhost") }
it 'should include persons diaspora handle' do
xml = person.profile.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.profile(person.profile)).to_xml
expect(xml).to include "foobar"
end
@ -185,14 +185,14 @@ describe Profile, :type => :model do
person.profile.tag_string = '#one'
person.profile.build_tags
person.profile.save
xml = person.profile.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.profile(person.profile)).to_xml
expect(xml).to include "#one"
end
it 'includes location' do
person.profile.location = 'Dark Side, Moon'
person.profile.save
xml = person.profile.to_diaspora_xml
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.profile(person.profile)).to_xml
expect(xml).to include "Dark Side, Moon"
end
end

View file

@ -214,7 +214,9 @@ describe Reshare, type: :model do
expect(Person).to receive(:find_or_fetch_by_identifier).and_return(@original_author)
allow(@response).to receive(:body).and_return(root_object.to_diaspora_xml)
allow(@response).to receive(:body).and_return(
Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(root_object)).to_xml
)
expect(Faraday.default_connection).to receive(:get).with(
URI.join(
@ -251,7 +253,9 @@ describe Reshare, type: :model do
context "saving the post" do
before do
allow(@response).to receive(:body).and_return(root_object.to_diaspora_xml)
allow(@response).to receive(:body).and_return(
Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(root_object)).to_xml
)
allow(Faraday.default_connection).to receive(:get).with(
URI.join(
reshare.root.author.url,