Commit 03dbb75d authored by Benjamin Neff's avatar Benjamin Neff Committed by Dennis Schubert
Browse files

don't save default avatars to the database

parent a7fa3eab
...@@ -68,7 +68,7 @@ class Profile < ActiveRecord::Base ...@@ -68,7 +68,7 @@ class Profile < ActiveRecord::Base
(self.person) ? self.person.diaspora_handle : self[:diaspora_handle] (self.person) ? self.person.diaspora_handle : self[:diaspora_handle]
end end
def image_url(size = :thumb_large) def image_url(size=:thumb_large)
result = if size == :thumb_medium && self[:image_url_medium] result = if size == :thumb_medium && self[:image_url_medium]
self[:image_url_medium] self[:image_url_medium]
elsif size == :thumb_small && self[:image_url_small] elsif size == :thumb_small && self[:image_url_small]
...@@ -77,14 +77,14 @@ class Profile < ActiveRecord::Base ...@@ -77,14 +77,14 @@ class Profile < ActiveRecord::Base
self[:image_url] self[:image_url]
end end
unless result if result
ActionController::Base.helpers.image_path('user/default.png')
else
if AppConfig.privacy.camo.proxy_remote_pod_images? if AppConfig.privacy.camo.proxy_remote_pod_images?
Diaspora::Camo.image_url(result) Diaspora::Camo.image_url(result)
else else
result result
end end
else
ActionController::Base.helpers.image_path("user/default.png")
end end
end end
...@@ -100,31 +100,16 @@ class Profile < ActiveRecord::Base ...@@ -100,31 +100,16 @@ class Profile < ActiveRecord::Base
self.attributes.merge(update_hash){|key, old, new| old.blank? ? new : old} self.attributes.merge(update_hash){|key, old, new| old.blank? ? new : old}
end end
def image_url= url def image_url=(url)
return image_url if url == '' super(build_image_url(url))
if url.nil? || url.match(/^https?:\/\//)
super(url)
else
super(absolutify_local_url(url))
end
end end
def image_url_small= url def image_url_small=(url)
return image_url if url == '' super(build_image_url(url))
if url.nil? || url.match(/^https?:\/\//)
super(url)
else
super(absolutify_local_url(url))
end
end end
def image_url_medium= url def image_url_medium=(url)
return image_url if url == '' super(build_image_url(url))
if url.nil? || url.match(/^https?:\/\//)
super(url)
else
super(absolutify_local_url(url))
end
end end
def date= params def date= params
...@@ -201,7 +186,9 @@ class Profile < ActiveRecord::Base ...@@ -201,7 +186,9 @@ class Profile < ActiveRecord::Base
self.attributes.keys - ["id", "created_at", "updated_at", "person_id"] self.attributes.keys - ["id", "created_at", "updated_at", "person_id"]
end end
def absolutify_local_url url def build_image_url(url)
"#{AppConfig.pod_uri.to_s.chomp("/")}#{url}" return nil if url.blank? || url.match(/user\/default/)
return url if url.match(/^https?:\/\//)
"#{AppConfig.pod_uri.to_s.chomp('/')}#{url}"
end end
end end
...@@ -124,25 +124,40 @@ describe Profile, :type => :model do ...@@ -124,25 +124,40 @@ describe Profile, :type => :model do
end end
end end
describe '#image_url=' do describe "image_url setters" do
%i(image_url image_url_small image_url_medium).each do |method|
describe "##{method}=" do
before do before do
@profile = FactoryGirl.build(:profile) @profile = FactoryGirl.build(:profile)
@profile.image_url = "http://tom.joindiaspora.com/images/user/tom.jpg" @profile.public_send("#{method}=", "http://tom.joindiaspora.com/images/user/tom.jpg")
@pod_url = AppConfig.pod_uri.to_s.chomp("/") @pod_url = AppConfig.pod_uri.to_s.chomp("/")
end end
it 'ignores an empty string' do it "saves nil when setting nil" do
expect {@profile.image_url = ""}.not_to change(@profile, :image_url) @profile.public_send("#{method}=", nil)
expect(@profile[method]).to be_nil
end end
it 'makes relative urls absolute' do it "saves nil when setting an empty string" do
@profile.image_url = "/relative/url" @profile.public_send("#{method}=", "")
expect(@profile.image_url).to eq("#{@pod_url}/relative/url") expect(@profile[method]).to be_nil
end
it "makes relative urls absolute" do
@profile.public_send("#{method}=", "/relative/url")
expect(@profile.public_send(method)).to eq("#{@pod_url}/relative/url")
end end
it "doesn't change absolute urls" do it "doesn't change absolute urls" do
@profile.image_url = "http://not/a/relative/url" @profile.public_send("#{method}=", "http://not/a/relative/url")
expect(@profile.image_url).to eq("http://not/a/relative/url") expect(@profile.public_send(method)).to eq("http://not/a/relative/url")
end
it "saves the default-url as nil" do
@profile.public_send("#{method}=", "/assets/user/default.png")
expect(@profile[method]).to be_nil
end
end
end end
end end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment