Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

feat: Category specific channels #7

Merged
merged 6 commits into from
Sep 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#if siteSettings.allow_category_slack_channel}}
<section class='field'>
<div class="slack-channel">
<label>
{{i18n 'slack.channel'}}
{{text-field value=category.slack_channel}}
</label>
</div>
</section>
{{/if}}
Original file line number Diff line number Diff line change
@@ -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;
}
}

});
}
};
3 changes: 3 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ en:
site_settings:
categories:
slack: 'Slack'
js:
slack:
channel: "Slack channel"
4 changes: 3 additions & 1 deletion config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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")'
8 changes: 7 additions & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ slack:
default: false
slack_full_names:
client: true
default: true
default: true
allow_category_slack_channel:
client: true
default: false
slack_category_name_in_title:
client: true
default: false
29 changes: 27 additions & 2 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down