From 2294e59ee3190b2c563c652cd096a1abd9df3a1c Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 28 Jun 2015 18:23:33 +0200 Subject: [PATCH] change public_url to atom_url and profile_url --- app/helpers/layout_helper.rb | 3 +- app/models/person.rb | 37 +++++++++---------- app/models/user.rb | 2 +- app/views/publics/webfinger.erb | 2 +- app/views/shared/_public_explain.haml | 2 +- app/views/users/public.atom.builder | 18 +++++----- app/workers/publish_to_hub.rb | 5 ++- features/support/poor_mans_webmock.rb | 2 +- lib/postzord/dispatcher.rb | 2 +- spec/lib/postzord/dispatcher_spec.rb | 2 +- spec/models/person_spec.rb | 51 ++++++++++++++++++++++----- spec/workers/publish_to_hub_spec.rb | 12 +++---- 12 files changed, 87 insertions(+), 51 deletions(-) diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb index 8c2a45011..646832677 100644 --- a/app/helpers/layout_helper.rb +++ b/app/helpers/layout_helper.rb @@ -32,7 +32,8 @@ module LayoutHelper def current_user_atom_tag return unless @person.present? - content_tag(:link, '', :rel => 'alternate', :href => "#{@person.public_url}.atom", :type => "application/atom+xml", :title => t('.public_feed', :name => @person.name)) + content_tag(:link, "", rel: "alternate", href: @person.atom_url, type: "application/atom+xml", + title: t(".public_feed", name: @person.name)) end def translation_missing_warnings diff --git a/app/models/person.rb b/app/models/person.rb index fc966c4eb..d95d3ddc5 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -195,33 +195,34 @@ class Person < ActiveRecord::Base end end + def username + @username ||= owner ? owner.username : diaspora_handle.split("@")[0] + end + def owns?(obj) self.id == obj.author_id end def url - begin - uri = URI.parse(self[:url]) - url = "#{uri.scheme}://#{uri.host}" - url += ":#{uri.port}" unless ["80", "443"].include?(uri.port.to_s) - url += "/" - rescue => e - url = self[:url] - end + uri = URI.parse(self[:url]) + url = "#{uri.scheme}://#{uri.host}" + url += ":#{uri.port}" unless %w(80 443).include?(uri.port.to_s) + url += "/" url + rescue + self[:url] + end + + def profile_url + "#{url}u/#{username}" + end + + def atom_url + "#{url}public/#{username}.atom" end def receive_url - "#{url}receive/users/#{self.guid}/" - end - - def public_url - if self.owner - username = self.owner.username - else - username = self.diaspora_handle.split("@")[0] - end - "#{url}public/#{username}" + "#{url}receive/users/#{guid}" end def public_key_hash diff --git a/app/models/user.rb b/app/models/user.rb index a2bbf55d0..a60d67e9c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -41,7 +41,7 @@ class User < ActiveRecord::Base has_one :profile, through: :person delegate :guid, :public_key, :posts, :photos, :owns?, :image_url, - :diaspora_handle, :name, :public_url, :profile, :url, + :diaspora_handle, :name, :atom_url, :profile_url, :profile, :url, :first_name, :last_name, :gender, :participations, to: :person delegate :id, :guid, to: :person, prefix: true diff --git a/app/views/publics/webfinger.erb b/app/views/publics/webfinger.erb index b29677424..3b8099441 100644 --- a/app/views/publics/webfinger.erb +++ b/app/views/publics/webfinger.erb @@ -7,7 +7,7 @@ true)%>/> - + diff --git a/app/views/shared/_public_explain.haml b/app/views/shared/_public_explain.haml index 4b181e5c0..89321a495 100644 --- a/app/views/shared/_public_explain.haml +++ b/app/views/shared/_public_explain.haml @@ -9,7 +9,7 @@ =t('.outside') %br %br - = link_to t('.atom_feed'), "#{current_user.public_url}.atom" + = link_to t('.atom_feed'), current_user.atom_url %br - if current_user.services - for service in current_user.services diff --git a/app/views/users/public.atom.builder b/app/views/users/public.atom.builder index acfa996d5..e5bfb6da8 100644 --- a/app/views/users/public.atom.builder +++ b/app/views/users/public.atom.builder @@ -1,12 +1,12 @@ -atom_feed({'xmlns:thr' => 'http://purl.org/syndication/thread/1.0', - 'xmlns:georss' => 'http://www.georss.org/georss', - 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/', - 'xmlns:media' => 'http://purl.org/syndication/atommedia', - 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0', - 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0', - 'xmlns:statusnet' => 'http://status.net/schema/api/1/', - :id => "#{@user.public_url}.atom", - :root_url => "#{@user.public_url}"}) do |feed| +atom_feed("xmlns:thr" => "http://purl.org/syndication/thread/1.0", + "xmlns:georss" => "http://www.georss.org/georss", + "xmlns:activity" => "http://activitystrea.ms/spec/1.0/", + "xmlns:media" => "http://purl.org/syndication/atommedia", + "xmlns:poco" => "http://portablecontacts.net/spec/1.0", + "xmlns:ostatus" => "http://ostatus.org/schema/1.0", + "xmlns:statusnet" => "http://status.net/schema/api/1/", + :id => @user.atom_url, + :root_url => @user.profile_url) do |feed| feed.tag! :generator, 'Diaspora', :uri => "#{AppConfig.pod_uri.to_s}" feed.title "#{@user.name}'s Public Feed" diff --git a/app/workers/publish_to_hub.rb b/app/workers/publish_to_hub.rb index 055bd0213..0e87c7936 100644 --- a/app/workers/publish_to_hub.rb +++ b/app/workers/publish_to_hub.rb @@ -6,9 +6,8 @@ module Workers class PublishToHub < Base sidekiq_options queue: :http_service - def perform(sender_public_url) - atom_url = sender_public_url + '.atom' - Pubsubhubbub.new(AppConfig.environment.pubsub_server.get).publish(atom_url) + def perform(sender_atom_url) + Pubsubhubbub.new(AppConfig.environment.pubsub_server.get).publish(sender_atom_url) end end end diff --git a/features/support/poor_mans_webmock.rb b/features/support/poor_mans_webmock.rb index 5b04d5c05..4b72570b2 100644 --- a/features/support/poor_mans_webmock.rb +++ b/features/support/poor_mans_webmock.rb @@ -4,7 +4,7 @@ module Workers class PublishToHub < Base - def perform(sender_public_url) + def perform(sender_atom_url) # don't publish to pubsubhubbub in cucumber end end diff --git a/lib/postzord/dispatcher.rb b/lib/postzord/dispatcher.rb index 540d4d96d..c438e4ff3 100644 --- a/lib/postzord/dispatcher.rb +++ b/lib/postzord/dispatcher.rb @@ -143,7 +143,7 @@ class Postzord::Dispatcher def deliver_to_hub logger.debug "event=post_to_service type=pubsub sender_handle=#{@sender.diaspora_handle}" - Workers::PublishToHub.perform_async(@sender.public_url) + Workers::PublishToHub.perform_async(@sender.atom_url) end # @param url [String] diff --git a/spec/lib/postzord/dispatcher_spec.rb b/spec/lib/postzord/dispatcher_spec.rb index d54d7bb81..a630ab1fa 100644 --- a/spec/lib/postzord/dispatcher_spec.rb +++ b/spec/lib/postzord/dispatcher_spec.rb @@ -278,7 +278,7 @@ describe Postzord::Dispatcher do it 'queues a job to notify the hub' do allow(Workers::PostToService).to receive(:perform_async).with(anything, anything, anything) - expect(Workers::PublishToHub).to receive(:perform_async).with(alice.public_url) + expect(Workers::PublishToHub).to receive(:perform_async).with(alice.atom_url) @zord.send(:deliver_to_services, nil, []) end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index f6bc6f123..6433fafb3 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -108,20 +108,55 @@ describe Person, :type => :model do end describe "valid url" do - it 'should allow for https urls' do - person = FactoryGirl.build(:person, :url => "https://example.com") - expect(person).to be_valid + context "https urls" do + let(:person) { FactoryGirl.build(:person, url: "https://example.com") } + + it "should add trailing slash" do + expect(person.url).to eq("https://example.com/") + end + + it "should return the receive url" do + expect(person.receive_url).to eq("https://example.com/receive/users/#{person.guid}") + end + + it "should return the atom url" do + expect(person.atom_url).to eq("https://example.com/public/#{person.username}.atom") + end + + it "should return the profile url" do + expect(person.profile_url).to eq("https://example.com/u/#{person.username}") + end end - it 'should always return the correct receive url' do - person = FactoryGirl.build(:person, :url => "https://example.com/a/bit/messed/up") - expect(person.receive_url).to eq("https://example.com/receive/users/#{person.guid}/") + context "messed up urls" do + let(:person) { FactoryGirl.build(:person, url: "https://example.com/a/bit/messed/up") } + + it "should return the correct url" do + expect(person.url).to eq("https://example.com/") + end + + it "should return the correct receive url" do + expect(person.receive_url).to eq("https://example.com/receive/users/#{person.guid}") + end + + it "should return the correct atom url" do + expect(person.atom_url).to eq("https://example.com/public/#{person.username}.atom") + end + + it "should return the correct profile url" do + expect(person.profile_url).to eq("https://example.com/u/#{person.username}") + end end - it 'should allow ports in the url' do - person = FactoryGirl.build(:person, :url => "https://example.com:3000/") + it "should allow ports in the url" do + person = FactoryGirl.build(:person, url: "https://example.com:3000/") expect(person.url).to eq("https://example.com:3000/") end + + it "should remove https port in the url" do + person = FactoryGirl.build(:person, url: "https://example.com:443/") + expect(person.url).to eq("https://example.com/") + end end describe '#diaspora_handle' do diff --git a/spec/workers/publish_to_hub_spec.rb b/spec/workers/publish_to_hub_spec.rb index d864ae2c9..8af3c09bb 100644 --- a/spec/workers/publish_to_hub_spec.rb +++ b/spec/workers/publish_to_hub_spec.rb @@ -2,15 +2,15 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -require 'spec_helper' +require "spec_helper" describe Workers::PublishToHub do - describe '.perform' do - it 'calls pubsubhubbub' do - url = "http://publiczone.com/" - m = double() + describe ".perform" do + it "calls pubsubhubbub" do + url = "http://example.com/public/username.atom" + m = double - expect(m).to receive(:publish).with(url+'.atom') + expect(m).to receive(:publish).with(url) expect(Pubsubhubbub).to receive(:new).with(AppConfig.environment.pubsub_server).and_return(m) Workers::PublishToHub.new.perform(url) end