You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.1 KiB

  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'].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_notification?
  69. attributes['trendable'].nil? && (provider.nil? || provider.requires_review_notification?)
  70. end
  71. def decaying?
  72. max_score_at && max_score_at >= Trends.links.options[:max_score_cooldown].ago && max_score_at < 1.day.ago
  73. end
  74. attr_writer :provider
  75. def local?
  76. false
  77. end
  78. def missing_image?
  79. width.present? && height.present? && image_file_name.blank?
  80. end
  81. def save_with_optional_image!
  82. save!
  83. rescue ActiveRecord::RecordInvalid
  84. self.image = nil
  85. save!
  86. end
  87. def history
  88. @history ||= Trends::History.new('links', id)
  89. end
  90. class << self
  91. private
  92. def image_styles(file)
  93. styles = {
  94. original: {
  95. geometry: '400x400>',
  96. file_geometry_parser: FastGeometryParser,
  97. convert_options: '-coalesce -strip',
  98. blurhash: BLURHASH_OPTIONS,
  99. },
  100. }
  101. styles[:original][:format] = 'jpg' if file.instance.image_content_type == 'image/gif'
  102. styles
  103. end
  104. end
  105. private
  106. def extract_dimensions
  107. file = image.queued_for_write[:original]
  108. return if file.nil?
  109. width, height = FastImage.size(file.path)
  110. return nil if width.nil?
  111. self.width = width
  112. self.height = height
  113. end
  114. end