From 4643ad255c4e4a5e7c13eaf41d5e330e64b345c9 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 3 Jul 2017 11:08:14 +0100 Subject: [PATCH] Rename plugin to discourse-chat-integration --- README.md | 2 +- config/settings.yml | 18 +++++++++--------- lib/discourse_chat/manager.rb | 2 +- .../provider/slack/slack_provider.rb | 14 +++++++------- plugin.rb | 14 +++++++------- spec/jobs/notify_chats_spec.rb | 6 +++--- spec/lib/discourse_chat/manager_spec.rb | 2 +- .../provider/slack/slack_provider_spec.rb | 8 ++++---- 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index bc4c45b..6f10ff9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# discourse-chat +# discourse-chat-integration This plugin is a work in progress, it is not yet ready for use \ No newline at end of file diff --git a/config/settings.yml b/config/settings.yml index e86d59a..3059834 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -1,27 +1,27 @@ plugins: - chat_enabled: + chat_integration_enabled: default: false - chat_discourse_username: + chat_integration_discourse_username: default: system - chat_delay_seconds: + chat_integration_delay_seconds: default: 20 ####################################### ########## SLACK SETTINGS ############# ####################################### - chat_slack_enabled: + chat_integration_slack_enabled: default: false - chat_slack_outbound_webhook_url: + chat_integration_slack_outbound_webhook_url: default: '' - chat_slack_discourse_excerpt_length: + chat_integration_slack_discourse_excerpt_length: default: 400 - chat_slack_icon_url: + chat_integration_slack_icon_url: default: '' - chat_slack_access_token: + chat_integration_slack_access_token: default: '' ####################################### ######### TELEGRAM SETTINGS ########### ####################################### - chat_telegram_enabled: + chat_integration_telegram_enabled: default: false diff --git a/lib/discourse_chat/manager.rb b/lib/discourse_chat/manager.rb index 244b14e..a1c1a85 100644 --- a/lib/discourse_chat/manager.rb +++ b/lib/discourse_chat/manager.rb @@ -2,7 +2,7 @@ module DiscourseChat module Manager def self.guardian - Guardian.new(User.find_by(username: SiteSetting.chat_discourse_username)) + Guardian.new(User.find_by(username: SiteSetting.chat_integration_discourse_username)) end def self.trigger_notifications(post_id) diff --git a/lib/discourse_chat/provider/slack/slack_provider.rb b/lib/discourse_chat/provider/slack/slack_provider.rb index d4aca42..329640b 100644 --- a/lib/discourse_chat/provider/slack/slack_provider.rb +++ b/lib/discourse_chat/provider/slack/slack_provider.rb @@ -3,7 +3,7 @@ require_relative "slack_message_formatter.rb" module DiscourseChat::Provider::SlackProvider PROVIDER_NAME = "slack".freeze - def self.excerpt(post, max_length = SiteSetting.chat_slack_discourse_excerpt_length) + def self.excerpt(post, max_length = SiteSetting.chat_integration_slack_discourse_excerpt_length) doc = Nokogiri::HTML.fragment(post.excerpt(max_length, remap_emoji: true, keep_onebox_source: true @@ -25,8 +25,8 @@ module DiscourseChat::Provider::SlackProvider category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]": "[#{topic.category.name}]" icon_url = - if !SiteSetting.chat_slack_icon_url.blank? - "#{Discourse.base_url}#{SiteSetting.chat_slack_icon_url}" + if !SiteSetting.chat_integration_slack_icon_url.blank? + "#{Discourse.base_url}#{SiteSetting.chat_integration_slack_icon_url}" elsif !SiteSetting.logo_small_url.blank? "#{Discourse.base_url}#{SiteSetting.logo_small_url}" end @@ -72,7 +72,7 @@ module DiscourseChat::Provider::SlackProvider attachments.concat message[:attachments] uri = URI("https://slack.com/api/chat.update" + - "?token=#{SiteSetting.chat_slack_access_token}" + + "?token=#{SiteSetting.chat_integration_slack_access_token}" + "&username=#{CGI::escape(record[:message][:username])}" + "&text=#{CGI::escape(record[:message][:text])}" + "&channel=#{record[:channel]}" + @@ -81,7 +81,7 @@ module DiscourseChat::Provider::SlackProvider ) else uri = URI("https://slack.com/api/chat.postMessage" + - "?token=#{SiteSetting.chat_slack_access_token}" + + "?token=#{SiteSetting.chat_integration_slack_access_token}" + "&username=#{CGI::escape(message[:username])}" + "&icon_url=#{CGI::escape(message[:icon_url])}" + "&channel=#{ message[:channel].gsub('#', '') }" + @@ -98,7 +98,7 @@ module DiscourseChat::Provider::SlackProvider def self.send_via_webhook(message) http = Net::HTTP.new("hooks.slack.com", 443) http.use_ssl = true - req = Net::HTTP::Post.new(URI(SiteSetting.chat_slack_outbound_webhook_url), 'Content-Type' =>'application/json') + req = Net::HTTP::Post.new(URI(SiteSetting.chat_integration_slack_outbound_webhook_url), 'Content-Type' =>'application/json') req.body = message.to_json response = http.request(req) response @@ -107,7 +107,7 @@ module DiscourseChat::Provider::SlackProvider def self.trigger_notification(post, channel) message = slack_message(post, channel) - if SiteSetting.chat_slack_access_token.empty? + if SiteSetting.chat_integration_slack_access_token.empty? self.send_via_webhook(message) else self.send_via_api(post, channel, message) diff --git a/plugin.rb b/plugin.rb index a26197a..6d6a504 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,15 +1,15 @@ -# name: discourse-chat +# name: discourse-chat-integration # about: This plugin integrates discourse with a number of chat providers # version: 0.1 -# url: https://github.com/discourse/discourse-chat +# url: https://github.com/discourse/discourse-chat-integration -enabled_site_setting :chat_enabled +enabled_site_setting :chat_integration_enabled after_initialize do module ::DiscourseChat - PLUGIN_NAME = "discourse-chat".freeze + PLUGIN_NAME = "discourse-chat-integration".freeze class Engine < ::Rails::Engine engine_name DiscourseChat::PLUGIN_NAME @@ -40,7 +40,7 @@ after_initialize do module ::Jobs class NotifyChats < Jobs::Base def execute(args) - return if not SiteSetting.chat_enabled? # Plugin may have been disabled since job triggered + return if not SiteSetting.chat_integration_enabled? # Plugin may have been disabled since job triggered ::DiscourseChat::Manager.trigger_notifications(args[:post_id]) end @@ -48,9 +48,9 @@ after_initialize do end DiscourseEvent.on(:post_created) do |post| - if SiteSetting.chat_enabled? + if SiteSetting.chat_integration_enabled? # This will run for every post, even PMs. Don't worry, they're filtered out later. - Jobs.enqueue_in(SiteSetting.chat_delay_seconds.seconds, + Jobs.enqueue_in(SiteSetting.chat_integration_delay_seconds.seconds, :notify_chats, post_id: post.id ) diff --git a/spec/jobs/notify_chats_spec.rb b/spec/jobs/notify_chats_spec.rb index 0da3f96..27d917f 100644 --- a/spec/jobs/notify_chats_spec.rb +++ b/spec/jobs/notify_chats_spec.rb @@ -12,7 +12,7 @@ RSpec.describe PostCreator do describe 'when a post is created' do describe 'when plugin is enabled' do before do - SiteSetting.chat_enabled = true + SiteSetting.chat_integration_enabled = true end it 'should schedule a chat notification job' do @@ -25,7 +25,7 @@ RSpec.describe PostCreator do job = Jobs::NotifyChats.jobs.last expect(job['at']) - .to eq((Time.zone.now + SiteSetting.chat_delay_seconds.seconds).to_f) + .to eq((Time.zone.now + SiteSetting.chat_integration_delay_seconds.seconds).to_f) expect(job['args'].first['post_id']).to eq(post.id) end @@ -34,7 +34,7 @@ RSpec.describe PostCreator do describe 'when plugin is not enabled' do before do - SiteSetting.chat_enabled = false + SiteSetting.chat_integration_enabled = false end it 'should not schedule a job for chat notifications' do diff --git a/spec/lib/discourse_chat/manager_spec.rb b/spec/lib/discourse_chat/manager_spec.rb index 72e3093..9ad11d6 100644 --- a/spec/lib/discourse_chat/manager_spec.rb +++ b/spec/lib/discourse_chat/manager_spec.rb @@ -109,7 +109,7 @@ RSpec.describe DiscourseChat::Manager do group.add(user) # Set the chat_user to the newly created non-admin user - SiteSetting.chat_discourse_username = 'david' + SiteSetting.chat_integration_discourse_username = 'david' # Create a category category = Fabricate(:category, name: "Test category") diff --git a/spec/lib/discourse_chat/provider/slack/slack_provider_spec.rb b/spec/lib/discourse_chat/provider/slack/slack_provider_spec.rb index 1e8ba74..4827480 100644 --- a/spec/lib/discourse_chat/provider/slack/slack_provider_spec.rb +++ b/spec/lib/discourse_chat/provider/slack/slack_provider_spec.rb @@ -53,12 +53,12 @@ RSpec.describe DiscourseChat::Provider::SlackProvider do describe '.trigger_notifications' do before do - SiteSetting.chat_slack_outbound_webhook_url = "https://hooks.slack.com/services/abcde" - SiteSetting.chat_slack_enabled = true + SiteSetting.chat_integration_slack_outbound_webhook_url = "https://hooks.slack.com/services/abcde" + SiteSetting.chat_integration_slack_enabled = true end before do - @stub1 = stub_request(:post, SiteSetting.chat_slack_outbound_webhook_url).to_return(body: "success") + @stub1 = stub_request(:post, SiteSetting.chat_integration_slack_outbound_webhook_url).to_return(body: "success") end @@ -71,7 +71,7 @@ RSpec.describe DiscourseChat::Provider::SlackProvider do describe 'with api token' do before do - SiteSetting.chat_slack_access_token = "magic" + SiteSetting.chat_integration_slack_access_token = "magic" @stub2 = stub_request(:post, %r{https://slack.com/api/chat.postMessage}).to_return(body: "{\"success\":true, \"ts\": \"#{Time.now.to_i}.012345\", \"message\": {\"attachments\": [], \"username\":\"blah\", \"text\":\"blah2\"} }", headers: {'Content-Type' => 'application/json'}) @stub3 = stub_request(:post, %r{https://slack.com/api/chat.update}).to_return(body: '{"success":true, "ts": "some_message_id"}', headers: {'Content-Type' => 'application/json'}) end