From 036a2f6274afe893bc44a40d70c1388da67b6a81 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 20 Oct 2017 15:32:01 +0200 Subject: [PATCH] Small code layout cleanup. --- app/models/authorization.rb | 2 +- app/models/avatar.rb | 130 ++++++++++++++++++------------------ app/models/setting.rb | 4 +- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/app/models/authorization.rb b/app/models/authorization.rb index 6ae251654..ab0a2acdd 100644 --- a/app/models/authorization.rb +++ b/app/models/authorization.rb @@ -58,7 +58,7 @@ class Authorization < ApplicationModel end # save/update avatar - if hash['info']['image'] + if hash['info'].present? && hash['info']['image'].present? avatar = Avatar.add( object: 'User', o_id: user.id, diff --git a/app/models/avatar.rb b/app/models/avatar.rb index 2684fe7bb..76456b1a7 100644 --- a/app/models/avatar.rb +++ b/app/models/avatar.rb @@ -104,83 +104,81 @@ add avatar by url end # fetch image based on http url - if data[:url] =~ /^http/ + if data[:url].present? + if data[:url] =~ /^http/ - # check if source ist already updated within last 2 minutes - if avatar_already_exists && avatar_already_exists.source_url == data[:url] - return if avatar_already_exists.updated_at > 2.minutes.ago - end + # check if source ist already updated within last 2 minutes + if avatar_already_exists && avatar_already_exists.source_url == data[:url] + return if avatar_already_exists.updated_at > 2.minutes.ago + end - # twitter workaround to get bigger avatar images - # see also https://dev.twitter.com/overview/general/user-profile-images-and-banners - if data[:url] =~ %r{//pbs.twimg.com/}i - data[:url].sub!(/normal\.(png|jpg|gif)$/, 'bigger.\1') - end + # twitter workaround to get bigger avatar images + # see also https://dev.twitter.com/overview/general/user-profile-images-and-banners + if data[:url] =~ %r{//pbs.twimg.com/}i + data[:url].sub!(/normal\.(png|jpg|gif)$/, 'bigger.\1') + end - # fetch image - response = UserAgent.get( - data[:url], - {}, - { - open_timeout: 4, - read_timeout: 6, - total_timeout: 6, - }, - ) - if !response.success? - logger.info "Can't fetch '#{data[:url]}' (maybe no avatar available), http code: #{response.code}" - return - end - logger.info "Fetchd image '#{data[:url]}', http code: #{response.code}" - mime_type = 'image' - if data[:url] =~ /\.png/i - mime_type = 'image/png' - end - if data[:url] =~ /\.(jpg|jpeg)/i - mime_type = 'image/jpeg' - end - if !data[:resize] - data[:resize] = {} - end - data[:resize][:content] = response.body - data[:resize][:mime_type] = mime_type - if !data[:full] - data[:full] = {} - end - data[:full][:content] = response.body - data[:full][:mime_type] = mime_type + # fetch image + response = UserAgent.get( + data[:url], + {}, + { + open_timeout: 4, + read_timeout: 6, + total_timeout: 6, + }, + ) + if !response.success? + logger.info "Can't fetch '#{data[:url]}' (maybe no avatar available), http code: #{response.code}" + return + end + logger.info "Fetchd image '#{data[:url]}', http code: #{response.code}" + mime_type = 'image' + if data[:url] =~ /\.png/i + mime_type = 'image/png' + end + if data[:url] =~ /\.(jpg|jpeg)/i + mime_type = 'image/jpeg' + end + if !data[:resize] + data[:resize] = {} + end + data[:resize][:content] = response.body + data[:resize][:mime_type] = mime_type + data[:full] ||= {} + data[:full][:content] = response.body + data[:full][:mime_type] = mime_type - # try zammad backend to find image based on email - elsif data[:url].present? && data[:url] =~ /@/ + # try zammad backend to find image based on email + elsif data[:url] =~ /@/ - # check if source ist already updated within last 3 minutes - if avatar_already_exists && avatar_already_exists.source_url == data[:url] - return if avatar_already_exists.updated_at > 2.minutes.ago - end + # check if source ist already updated within last 3 minutes + if avatar_already_exists && avatar_already_exists.source_url == data[:url] + return if avatar_already_exists.updated_at > 2.minutes.ago + end - # fetch image - image = Service::Image.user(data[:url]) - return if !image - if !data[:resize] - data[:resize] = {} + # fetch image + image = Service::Image.user(data[:url]) + return if !image + data[:resize] ||= {} + data[:resize] = image + data[:full] ||= {} + data[:full] = image end - data[:resize] = image - if !data[:full] - data[:full] = {} - end - data[:full] = image end # check if avatar need to be updated - record[:store_hash] = Digest::MD5.hexdigest(data[:resize][:content]) - if avatar_already_exists && avatar_already_exists.store_hash == record[:store_hash] - avatar_already_exists.touch - return avatar_already_exists + if data[:resize].present? && data[:resize][:content].present? + record[:store_hash] = Digest::MD5.hexdigest(data[:resize][:content]) + if avatar_already_exists && avatar_already_exists.store_hash == record[:store_hash] + avatar_already_exists.touch + return avatar_already_exists + end end # store images object_name = "Avatar::#{data[:object]}" - if data[:full] + if data[:full].present? store_full = Store.add( object: "#{object_name}::Full", o_id: data[:o_id], @@ -194,7 +192,7 @@ add avatar by url record[:store_full_id] = store_full.id record[:store_hash] = Digest::MD5.hexdigest(data[:full][:content]) end - if data[:resize] + if data[:resize].present? store_resize = Store.add( object: "#{object_name}::Resize", o_id: data[:o_id], @@ -206,9 +204,11 @@ add avatar by url created_by_id: data[:created_by_id], ) record[:store_resize_id] = store_resize.id - record[:store_hash] = Digest::MD5.hexdigest(data[:resize][:content] ) + record[:store_hash] = Digest::MD5.hexdigest(data[:resize][:content]) end + return if record[:store_resize_id].blank? || record[:store_hash].blank? + # update existing if avatar_already_exists avatar_already_exists.update!(record) diff --git a/app/models/setting.rb b/app/models/setting.rb index 1b14e840d..57479d4aa 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -38,7 +38,7 @@ set config setting end setting.state_current = { value: value } setting.save! - logger.info "Setting.set(#{name}, #{value.inspect})" + logger.info "Setting.set('#{name}', #{value.inspect})" end =begin @@ -72,7 +72,7 @@ reset config setting to default return true if !force && setting.state_current == setting.state_initial setting.state_current = setting.state_initial setting.save! - logger.info "Setting.reset(#{name}, #{setting.state_current.inspect})" + logger.info "Setting.reset('#{name}', #{setting.state_current.inspect})" end =begin