Fix rubocop issues, introduce usage of frozen literal to improve performance
This commit is contained in:
		@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Account < ApplicationRecord
 | 
			
		||||
  include Targetable
 | 
			
		||||
  include PgSearch
 | 
			
		||||
@@ -92,11 +94,11 @@ class Account < ApplicationRecord
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def favourited?(status)
 | 
			
		||||
    (status.reblog? ? status.reblog : status).favourites.where(account: self).count > 0
 | 
			
		||||
    (status.reblog? ? status.reblog : status).favourites.where(account: self).count.positive?
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def reblogged?(status)
 | 
			
		||||
    (status.reblog? ? status.reblog : status).reblogs.where(account: self).count > 0
 | 
			
		||||
    (status.reblog? ? status.reblog : status).reblogs.where(account: self).count.positive?
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def keypair
 | 
			
		||||
@@ -115,8 +117,8 @@ class Account < ApplicationRecord
 | 
			
		||||
  def avatar_remote_url=(url)
 | 
			
		||||
    self.avatar = URI.parse(url) unless self[:avatar_remote_url] == url
 | 
			
		||||
    self[:avatar_remote_url] = url
 | 
			
		||||
  rescue OpenURI::HTTPError
 | 
			
		||||
    #
 | 
			
		||||
  rescue OpenURI::HTTPError => e
 | 
			
		||||
    Rails.logger.debug "Error fetching remote avatar: #{e}"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def object_type
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class ApplicationRecord < ActiveRecord::Base
 | 
			
		||||
  self.abstract_class = true
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Block < ApplicationRecord
 | 
			
		||||
  belongs_to :account
 | 
			
		||||
  belongs_to :target_account, class_name: 'Account'
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
module Paginable
 | 
			
		||||
  extend ActiveSupport::Concern
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
module Streamable
 | 
			
		||||
  extend ActiveSupport::Concern
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
module Targetable
 | 
			
		||||
  extend ActiveSupport::Concern
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class DomainBlock < ApplicationRecord
 | 
			
		||||
  validates :domain, presence: true, uniqueness: true
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Favourite < ApplicationRecord
 | 
			
		||||
  include Paginable
 | 
			
		||||
  include Streamable
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Feed
 | 
			
		||||
  def initialize(type, account)
 | 
			
		||||
    @type    = type
 | 
			
		||||
@@ -28,6 +30,6 @@ class Feed
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def redis
 | 
			
		||||
    $redis
 | 
			
		||||
    Redis.current
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Follow < ApplicationRecord
 | 
			
		||||
  include Paginable
 | 
			
		||||
  include Streamable
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class FollowSuggestion
 | 
			
		||||
  class << self
 | 
			
		||||
    def get(for_account_id, limit = 10)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class MediaAttachment < ApplicationRecord
 | 
			
		||||
  IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
 | 
			
		||||
  VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
 | 
			
		||||
@@ -6,9 +8,9 @@ class MediaAttachment < ApplicationRecord
 | 
			
		||||
  belongs_to :status,  inverse_of: :media_attachments
 | 
			
		||||
 | 
			
		||||
  has_attached_file :file,
 | 
			
		||||
    styles: -> (f) { file_styles f },
 | 
			
		||||
    processors: -> (f) { f.video? ? [:transcoder] : [:thumbnail] },
 | 
			
		||||
    convert_options: { all: "-strip" }
 | 
			
		||||
                    styles: -> (f) { file_styles f },
 | 
			
		||||
                    processors: -> (f) { f.video? ? [:transcoder] : [:thumbnail] },
 | 
			
		||||
                    convert_options: { all: '-strip' }
 | 
			
		||||
  validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
 | 
			
		||||
  validates_attachment_size :file, less_than: 4.megabytes
 | 
			
		||||
 | 
			
		||||
