Skip to content

Commit cdad3b8

Browse files
authored
[CHA-794] Add Query Threads (#165)
* Add Query Threads * Fix lints * Fix team based test * Fix team based test * Fix lint * Fix team based test * Fix test * Make call query threads directly * Fix test * Simplify test * Refactor test * Refactor test
1 parent fa26e75 commit cdad3b8

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

lib/stream-chat/client.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,16 @@ def list_imports(options)
929929
get('imports', params: options)
930930
end
931931

932+
sig { params(filter: StringKeyHash, sort: T.nilable(T::Hash[String, Integer]), options: T.untyped).returns(StreamChat::StreamResponse) }
933+
def query_threads(filter, sort: nil, **options)
934+
params = {}.merge(options).merge({
935+
filter: filter,
936+
sort: StreamChat.get_sort_fields(sort)
937+
})
938+
939+
post('threads', data: params)
940+
end
941+
932942
private
933943

934944
sig { returns(T::Hash[String, String]) }

spec/client_spec.rb

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ def loop_times(times)
171171
response = @client.update_user_partial({
172172
id: user_id,
173173
set: {
174-
team: 'blue',
174+
teams: ['blue'],
175175
teams_role: { 'blue' => 'admin' }
176176
}
177177
})
178178

179-
expect(response['users'][user_id]['team']).to eq('blue')
179+
expect(response['users'][user_id]['teams']).to eq(['blue'])
180180
expect(response['users'][user_id]['teams_role']['blue']).to eq('admin')
181181
end
182182

@@ -960,4 +960,64 @@ def loop_times(times)
960960
end
961961
end
962962
end
963+
964+
describe '#query_threads' do
965+
before(:all) do
966+
# Create a dedicated random user for this block
967+
@thread_test_user = { id: SecureRandom.uuid }
968+
@client.upsert_users([@thread_test_user])
969+
970+
# Create a channel and send a message to create a thread
971+
@thread_channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true })
972+
@thread_channel.create(@thread_test_user[:id])
973+
974+
# Send a message to create a thread
975+
@thread_message = @thread_channel.send_message({ text: 'Thread parent message' }, @thread_test_user[:id])
976+
977+
# Send a reply to create a thread
978+
@thread_channel.send_message({ text: 'Thread reply', parent_id: @thread_message['message']['id'] }, @thread_test_user[:id])
979+
end
980+
981+
after(:all) do
982+
@thread_channel.delete
983+
@client.delete_user(@thread_test_user[:id])
984+
end
985+
986+
it 'queries threads with filter' do
987+
filter = {
988+
'created_by_user_id' => { '$eq' => @thread_test_user[:id] }
989+
}
990+
991+
response = @client.query_threads(filter, user_id: @thread_test_user[:id])
992+
993+
expect(response).to include 'threads'
994+
expect(response['threads'].length).to be >= 1
995+
end
996+
997+
it 'queries threads with sort' do
998+
sort = {
999+
'created_at' => -1
1000+
}
1001+
1002+
response = @client.query_threads({}, sort: sort, user_id: @thread_test_user[:id])
1003+
1004+
expect(response).to include 'threads'
1005+
expect(response['threads'].length).to be >= 1
1006+
end
1007+
1008+
it 'queries threads with both filter and sort' do
1009+
filter = {
1010+
'created_by_user_id' => { '$eq' => @thread_test_user[:id] }
1011+
}
1012+
1013+
sort = {
1014+
'created_at' => -1
1015+
}
1016+
1017+
response = @client.query_threads(filter, sort: sort, user_id: @thread_test_user[:id])
1018+
1019+
expect(response).to include 'threads'
1020+
expect(response['threads'].length).to be >= 1
1021+
end
1022+
end
9631023
end

0 commit comments

Comments
 (0)