FEATURE: Render Topics from Selected Categories/Tags on `/knowledge-explorer`

Add category/tag settings

Rename to knowledge explorer

Missed rename

Render a deduplicated list of topics from all selected tags and categories

Exclude category topic ids from rendered list
This commit is contained in:
Justin DiRose 2019-06-28 11:54:19 -05:00
parent 6add4cfc5f
commit 1789bac0cf
7 changed files with 102 additions and 5 deletions

View File

@ -0,0 +1,64 @@
module KnowledgeExplorer
class KnowledgeExplorerController< ApplicationController
skip_before_action :check_xhr
before_action :init_guardian
def index
category_topic_lists = []
tag_topic_lists = []
knowledge_explorer_categories.each do |c|
if topic_list = TopicQuery.new(current_user, category: c.id).list_latest
category_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
end
end
knowledge_explorer_tags.each do |t|
if topic_list = TopicQuery.new(current_user, tags: t.name).list_latest
tag_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
end
end
topics = []
category_topic_lists.each do |list|
list[:topic_list][:topics].each do |t|
if !topics.any?{|item| item[:id] == t[:id]}
if t[:id] != Category.find(t[:category_id]).topic_id
topics << t
end
end
end
end
tag_topic_lists.each do |list|
list[:topic_list][:topics].each do |t|
if !topics.any?{|item| item[:id] == t[:id]}
topics << t
end
end
end
render json: topics
end
private
def init_guardian
@guardian = Guardian.new(current_user)
end
def knowledge_explorer_categories
selected_categories = SiteSetting.knowledge_explorer_categories.split("|")
categories = Category.where('slug IN (?)', selected_categories)
categories.select { |c| @guardian.can_see_category?(c) }
end
def knowledge_explorer_tags
selected_tags = SiteSetting.knowledge_explorer_tags.split("|")
tags = Tag.where('name IN (?)', selected_tags)
end
end
end

View File

@ -1,3 +1,5 @@
en:
site_settings:
knowledge_base_enabled: "Enable the Knowledge Base Plugin"
knowledge_explorer_enabled: "Enable the Knowledge Explorer Plugin"
knowledge_explorer_categories: "A list of category slugs to include in the knowledge explorer"
knowledge_explorer_tags: "A list of tags to include in the knowledge explorer"

5
config/routes.rb Normal file
View File

@ -0,0 +1,5 @@
require_dependency "knowledge_explorer_constraint"
KnowledgeExplorer::Engine.routes.draw do
get "/" => "knowledge_explorer#index", constraints: KnowledgeExplorerConstraint.new
end

View File

@ -1,4 +1,12 @@
plugins:
knowledge_base_enabled:
knowledge_explorer_enabled:
default: false
client: true
knowledge_explorer_categories:
type: list
default: ""
client: true
knowledge_explorer_tags:
type: list
default: ""
client: true

View File

@ -0,0 +1,11 @@
module ::KnowledgeExplorer
class Engine < ::Rails::Engine
isolate_namespace KnowledgeExplorer
config.after_initialize do
Discourse::Application.routes.append do
mount ::KnowledgeExplorer::Engine, at: "/knowledge-explorer"
end
end
end
end

View File

@ -0,0 +1,5 @@
class KnowledgeExplorerConstraint
def matches?(request)
SiteSetting.knowledge_explorer_enabled
end
end

View File

@ -1,8 +1,10 @@
# name: discourse-knowledge-base
# about: A plugin to make it easy to find knowledge base articles in Discourse
# name: discourse-knowledge-explorer
# about: A plugin to make it easy to explore and find knowledge base-type articles in Discourse
# version: 0.1
# author: Justin DiRose
# license:
# url:
enabled_site_setting :knowledge_base_enabled
enabled_site_setting :knowledge_explorer_enabled
load File.expand_path('../lib/knowledge_explorer/engine.rb', __FILE__)