diff --git a/backend/app/controllers/posts_controller.rb b/backend/app/controllers/posts_controller.rb index 5658ebb..f76e83a 100644 --- a/backend/app/controllers/posts_controller.rb +++ b/backend/app/controllers/posts_controller.rb @@ -216,7 +216,8 @@ class PostsController < ApplicationController preflight = PostCreatePreflight.new( attributes: post_create_attributes, - thumbnail: params[:thumbnail]).run + thumbnail: params[:thumbnail], + host: request.base_url).run return render json: dry_run_json(preflight) if bool?(:dry) if preflight[:existing_post_id].present? post = Post.new(url: preflight[:url]) @@ -273,7 +274,11 @@ class PostsController < ApplicationController return head :payload_too_large if request.content_length.to_i > MAX_BULK_REQUEST_BYTES posts = parse_bulk_posts_manifest thumbnails = parse_bulk_thumbnails(posts.length) - result = PostBulkCreator.new(actor: current_user, posts:, thumbnails:).run + result = PostBulkCreator.new( + actor: current_user, + posts:, + thumbnails:, + host: request.base_url).run render json: result rescue JSON::ParserError render_bad_request 'posts manifest の JSON が不正です.' @@ -627,7 +632,7 @@ class PostsController < ApplicationController return nil if post_id.blank? post = Post.with_attached_thumbnail.find_by(id: post_id) - PostCompactRepr.base(post) + PostCompactRepr.base(post, host: request.base_url) end def dry_run_json preflight diff --git a/backend/app/representations/post_compact_repr.rb b/backend/app/representations/post_compact_repr.rb index eca81aa..94fcdc7 100644 --- a/backend/app/representations/post_compact_repr.rb +++ b/backend/app/representations/post_compact_repr.rb @@ -4,11 +4,11 @@ module PostCompactRepr module_function - def base post + def base post, host: nil return nil if post.nil? PostRepr - .common(post) + .common(post, host:) .slice( 'id', 'title', diff --git a/backend/app/representations/post_repr.rb b/backend/app/representations/post_repr.rb index d113263..2ea60fe 100644 --- a/backend/app/representations/post_repr.rb +++ b/backend/app/representations/post_repr.rb @@ -17,8 +17,8 @@ module PostRepr module_function - def base post, current_user = nil - json = common(post) + def base post, current_user = nil, host: nil + json = common(post, host:) json['tags'] = tag_json(post) json['uploaded_user'] = post.uploaded_user && UserRepr.base(post.uploaded_user) json['viewed'] = current_user ? current_user.viewed?(post) : false @@ -26,31 +26,31 @@ module PostRepr end def detail post, current_user = nil, parent_posts: [], child_posts: [], - sibling_posts: { }, related: [] - base(post, current_user).merge( - 'parent_posts' => cards(parent_posts), - 'child_posts' => cards(child_posts), + sibling_posts: { }, related: [], host: nil + base(post, current_user, host:).merge( + 'parent_posts' => cards(parent_posts, host:), + 'child_posts' => cards(child_posts, host:), 'sibling_posts' => sibling_posts.transform_keys(&:to_s).transform_values { |posts| - cards(posts) + cards(posts, host:) }, - 'related' => cards(related)) + 'related' => cards(related, host:)) end - def card post - common(post).merge('parent_posts' => [], 'child_posts' => []) + def card post, host: nil + common(post, host:).merge('parent_posts' => [], 'child_posts' => []) end - def cards posts - posts.map { |post| card(post) } + def cards posts, host: nil + posts.map { |post| card(post, host:) } end - def many posts, current_user = nil - posts.map { |p| base(p, current_user) } + def many posts, current_user = nil, host: nil + posts.map { |p| base(p, current_user, host:) } end - def common post + def common post, host: nil BASE_FIELDS.to_h { |field| [field.to_s, post.public_send(field)] } - .merge('thumbnail' => thumbnail_url(post)) + .merge('thumbnail' => thumbnail_url(post, host:)) end def tag_json post @@ -65,10 +65,13 @@ module PostRepr } end - def thumbnail_url post + def thumbnail_url post, host: nil return nil unless post.thumbnail.attached? - Rails.application.routes.url_helpers.rails_blob_url(post.thumbnail, only_path: false) + Rails.application.routes.url_helpers.rails_storage_proxy_url( + post.thumbnail, + only_path: false, + host:) rescue ActionController::UrlGenerationError, ArgumentError, URI::InvalidURIError => e Rails.logger.warn( "PostRepr.thumbnail_url failed post_id=#{post.id} " \ diff --git a/backend/app/services/post_bulk_creator.rb b/backend/app/services/post_bulk_creator.rb index 8bb6eca..1e75e24 100644 --- a/backend/app/services/post_bulk_creator.rb +++ b/backend/app/services/post_bulk_creator.rb @@ -1,8 +1,9 @@ class PostBulkCreator - def initialize actor:, posts:, thumbnails: + def initialize actor:, posts:, thumbnails:, host: nil @actor_id = actor.id @posts = posts @thumbnails = thumbnails + @host = host end def run @@ -64,7 +65,8 @@ class PostBulkCreator preflight = PostCreatePreflight.new( attributes: attributes, - thumbnail: thumbnail_for(index, attributes)).run + thumbnail: thumbnail_for(index, attributes), + host: @host).run if preflight[:existing_post_id].present? return { status: 'skipped', @@ -195,6 +197,6 @@ class PostBulkCreator end def compact_existing_post post - PostCompactRepr.base(post) + PostCompactRepr.base(post, host: @host) end end diff --git a/backend/app/services/post_create_preflight.rb b/backend/app/services/post_create_preflight.rb index c633b47..04d3492 100644 --- a/backend/app/services/post_create_preflight.rb +++ b/backend/app/services/post_create_preflight.rb @@ -9,9 +9,10 @@ class PostCreatePreflight end end - def initialize attributes:, thumbnail: nil + def initialize attributes:, thumbnail: nil, host: nil @attributes = attributes.symbolize_keys @thumbnail = thumbnail + @host = host end def run @@ -124,7 +125,7 @@ class PostCreatePreflight return nil if post_id.blank? post = Post.with_attached_thumbnail.find_by(id: post_id) - PostCompactRepr.base(post) + PostCompactRepr.base(post, host: @host) end def final_field_warnings field_warnings