1
0

preview_card.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. # published_at :datetime
  33. # image_description :string default(""), not null
  34. # author_account_id :bigint(8)
  35. #
  36. class PreviewCard < ApplicationRecord
  37. include Attachmentable
  38. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
  39. LIMIT = Rails.configuration.x.use_vips ? 8.megabytes : 2.megabytes
  40. BLURHASH_OPTIONS = {
  41. x_comp: 4,
  42. y_comp: 4,
  43. }.freeze
  44. # URL size limit to safely store in PosgreSQL's unique indexes
  45. # Technically this is a byte-size limit but we use it as a
  46. # character limit to work with length validation
  47. URL_CHARACTER_LIMIT = 2692
  48. self.inheritance_column = false
  49. enum :type, { link: 0, photo: 1, video: 2, rich: 3 }
  50. enum :link_type, { unknown: 0, article: 1 }
  51. has_many :preview_cards_statuses, dependent: :delete_all, inverse_of: :preview_card
  52. has_many :statuses, through: :preview_cards_statuses
  53. has_one :trend, class_name: 'PreviewCardTrend', inverse_of: :preview_card, dependent: :destroy
  54. belongs_to :author_account, class_name: 'Account', optional: true
  55. has_attached_file :image,
  56. processors: [Rails.configuration.x.use_vips ? :lazy_thumbnail : :thumbnail, :blurhash_transcoder],
  57. styles: ->(f) { image_styles(f) },
  58. convert_options: { all: '-quality 90 +profile "!icc,*" +set date:modify +set date:create +set date:timestamp' },
  59. validate_media_type: false
  60. validates :url, presence: true, uniqueness: true, url: true, length: { maximum: URL_CHARACTER_LIMIT }
  61. validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
  62. validates_attachment_size :image, less_than: LIMIT
  63. remotable_attachment :image, LIMIT
  64. scope :cached, -> { where.not(image_file_name: [nil, '']) }
  65. before_save :extract_dimensions, if: :link?
  66. # This can be set by the status when retrieving the preview card using the join model
  67. attr_accessor :original_url
  68. def appropriate_for_trends?
  69. link? && article? && title.present? && description.present? && image.present? && provider_name.present?
  70. end
  71. def domain
  72. @domain ||= Addressable::URI.parse(url).normalized_host
  73. end
  74. def provider
  75. @provider ||= PreviewCardProvider.matching_domain(domain)
  76. end
  77. def trendable?
  78. if attributes['trendable'].nil?
  79. provider&.trendable?
  80. else
  81. attributes['trendable']
  82. end
  83. end
  84. def requires_review?
  85. attributes['trendable'].nil? && (provider.nil? || provider.requires_review?)
  86. end
  87. def requires_review_notification?
  88. attributes['trendable'].nil? && (provider.nil? || provider.requires_review_notification?)
  89. end
  90. def decaying?
  91. max_score_at && max_score_at >= Trends.links.options[:max_score_cooldown].ago && max_score_at < 1.day.ago
  92. end
  93. attr_writer :provider
  94. def local?
  95. false
  96. end
  97. def missing_image?
  98. width.present? && height.present? && image_file_name.blank?
  99. end
  100. def save_with_optional_image!
  101. save!
  102. rescue ActiveRecord::RecordInvalid
  103. self.image = nil
  104. save!
  105. end
  106. def history
  107. @history ||= Trends::History.new('links', id)
  108. end
  109. def authors
  110. @authors ||= [PreviewCard::Author.new(self)]
  111. end
  112. class Author < ActiveModelSerializers::Model
  113. attributes :name, :url, :account
  114. def initialize(preview_card)
  115. super(
  116. name: preview_card.author_name,
  117. url: preview_card.author_url,
  118. account: preview_card.author_account,
  119. )
  120. end
  121. end
  122. class << self
  123. private
  124. def image_styles(file)
  125. styles = {
  126. original: {
  127. pixels: 230_400, # 640x360px
  128. file_geometry_parser: FastGeometryParser,
  129. convert_options: '-coalesce',
  130. blurhash: BLURHASH_OPTIONS,
  131. },
  132. }
  133. styles[:original][:format] = 'jpg' if file.instance.image_content_type == 'image/gif'
  134. styles
  135. end
  136. end
  137. private
  138. def extract_dimensions
  139. file = image.queued_for_write[:original]
  140. return if file.nil?
  141. width, height = FastImage.size(file.path)
  142. return nil if width.nil?
  143. self.width = width
  144. self.height = height
  145. end
  146. end