about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-01-20 01:00:14 +0100
committerEugen Rochko <eugen@zeonfederated.com>2017-01-20 01:00:14 +0100
commitf0de621e76b5a5ba3f7e67bd88c0183aac22b985 (patch)
tree06ecec4ca807eadc7ec743d64074fb9030e172f7 /app/models
parent8d0284f8d9bc22a6dd3bad1054fd8d78cbf32060 (diff)
Fix #463 - Fetch and display previews of URLs using OpenGraph tags
Diffstat (limited to 'app/models')
-rw-r--r--app/models/preview_card.rb20
-rw-r--r--app/models/status.rb1
2 files changed, 21 insertions, 0 deletions
diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb
new file mode 100644
index 000000000..e59b05eb8
--- /dev/null
+++ b/app/models/preview_card.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class PreviewCard < ApplicationRecord
+  IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
+
+  belongs_to :status
+
+  has_attached_file :image, styles: { original: '120x120#' }, convert_options: { all: '-quality 80 -strip' }
+
+  validates :url, presence: true
+  validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
+  validates_attachment_size :image, less_than: 1.megabytes
+
+  def save_with_optional_image!
+    save!
+  rescue ActiveRecord::RecordInvalid
+    self.image = nil
+    save!
+  end
+end
diff --git a/app/models/status.rb b/app/models/status.rb
index 5710f9cca..d5f52b55c 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -23,6 +23,7 @@ class Status < ApplicationRecord
   has_and_belongs_to_many :tags
 
   has_one :notification, as: :activity, dependent: :destroy
+  has_one :preview_card, dependent: :destroy
 
   validates :account, presence: true
   validates :uri, uniqueness: true, unless: 'local?'