diff --git a/Changelog.md b/Changelog.md index 2270e6473..493055112 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,7 @@ * Update perfect-scrollbar [#6085](https://github.com/diaspora/diaspora/pull/6085) * Remove top margin for first heading in a post [#6110](https://github.com/diaspora/diaspora/pull/6110) * Add link to pod statistics in right navigation [#6117](https://github.com/diaspora/diaspora/pull/6117) +* Refactor person related URL generation [#6168](https://github.com/diaspora/diaspora/pull/6168) ## Bug fixes * Precompile facebox images [#6105](https://github.com/diaspora/diaspora/pull/6105) diff --git a/app/models/person.rb b/app/models/person.rb index 9cfac1821..814dfe1e0 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -204,25 +204,21 @@ class Person < ActiveRecord::Base end def url - uri = URI.parse(self[:url]) - url = "#{uri.scheme}://#{uri.host}" - url += ":#{uri.port}" unless %w(80 443).include?(uri.port.to_s) - url += "/" - url + url_to "/" rescue self[:url] end def profile_url - "#{url}u/#{username}" + url_to "/u/#{username}" end def atom_url - "#{url}public/#{username}.atom" + url_to "/public/#{username}.atom" end def receive_url - "#{url}receive/users/#{guid}" + url_to "/receive/users/#{guid}" end def public_key_hash @@ -321,11 +317,9 @@ class Person < ActiveRecord::Base # @param person [Person] # @param url [String] def update_url(url) - location = URI.parse(url) - newuri = "#{location.scheme}://#{location.host}" - newuri += ":#{location.port}" unless ["80", "443"].include?(location.port.to_s) - newuri += "/" - self.update_attributes(:url => newuri) + @uri = URI.parse(url) + @uri.path = "/" + update_attributes(:url => @uri.to_s) end def lock_access! @@ -349,6 +343,18 @@ class Person < ActiveRecord::Base private + # @return [URI] + def uri + @uri ||= URI.parse(self[:url]) + @uri.dup + end + + # @param path [String] + # @return [String] + def url_to(path) + uri.tap {|uri| uri.path = path }.to_s + end + def fix_profile Webfinger.new(self.diaspora_handle).fetch self.reload diff --git a/spec/workers/http_multi_spec.rb b/spec/workers/http_multi_spec.rb index 594f107eb..a4831614b 100644 --- a/spec/workers/http_multi_spec.rb +++ b/spec/workers/http_multi_spec.rb @@ -110,8 +110,7 @@ describe Workers::HttpMulti do it 'updates http users who have moved to https' do person = @people.first - person.url = 'http://remote.net/' - person.save + person.update_url("http://remote.net/") response = Typhoeus::Response.new( code: 301, @@ -123,8 +122,7 @@ describe Workers::HttpMulti do Typhoeus.stub(person.receive_url).and_return response Workers::HttpMulti.new.perform bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private" - person.reload - expect(person.url).to eq("https://remote.net/") + expect(Person.find(person.id).url).to eq("https://remote.net/") end it 'only sends to users with valid RSA keys' do