Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gravatar support #131

Closed
wants to merge 9 commits into from
23 changes: 15 additions & 8 deletions app/controllers/user_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def terms
if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
# We are creating an account with OpenID and no password
# was specified so create a random one
@user.pass_crypt = SecureRandom.base64(16)
@user.pass_crypt_confirmation = @user.pass_crypt
@user.pass_crypt = SecureRandom.base64(16)
@user.pass_crypt_confirmation = @user.pass_crypt
end

if @user
Expand Down Expand Up @@ -127,7 +127,7 @@ def save
@user.terms_agreed = Time.now.getutc
@user.terms_seen = true
@user.openid_url = nil if @user.openid_url and @user.openid_url.empty?

if @user.save
flash[:piwik_goal] = PIWIK_SIGNUP_GOAL if defined?(PIWIK_SIGNUP_GOAL)
flash[:notice] = t 'user.new.flash create success message', :email => @user.email
Expand Down Expand Up @@ -299,7 +299,7 @@ def confirm
referer = token.referer
token.destroy

if session[:token]
if session[:token]
token = UserToken.find_by_token(session[:token])
session.delete(:token)
else
Expand Down Expand Up @@ -603,7 +603,7 @@ def openid_expand_url(openid_url)
else
return openid_url
end
end
end

##
# process a successful login
Expand Down Expand Up @@ -663,8 +663,15 @@ def update_user(user, params)
user.languages = params[:user][:languages].split(",")

case params[:image_action]
when "new" then user.image = params[:user][:image]
when "delete" then user.image = nil
when "new" then
user.image = params[:user][:image]
user.image_use_gravatar = false
when "delete" then
user.image = nil
user.image_use_gravatar = false
when "gravatar" then
user.image = nil
user.image_use_gravatar = true
end

user.home_lat = params[:user][:home_lat]
Expand Down Expand Up @@ -755,7 +762,7 @@ def choose_layout
##
#
def disable_terms_redirect
# this is necessary otherwise going to the user terms page, when
# this is necessary otherwise going to the user terms page, when
# having not agreed already would cause an infinite redirect loop.
# it's .now so that this doesn't propagate to other pages.
flash.now[:skip_terms] = true
Expand Down
46 changes: 43 additions & 3 deletions app/helpers/user_helper.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
module UserHelper
# User images

def user_image(user, options = {})
options[:class] ||= "user_image"

image_tag user.image.url(:large), options
if user.image_use_gravatar
user_gravatar_tag(user, options)
else
image_tag user.image.url(:large), options
end
end

def user_thumbnail(user, options = {})
options[:class] ||= "user_thumbnail"

image_tag user.image.url(:small), options
if user.image_use_gravatar
user_gravatar_tag(user, options)
else
image_tag user.image.url(:small), options
end
end

def user_thumbnail_tiny(user, options = {})
options[:class] ||= "user_thumbnail_tiny"

image_tag user.image.url(:small), options
if user.image_use_gravatar
user_gravatar_tag(user, options)
else
image_tag user.image.url(:small), options
end
end

def user_image_url(user, options = {})
if user.image_use_gravatar
user_gravatar_url(user, options)
else
"http://#{SERVER_URL}#{image_path(user.image.url)}"
end
end

# OpenID support

def openid_logo
image_tag "openid_small.png", :alt => t('user.login.openid_logo_alt'), :class => "openid_logo"
end
Expand All @@ -29,4 +53,20 @@ def openid_button(name, url)
:title => t("user.login.openid_providers.#{name}.title")
)
end

# Gravatar support

# See http://en.gravatar.com/site/implement/images/ for details.
def user_gravatar_url(user, options = {})
size = options[:size] || 100
hash = Digest::MD5::hexdigest(user.email.downcase)
default_image_url = "http://#{SERVER_URL}#{image_path("users/images/large.png")}"
url = "http://www.gravatar.com/avatar/#{hash}.jpg?s=#{size}&d=#{u(default_image_url)}"
end

def user_gravatar_tag(user, options = {})
url = user_gravatar_url(user, options)
options.delete(:size)
image_tag url, options
end
end
10 changes: 5 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ class User < ActiveRecord::Base
validates_inclusion_of :preferred_editor, :in => Editors::ALL_EDITORS, :allow_nil => true

