このコミットが含まれているのは:
@@ -18,7 +18,12 @@ module PostRepr
|
|||||||
module_function
|
module_function
|
||||||
|
|
||||||
def base post, current_user = nil, host: nil
|
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['tags'] = tag_json(post)
|
||||||
json['uploaded_user'] = post.uploaded_user && UserRepr.base(post.uploaded_user)
|
json['uploaded_user'] = post.uploaded_user && UserRepr.base(post.uploaded_user)
|
||||||
json['viewed'] = current_user ? current_user.viewed?(post) : false
|
json['viewed'] = current_user ? current_user.viewed?(post) : false
|
||||||
@@ -27,6 +32,7 @@ module PostRepr
|
|||||||
|
|
||||||
def detail post, current_user = nil, parent_posts: [], child_posts: [],
|
def detail post, current_user = nil, parent_posts: [], child_posts: [],
|
||||||
sibling_posts: { }, related: [], host: nil
|
sibling_posts: { }, related: [], host: nil
|
||||||
|
if host.present?
|
||||||
base(post, current_user, host:).merge(
|
base(post, current_user, host:).merge(
|
||||||
'parent_posts' => cards(parent_posts, host:),
|
'parent_posts' => cards(parent_posts, host:),
|
||||||
'child_posts' => cards(child_posts, host:),
|
'child_posts' => cards(child_posts, host:),
|
||||||
@@ -34,23 +40,50 @@ module PostRepr
|
|||||||
cards(posts, host:)
|
cards(posts, host:)
|
||||||
},
|
},
|
||||||
'related' => cards(related, 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
|
end
|
||||||
|
|
||||||
def card post, host: nil
|
def card post, host: nil
|
||||||
|
if host.present?
|
||||||
common(post, host:).merge('parent_posts' => [], 'child_posts' => [])
|
common(post, host:).merge('parent_posts' => [], 'child_posts' => [])
|
||||||
|
else
|
||||||
|
common(post).merge('parent_posts' => [], 'child_posts' => [])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cards posts, host: nil
|
def cards posts, host: nil
|
||||||
|
if host.present?
|
||||||
posts.map { |post| card(post, host:) }
|
posts.map { |post| card(post, host:) }
|
||||||
|
else
|
||||||
|
posts.map { |post| card(post) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def many posts, current_user = nil, host: nil
|
def many posts, current_user = nil, host: nil
|
||||||
|
if host.present?
|
||||||
posts.map { |p| base(p, current_user, host:) }
|
posts.map { |p| base(p, current_user, host:) }
|
||||||
|
else
|
||||||
|
posts.map { |p| base(p, current_user) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def common post, host: nil
|
def common post, host: nil
|
||||||
BASE_FIELDS.to_h { |field| [field.to_s, post.public_send(field)] }
|
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
|
end
|
||||||
|
|
||||||
def tag_json post
|
def tag_json post
|
||||||
@@ -68,10 +101,10 @@ module PostRepr
|
|||||||
def thumbnail_url post, host: nil
|
def thumbnail_url post, host: nil
|
||||||
return nil unless post.thumbnail.attached?
|
return nil unless post.thumbnail.attached?
|
||||||
|
|
||||||
Rails.application.routes.url_helpers.rails_storage_proxy_url(
|
options = { only_path: false }
|
||||||
post.thumbnail,
|
options[:host] = host if host.present?
|
||||||
only_path: false,
|
|
||||||
host:)
|
Rails.application.routes.url_helpers.rails_storage_proxy_url(post.thumbnail, **options)
|
||||||
rescue ActionController::UrlGenerationError, ArgumentError, URI::InvalidURIError => e
|
rescue ActionController::UrlGenerationError, ArgumentError, URI::InvalidURIError => e
|
||||||
Rails.logger.warn(
|
Rails.logger.warn(
|
||||||
"PostRepr.thumbnail_url failed post_id=#{post.id} " \
|
"PostRepr.thumbnail_url failed post_id=#{post.id} " \
|
||||||
|
|||||||
新しい課題から参照
ユーザをブロックする