Skip to content

Commit 2546708

Browse files
committed
Add Query Threads
1 parent fa26e75 commit 2546708

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

lib/stream-chat/client.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require 'stream-chat/util'
1616
require 'stream-chat/types'
1717
require 'stream-chat/moderation'
18+
require 'stream-chat/thread'
1819

1920
module StreamChat
2021
DEFAULT_BLOCKLIST = 'profanity_en_2020_v1'
@@ -39,6 +40,9 @@ class Client
3940
sig { returns(Moderation) }
4041
attr_reader :moderation
4142

43+
sig { returns(Thread) }
44+
attr_reader :thread
45+
4246
# initializes a Stream Chat API Client
4347
#
4448
# @param [string] api_key your application api_key
@@ -69,6 +73,7 @@ def initialize(api_key, api_secret, timeout = nil, **options)
6973
end
7074
@conn = T.let(conn, Faraday::Connection)
7175
@moderation = T.let(Moderation.new(self), Moderation)
76+
@thread = T.let(Thread.new(self), Thread)
7277
end
7378

7479
# initializes a Stream Chat API Client from STREAM_KEY and STREAM_SECRET
@@ -929,6 +934,11 @@ def list_imports(options)
929934
get('imports', params: options)
930935
end
931936

937+
sig { params(filter: StringKeyHash, sort: T.nilable(T::Hash[String, Integer]), options: T.untyped).returns(StreamChat::StreamResponse) }
938+
def query_threads(filter, sort, **options)
939+
@thread.query_threads(filter, sort, **options)
940+
end
941+
932942
private
933943

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

lib/stream-chat/thread.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
require 'stream-chat/client'
5+
require 'stream-chat/errors'
6+
require 'stream-chat/util'
7+
require 'stream-chat/types'
8+
9+
module StreamChat
10+
class Thread
11+
extend T::Sig
12+
13+
sig { returns(StreamChat::Client) }
14+
attr_reader :client
15+
16+
sig { params(client: StreamChat::Client).void }
17+
def initialize(client)
18+
@client = client
19+
end
20+
21+
# Queries threads based on filter conditions and sort parameters.
22+
#
23+
# The queryThreads endpoint allows you to list and paginate threads. The
24+
# endpoint supports filtering on numerous criteria and sorting by various fields.
25+
# This endpoint is useful for displaying threads in a chat application.
26+
#
27+
# @param [StringKeyHash] filter MongoDB-style filter conditions
28+
# @param [T.nilable(T::Hash[String, Integer])] sort Sort parameters
29+
# @param [T.untyped] options Additional options like limit, offset, next, etc.
30+
# @return [StreamChat::StreamResponse]
31+
sig { params(filter: StringKeyHash, sort: T.nilable(T::Hash[String, Integer]), options: T.untyped).returns(StreamChat::StreamResponse) }
32+
def query_threads(filter = {}, sort: nil, **options)
33+
params = {}.merge(options).merge({
34+
filter: filter,
35+
sort: StreamChat.get_sort_fields(sort)
36+
})
37+
38+
@client.post('threads', data: params)
39+
end
40+
end
41+
end

spec/thread_spec.rb

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# frozen_string_literal: true
2+
3+
require 'stream-chat'
4+
require 'faraday'
5+
require 'securerandom'
6+
7+
describe StreamChat::Thread do
8+
before(:all) do
9+
@client = StreamChat::Client.from_env
10+
@created_users = []
11+
end
12+
13+
before(:each) do
14+
@random_user = { id: SecureRandom.uuid }
15+
@created_users.push(@random_user[:id])
16+
@client.upsert_users([@random_user])
17+
end
18+
19+
after(:all) do
20+
curr_idx = 0
21+
batch_size = 25
22+
23+
slice = @created_users.slice(0, batch_size)
24+
25+
while !slice.nil? && !slice.empty?
26+
@client.delete_users(slice, user: StreamChat::HARD_DELETE, messages: StreamChat::HARD_DELETE)
27+
28+
curr_idx += batch_size
29+
slice = @created_users.slice(curr_idx, batch_size)
30+
end
31+
end
32+
33+
describe '#query_threads' do
34+
it 'queries threads with filter' do
35+
# Create a channel and send a message to create a thread
36+
channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true })
37+
channel.create(@random_user[:id])
38+
39+
# Send a message to create a thread
40+
message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id])
41+
42+
# Send a reply to create a thread
43+
channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id])
44+
45+
# Query threads with filter
46+
filter = {
47+
'created_by_user_id' => { '$eq' => @random_user[:id] }
48+
}
49+
50+
response = @client.thread.query_threads(filter, user_id: @random_user[:id])
51+
52+
# Verify the response
53+
expect(response).to include 'threads'
54+
expect(response['threads'].length).to be >= 1
55+
56+
# Clean up
57+
channel.delete
58+
end
59+
60+
it 'queries threads with sort' do
61+
# Create a channel and send a message to create a thread
62+
channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true })
63+
channel.create(@random_user[:id])
64+
65+
# Send a message to create a thread
66+
message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id])
67+
68+
# Send a reply to create a thread
69+
channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id])
70+
71+
# Query threads with sort
72+
sort = {
73+
'created_at' => -1
74+
}
75+
76+
response = @client.thread.query_threads(sort: sort, user_id: @random_user[:id])
77+
78+
# Verify the response
79+
expect(response).to include 'threads'
80+
expect(response['threads'].length).to be >= 1
81+
82+
# Clean up
83+
channel.delete
84+
end
85+
86+
it 'queries threads with both filter and sort' do
87+
# Create a channel and send a message to create a thread
88+
channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true })
89+
channel.create(@random_user[:id])
90+
91+
# Send a message to create a thread
92+
message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id])
93+
94+
# Send a reply to create a thread
95+
channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id])
96+
97+
# Query threads with both filter and sort
98+
filter = {
99+
'created_by_user_id' => { '$eq' => @random_user[:id] }
100+
}
101+
102+
sort = {
103+
'created_at' => -1
104+
}
105+
106+
response = @client.thread.query_threads(filter, sort: sort, user_id: @random_user[:id])
107+
108+
# Verify the response
109+
expect(response).to include 'threads'
110+
expect(response['threads'].length).to be >= 1
111+
112+
# Clean up
113+
channel.delete
114+
end
115+
end
116+
end

0 commit comments

Comments
 (0)