preview_card.rb 4.2 KB

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