]> cat aescling's git repositories - mastodon.git/commitdiff
Simplify the og:image and og:description code in stream_entries/show (#1934)
authorMatt Jankowski <mjankowski@thoughtbot.com>
Sun, 16 Apr 2017 14:38:02 +0000 (10:38 -0400)
committerEugen <eugen@zeonfederated.com>
Sun, 16 Apr 2017 14:38:02 +0000 (16:38 +0200)
app/models/status.rb
app/views/stream_entries/_og_description.html.haml [new file with mode: 0644]
app/views/stream_entries/_og_image.html.haml [new file with mode: 0644]
app/views/stream_entries/show.html.haml

index 16cd4383f15d33f3c3a0a9a252b44ca048704c87..c0a5d9d1b120bdb82cd788e14ec7980e19e67999 100644 (file)
@@ -110,6 +110,10 @@ class Status < ApplicationRecord
     results
   end
 
+  def non_sensitive_with_media?
+    !sensitive? && media_attachments.any?
+  end
+
   class << self
     def as_home_timeline(account)
       where(account: [account] + account.following)
diff --git a/app/views/stream_entries/_og_description.html.haml b/app/views/stream_entries/_og_description.html.haml
new file mode 100644 (file)
index 0000000..5762aca
--- /dev/null
@@ -0,0 +1,4 @@
+- if activity.is_a?(Status) && activity.spoiler_text?
+  %meta{ property: 'og:description', content: activity.spoiler_text }/
+- else
+  %meta{ property: 'og:description', content: activity.content }/
diff --git a/app/views/stream_entries/_og_image.html.haml b/app/views/stream_entries/_og_image.html.haml
new file mode 100644 (file)
index 0000000..f725209
--- /dev/null
@@ -0,0 +1,6 @@
+- if activity.is_a?(Status) && activity.non_sensitive_with_media?
+  %meta{ property: 'og:image', content: full_asset_url(activity.media_attachments.first.file.url(:small)) }/
+- else
+  %meta{ property: 'og:image', content: full_asset_url(account.avatar.url(:original)) }/
+  %meta{ property: 'og:image:width', content: '120' }/
+  %meta{ property: 'og:image:height', content: '120' }/
index eb8387ccb6c58910fb804a08bf9e7ac05d0ad079..dea5e9d4080e1bc0ece3bbfdc520f4ad585003e3 100644 (file)
@@ -6,17 +6,8 @@
   %meta{ property: 'og:type', content: 'article' }/
   %meta{ property: 'og:title', content: "#{@account.username} on #{Rails.configuration.x.local_domain}" }/
 
-  - if @stream_entry.activity.is_a?(Status) && !@stream_entry.activity.spoiler_text.blank?
-    %meta{ property: 'og:description', content: @stream_entry.activity.spoiler_text }/
-  - else
-    %meta{ property: 'og:description', content: @stream_entry.activity.content }/
-
-  - if @stream_entry.activity.is_a?(Status) && !@stream_entry.activity.sensitive? && @stream_entry.activity.media_attachments.size > 0
-    %meta{ property: 'og:image', content: full_asset_url(@stream_entry.activity.media_attachments.first.file.url(:small)) }/
-  - else
-    %meta{ property: 'og:image', content: full_asset_url(@account.avatar.url(:original)) }/
-    %meta{ property: 'og:image:width', content: '120' }/
-    %meta{ property: 'og:image:height', content: '120' }/
+  = render 'stream_entries/og_description', activity: @stream_entry.activity
+  = render 'stream_entries/og_image', activity: @stream_entry.activity, account: @account
 
   %meta{ property: 'twitter:card', content: 'summary' }/