From 4c2e4de5c08cbc2f4c04647264eb3dcedf1ece2a Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 21 Mar 2021 10:21:25 +0000 Subject: [PATCH] Handle errors checking for gravatars --- .rubocop_todo.yml | 2 +- app/controllers/users_controller.rb | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index cac193504..cc90d168e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -68,7 +68,7 @@ Metrics/BlockNesting: # Offense count: 24 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 582 + Max: 587 # Offense count: 52 # Configuration parameters: IgnoredMethods. diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 6aa98f7ee..e4dd1b2dc 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -753,11 +753,17 @@ class UsersController < ApplicationController # code from example https://en.gravatar.com/site/implement/images/ruby/ return false if user.avatar.attached? - 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)) + begin + 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)) + available = response.success? + rescue StandardError + available = false + end + oldsetting = user.image_use_gravatar - user.image_use_gravatar = response.success? + user.image_use_gravatar = available oldsetting != user.image_use_gravatar end -- 2.39.5