diff --git a/app/controllers/knowledge_explorer/knowledge_explorer_controller.rb b/app/controllers/knowledge_explorer/knowledge_explorer_controller.rb new file mode 100644 index 0000000..3f0a300 --- /dev/null +++ b/app/controllers/knowledge_explorer/knowledge_explorer_controller.rb @@ -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 diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 859ef99..57b67ee 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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" diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..cb6d925 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,5 @@ +require_dependency "knowledge_explorer_constraint" + +KnowledgeExplorer::Engine.routes.draw do + get "/" => "knowledge_explorer#index", constraints: KnowledgeExplorerConstraint.new +end diff --git a/config/settings.yml b/config/settings.yml index 430b38c..58b3534 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -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 diff --git a/lib/knowledge_explorer/engine.rb b/lib/knowledge_explorer/engine.rb new file mode 100644 index 0000000..f81ec2d --- /dev/null +++ b/lib/knowledge_explorer/engine.rb @@ -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 diff --git a/lib/knowledge_explorer_constraint.rb b/lib/knowledge_explorer_constraint.rb new file mode 100644 index 0000000..572d332 --- /dev/null +++ b/lib/knowledge_explorer_constraint.rb @@ -0,0 +1,5 @@ +class KnowledgeExplorerConstraint + def matches?(request) + SiteSetting.knowledge_explorer_enabled + end +end diff --git a/plugin.rb b/plugin.rb index 7be16b2..f290269 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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__)