Remove default enabling of gravatar, check on initial confirmation of e-mail address...
[rails.git] / app / controllers / user_controller.rb
index c8abb4d85a3f84ce1389e956b72cad9f558c8319..426a9642472256f079c846ae45d252213d5cd09c 100644 (file)
@@ -297,6 +297,7 @@ class UserController < ApplicationController
         user = token.user
         user.status = "active"
         user.email_valid = true
+        gravatar_enable(user)
         user.save!
         referer = token.referer
         token.destroy
@@ -348,6 +349,7 @@ class UserController < ApplicationController
         @user.email = @user.new_email
         @user.new_email = nil
         @user.email_valid = true
+        gravatar_enable(@user)
         if @user.save
           flash[:notice] = t "user.confirm_email.success"
         else
@@ -799,4 +801,23 @@ class UserController < ApplicationController
 
     !blocked
   end
+
+  ##
+  # check if this user has a gravatar and set the user pref is true
+  def gravatar_enable(user)
+    # code from example https://en.gravatar.com/site/implement/images/ruby/
+    return if user.image.present?
+    hash = Digest::MD5.hexdigest(user.email.downcase)
+    url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without  d=404 we will always get an image back
+    response = OSM.http_client.get(URI.parse(url))
+    oldsetting = user.image_use_gravatar
+    user.image_use_gravatar = response.success?
+    if oldsetting != user.image_use_gravatar
+      flash[:warning] = if user.image_use_gravatar
+                          t "user.account.gravatar.enabled"
+                        else
+                          t "user.account.gravatar.disabled"
+                        end
+    end
+  end
 end