Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix reloading avatars by removing the patching

Oliver Güntherfb6d85d50bed
diff --git a/app/helpers/avatar_helper.rb b/app/helpers/avatar_helper.rb
deleted file mode 100644
index 3136dbe3e62..00000000000
--- a/app/helpers/avatar_helper.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2022 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-
-module AvatarHelper
- # Returns the avatar image tag for the given +user+ if avatars are enabled
- # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <[email protected]>')
- def avatar(_user, _options = {})
- ''.html_safe
- end
-
- # Returns the avatar image url for the given +user+ if avatars are enabled
- def avatar_url(_user, _options = {})
- ''.html_safe
- end
-end
-
-ActiveSupport.run_load_hooks(:op_helpers_avatar, AvatarHelper)
diff --git a/app/models/user.rb b/app/models/user.rb
index 50bdf4b80ac..54252e08eac 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -39,6 +39,7 @@ class User < Principal
}.freeze
include ::Associations::Groupable
+ include ::Users::Avatars
extend DeprecatedAlias
has_many :categories, foreign_key: 'assigned_to_id',
diff --git a/modules/avatars/app/helpers/avatar_helper.rb b/modules/avatars/app/helpers/avatar_helper.rb
index 6a2eb791bf9..61c097b6017 100644
--- a/modules/avatars/app/helpers/avatar_helper.rb
+++ b/modules/avatars/app/helpers/avatar_helper.rb
@@ -1,6 +1,13 @@
-# OpenProject Avatars plugin
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
#
-# Copyright (C) 2017 OpenProject GmbH
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@@ -15,11 +22,12 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
require 'gravatar_image_tag'
-require 'avatar_helper'
-AvatarHelper.class_eval do
+module AvatarHelper
include ::GravatarImageTag
include ::AngularHelper
@@ -29,103 +37,100 @@ AvatarHelper.class_eval do
# Override gems's method in order to avoid deprecated URI.escape
GravatarImageTag.define_singleton_method(:url_params) do |gravatar_params|
- return nil if gravatar_params.keys.size == 0
+ return nil if gravatar_params.empty?
array = gravatar_params.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
"?#{array.join('&')}"
end
- module InstanceMethods
- # Returns the avatar image tag for the given +user+ if avatars are enabled
- # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <[email protected]>')
- def avatar(principal, options = {})
- build_principal_avatar_tag principal, options
- rescue StandardError => e
- Rails.logger.error "Failed to create avatar for #{principal}: #{e}"
- ''.html_safe
- end
+ # Returns the avatar image tag for the given +user+ if avatars are enabled
+ # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <[email protected]>')
+ def avatar(principal, options = {})
+ build_principal_avatar_tag principal, options
+ rescue StandardError => e
+ Rails.logger.error "Failed to create avatar for #{principal}: #{e}"
+ ''.html_safe
+ end
- def avatar_url(user, options = {})
- if local_avatar? user
- user_avatar_url(user.id)
- elsif avatar_manager.gravatar_enabled?
- build_gravatar_image_url user, options
- else
- super
- end
- rescue StandardError => e
- Rails.logger.error "Failed to create avatar url for #{user}: #{e}"
- ''.html_safe
+ def avatar_url(user, options = {})
+ if local_avatar? user
+ user_avatar_url(user.id)
+ elsif avatar_manager.gravatar_enabled?
+ build_gravatar_image_url user, options
+ else
+ super
end
+ rescue StandardError => e
+ Rails.logger.error "Failed to create avatar url for #{user}: #{e}"
+ ''.html_safe
+ end
- def local_avatar?(user)
- return false unless avatar_manager.local_avatars_enabled?
-
- user.respond_to?(:local_avatar_attachment) && user.local_avatar_attachment
- end
+ def local_avatar?(user)
+ return false unless avatar_manager.local_avatars_enabled?
+ return false unless user.is_a?(User)
- def avatar_manager
- ::OpenProject::Avatars::AvatarManager
- end
+ user.local_avatar_attachment
+ end
- def build_gravatar_image_url(user, options = {})
- mail = extract_email_address(user)
- raise ArgumentError.new('Invalid Mail') unless mail.present?
+ def avatar_manager
+ ::OpenProject::Avatars::AvatarManager
+ end
- opts = options.merge(gravatar: default_gravatar_options)
- # gravatar_image_url expects gravatar options as second arg
- if opts[:gravatar]
- opts.merge!(opts.delete(:gravatar))
- end
+ def build_gravatar_image_url(user, options = {})
+ mail = extract_email_address(user)
+ raise ArgumentError.new('Invalid Mail') if mail.blank?
- gravatar_image_url(mail, opts)
+ opts = options.merge(gravatar: default_gravatar_options)
+ # gravatar_image_url expects gravatar options as second arg
+ if opts[:gravatar]
+ opts.merge!(opts.delete(:gravatar))
end
- def build_principal_avatar_tag(user, options = {})
- tag_options = merge_default_avatar_options(user, options)
-
- principal_type = API::V3::Principals::PrincipalType.for(user)
- principal = {
- href: API::V3::Utilities::PathHelper::ApiV3Path.send(principal_type, user.id),
- name: user.name,
- id: user.id
- }
-
- angular_component_tag 'op-principal',
- class: tag_options[:class],
- inputs: {
- principal: principal,
- size: tag_options[:size],
- hideName: tag_options[:hide_name]
- }
- end
+ gravatar_image_url(mail, opts)
+ end
- def merge_default_avatar_options(user, options)
- default_options = {
- size: 'default',
- hide_name: true
- }
+ def build_principal_avatar_tag(user, options = {})
+ tag_options = merge_default_avatar_options(user, options)
+
+ principal_type = API::V3::Principals::PrincipalType.for(user)
+ principal = {
+ href: API::V3::Utilities::PathHelper::ApiV3Path.send(principal_type, user.id),
+ name: user.name,
+ id: user.id
+ }
+
+ angular_component_tag 'op-principal',
+ class: tag_options[:class],
+ inputs: {
+ principal:,
+ size: tag_options[:size],
+ hideName: tag_options[:hide_name]
+ }
+ end
- default_options[:title] = h(user.name) if user.respond_to?(:name)
+ def merge_default_avatar_options(user, options)
+ default_options = {
+ size: 'default',
+ hide_name: true
+ }
- options.reverse_merge(default_options)
- end
+ default_options[:title] = h(user.name) if user.respond_to?(:name)
- def default_gravatar_options
- {
- secure: OpenProject::Configuration.secure_connection?,
- default: OpenProject::Configuration.gravatar_fallback_image
- }
- end
+ options.reverse_merge(default_options)
+ end
- ##
- # Get a mail address used for Gravatar
- def extract_email_address(object)
- if object.respond_to?(:mail)
- object.mail
- end
- end
+ def default_gravatar_options
+ {
+ secure: OpenProject::Configuration.secure_connection?,
+ default: OpenProject::Configuration.gravatar_fallback_image
+ }
end
- prepend InstanceMethods
+ ##
+ # Get a mail address used for Gravatar
+ def extract_email_address(object)
+ if object.respond_to?(:mail)
+ object.mail
+ end
+ end
end
diff --git a/modules/avatars/app/models/users/avatars.rb b/modules/avatars/app/models/users/avatars.rb
index 4b5d96b30e8..ed474f73505 100644
--- a/modules/avatars/app/models/users/avatars.rb
+++ b/modules/avatars/app/models/users/avatars.rb
@@ -16,63 +16,48 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-module OpenProject::Avatars
- module Patches
- module UserPatch
- def self.included(base) # :nodoc:
- base.class_eval do
- acts_as_attachable
+module Users
+ module Avatars
+ extend ActiveSupport::Concern
- include InstanceMethods
+ included do
+ acts_as_attachable
+ end
- class << self
- def get_local_avatar(user_id)
- Attachment.find_by(container_id: user_id, container_type: 'Principal', description: 'avatar')
- end
- end
- end
+ class_methods do
+ def get_local_avatar(user_id)
+ Attachment.find_by(container_id: user_id, container_type: 'Principal', description: 'avatar')
end
+ end
- module InstanceMethods
- def reload(*args)
- reset_avatar_attachment_cache!
-
- super
- end
+ def reload(*args)
+ reset_avatar_attachment_cache!
- def local_avatar_attachment
- # @local_avatar_attachment can legitimately be nil which is why the
- # typical
- # inst_var ||= calculation
- # pattern does not work for caching
- return @local_avatar_attachment if @local_avatar_attachment_calculated
+ super
+ end
- @local_avatar_attachment_calculated ||= begin
- @local_avatar_attachment = attachments.find_by_description('avatar')
+ def local_avatar_attachment
+ defined?(@local_avatar_attachment) || begin
+ @local_avatar_attachment = attachments.find_by(description: 'avatar')
+ end
- true
- end
+ @local_avatar_attachment
+ end
- @local_avatar_attachment
- end
+ def local_avatar_attachment=(file)
+ local_avatar_attachment&.destroy
+ reset_avatar_attachment_cache!
- def local_avatar_attachment=(file)
- local_avatar_attachment&.destroy
- reset_avatar_attachment_cache!
+ @local_avatar_attachment = Attachments::CreateService
+ .new(user: User.system, contract_class: EmptyContract)
+ .call(file:, container: self, filename: file.original_filename, description: 'avatar')
+ .result
- @local_avatar_attachment = Attachments::CreateService
- .new(user: User.system, contract_class: EmptyContract)
- .call(file: file, container: self, filename: file.original_filename, description: 'avatar')
- .result
-
- touch
- end
+ touch
+ end
- def reset_avatar_attachment_cache!
- @local_avatar_attachment = nil
- @local_avatar_attachment_calculated = nil
- end
- end
+ def reset_avatar_attachment_cache!
+ @local_avatar_attachment = nil
end
end
end
diff --git a/modules/avatars/lib/open_project/avatars/engine.rb b/modules/avatars/lib/open_project/avatars/engine.rb
index fe034de716b..26ba3138328 100644
--- a/modules/avatars/lib/open_project/avatars/engine.rb
+++ b/modules/avatars/lib/open_project/avatars/engine.rb
@@ -51,15 +51,5 @@ module OpenProject::Avatars
path: ->(params) { edit_user_path(params[:user], tab: :avatar) },
label: :label_avatar,
only_if: ->(*) { User.current.admin? && ::OpenProject::Avatars::AvatarManager.avatars_enabled? }
-
- initializer 'patch avatar helper' do
- Rails.autoloaders.main.ignore(config.root.join('lib/open_project/avatars/patches/avatar_helper_patch.rb'))
-
- ActiveSupport.on_load(:op_helpers_avatar) do
- require_relative './patches/avatar_helper_patch'
- end
- end
-
- patches %i[User]
end
end