diff --git a/backend/app/representations/post_repr.rb b/backend/app/representations/post_repr.rb index 2ea60fe..1590ce0 100644 --- a/backend/app/representations/post_repr.rb +++ b/backend/app/representations/post_repr.rb @@ -18,7 +18,12 @@ module PostRepr module_function def base post, current_user = nil, host: nil - json = common(post, host:) + json = + if host.present? + common(post, host:) + else + common(post) + end 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 @@ -27,30 +32,58 @@ module PostRepr def detail post, current_user = nil, parent_posts: [], 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, host:) - }, - 'related' => cards(related, host:)) + if host.present? + 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, host:) + }, + 'related' => cards(related, host:)) + else + base(post, current_user).merge( + 'parent_posts' => cards(parent_posts), + 'child_posts' => cards(child_posts), + 'sibling_posts' => sibling_posts.transform_keys(&:to_s).transform_values { |posts| + cards(posts) + }, + 'related' => cards(related)) + end end def card post, host: nil - common(post, host:).merge('parent_posts' => [], 'child_posts' => []) + if host.present? + common(post, host:).merge('parent_posts' => [], 'child_posts' => []) + else + common(post).merge('parent_posts' => [], 'child_posts' => []) + end end def cards posts, host: nil - posts.map { |post| card(post, host:) } + if host.present? + posts.map { |post| card(post, host:) } + else + posts.map { |post| card(post) } + end end def many posts, current_user = nil, host: nil - posts.map { |p| base(p, current_user, host:) } + if host.present? + posts.map { |p| base(p, current_user, host:) } + else + posts.map { |p| base(p, current_user) } + end end def common post, host: nil BASE_FIELDS.to_h { |field| [field.to_s, post.public_send(field)] } - .merge('thumbnail' => thumbnail_url(post, host:)) + .merge( + 'thumbnail' => + if host.present? + thumbnail_url(post, host:) + else + thumbnail_url(post) + end) end def tag_json post @@ -68,10 +101,10 @@ module PostRepr def thumbnail_url post, host: nil return nil unless post.thumbnail.attached? - Rails.application.routes.url_helpers.rails_storage_proxy_url( - post.thumbnail, - only_path: false, - host:) + options = { only_path: false } + options[:host] = host if host.present? + + Rails.application.routes.url_helpers.rails_storage_proxy_url(post.thumbnail, **options) rescue ActionController::UrlGenerationError, ArgumentError, URI::InvalidURIError => e Rails.logger.warn( "PostRepr.thumbnail_url failed post_id=#{post.id} " \