]> git.openstreetmap.org Git - rails.git/commitdiff
Implemented Gravatar support
authorPaweł Paprota <ppawel@fastmail.fm>
Fri, 12 Oct 2012 05:35:19 +0000 (07:35 +0200)
committerTom Hughes <tom@compton.nu>
Thu, 18 Oct 2012 17:59:38 +0000 (18:59 +0100)
app/controllers/user_controller.rb
app/helpers/user_helper.rb
app/models/user.rb
app/views/user/account.html.erb
app/views/user/api_read.builder
config/locales/en.yml
db/migrate/20121012044047_add_image_use_gravatar_to_users.rb [new file with mode: 0644]

index 9e158524393a95e1613c53caa7d2894ed06f62a8..b61579d92b50bb44cd4963e3987567e8d1e90bfa 100644 (file)
@@ -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
index 70125e8047e5f1fc13f35dad24a59da2d5148846..7113cf1b2d48bd324545031de8dbcd403349be02 100644 (file)
@@ -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
index 3b2a12ca0314ec0cbcae633b08cf0bb733cfbef8..3b43130faee924f5fdd5293eca3214318b56040d 100644 (file)
@@ -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? }
index 6745f1b34fe248c18afac2d8cca5dac7c0db36a7..7adea7c2cec3884c19b6ab07b88735f4c10a6fc0 100644 (file)
       <%= 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>
 
       <%= 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>
index db7bcc2b7bf98953f2326d9f810db6f5a552ee1a..71bf292081e0dd2f3dfdd01cdc04cce7c07b70ab 100644 (file)
@@ -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 }
index 07e12a81e6e34fb7bca03dbdb4d76ece7b828127..bc43ab4bfbdff626782e1f5b222fb8c030c38510 100644 (file)
@@ -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.
@@ -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:
@@ -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 (file)
index 0000000..e430d5b
--- /dev/null
@@ -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