preview_card.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: preview_cards
  5. #
  6. # id :bigint(8) not null, primary key
  7. # url :string default(""), not null
  8. # title :string default(""), not null
  9. # description :string default(""), not null
  10. # image_file_name :string
  11. # image_content_type :string
  12. # image_file_size :integer
  13. # image_updated_at :datetime
  14. # type :integer default("link"), not null
  15. # html :text default(""), not null
  16. # author_name :string default(""), not null
  17. # author_url :string default(""), not null
  18. # provider_name :string default(""), not null
  19. # provider_url :string default(""), not null
  20. # width :integer default(0), not null
  21. # height :integer default(0), not null
  22. # created_at :datetime not null
  23. # updated_at :datetime not null
  24. # embed_url :string default(""), not null
  25. # image_storage_schema_version :integer
  26. # blurhash :string
  27. # language :string
  28. # max_score :float
  29. # max_score_at :datetime
  30. # trendable :boolean
  31. # link_type :integer
  32. #
  33. class PreviewCard < ApplicationRecord
  34. include Attachmentable
  35. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
  36. LIMIT = 1.megabytes
  37. BLURHASH_OPTIONS = {
  38. x_comp: 4,
  39. y_comp: 4,
  40. }.freeze
  41. self.inheritance_column = false
  42. enum type: [:link, :photo, :video, :rich]
  43. enum link_type: [:unknown, :article]
  44. has_and_belongs_to_many :statuses
  45. has_one :trend, class_name: 'PreviewCardTrend', inverse_of: :preview_card, dependent: :destroy
  46. has_attached_file :image, processors: [:thumbnail, :blurhash_transcoder], styles: ->(f) { image_styles(f) }, convert_options: { all: '-quality 90 +profile "!icc,*" +set modify-date +set create-date' }, validate_media_type: false
  47. validates :url, presence: true, uniqueness: true
  48. validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
  49. validates_attachment_size :image, less_than: LIMIT
  50. remotable_attachment :image, LIMIT
  51. scope :cached, -> { where.not(image_file_name: [nil, '']) }
  52. before_save :extract_dimensions, if: :link?
  53. def appropriate_for_trends?
  54. link? && article? && title.present? && description.present? && image.present? && provider_name.present?
  55. end
  56. def domain
  57. @domain ||= Addressable::URI.parse(url).normalized_host
  58. end
  59. def provider
  60. @provider ||= PreviewCardProvider.matching_domain(domain)
  61. end
  62. def trendable?
  63. if attributes['trendable'].nil?
  64. provider&.trendable?
  65. else
  66. attributes['trendable']
  67. end
  68. end
  69. def requires_review?
  70. attributes['trendable'].nil? && (provider.nil? || provider.requires_review?)
  71. end
  72. def requires_review_notification?
  73. attributes['trendable'].nil? && (provider.nil? || provider.requires_review_notification?)
  74. end
  75. def decaying?
  76. max_score_at && max_score_at >= Trends.links.options[:max_score_cooldown].ago && max_score_at < 1.day.ago
  77. end
  78. attr_writer :provider
  79. def local?
  80. false
  81. end
  82. def missing_image?
  83. width.present? && height.present? && image_file_name.blank?
  84. end
  85. def save_with_optional_image!
  86. save!
  87. rescue ActiveRecord::RecordInvalid
  88. self.image = nil
  89. save!
  90. end
  91. def history
  92. @history ||= Trends::History.new('links', id)
  93. end
  94. class << self
  95. private
  96. def image_styles(file)
  97. styles = {
  98. original: {
  99. geometry: '400x400>',
  100. file_geometry_parser: FastGeometryParser,
  101. convert_options: '-coalesce',
  102. blurhash: BLURHASH_OPTIONS,
  103. },
  104. }
  105. styles[:original][:format] = 'jpg' if file.instance.image_content_type == 'image/gif'
  106. styles
  107. end
  108. end
  109. private
  110. def extract_dimensions
  111. file = image.queued_for_write[:original]
  112. return if file.nil?
  113. width, height = FastImage.size(file.path)
  114. return nil if width.nil?
  115. self.width = width
  116. self.height = height
  117. end
  118. end