From 51f9a56137794f4c7929c6ca773a4ada93bdfc12 Mon Sep 17 00:00:00 2001 From: jbrw Date: Tue, 24 Nov 2020 18:53:05 -0500 Subject: [PATCH] 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 --- .../discourse_category_onebox.mustache | 33 +++++++++++++++++++ lib/oneboxer.rb | 20 +++++++++++ spec/requests/onebox_controller_spec.rb | 23 +++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 lib/onebox/templates/discourse_category_onebox.mustache diff --git a/lib/onebox/templates/discourse_category_onebox.mustache b/lib/onebox/templates/discourse_category_onebox.mustache new file mode 100644 index 00000000000..68f41074adf --- /dev/null +++ b/lib/onebox/templates/discourse_category_onebox.mustache @@ -0,0 +1,33 @@ + diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index b5caa926a69..a9bdabdddf5 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -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 || "#{url}" @@ -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 diff --git a/spec/requests/onebox_controller_spec.rb b/spec/requests/onebox_controller_spec.rb index 122f7d330b2..9f10a920eca 100644 --- a/spec/requests/onebox_controller_spec.rb +++ b/spec/requests/onebox_controller_spec.rb @@ -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