changeset 253:d726e8b92dd1

Support animated gif (same as video)
author nanaya <me@nanaya.net>
date Mon, 25 Mar 2024 02:49:50 +0900
parents 151bc6d97d39
children 89f6c0016bab
files app/lib/legit_client.rb app/views/tweets/_tweet.html.erb
diffstat 2 files changed, 22 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/app/lib/legit_client.rb	Tue Oct 17 00:04:11 2023 +0900
+++ b/app/lib/legit_client.rb	Mon Mar 25 02:49:50 2024 +0900
@@ -56,17 +56,18 @@
     json.each do |entity_media|
       val = {}
 
-      if entity_media['type'] == 'photo'
-        val[:image_url] = entity_media['media_url_https']
-      elsif entity_media['type'] == 'video'
-        val[:variants] = entity_media['video_info']['variants']
-          .filter { |variant| variant['bitrate'].present? }
-          .map do |variant|
-            {
-              bitrate: variant['bitrate'],
-              url: variant['url'],
-            }
-          end
+      case entity_media['type']
+        when 'animated_gif', 'video'
+          val[:variants] = entity_media['video_info']['variants']
+            .filter { |variant| variant['bitrate'].present? }
+            .map do |variant|
+              {
+                bitrate: variant['bitrate'],
+                url: variant['url'],
+              }
+            end
+        when 'photo'
+          val[:image_url] = entity_media['media_url_https'].sub(/\.([^.]+)$/, '?format=\1')
       end
 
       if !val.empty?
--- a/app/views/tweets/_tweet.html.erb	Tue Oct 17 00:04:11 2023 +0900
+++ b/app/views/tweets/_tweet.html.erb	Mon Mar 25 02:49:50 2024 +0900
@@ -40,15 +40,16 @@
 
   <p>
     <% tweet[:entity_media].each do |_short_url, media| %>
-      <% if media[:type] == 'photo' %>
-        <%= link_to "#{media[:image_url]}?name=orig" do %>
-          <%= image_tag "#{media[:image_url]}?name=small", :alt => "attachment #{media[:id]}" -%>
-        <% end %>
-      <% elsif media[:type] == 'video' %>
-        <%= video_tag media[:variants]
-          .sort_by { |variant| -variant[:bitrate] }
-          .map { |variant| variant[:url] }, width: '100%', controls: true
-        %>
+      <% case media[:type] %>
+        <% when 'photo' %>
+          <%= link_to "#{media[:image_url]}&name=orig" do %>
+            <%= image_tag "#{media[:image_url]}&name=small", :alt => "attachment #{media[:id]}" -%>
+          <% end %>
+        <% when 'animated_gif', 'video' %>
+          <%= video_tag media[:variants]
+            .sort_by { |variant| -variant[:bitrate] }
+            .map { |variant| variant[:url] }, width: '100%', controls: true
+          %>
       <% end %>
     <% end %>
   </p>