diff --git a/lib/discourse_chat/provider/slack/slack_provider.rb b/lib/discourse_chat/provider/slack/slack_provider.rb
index 921687e..d4aca42 100644
--- a/lib/discourse_chat/provider/slack/slack_provider.rb
+++ b/lib/discourse_chat/provider/slack/slack_provider.rb
@@ -1,3 +1,5 @@
+require_relative "slack_message_formatter.rb"
+
module DiscourseChat::Provider::SlackProvider
PROVIDER_NAME = "slack".freeze
@@ -31,7 +33,7 @@ module DiscourseChat::Provider::SlackProvider
message = {
channel: channel,
- username: SiteSetting.title,
+ username: SiteSetting.title || "Discourse",
icon_url: icon_url,
attachments: []
}
@@ -57,7 +59,7 @@ module DiscourseChat::Provider::SlackProvider
message
end
- def self.send_via_api(message)
+ def self.send_via_api(post, channel, message)
http = Net::HTTP.new("slack.com", 443)
http.use_ssl = true
@@ -90,14 +92,16 @@ module DiscourseChat::Provider::SlackProvider
response = http.request(Net::HTTP::Post.new(uri))
DiscourseChat.pstore_set("slack_topic_#{post.topic.id}_#{channel}", JSON.parse(response.body) )
+ response
end
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.slack_outbound_webhook_url), 'Content-Type' =>'application/json')
+ req = Net::HTTP::Post.new(URI(SiteSetting.chat_slack_outbound_webhook_url), 'Content-Type' =>'application/json')
req.body = message.to_json
response = http.request(req)
+ response
end
def self.trigger_notification(post, channel)
@@ -106,7 +110,7 @@ module DiscourseChat::Provider::SlackProvider
if SiteSetting.chat_slack_access_token.empty?
self.send_via_webhook(message)
else
- self.send_via_api(message)
+ self.send_via_api(post, channel, message)
end
end
diff --git a/spec/lib/discourse_chat/provider/slack/slack_message_formatter_spec.rb b/spec/lib/discourse_chat/provider/slack/slack_message_formatter_spec.rb
new file mode 100644
index 0000000..44d8ffe
--- /dev/null
+++ b/spec/lib/discourse_chat/provider/slack/slack_message_formatter_spec.rb
@@ -0,0 +1,29 @@
+require 'rails_helper'
+
+RSpec.describe DiscourseChat::Provider::SlackProvider::SlackMessageFormatter do
+ describe '.format' do
+ context 'links' do
+ it 'should return the right message' do
+ expect(described_class.format("test"))
+ .to eq('')
+ end
+
+ describe 'when text contains a link with an incomplete URL' do
+ it 'should return the right message' do
+ expect(described_class.format("test "))
+ .to eq("test ")
+
+ SiteSetting.force_https = true
+
+ expect(described_class.format("test "))
+ .to eq("test ")
+ end
+ end
+
+ it "should not raise an error with unparseable urls" do
+ expect(described_class.format("test")).to eq("")
+ end
+
+ end
+ end
+end
diff --git a/spec/lib/discourse_chat/provider/slack/slack_provider_spec.rb b/spec/lib/discourse_chat/provider/slack/slack_provider_spec.rb
new file mode 100644
index 0000000..1e8ba74
--- /dev/null
+++ b/spec/lib/discourse_chat/provider/slack/slack_provider_spec.rb
@@ -0,0 +1,103 @@
+require 'rails_helper'
+
+RSpec.describe DiscourseChat::Provider::SlackProvider do
+ let(:post) { Fabricate(:post) }
+
+ describe '.excerpt' do
+ describe 'when post contains emoijs' do
+ before do
+ post.update!(raw: ':slight_smile: This is a test')
+ end
+
+ it 'should return the right excerpt' do
+ expect(described_class.excerpt(post)).to eq('🙂 This is a test')
+ end
+ end
+
+ describe 'when post contains onebox' do
+ it 'should return the right excerpt' do
+ post.update!(cooked: <<~COOKED
+
+ COOKED
+ )
+
+ expect(described_class.excerpt(post))
+ .to eq('')
+ end
+ end
+ end
+
+ describe '.trigger_notifications' do
+ before do
+ SiteSetting.chat_slack_outbound_webhook_url = "https://hooks.slack.com/services/abcde"
+ SiteSetting.chat_slack_enabled = true
+ end
+
+ before do
+ @stub1 = stub_request(:post, SiteSetting.chat_slack_outbound_webhook_url).to_return(body: "success")
+ end
+
+
+
+ it 'sends a webhook request' do
+ expect(@stub1).to have_been_requested.times(0)
+ described_class.trigger_notification(post, '#general')
+ expect(@stub1).to have_been_requested.once
+ end
+
+ describe 'with api token' do
+ before do
+ SiteSetting.chat_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
+
+ it 'sends an api request' do
+ expect(@stub2).to have_been_requested.times(0)
+
+ described_class.trigger_notification(post, '#general')
+ expect(@stub1).to have_been_requested.times(0)
+ expect(@stub2).to have_been_requested.once
+ end
+
+ it 'correctly merges replies' do
+ second_post = Fabricate(:post, topic: post.topic, post_number:2)
+ expect(@stub2).to have_been_requested.times(0)
+ expect(@stub3).to have_been_requested.times(0)
+
+ described_class.trigger_notification(post, '#general')
+ described_class.trigger_notification(second_post, '#general')
+ expect(@stub1).to have_been_requested.times(0)
+ expect(@stub2).to have_been_requested.once # Initial creation of message
+ expect(@stub3).to have_been_requested.once # Requests to update the existing message
+ end
+
+ end
+
+ end
+
+end