FEATURE: Onebox local categories (#11311)

* FEATURE: onebox for local categories

This commit adjusts the category onebox to look more like the category boxes do on the category page.

Co-authored-by: Jordan Vidrine <jordan@jordanvidrine.com>
This commit is contained in:
jbrw 2020-11-24 18:53:05 -05:00 committed by GitHub
parent 416f984c5e
commit 51f9a56137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,33 @@
<aside class="onebox" style="border-left: 5px solid #{{color}}">
<article class="onebox-body category-onebox">
{{#logo_url}}
<img src="{{logo_url}}" class="thumbnail" />
{{/logo_url}}
<h3>
<a class="badge-wrapper bullet" href="{{url}}">
<span class="clear-badge"><span>{{{name}}}</span></span>
</a>
</h3>
{{#description}}
<div>
<span class="description">
<p>{{{description}}}</p>
</span>
</div>
{{/description}}
{{#has_subcategories}}
<div class="subcategories">
{{#subcategories}}
<span class="subcategory">
<a class="badge-wrapper bullet" href="{{url}}">
<span class="badge-category-bg" style="background-color: #{{{color}}}"></span>
<span class="badge-category clear-badge"><span class="category-name">{{{name}}}</span></span>
</a>
</span>
{{/subcategories}}
</div>
<div class="clearfix"></div>
{{/has_subcategories}}
</article>
<div class="clearfix"></div>
</aside>

View File

@ -188,6 +188,7 @@ module Oneboxer
when "uploads" then local_upload_html(url)
when "topics" then local_topic_html(url, route, opts)
when "users" then local_user_html(url, route)
when "list" then local_category_html(url, route)
end
html = html.presence || "<a href='#{url}'>#{url}</a>"
@ -293,6 +294,25 @@ module Oneboxer
end
end
def self.local_category_html(url, route)
return unless route[:category_slug_path_with_id]
category = Category.find_by_slug_path_with_id(route[:category_slug_path_with_id])
if Guardian.new.can_see_category?(category)
args = {
url: category.url,
name: category.name,
color: category.color,
logo_url: category.uploaded_logo&.url,
description: category.description,
has_subcategories: category.subcategories.present?,
subcategories: category.subcategories.collect { |sc| { name: sc.name, color: sc.color, url: sc.url } }
}
Mustache.render(template("discourse_category_onebox"), args)
end
end
def self.blocked_domains
SiteSetting.blocked_onebox_domains.split("|")
end

View File

@ -188,5 +188,28 @@ describe OneboxController do
get "/onebox.json", params: { url: url }
expect(response.body).to include('blockquote')
end
context 'local categories' do
fab!(:category) { Fabricate(:category) }
it 'oneboxes a public category' do
get "/onebox.json", params: { url: category.url }
expect(response.body).to include('aside')
end
it 'includes subcategories' do
subcategory = Fabricate(:category, name: "child", parent_category_id: category.id)
get "/onebox.json", params: { url: subcategory.url }
expect(response.body).not_to include('subcategories')
end
it 'does not onebox restricted categories' do
staff_category = Fabricate(:private_category, group: Group[:staff])
get "/onebox.json", params: { url: staff_category.url }
expect(response.body).not_to include('aside')
end
end
end
end