mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 11:11:13 +08:00
DEV: adds first_messages/last_messages to thread SDK (#26861)
This commit introduces several enhancements to the ChatSDK module, aiming to improve the functionality and usability of chat thread interactions. Here's what has been changed and added: 1. **New Method: `first_messages`:** - Added a method to retrieve the first set of messages from a specified chat thread. - This method is particularly useful for fetching initial messages when entering a chat thread. - Parameters include `thread_id`, `guardian`, and an optional `page_size` which defaults to 10. - Usage example added to demonstrate fetching the first 15 messages from a thread. 2. **New Method: `last_messages`:** - Added a method to retrieve the last set of messages from a specified chat thread. - This method supports reverse pagination, where the user may want to see the most recent messages first. - Similar to `first_messages`, it accepts `thread_id`, `guardian`, and an optional `page_size` parameter, defaulting to 10. - Usage example provided to illustrate fetching the last 20 messages from a thread.
This commit is contained in:
@ -5,12 +5,7 @@ describe ChatSDK::Thread do
|
||||
fab!(:thread_1) { Fabricate(:chat_thread) }
|
||||
|
||||
let(:params) do
|
||||
{
|
||||
title: "New Title",
|
||||
channel_id: thread_1.channel_id,
|
||||
thread_id: thread_1.id,
|
||||
guardian: Discourse.system_user.guardian,
|
||||
}
|
||||
{ title: "New Title", thread_id: thread_1.id, guardian: Discourse.system_user.guardian }
|
||||
end
|
||||
|
||||
it "changes the title" do
|
||||
@ -23,7 +18,9 @@ describe ChatSDK::Thread do
|
||||
it "fails" do
|
||||
params.delete(:thread_id)
|
||||
|
||||
expect { described_class.update_title(**params) }.to raise_error("Thread can't be blank")
|
||||
expect { described_class.update_title(**params) }.to raise_error(
|
||||
"missing keyword: :thread_id",
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@ -67,6 +64,67 @@ describe ChatSDK::Thread do
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe ".first_messages" do
|
||||
fab!(:thread_1) { Fabricate(:chat_thread) }
|
||||
fab!(:messages) do
|
||||
Fabricate.times(5, :chat_message, thread: thread_1, chat_channel: thread_1.channel)
|
||||
end
|
||||
|
||||
let(:params) { { thread_id: thread_1.id, guardian: Discourse.system_user.guardian } }
|
||||
|
||||
it "returns messages" do
|
||||
expect(described_class.first_messages(**params)).to eq([thread_1.original_message, *messages])
|
||||
end
|
||||
end
|
||||
|
||||
describe ".last_messages" do
|
||||
fab!(:thread_1) { Fabricate(:chat_thread) }
|
||||
fab!(:messages) do
|
||||
Fabricate.times(
|
||||
5,
|
||||
:chat_message,
|
||||
thread: thread_1,
|
||||
chat_channel: thread_1.channel,
|
||||
use_service: true,
|
||||
)
|
||||
end
|
||||
|
||||
let(:params) do
|
||||
{ thread_id: thread_1.id, guardian: Discourse.system_user.guardian, page_size: 5 }
|
||||
end
|
||||
|
||||
it "returns messages" do
|
||||
expect(described_class.last_messages(**params)).to eq([*messages])
|
||||
end
|
||||
end
|
||||
|
||||
describe ".messages" do
|
||||
fab!(:thread_1) { Fabricate(:chat_thread) }
|
||||
fab!(:messages) do
|
||||
Fabricate.times(
|
||||
5,
|
||||
:chat_message,
|
||||
thread: thread_1,
|
||||
chat_channel: thread_1.channel,
|
||||
use_service: true,
|
||||
)
|
||||
end
|
||||
|
||||
let(:params) { { thread_id: thread_1.id, guardian: Discourse.system_user.guardian } }
|
||||
|
||||
it "returns messages" do
|
||||
expect(described_class.messages(**params)).to eq([thread_1.original_message, *messages])
|
||||
end
|
||||
|
||||
describe "page_size:" do
|
||||
before { params[:page_size] = 2 }
|
||||
|
||||
it "limits returned messages" do
|
||||
expect(described_class.messages(**params)).to eq([thread_1.original_message, messages[0]])
|
||||
end
|
||||
end
|
||||
|
||||
context "when target_message doesn’t exist" do
|
||||
it "fails" do
|
||||
|
Reference in New Issue
Block a user