1
0

preview_card.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_and_belongs_to_many :statuses
  47. has_one :trend, class_name: 'PreviewCardTrend', inverse_of: :preview_card, dependent: :destroy
  48. 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
  49. validates :url, presence: true, uniqueness: true, url: true
  50. validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
  51. validates_attachment_size :image, less_than: LIMIT
  52. remotable_attachment :image, LIMIT
  53. scope :cached, -> { where.not(image_file_name: [nil, '']) }
  54. before_save :extract_dimensions, if: :link?
  55. def appropriate_for_trends?
  56. link? && article? && title.present? && description.present? && image.present? && provider_name.present?
  57. end
  58. def domain
  59. @domain ||= Addressable::URI.parse(url).normalized_host
  60. end
  61. def provider
  62. @provider ||= PreviewCardProvider.matching_domain(domain)
  63. end
  64. def trendable?
  65. if attributes['trendable'].nil?
  66. provider&.trendable?
  67. else
  68. attributes['trendable']
  69. end
  70. end
  71. def requires_review?
  72. attributes['trendable'].nil? && (provider.nil? || provider.requires_review?)
  73. end
  74. def requires_review_notification?
  75. attributes['trendable'].nil? && (provider.nil? || provider.requires_review_notification?)
  76. end
  77. def decaying?
  78. max_score_at && max_score_at >= Trends.links.options[:max_score_cooldown].ago && max_score_at < 1.day.ago
  79. end
  80. attr_writer :provider
  81. def local?
  82. false
  83. end
  84. def missing_image?
  85. width.present? && height.present? && image_file_name.blank?
  86. end
  87. def save_with_optional_image!
  88. save!
  89. rescue ActiveRecord::RecordInvalid
  90. self.image = nil
  91. save!
  92. end
  93. def history
  94. @history ||= Trends::History.new('links', id)
  95. end
  96. class << self
  97. private
  98. def image_styles(file)
  99. styles = {
  100. original: {
  101. pixels: 230_400, # 640x360px
  102. file_geometry_parser: FastGeometryParser,
  103. convert_options: '-coalesce',
  104. blurhash: BLURHASH_OPTIONS,
  105. },
  106. }
  107. styles[:original][:format] = 'jpg' if file.instance.image_content_type == 'image/gif'
  108. styles
  109. end
  110. end
  111. private
  112. def extract_dimensions
  113. file = image.queued_for_write[:original]
  114. return if file.nil?
  115. width, height = FastImage.size(file.path)
  116. return nil if width.nil?
  117. self.width = width
  118. self.height = height
  119. end
  120. end