attr_accessible :display_name, :email, :email_confirmation, :openid_url,
:pass_crypt, :pass_crypt_confirmation, :consider_pd
:pass_crypt, :pass_crypt_confirmation, :consider_pd, :use_gravatar

after_initialize :set_defaults
before_save :encrypt_password

has_attached_file :image,
has_attached_file :image,
:default_url => "/assets/:class/:attachment/:style.png",
:styles => { :large => "100x100>", :small => "50x50>" }

Expand Down Expand Up @@ -82,7 +82,7 @@ def self.authenticate(options)
token.update_column(:expiry, 1.week.from_now) if token and user

return user
end
end

def to_xml
doc = OSM::API.new.get_xml_doc
Expand Down Expand Up @@ -125,7 +125,7 @@ def preferred_language_from(array)
end

def nearby(radius = NEARBY_RADIUS, num = NEARBY_USERS)
if self.home_lon and self.home_lat
if self.home_lon and self.home_lat
gc = OSM::GreatCircle.new(self.home_lat, self.home_lon)
bounds = gc.bounds(radius)
sql_for_distance = gc.sql_for_distance("home_lat", "home_lon")
Expand Down Expand Up @@ -182,7 +182,7 @@ def has_role?(role)
end

##
# returns the first active block which would require users to view
# returns the first active block which would require users to view
# a message, or nil if there are none.
def blocked_on_view
blocks.active.detect { |b| b.needs_view? }
Expand Down
49 changes: 28 additions & 21 deletions app/views/user/account.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,33 @@
<%= t 'user.account.image' %>
</td>
<td valign="top">
<% if @user.image.file? %>
<table id="accountImage">
<tr>
<td rowspan="3" valign="top"><%= user_image @user %></td>
<td><%= radio_button_tag "image_action", "keep", true %></td>
<td><%= t 'user.account.keep image' %></td>
</tr>
<tr>
<td><%= radio_button_tag "image_action", "delete" %></td>
<td><%= t 'user.account.delete image' %></td>
</tr>
<tr>
<td><%= radio_button_tag "image_action", "new" %></td>
<td><%= t 'user.account.replace image' %><br /><%= f.file_field :image, :onchange => "$('image_action_new').prop('checked', true)" %><br /><span class="minorNote"><%= t 'user.account.image size hint' %></span></td>
</tr>
</table>
<% else %>
<%= hidden_field_tag "image_action", "new" %>
<%= t 'user.account.new image' %><br /><%= f.file_field :image %><br /><span class="minorNote"><%= t 'user.account.image size hint' %></span>
<% end %>
<table id="accountImage">
<% if @user.image.file? %>
<tr>
<td rowspan="4" valign="top"><%= user_image @user %></td>
<td><%= radio_button_tag "image_action", "keep", !@user.image_use_gravatar %></td>
<td><%= t 'user.account.keep image' %></td>
</tr>
<tr>
<td><%= radio_button_tag "image_action", "delete" %></td>
<td><%= t 'user.account.delete image' %></td>
</tr>
<tr>
<td><%= radio_button_tag "image_action", "new" %></td>
<td><%= t 'user.account.replace image' %><br /><%= f.file_field :image, :onchange => "$('image_action_new').prop('checked', true)" %><br /><span class="minorNote"><%= t 'user.account.image size hint' %></span></td>
</tr>
<% else %>
<tr>
<td rowspan="2" valign="top"><%= user_image @user %></td>
<td><%= radio_button_tag "image_action", "new", !@user.image_use_gravatar %></td>
<td><%= t 'user.account.new image' %> <%= f.file_field :image %><br /><span class="minorNote"><%= t 'user.account.image size hint' %></span></td>
</tr>
<% end %>
<tr>
<td><%= radio_button_tag "image_action", "gravatar", @user.image_use_gravatar %></td>
<td><%= t 'user.account.gravatar.gravatar' %> <span class="minorNote">(<a href="<%= t 'user.account.gravatar.link' %>" target="_new"><%= t 'user.account.gravatar.link text' %></a>)</span></td>
</tr>
</table>
</td>
</tr>

Expand All @@ -123,7 +130,7 @@
<%= content_tag "div", "", :id => "map", :class => "user_map set_location" %>
</td>
</tr>

