Move account header and avatar methods to a concern (#2825)

This commit is contained in:
Matt Jankowski 2017-05-05 15:37:02 -04:00 committed by Eugen Rochko
parent ee3e0a93f4
commit 4e05751346
4 changed files with 89 additions and 67 deletions

View file

@ -39,28 +39,18 @@
#
class Account < ApplicationRecord
include Targetable
MENTION_RE = /(?:^|[^\/\w])@([a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
include AccountAvatar
include AccountHeader
include Attachmentable
include Targetable
# Local users
has_one :user, inverse_of: :account
validates :username, presence: true, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, if: 'local?'
validates :username, presence: true, uniqueness: { scope: :domain, case_sensitive: true }, unless: 'local?'
# Avatar upload
has_attached_file :avatar, styles: ->(f) { avatar_styles(f) }, convert_options: { all: '-quality 80 -strip' }
validates_attachment_content_type :avatar, content_type: IMAGE_MIME_TYPES
validates_attachment_size :avatar, less_than: 2.megabytes
# Header upload
has_attached_file :header, styles: ->(f) { header_styles(f) }, convert_options: { all: '-quality 80 -strip' }
validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES
validates_attachment_size :header, less_than: 2.megabytes
include Attachmentable
# Local user profile validations
validates :display_name, length: { maximum: 30 }, if: 'local?'
validates :note, length: { maximum: 160 }, if: 'local?'
@ -206,7 +196,7 @@ class Account < ApplicationRecord
OStatus2::Subscription.new(remote_url, secret: secret, lease_seconds: 86_400 * 30, webhook: webhook_url, hub: hub_url)
end
def save_with_optional_avatar!
def save_with_optional_media!
save!
rescue ActiveRecord::RecordInvalid
self.avatar = nil
@ -216,44 +206,6 @@ class Account < ApplicationRecord
save!
end
def avatar_original_url
avatar.url(:original)
end
def avatar_static_url
avatar_content_type == 'image/gif' ? avatar.url(:static) : avatar_original_url
end
def header_original_url
header.url(:original)
end
def header_static_url
header_content_type == 'image/gif' ? header.url(:static) : header_original_url
end
def avatar_remote_url=(url)
parsed_url = Addressable::URI.parse(url).normalize
return if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? || self[:avatar_remote_url] == url
self.avatar = URI.parse(parsed_url.to_s)
self[:avatar_remote_url] = url
rescue OpenURI::HTTPError => e
Rails.logger.debug "Error fetching remote avatar: #{e}"
end
def header_remote_url=(url)
parsed_url = Addressable::URI.parse(url).normalize
return if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? || self[:header_remote_url] == url
self.header = URI.parse(parsed_url.to_s)
self[:header_remote_url] = url
rescue OpenURI::HTTPError => e
Rails.logger.debug "Error fetching remote header: #{e}"
end
def object_type
:person
end
@ -372,18 +324,6 @@ class Account < ApplicationRecord
def follow_mapping(query, field)
query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true }
end
def avatar_styles(file)
styles = { original: '120x120#' }
styles[:static] = { format: 'png' } if file.content_type == 'image/gif'
styles
end
def header_styles(file)
styles = { original: '700x335#' }
styles[:static] = { format: 'png' } if file.content_type == 'image/gif'
styles
end
end
before_create :generate_keys

View file

@ -0,0 +1,41 @@
# frozen_string_literal: true
module AccountAvatar
extend ActiveSupport::Concern
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
class_methods do
def avatar_styles(file)
styles = { original: '120x120#' }
styles[:static] = { format: 'png' } if file.content_type == 'image/gif'
styles
end
private :avatar_styles
end
included do
# Avatar upload
has_attached_file :avatar, styles: ->(f) { avatar_styles(f) }, convert_options: { all: '-quality 80 -strip' }
validates_attachment_content_type :avatar, content_type: IMAGE_MIME_TYPES
validates_attachment_size :avatar, less_than: 2.megabytes
def avatar_original_url
avatar.url(:original)
end
def avatar_static_url
avatar_content_type == 'image/gif' ? avatar.url(:static) : avatar_original_url
end
def avatar_remote_url=(url)
parsed_url = Addressable::URI.parse(url).normalize
return if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? || self[:avatar_remote_url] == url
self.avatar = URI.parse(parsed_url.to_s)
self[:avatar_remote_url] = url
rescue OpenURI::HTTPError => e
Rails.logger.debug "Error fetching remote avatar: #{e}"
end
end
end

View file

@ -0,0 +1,41 @@
# frozen_string_literal: true
module AccountHeader
extend ActiveSupport::Concern
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
class_methods do
def header_styles(file)
styles = { original: '700x335#' }
styles[:static] = { format: 'png' } if file.content_type == 'image/gif'
styles
end
private :header_styles
end
included do
# Header upload
has_attached_file :header, styles: ->(f) { header_styles(f) }, convert_options: { all: '-quality 80 -strip' }
validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES
validates_attachment_size :header, less_than: 2.megabytes
def header_original_url
header.url(:original)
end
def header_static_url
header_content_type == 'image/gif' ? header.url(:static) : header_original_url
end
def header_remote_url=(url)
parsed_url = Addressable::URI.parse(url).normalize
return if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? || self[:header_remote_url] == url
self.header = URI.parse(parsed_url.to_s)
self[:header_remote_url] = url
rescue OpenURI::HTTPError => e
Rails.logger.debug "Error fetching remote header: #{e}"
end
end
end

View file

@ -26,7 +26,7 @@ class UpdateRemoteProfileService < BaseService
old_hub_url = account.hub_url
account.hub_url = hub_link['href'] if !hub_link.nil? && !hub_link['href'].blank? && (hub_link['href'] != old_hub_url)
account.save_with_optional_avatar!
account.save_with_optional_media!
Pubsubhubbub::SubscribeWorker.perform_async(account.id) if resubscribe && (account.hub_url != old_hub_url)
end