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:
Gary Pendergast 2025-02-25 14:40:51 +11:00 committed by GitHub
parent 954009cd38
commit 61a727a0d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View File

@ -88,6 +88,16 @@ module DiscourseChatIntegration
message = generate_discord_message(post)
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)
raise ::DiscourseChatIntegration::ProviderError.new(
info: {

View File

@ -41,6 +41,7 @@ RSpec.describe DiscourseChatIntegration::Provider::DiscordProvider do
stub1 =
stub_request(:post, "https://discord.com/api/webhooks/1234/abcd?wait=true").to_return(
status: 400,
body: '{"message": "This is an error!", "code": 400}',
)
expect(stub1).to have_been_requested.times(0)
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
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
describe ".get_channel_by_name" do