preview_card.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #
  35. class PreviewCard < ApplicationRecord
  36. include Attachmentable
  37. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
  38. LIMIT = 2.megabytes
  39. BLURHASH_OPTIONS = {
  40. x_comp: 4,
  41. y_comp: 4,
  42. }.freeze
  43. self.inheritance_column = false
  44. enum type: { link: 0, photo: 1, video: 2, rich: 3 }
  45. enum link_type: { unknown: 0, article: 1 }
  46. has_many :preview_cards_statuses, dependent: :delete_all, inverse_of: :preview_card
  47. has_many :statuses, through: :preview_cards_statuses
  48. has_one :trend, class_name: 'PreviewCardTrend', inverse_of: :preview_card, dependent: :destroy
  49. has_attached_file :image, processors: [:thumbnail, :blurhash_transcoder], styles: ->(f) { image_styles(f) }, convert_options: { all: '-quality 90 +profile "!icc,*" +set date:modify +set date:create +set date:timestamp' }, validate_media_type: false
  50. validates :url, presence: true, uniqueness: true, url: true
  51. validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
  52. validates_attachment_size :image, less_than: LIMIT
  53. remotable_attachment :image, LIMIT
  54. scope :cached, -> { where.not(image_file_name: [nil, '']) }
  55. before_save :extract_dimensions, if: :link?
  56. # This can be set by the status when retrieving the preview card using the join model
  57. attr_accessor :original_url
  58. def appropriate_for_trends?
  59. link? && article? && title.present? && description.present? && image.present? && provider_name.present?
  60. end
  61. def domain
  62. @domain ||= Addressable::URI.parse(url).normalized_host
  63. end
  64. def provider
  65. @provider ||= PreviewCardProvider.matching_domain(domain)
  66. end
  67. def trendable?
  68. if attributes['trendable'].nil?
  69. provider&.trendable?
  70. else
  71. attributes['trendable']
  72. end
  73. end
  74. def requires_review?
  75. attributes['trendable'].nil? && (provider.nil? || provider.requires_review?)
  76. end
  77. def requires_review_notification?
  78. attributes['trendable'].nil? && (provider.nil? || provider.requires_review_notification?)
  79. end
  80. def decaying?
  81. max_score_at && max_score_at >= Trends.links.options[:max_score_cooldown].ago && max_score_at < 1.day.ago
  82. end
  83. attr_writer :provider
  84. def local?
  85. false
  86. end
  87. def missing_image?
  88. width.present? && height.present? && image_file_name.blank?
  89. end
  90. def save_with_optional_image!
  91. save!
  92. rescue ActiveRecord::RecordInvalid
  93. self.image = nil
  94. save!
  95. end
  96. def history
  97. @history ||= Trends::History.new('links', id)
  98. end
  99. class << self
  100. private
  101. def image_styles(file)
  102. styles = {
  103. original: {
  104. pixels: 230_400, # 640x360px
  105. file_geometry_parser: FastGeometryParser,
  106. convert_options: '-coalesce',
  107. blurhash: BLURHASH_OPTIONS,
  108. },
  109. }
  110. styles[:original][:format] = 'jpg' if file.instance.image_content_type == 'image/gif'
  111. styles
  112. end
  113. end
  114. private
  115. def extract_dimensions
  116. file = image.queued_for_write[:original]
  117. return if file.nil?
  118. width, height = FastImage.size(file.path)
  119. return nil if width.nil?
  120. self.width = width
  121. self.height = height
  122. end
  123. end