From 8c32c132a414ef7ded3ff38713e3cb65cf5145c4 Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 23 Feb 2016 17:28:08 +0100 Subject: [PATCH 1/5] feat: Category specific channels This adds the ability to turn on category specific channels, which can be specified in the category settings when the setting is enabled. If the category does not have a specific channel set, it falls back to the parent category if there is one, otherwise it just uses the global channel specified in the settings. --- .../slack-settings.hbs | 10 ++++++++ .../extend-category-for-slack.js.es6 | 24 ++++++++++++++++++ config/locales/client.en.yml | 3 +++ config/locales/server.en.yml | 1 + config/settings.yml | 5 +++- plugin.rb | 25 +++++++++++++++++-- 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 assets/javascripts/discourse/connectors/category-custom-settings/slack-settings.hbs create mode 100644 assets/javascripts/discourse/pre-initializers/extend-category-for-slack.js.es6 diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/slack-settings.hbs b/assets/javascripts/discourse/connectors/category-custom-settings/slack-settings.hbs new file mode 100644 index 0000000..f382055 --- /dev/null +++ b/assets/javascripts/discourse/connectors/category-custom-settings/slack-settings.hbs @@ -0,0 +1,10 @@ +{{#if siteSettings.allow_category_slack_channel}} +
+
+ +
+
+{{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/pre-initializers/extend-category-for-slack.js.es6 b/assets/javascripts/discourse/pre-initializers/extend-category-for-slack.js.es6 new file mode 100644 index 0000000..500539e --- /dev/null +++ b/assets/javascripts/discourse/pre-initializers/extend-category-for-slack.js.es6 @@ -0,0 +1,24 @@ +import property from 'ember-addons/ember-computed-decorators'; +import Category from 'discourse/models/category'; + +export default { + name: 'extend-category-for-slack', + before: 'inject-discourse-objects', + initialize() { + + Category.reopen({ + + @property('custom_fields.slack_channel') + slack_channel: { + get(channelField) { + return channelField; + }, + set(value) { + this.set("custom_fields.slack_channel", value); + return value; + } + } + + }); + } +}; \ No newline at end of file diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 5245e97..0f0a700 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -4,3 +4,6 @@ en: site_settings: categories: slack: 'Slack' + js: + slack: + channel: "Slack channel" \ No newline at end of file diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 69c41f6..b793a48 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -6,3 +6,4 @@ en: slack_emoji: 'Slack emoji to use.' slack_posts: 'Post all new posts to Slack (not just new topics).' slack_full_names: 'Shows user full names instead of usernames (shows username if user did not set one)' + allow_category_slack_channel: 'Allows each category to specify a Slack channel to post to, falls back to parent categories or the global channel' \ No newline at end of file diff --git a/config/settings.yml b/config/settings.yml index b6431f6..ad042cf 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -16,4 +16,7 @@ slack: default: false slack_full_names: client: true - default: true \ No newline at end of file + default: true + allow_category_slack_channel: + client: true + default: false \ No newline at end of file diff --git a/plugin.rb b/plugin.rb index 119cb6e..10c5d03 100644 --- a/plugin.rb +++ b/plugin.rb @@ -25,19 +25,40 @@ display_name = (SiteSetting.slack_full_names && user.try(:name) && user.name.length > 0) ? user.name : user.username + # Default to the global site channel + channel = SiteSetting.slack_channel + + # We might have a category specific channel to post to + if SiteSetting.allow_category_slack_channel + category = topic.category + + # We walk up the categories to the root unless we find a + # channel setting on the category + while category != nil do + cat_channel = category.custom_fields["slack_channel"] + + if cat_channel != nil + channel = cat_channel + break + end + + category = category.parent_category + end + end + request = Net::HTTP::Post.new(uri.path) request.add_field('Content-Type', 'application/json') request.body = { :username => SiteSetting.title, :icon_emoji => SiteSetting.slack_emoji, - :channel => SiteSetting.slack_channel, + :channel => channel, :attachments => [ { :fallback => "New " + (post.try(:is_first_post?) ? "topic" : "post in #{topic.title}") + " by #{display_name} - #{post_url}", :pretext => "New " + (post.try(:is_first_post?) ? "topic" : "post") + " by #{display_name}:", :title => topic.title, :title_link => post_url, - :text => post.excerpt(200, text_entities: true, strip_links: true) + :text => post.excerpt(200, text_entities: false, strip_links: true) } ] }.to_json From cf0588262cfacb5f4b117eeac82c47e569ca234c Mon Sep 17 00:00:00 2001 From: Jake Date: Tue, 23 Feb 2016 17:37:20 +0100 Subject: [PATCH 2/5] fix: Remove copy-pasted class --- .../connectors/category-custom-settings/slack-settings.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/slack-settings.hbs b/assets/javascripts/discourse/connectors/category-custom-settings/slack-settings.hbs index f382055..4e28fd8 100644 --- a/assets/javascripts/discourse/connectors/category-custom-settings/slack-settings.hbs +++ b/assets/javascripts/discourse/connectors/category-custom-settings/slack-settings.hbs @@ -3,7 +3,7 @@
From b39ffdaa18a67e22d8a3e1a363118b0ee673cc0e Mon Sep 17 00:00:00 2001 From: Richard Xia Date: Tue, 5 Apr 2016 00:07:00 -0700 Subject: [PATCH 3/5] Do not post message if category has no Slack channel. --- plugin.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin.rb b/plugin.rb index 10c5d03..458d9c2 100644 --- a/plugin.rb +++ b/plugin.rb @@ -46,6 +46,8 @@ end end + next if channel.blank? + request = Net::HTTP::Post.new(uri.path) request.add_field('Content-Type', 'application/json') request.body = { From fd45f401905da269de4a6f52202c6b41e0278c5c Mon Sep 17 00:00:00 2001 From: Dave Eargle Date: Thu, 25 Aug 2016 16:39:28 -0400 Subject: [PATCH 4/5] conditionally adding the category slug to the title --- config/locales/server.en.yml | 5 +++-- config/settings.yml | 5 ++++- plugin.rb | 6 ++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index b793a48..ba0e5a2 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2,8 +2,9 @@ en: site_settings: slack_enabled: 'Check this to enable the Slack integration.' slack_url: 'Slack URL including access token.' - slack_channel: 'Slack channel to post this message to. (i.e. #general, @username)' + slack_channel: 'Slack channel to post this message to. (i.e. #general, @username). Leaving blank for no fallback is helpful if using "allow category slack channel" setting.' slack_emoji: 'Slack emoji to use.' slack_posts: 'Post all new posts to Slack (not just new topics).' slack_full_names: 'Shows user full names instead of usernames (shows username if user did not set one)' - allow_category_slack_channel: 'Allows each category to specify a Slack channel to post to, falls back to parent categories or the global channel' \ No newline at end of file + allow_category_slack_channel: 'Allows each category to specify a Slack channel to post to, falls back to parent categories or the global channel' + slack_category_name_in_title: 'Display the category name in the title of the post sent to Slack (e.g., "[category name] topic title")' diff --git a/config/settings.yml b/config/settings.yml index ad042cf..358518f 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -19,4 +19,7 @@ slack: default: true allow_category_slack_channel: client: true - default: false \ No newline at end of file + default: false + slack_category_name_in_title: + client: true + default: false diff --git a/plugin.rb b/plugin.rb index 458d9c2..02d94e7 100644 --- a/plugin.rb +++ b/plugin.rb @@ -28,9 +28,9 @@ # Default to the global site channel channel = SiteSetting.slack_channel + category = topic.category # We might have a category specific channel to post to if SiteSetting.allow_category_slack_channel - category = topic.category # We walk up the categories to the root unless we find a # channel setting on the category @@ -48,6 +48,8 @@ next if channel.blank? + show_category_name = SiteSetting.slack_category_name_in_title + request = Net::HTTP::Post.new(uri.path) request.add_field('Content-Type', 'application/json') request.body = { @@ -58,7 +60,7 @@ { :fallback => "New " + (post.try(:is_first_post?) ? "topic" : "post in #{topic.title}") + " by #{display_name} - #{post_url}", :pretext => "New " + (post.try(:is_first_post?) ? "topic" : "post") + " by #{display_name}:", - :title => topic.title, + :title => (show_category_name ? "[" + category.name + "] " : "") + topic.title, :title_link => post_url, :text => post.excerpt(200, text_entities: false, strip_links: true) } From def8b7f5132c167d345f44d4d36260554c36c06f Mon Sep 17 00:00:00 2001 From: Jake Shadle Date: Fri, 26 Aug 2016 13:52:58 +0200 Subject: [PATCH 5/5] fix: Make text_entities: true --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 02d94e7..1f3fc75 100644 --- a/plugin.rb +++ b/plugin.rb @@ -62,7 +62,7 @@ :pretext => "New " + (post.try(:is_first_post?) ? "topic" : "post") + " by #{display_name}:", :title => (show_category_name ? "[" + category.name + "] " : "") + topic.title, :title_link => post_url, - :text => post.excerpt(200, text_entities: false, strip_links: true) + :text => post.excerpt(200, text_entities: true, strip_links: true) } ] }.to_json