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..4e28fd8 --- /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..ba0e5a2 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2,7 +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' + 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 b6431f6..358518f 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -16,4 +16,10 @@ 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 + slack_category_name_in_title: + client: true + default: false diff --git a/plugin.rb b/plugin.rb index 119cb6e..1f3fc75 100644 --- a/plugin.rb +++ b/plugin.rb @@ -25,17 +25,42 @@ 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 + + category = topic.category + # We might have a category specific channel to post to + if SiteSetting.allow_category_slack_channel + + # 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 + + 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 = { :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 => (show_category_name ? "[" + category.name + "] " : "") + topic.title, :title_link => post_url, :text => post.excerpt(200, text_entities: true, strip_links: true) }