Reviewed-on: #413 Co-authored-by: miteruzo <miteruzo@naver.com> Co-committed-by: miteruzo <miteruzo@naver.com>
このコミットはPull リクエスト #413 でマージされました.
このコミットが含まれているのは:
@@ -17,8 +17,13 @@ module PostRepr
|
||||
|
||||
module_function
|
||||
|
||||
def base post, current_user = nil
|
||||
json = common(post)
|
||||
def base post, current_user = nil, host: nil
|
||||
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
|
||||
@@ -26,31 +31,59 @@ 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' => sibling_posts.transform_keys(&:to_s).transform_values { |posts|
|
||||
cards(posts)
|
||||
},
|
||||
'related' => cards(related))
|
||||
sibling_posts: { }, related: [], host: nil
|
||||
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
|
||||
common(post).merge('parent_posts' => [], 'child_posts' => [])
|
||||
def card post, host: nil
|
||||
if host.present?
|
||||
common(post, host:).merge('parent_posts' => [], 'child_posts' => [])
|
||||
else
|
||||
common(post).merge('parent_posts' => [], 'child_posts' => [])
|
||||
end
|
||||
end
|
||||
|
||||
def cards posts
|
||||
posts.map { |post| card(post) }
|
||||
def cards posts, host: nil
|
||||
if host.present?
|
||||
posts.map { |post| card(post, host:) }
|
||||
else
|
||||
posts.map { |post| card(post) }
|
||||
end
|
||||
end
|
||||
|
||||
def many posts, current_user = nil
|
||||
posts.map { |p| base(p, current_user) }
|
||||
def many posts, current_user = nil, host: nil
|
||||
if host.present?
|
||||
posts.map { |p| base(p, current_user, host:) }
|
||||
else
|
||||
posts.map { |p| base(p, current_user) }
|
||||
end
|
||||
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' =>
|
||||
if host.present?
|
||||
thumbnail_url(post, host:)
|
||||
else
|
||||
thumbnail_url(post)
|
||||
end)
|
||||
end
|
||||
|
||||
def tag_json post
|
||||
@@ -65,11 +98,22 @@ 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)
|
||||
rescue
|
||||
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
|
||||
payload = {
|
||||
post_id: post.id,
|
||||
attachment_id: post.thumbnail.attachment&.id,
|
||||
blob_id: post.thumbnail.blob&.id,
|
||||
error_class: e.class,
|
||||
message: e.message }
|
||||
|
||||
Rails.logger.warn("PostRepr.thumbnail_url failed #{ payload.to_json }")
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
新しい課題から参照
ユーザをブロックする