<tr>
<td></td>
<td class="submitButton"><%= submit_tag t('user.account.save changes button') %></td>
Expand Down
6 changes: 3 additions & 3 deletions app/views/user/api_read.builder
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ xml.osm("version" => API_VERSION, "generator" => GENERATOR) do
else
xml.tag! "contributor-terms", :agreed => !!@this_user.terms_agreed
end
if @this_user.image.file?
xml.tag! "img", :href => "http://#{SERVER_URL}#{@this_user.image.url}"
if @this_user.image.file? or @this_user.image_use_gravatar
xml.tag! "img", :href => user_image_url(@this_user, :size => 256)
end
xml.tag! "roles" do
@this_user.roles.each do |role|
Expand All @@ -35,7 +35,7 @@ xml.osm("version" => API_VERSION, "generator" => GENERATOR) do
xml.tag! "home", :lat => @this_user.home_lat,
:lon => @this_user.home_lon,
:zoom => @this_user.home_zoom
end
end
if @this_user.languages
xml.tag! "languages" do
@this_user.languages.split(",") { |lang| xml.tag! "lang", lang }
Expand Down
34 changes: 19 additions & 15 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1023,22 +1023,22 @@ en:
code</a> explains your rights and responsibilities.
intro_3_html: |
The cartography in our map tiles, and our documentation, are
licensed under the <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative
licensed under the <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative
Commons Attribution-ShareAlike 2.0</a> license (CC-BY-SA).
credit_title_html: How to credit OpenStreetMap
credit_1_html: |
We require that you use the credit &ldquo;&copy; OpenStreetMap
We require that you use the credit &ldquo;&copy; OpenStreetMap
contributors&rdquo;.
credit_2_html: |
You must also make it clear that the data is available under the Open
Database License, and if using our map tiles, that the cartography is
licensed as CC-BY-SA. You may do this by linking to
<a href="http://www.openstreetmap.org/copyright">this copyright page</a>.
Alternatively, and as a requirement if you are distributing OSM in a
data form, you can name and link directly to the license(s). In media
where links are not possible (e.g. printed works), we suggest you
direct your readers to openstreetmap.org (perhaps by expanding
'OpenStreetMap' to this full address), to opendatacommons.org, and
You must also make it clear that the data is available under the Open
Database License, and if using our map tiles, that the cartography is
licensed as CC-BY-SA. You may do this by linking to
<a href="http://www.openstreetmap.org/copyright">this copyright page</a>.
Alternatively, and as a requirement if you are distributing OSM in a
data form, you can name and link directly to the license(s). In media
where links are not possible (e.g. printed works), we suggest you
direct your readers to openstreetmap.org (perhaps by expanding
'OpenStreetMap' to this full address), to opendatacommons.org, and
if relevant, to creativecommons.org.
credit_3_html: |
For a browsable electronic map, the credit should appear in the corner of the map.
Expand Down Expand Up @@ -1106,10 +1106,10 @@ en:
copyrighted sources (e.g. Google Maps or printed maps) without
explicit permission from the copyright holders.
infringement_2_html: |
If you believe that copyrighted material has been inappropriately
added to the OpenStreetMap database or this site, please refer
to our <a href="http://www.osmfoundation.org/wiki/License/Takedown_procedure">takedown
procedure</a> or file directly at our
If you believe that copyrighted material has been inappropriately
added to the OpenStreetMap database or this site, please refer
to our <a href="http://www.osmfoundation.org/wiki/License/Takedown_procedure">takedown
procedure</a> or file directly at our
<a href="http://dmca.openstreetmap.org/">on-line filing page</a>.
notifier:
diary_comment_notification:
Expand Down Expand Up @@ -1776,6 +1776,10 @@ en:
preferred languages: "Preferred Languages:"
preferred editor: "Preferred Editor:"
image: "Image:"
gravatar:
gravatar: "Use Gravatar"
link: "http://wiki.openstreetmap.org/wiki/Gravatar"
link text: "what is this?"
new image: "Add an image"
keep image: "Keep the current image"
delete image: "Remove the current image"
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20121012044047_add_image_use_gravatar_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AddImageUseGravatarToUsers < ActiveRecord::Migration
def self.up
add_column :users, :image_use_gravatar, :boolean, :null => false, :default => false

User.all.each do |user|
# For people who don't have images on osm.org, enable Gravatar.
user.image_use_gravatar = !user.image.file?
user.save
end
end

def self.down
remove_column :users, :image_use_gravatar
end
end