mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 17:33:27 +08:00
DEV: supports blocks in chat message sdk (#29839)
Blocks have been added in 582de0ffe3
but were not yet supported in the SDK.
This commit is contained in:
@ -116,6 +116,7 @@ module ChatSDK
|
|||||||
enforce_membership: false,
|
enforce_membership: false,
|
||||||
force_thread: false,
|
force_thread: false,
|
||||||
strip_whitespaces: true,
|
strip_whitespaces: true,
|
||||||
|
blocks: nil,
|
||||||
**params,
|
**params,
|
||||||
&block
|
&block
|
||||||
)
|
)
|
||||||
@ -127,6 +128,7 @@ module ChatSDK
|
|||||||
in_reply_to_id:,
|
in_reply_to_id:,
|
||||||
thread_id:,
|
thread_id:,
|
||||||
upload_ids:,
|
upload_ids:,
|
||||||
|
blocks:,
|
||||||
**params,
|
**params,
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
@ -145,6 +147,7 @@ module ChatSDK
|
|||||||
on_failed_policy(:ensure_valid_thread_for_channel) do
|
on_failed_policy(:ensure_valid_thread_for_channel) do
|
||||||
raise "Couldn't find thread with id: `#{thread_id}`"
|
raise "Couldn't find thread with id: `#{thread_id}`"
|
||||||
end
|
end
|
||||||
|
on_failed_policy(:accept_blocks) { raise "Only bots can create messages with blocks" }
|
||||||
on_failed_policy(:allowed_to_join_channel) do
|
on_failed_policy(:allowed_to_join_channel) do
|
||||||
raise "User with id: `#{guardian.user.id}` can't join this channel"
|
raise "User with id: `#{guardian.user.id}` can't join this channel"
|
||||||
end
|
end
|
||||||
|
@ -6,7 +6,13 @@ describe ChatSDK::Message do
|
|||||||
|
|
||||||
let(:guardian) { Discourse.system_user.guardian }
|
let(:guardian) { Discourse.system_user.guardian }
|
||||||
let(:params) do
|
let(:params) do
|
||||||
{ enforce_membership: false, raw: "something", channel_id: channel_1.id, guardian: guardian }
|
{
|
||||||
|
blocks: nil,
|
||||||
|
enforce_membership: false,
|
||||||
|
raw: "something",
|
||||||
|
channel_id: channel_1.id,
|
||||||
|
guardian: guardian,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates the message" do
|
it "creates the message" do
|
||||||
@ -15,6 +21,55 @@ describe ChatSDK::Message do
|
|||||||
expect(message.message).to eq("something")
|
expect(message.message).to eq("something")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when providing blocks" do
|
||||||
|
before do
|
||||||
|
params[:blocks] = [
|
||||||
|
{
|
||||||
|
type: "actions",
|
||||||
|
elements: [{ type: "button", value: "foo", text: { type: "plain_text", text: "Foo" } }],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when user is a bot" do
|
||||||
|
it "saves the blocks" do
|
||||||
|
message = described_class.create(**params)
|
||||||
|
|
||||||
|
expect(message.blocks[0]).to include(
|
||||||
|
"type" => "actions",
|
||||||
|
"schema_version" => 1,
|
||||||
|
"block_id" => an_instance_of(String),
|
||||||
|
"elements" => [
|
||||||
|
{
|
||||||
|
"schema_version" => 1,
|
||||||
|
"type" => "button",
|
||||||
|
"value" => "foo",
|
||||||
|
"action_id" => an_instance_of(String),
|
||||||
|
"text" => {
|
||||||
|
"type" => "plain_text",
|
||||||
|
"text" => "Foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when user is not a bot" do
|
||||||
|
fab!(:user)
|
||||||
|
|
||||||
|
let(:guardian) { user.guardian }
|
||||||
|
|
||||||
|
before { channel_1.add(user) }
|
||||||
|
|
||||||
|
it "fails" do
|
||||||
|
expect { described_class.create(**params) }.to raise_error(
|
||||||
|
"Only bots can create messages with blocks",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "sets created_by_sdk to true" do
|
it "sets created_by_sdk to true" do
|
||||||
message = described_class.create(**params)
|
message = described_class.create(**params)
|
||||||
expect(message).to have_attributes(created_by_sdk: true)
|
expect(message).to have_attributes(created_by_sdk: true)
|
||||||
|
Reference in New Issue
Block a user