@@ -20,8 +22,8 @@ class MediaAttachment < ApplicationRecord
 | 
			
		||||
 | 
			
		||||
  def file_remote_url=(url)
 | 
			
		||||
    self.file = URI.parse(url)
 | 
			
		||||
  rescue OpenURI::HTTPError
 | 
			
		||||
    #
 | 
			
		||||
  rescue OpenURI::HTTPError => e
 | 
			
		||||
    Rails.logger.debug "Error fetching remote attachment: #{e}"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def image?
 | 
			
		||||
@@ -43,19 +45,19 @@ class MediaAttachment < ApplicationRecord
 | 
			
		||||
      if f.instance.image?
 | 
			
		||||
        {
 | 
			
		||||
          original: '100%',
 | 
			
		||||
          small: '510x680>'
 | 
			
		||||
          small: '510x680>',
 | 
			
		||||
        }
 | 
			
		||||
      else
 | 
			
		||||
        {
 | 
			
		||||
          small: {
 | 
			
		||||
            convert_options: {
 | 
			
		||||
              output: {
 | 
			
		||||
                vf: 'scale=\'min(510\, iw):min(680\, ih)\':force_original_aspect_ratio=decrease'
 | 
			
		||||
              }
 | 
			
		||||
                vf: 'scale=\'min(510\, iw):min(680\, ih)\':force_original_aspect_ratio=decrease',
 | 
			
		||||
              },
 | 
			
		||||
            },
 | 
			
		||||
            format: 'png',
 | 
			
		||||
            time: 1
 | 
			
		||||
          }
 | 
			
		||||
            time: 1,
 | 
			
		||||
          },
 | 
			
		||||
        }
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Mention < ApplicationRecord
 | 
			
		||||
  belongs_to :account, inverse_of: :mentions
 | 
			
		||||
  belongs_to :status
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Status < ApplicationRecord
 | 
			
		||||
  include Paginable
 | 
			
		||||
  include Streamable
 | 
			
		||||
@@ -89,22 +91,17 @@ class Status < ApplicationRecord
 | 
			
		||||
 | 
			
		||||
    def as_public_timeline(account = nil)
 | 
			
		||||
      query = joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id').where('accounts.silenced = FALSE')
 | 
			
		||||
 | 
			
		||||
      unless account.nil?
 | 
			
		||||
        query = filter_timeline(query, account)
 | 
			
		||||
      end
 | 
			
		||||
      query = filter_timeline(query, account) unless account.nil?
 | 
			
		||||
 | 
			
		||||
      query.with_includes.with_counters
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def as_tag_timeline(tag, account = nil)
 | 
			
		||||
      query = tag.statuses
 | 
			
		||||
        .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
 | 
			
		||||
        .where('accounts.silenced = FALSE')
 | 
			
		||||
                 .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
 | 
			
		||||
                 .where('accounts.silenced = FALSE')
 | 
			
		||||
 | 
			
		||||
      unless account.nil?
 | 
			
		||||
        query = filter_timeline(query, account)
 | 
			
		||||
      end
 | 
			
		||||
      query = filter_timeline(query, account) unless account.nil?
 | 
			
		||||
 | 
			
		||||
      query.with_includes.with_counters
 | 
			
		||||
    end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class StreamEntry < ApplicationRecord
 | 
			
		||||
  include Paginable
 | 
			
		||||
 | 
			
		||||
@@ -15,7 +17,11 @@ class StreamEntry < ApplicationRecord
 | 
			
		||||
  scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES, favourite: [:account, :stream_entry, status: STATUS_INCLUDES], follow: [:target_account, :stream_entry]) }
 | 
			
		||||
 | 
			
		||||
  def object_type
 | 
			
		||||
    orphaned? ? :activity : (targeted? ? :activity : activity.object_type)
 | 
			
		||||
    if orphaned?
 | 
			
		||||
      :activity
 | 
			
		||||
    else
 | 
			
		||||
      targeted? ? :activity : activity.object_type
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def verb
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class Tag < ApplicationRecord
 | 
			
		||||
  has_and_belongs_to_many :statuses
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class User < ApplicationRecord
 | 
			
		||||
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user