diff --git a/lib/stream-chat/client.rb b/lib/stream-chat/client.rb index 87f47aa..8d4944c 100644 --- a/lib/stream-chat/client.rb +++ b/lib/stream-chat/client.rb @@ -929,6 +929,16 @@ def list_imports(options) get('imports', params: options) end + sig { params(filter: StringKeyHash, sort: T.nilable(T::Hash[String, Integer]), options: T.untyped).returns(StreamChat::StreamResponse) } + def query_threads(filter, sort: nil, **options) + params = {}.merge(options).merge({ + filter: filter, + sort: StreamChat.get_sort_fields(sort) + }) + + post('threads', data: params) + end + private sig { returns(T::Hash[String, String]) } diff --git a/spec/client_spec.rb b/spec/client_spec.rb index dd4ede4..b0e5dc9 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -171,12 +171,12 @@ def loop_times(times) response = @client.update_user_partial({ id: user_id, set: { - team: 'blue', + teams: ['blue'], teams_role: { 'blue' => 'admin' } } }) - expect(response['users'][user_id]['team']).to eq('blue') + expect(response['users'][user_id]['teams']).to eq(['blue']) expect(response['users'][user_id]['teams_role']['blue']).to eq('admin') end @@ -960,4 +960,64 @@ def loop_times(times) end end end + + describe '#query_threads' do + before(:all) do + # Create a dedicated random user for this block + @thread_test_user = { id: SecureRandom.uuid } + @client.upsert_users([@thread_test_user]) + + # Create a channel and send a message to create a thread + @thread_channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) + @thread_channel.create(@thread_test_user[:id]) + + # Send a message to create a thread + @thread_message = @thread_channel.send_message({ text: 'Thread parent message' }, @thread_test_user[:id]) + + # Send a reply to create a thread + @thread_channel.send_message({ text: 'Thread reply', parent_id: @thread_message['message']['id'] }, @thread_test_user[:id]) + end + + after(:all) do + @thread_channel.delete + @client.delete_user(@thread_test_user[:id]) + end + + it 'queries threads with filter' do + filter = { + 'created_by_user_id' => { '$eq' => @thread_test_user[:id] } + } + + response = @client.query_threads(filter, user_id: @thread_test_user[:id]) + + expect(response).to include 'threads' + expect(response['threads'].length).to be >= 1 + end + + it 'queries threads with sort' do + sort = { + 'created_at' => -1 + } + + response = @client.query_threads({}, sort: sort, user_id: @thread_test_user[:id]) + + expect(response).to include 'threads' + expect(response['threads'].length).to be >= 1 + end + + it 'queries threads with both filter and sort' do + filter = { + 'created_by_user_id' => { '$eq' => @thread_test_user[:id] } + } + + sort = { + 'created_at' => -1 + } + + response = @client.query_threads(filter, sort: sort, user_id: @thread_test_user[:id]) + + expect(response).to include 'threads' + expect(response['threads'].length).to be >= 1 + end + end end