mirror of
https://github.com/discourse/discourse-chat-integration.git
synced 2025-03-06 17:59:29 +00:00
FIX: Sharing to Discord forum channels requires a thread_name (#251)
Discourse is able to share topics and replies to normal Discord channels, but it doesn't work with forum channels, which require an additional `thread_name` parameter. This change fixes the issue by checking for the specific error the Discord returns when trying to share to a forum channel, adding the `thread_name`, then trying again. This is intentionally a minimal fix, additional work would be required to make the sharing more closely fit the style of Discord forum channels.
This commit is contained in:
parent
954009cd38
commit
61a727a0d9
@ -88,6 +88,16 @@ module DiscourseChatIntegration
|
|||||||
message = generate_discord_message(post)
|
message = generate_discord_message(post)
|
||||||
response = send_message(webhook_url, message)
|
response = send_message(webhook_url, message)
|
||||||
|
|
||||||
|
# If the message fails to send, it might be because it's a forum channel.
|
||||||
|
if !response.kind_of?(Net::HTTPSuccess)
|
||||||
|
error = JSON.parse(response.body)
|
||||||
|
# Error code 220001 occurs when trying to post to a forum channel without a thread_name.
|
||||||
|
if error["code"] == 220_001
|
||||||
|
message[:thread_name] = message[:embeds][0][:title]
|
||||||
|
response = send_message(webhook_url, message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if !response.kind_of?(Net::HTTPSuccess)
|
if !response.kind_of?(Net::HTTPSuccess)
|
||||||
raise ::DiscourseChatIntegration::ProviderError.new(
|
raise ::DiscourseChatIntegration::ProviderError.new(
|
||||||
info: {
|
info: {
|
||||||
|
@ -41,6 +41,7 @@ RSpec.describe DiscourseChatIntegration::Provider::DiscordProvider do
|
|||||||
stub1 =
|
stub1 =
|
||||||
stub_request(:post, "https://discord.com/api/webhooks/1234/abcd?wait=true").to_return(
|
stub_request(:post, "https://discord.com/api/webhooks/1234/abcd?wait=true").to_return(
|
||||||
status: 400,
|
status: 400,
|
||||||
|
body: '{"message": "This is an error!", "code": 400}',
|
||||||
)
|
)
|
||||||
expect(stub1).to have_been_requested.times(0)
|
expect(stub1).to have_been_requested.times(0)
|
||||||
expect { described_class.trigger_notification(post, chan1, nil) }.to raise_exception(
|
expect { described_class.trigger_notification(post, chan1, nil) }.to raise_exception(
|
||||||
@ -48,6 +49,20 @@ RSpec.describe DiscourseChatIntegration::Provider::DiscordProvider do
|
|||||||
)
|
)
|
||||||
expect(stub1).to have_been_requested.once
|
expect(stub1).to have_been_requested.once
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "handles posting to forum channels" do
|
||||||
|
stub1 =
|
||||||
|
stub_request(:post, "https://discord.com/api/webhooks/1234/abcd?wait=true")
|
||||||
|
.with { |request| !JSON.parse(request.body)["thread_name"].present? }
|
||||||
|
.to_return(status: 400, body: '{"message": "This is an error!", "code": 220001}')
|
||||||
|
stub2 =
|
||||||
|
stub_request(:post, "https://discord.com/api/webhooks/1234/abcd?wait=true")
|
||||||
|
.with { |request| JSON.parse(request.body)["thread_name"].present? }
|
||||||
|
.to_return(status: 200)
|
||||||
|
described_class.trigger_notification(post, chan1, nil)
|
||||||
|
expect(stub1).to have_been_requested.once
|
||||||
|
expect(stub2).to have_been_requested.once
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".get_channel_by_name" do
|
describe ".get_channel_by_name" do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user