Fix reloading avatars by removing the patching

This commit is contained in:
Oliver Günther
2022-05-24 09:52:28 +02:00
parent f45aa936bb
commit fb6d85d50b
5 changed files with 137 additions and 198 deletions
-42
View File
@@ -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 <joe@foo.bar>')
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)
+1
View File
@@ -39,6 +39,7 @@ class User < Principal
}.freeze
include ::Associations::Groupable
include ::Users::Avatars
extend DeprecatedAlias
has_many :categories, foreign_key: 'assigned_to_id',
+102 -97
View File
@@ -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 <joe@foo.bar>')
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
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 avatar_manager
::OpenProject::Avatars::AvatarManager
end
def build_gravatar_image_url(user, options = {})
mail = extract_email_address(user)
raise ArgumentError.new('Invalid Mail') unless mail.present?
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
gravatar_image_url(mail, opts)
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
def merge_default_avatar_options(user, options)
default_options = {
size: 'default',
hide_name: true
}
default_options[:title] = h(user.name) if user.respond_to?(:name)
options.reverse_merge(default_options)
end
def default_gravatar_options
{
secure: OpenProject::Configuration.secure_connection?,
default: OpenProject::Configuration.gravatar_fallback_image
}
end
##
# Get a mail address used for Gravatar
def extract_email_address(object)
if object.respond_to?(:mail)
object.mail
end
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 <joe@foo.bar>')
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
prepend InstanceMethods
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?
return false unless user.is_a?(User)
user.local_avatar_attachment
end
def avatar_manager
::OpenProject::Avatars::AvatarManager
end
def build_gravatar_image_url(user, options = {})
mail = extract_email_address(user)
raise ArgumentError.new('Invalid Mail') if mail.blank?
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
gravatar_image_url(mail, opts)
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:,
size: tag_options[:size],
hideName: tag_options[:hide_name]
}
end
def merge_default_avatar_options(user, options)
default_options = {
size: 'default',
hide_name: true
}
default_options[:title] = h(user.name) if user.respond_to?(:name)
options.reverse_merge(default_options)
end
def default_gravatar_options
{
secure: OpenProject::Configuration.secure_connection?,
default: OpenProject::Configuration.gravatar_fallback_image
}
end
##
# Get a mail address used for Gravatar
def extract_email_address(object)
if object.respond_to?(:mail)
object.mail
end
end
end
+34 -49
View File
@@ -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
def reload(*args)
reset_avatar_attachment_cache!
super
end
def local_avatar_attachment
defined?(@local_avatar_attachment) || begin
@local_avatar_attachment = attachments.find_by(description: 'avatar')
end
module InstanceMethods
def reload(*args)
reset_avatar_attachment_cache!
@local_avatar_attachment
end
super
end
def local_avatar_attachment=(file)
local_avatar_attachment&.destroy
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
@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_calculated ||= begin
@local_avatar_attachment = attachments.find_by_description('avatar')
touch
end
true
end
@local_avatar_attachment
end
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: file, container: self, filename: file.original_filename, description: 'avatar')
.result
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
@@ -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