From bdf83c353f8ff8c9794a11a4133f74129fac8670 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 20 Sep 2024 08:39:48 -0400 Subject: [PATCH] Move default embed size knowledge into `OEmbedSerializer` (#31990) Co-authored-by: Claire --- app/controllers/api/oembed_controller.rb | 10 +--------- app/controllers/api/web/embeds_controller.rb | 2 +- app/serializers/oembed_serializer.rb | 6 ++++-- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/oembed_controller.rb b/app/controllers/api/oembed_controller.rb index 66da65bed..b7f22824a 100644 --- a/app/controllers/api/oembed_controller.rb +++ b/app/controllers/api/oembed_controller.rb @@ -7,7 +7,7 @@ class Api::OEmbedController < Api::BaseController before_action :require_public_status! def show - render json: @status, serializer: OEmbedSerializer, width: maxwidth_or_default, height: maxheight_or_default + render json: @status, serializer: OEmbedSerializer, width: params[:maxwidth], height: params[:maxheight] end private @@ -23,12 +23,4 @@ class Api::OEmbedController < Api::BaseController def status_finder StatusFinder.new(params[:url]) end - - def maxwidth_or_default - (params[:maxwidth].presence || 400).to_i - end - - def maxheight_or_default - params[:maxheight].present? ? params[:maxheight].to_i : nil - end end diff --git a/app/controllers/api/web/embeds_controller.rb b/app/controllers/api/web/embeds_controller.rb index 63c3f2d90..f82c1c50d 100644 --- a/app/controllers/api/web/embeds_controller.rb +++ b/app/controllers/api/web/embeds_controller.rb @@ -9,7 +9,7 @@ class Api::Web::EmbedsController < Api::Web::BaseController return not_found if @status.hidden? if @status.local? - render json: @status, serializer: OEmbedSerializer, width: 400 + render json: @status, serializer: OEmbedSerializer else return not_found unless user_signed_in? diff --git a/app/serializers/oembed_serializer.rb b/app/serializers/oembed_serializer.rb index 19fa5ddec..a693a961f 100644 --- a/app/serializers/oembed_serializer.rb +++ b/app/serializers/oembed_serializer.rb @@ -8,6 +8,8 @@ class OEmbedSerializer < ActiveModel::Serializer div1: 'font-weight: 500;', }.freeze + DEFAULT_WIDTH = 400 + include RoutingHelper include ActionView::Helpers::TagHelper @@ -57,10 +59,10 @@ class OEmbedSerializer < ActiveModel::Serializer end def width - instance_options[:width] + (instance_options[:width] || DEFAULT_WIDTH).to_i end def height - instance_options[:height] + instance_options[:height].presence&.to_i end end