From: Paweł Paprota Date: Fri, 12 Oct 2012 05:35:19 +0000 (+0200) Subject: Implemented Gravatar support X-Git-Tag: live~5283 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/ad434992058ed270b3380d882b91c7f2a91434be Implemented Gravatar support --- diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 9e1585243..b61579d92 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -51,8 +51,8 @@ class UserController < ApplicationController 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 @@ -127,7 +127,7 @@ class UserController < ApplicationController @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 @@ -299,7 +299,7 @@ class UserController < ApplicationController referer = token.referer token.destroy - if session[:token] + if session[:token] token = UserToken.find_by_token(session[:token]) session.delete(:token) else @@ -603,7 +603,7 @@ private else return openid_url end - end + end ## # process a successful login @@ -663,8 +663,15 @@ private 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] @@ -755,7 +762,7 @@ private ## # 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 diff --git a/app/helpers/user_helper.rb b/app/helpers/user_helper.rb index 70125e804..7113cf1b2 100644 --- a/app/helpers/user_helper.rb +++ b/app/helpers/user_helper.rb @@ -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 @@ -29,4 +53,20 @@ module UserHelper :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 diff --git a/app/models/user.rb b/app/models/user.rb index 3b2a12ca0..3b43130fa 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,12 +45,13 @@ 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, + :image_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>" } @@ -82,7 +83,7 @@ class User < ActiveRecord::Base 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 @@ -125,7 +126,7 @@ class User < ActiveRecord::Base 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") @@ -182,7 +183,7 @@ class User < ActiveRecord::Base 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? } diff --git a/app/views/user/account.html.erb b/app/views/user/account.html.erb index 6745f1b34..7adea7c2c 100644 --- a/app/views/user/account.html.erb +++ b/app/views/user/account.html.erb @@ -85,26 +85,33 @@ <%= t 'user.account.image' %> - <% if @user.image.file? %> - - - - - - - - - - - - - - -
<%= user_image @user %><%= radio_button_tag "image_action", "keep", true %><%= t 'user.account.keep image' %>
<%= radio_button_tag "image_action", "delete" %><%= t 'user.account.delete image' %>
<%= radio_button_tag "image_action", "new" %><%= t 'user.account.replace image' %>
<%= f.file_field :image, :onchange => "$('image_action_new').prop('checked', true)" %>
<%= t 'user.account.image size hint' %>
- <% else %> - <%= hidden_field_tag "image_action", "new" %> - <%= t 'user.account.new image' %>
<%= f.file_field :image %>
<%= t 'user.account.image size hint' %> - <% end %> + + <% if @user.image.file? %> + + + + + + + + + + + + + + <% else %> + + + + + + <% end %> + + + + +
<%= user_image @user %><%= radio_button_tag "image_action", "keep", !@user.image_use_gravatar %><%= t 'user.account.keep image' %>
<%= radio_button_tag "image_action", "delete" %><%= t 'user.account.delete image' %>
<%= radio_button_tag "image_action", "new" %><%= t 'user.account.replace image' %>
<%= f.file_field :image, :onchange => "$('image_action_new').prop('checked', true)" %>
<%= t 'user.account.image size hint' %>
<%= user_image @user %><%= radio_button_tag "image_action", "new", !@user.image_use_gravatar %><%= t 'user.account.new image' %> <%= f.file_field :image %>
<%= t 'user.account.image size hint' %>
<%= radio_button_tag "image_action", "gravatar", @user.image_use_gravatar %><%= t 'user.account.gravatar.gravatar' %> (<%= t 'user.account.gravatar.link text' %>)
@@ -123,7 +130,7 @@ <%= content_tag "div", "", :id => "map", :class => "user_map set_location" %> - + <%= submit_tag t('user.account.save changes button') %> diff --git a/app/views/user/api_read.builder b/app/views/user/api_read.builder index db7bcc2b7..71bf29208 100644 --- a/app/views/user/api_read.builder +++ b/app/views/user/api_read.builder @@ -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| @@ -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 } diff --git a/config/locales/en.yml b/config/locales/en.yml index 07e12a81e..bc43ab4bf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1023,22 +1023,22 @@ en: code explains your rights and responsibilities. intro_3_html: | The cartography in our map tiles, and our documentation, are - licensed under the Creative + licensed under the Creative Commons Attribution-ShareAlike 2.0 license (CC-BY-SA). credit_title_html: How to credit OpenStreetMap credit_1_html: | - We require that you use the credit “© OpenStreetMap + We require that you use the credit “© OpenStreetMap contributors”. 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 - this copyright page. - 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 + this copyright page. + 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. @@ -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 takedown - procedure 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 takedown + procedure or file directly at our on-line filing page. notifier: diary_comment_notification: @@ -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" diff --git a/db/migrate/20121012044047_add_image_use_gravatar_to_users.rb b/db/migrate/20121012044047_add_image_use_gravatar_to_users.rb new file mode 100644 index 000000000..e430d5b3c --- /dev/null +++ b/db/migrate/20121012044047_add_image_use_gravatar_to_users.rb @@ -0,0 +1,14 @@ +class AddImageUseGravatarToUsers < ActiveRecord::Migration + def self.up + add_column :users, :image_use_gravatar, :boolean, :null => false, :default => false + + # For people who don't have images on osm.org, enable Gravatar. + User.where(:image_file_name => nil).update_all(:image_use_gravatar => true) + + change_column_default :users, :image_use_gravatar, true + end + + def self.down + remove_column :users, :image_use_gravatar + end +end