From 2546708e7b7c5fbb6fe27e45aff7a96afa428232 Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 11:47:13 +0200 Subject: [PATCH 01/12] Add Query Threads --- lib/stream-chat/client.rb | 10 ++++ lib/stream-chat/thread.rb | 41 ++++++++++++++ spec/thread_spec.rb | 116 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 lib/stream-chat/thread.rb create mode 100644 spec/thread_spec.rb diff --git a/lib/stream-chat/client.rb b/lib/stream-chat/client.rb index 87f47aa..054acbf 100644 --- a/lib/stream-chat/client.rb +++ b/lib/stream-chat/client.rb @@ -15,6 +15,7 @@ require 'stream-chat/util' require 'stream-chat/types' require 'stream-chat/moderation' +require 'stream-chat/thread' module StreamChat DEFAULT_BLOCKLIST = 'profanity_en_2020_v1' @@ -39,6 +40,9 @@ class Client sig { returns(Moderation) } attr_reader :moderation + sig { returns(Thread) } + attr_reader :thread + # initializes a Stream Chat API Client # # @param [string] api_key your application api_key @@ -69,6 +73,7 @@ def initialize(api_key, api_secret, timeout = nil, **options) end @conn = T.let(conn, Faraday::Connection) @moderation = T.let(Moderation.new(self), Moderation) + @thread = T.let(Thread.new(self), Thread) end # initializes a Stream Chat API Client from STREAM_KEY and STREAM_SECRET @@ -929,6 +934,11 @@ 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, **options) + @thread.query_threads(filter, sort, **options) + end + private sig { returns(T::Hash[String, String]) } diff --git a/lib/stream-chat/thread.rb b/lib/stream-chat/thread.rb new file mode 100644 index 0000000..f5feb10 --- /dev/null +++ b/lib/stream-chat/thread.rb @@ -0,0 +1,41 @@ +# typed: strict +# frozen_string_literal: true + +require 'stream-chat/client' +require 'stream-chat/errors' +require 'stream-chat/util' +require 'stream-chat/types' + +module StreamChat + class Thread + extend T::Sig + + sig { returns(StreamChat::Client) } + attr_reader :client + + sig { params(client: StreamChat::Client).void } + def initialize(client) + @client = client + end + + # Queries threads based on filter conditions and sort parameters. + # + # The queryThreads endpoint allows you to list and paginate threads. The + # endpoint supports filtering on numerous criteria and sorting by various fields. + # This endpoint is useful for displaying threads in a chat application. + # + # @param [StringKeyHash] filter MongoDB-style filter conditions + # @param [T.nilable(T::Hash[String, Integer])] sort Sort parameters + # @param [T.untyped] options Additional options like limit, offset, next, etc. + # @return [StreamChat::StreamResponse] + 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) + }) + + @client.post('threads', data: params) + end + end +end \ No newline at end of file diff --git a/spec/thread_spec.rb b/spec/thread_spec.rb new file mode 100644 index 0000000..cb5fcbb --- /dev/null +++ b/spec/thread_spec.rb @@ -0,0 +1,116 @@ +# frozen_string_literal: true + +require 'stream-chat' +require 'faraday' +require 'securerandom' + +describe StreamChat::Thread do + before(:all) do + @client = StreamChat::Client.from_env + @created_users = [] + end + + before(:each) do + @random_user = { id: SecureRandom.uuid } + @created_users.push(@random_user[:id]) + @client.upsert_users([@random_user]) + end + + after(:all) do + curr_idx = 0 + batch_size = 25 + + slice = @created_users.slice(0, batch_size) + + while !slice.nil? && !slice.empty? + @client.delete_users(slice, user: StreamChat::HARD_DELETE, messages: StreamChat::HARD_DELETE) + + curr_idx += batch_size + slice = @created_users.slice(curr_idx, batch_size) + end + end + + describe '#query_threads' do + it 'queries threads with filter' do + # Create a channel and send a message to create a thread + channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) + channel.create(@random_user[:id]) + + # Send a message to create a thread + message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) + + # Send a reply to create a thread + channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) + + # Query threads with filter + filter = { + 'created_by_user_id' => { '$eq' => @random_user[:id] } + } + + response = @client.thread.query_threads(filter, user_id: @random_user[:id]) + + # Verify the response + expect(response).to include 'threads' + expect(response['threads'].length).to be >= 1 + + # Clean up + channel.delete + end + + it 'queries threads with sort' do + # Create a channel and send a message to create a thread + channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) + channel.create(@random_user[:id]) + + # Send a message to create a thread + message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) + + # Send a reply to create a thread + channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) + + # Query threads with sort + sort = { + 'created_at' => -1 + } + + response = @client.thread.query_threads(sort: sort, user_id: @random_user[:id]) + + # Verify the response + expect(response).to include 'threads' + expect(response['threads'].length).to be >= 1 + + # Clean up + channel.delete + end + + it 'queries threads with both filter and sort' do + # Create a channel and send a message to create a thread + channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) + channel.create(@random_user[:id]) + + # Send a message to create a thread + message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) + + # Send a reply to create a thread + channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) + + # Query threads with both filter and sort + filter = { + 'created_by_user_id' => { '$eq' => @random_user[:id] } + } + + sort = { + 'created_at' => -1 + } + + response = @client.thread.query_threads(filter, sort: sort, user_id: @random_user[:id]) + + # Verify the response + expect(response).to include 'threads' + expect(response['threads'].length).to be >= 1 + + # Clean up + channel.delete + end + end +end From 7f214b6fb4d2cca5112634eec06cbd9c2a12d38c Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 11:49:10 +0200 Subject: [PATCH 02/12] Fix lints --- lib/stream-chat/thread.rb | 8 ++++---- spec/thread_spec.rb | 40 +++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/stream-chat/thread.rb b/lib/stream-chat/thread.rb index f5feb10..fb88132 100644 --- a/lib/stream-chat/thread.rb +++ b/lib/stream-chat/thread.rb @@ -31,11 +31,11 @@ def initialize(client) 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) - }) + filter: filter, + sort: StreamChat.get_sort_fields(sort) + }) @client.post('threads', data: params) end end -end \ No newline at end of file +end diff --git a/spec/thread_spec.rb b/spec/thread_spec.rb index cb5fcbb..9871bab 100644 --- a/spec/thread_spec.rb +++ b/spec/thread_spec.rb @@ -35,24 +35,24 @@ # Create a channel and send a message to create a thread channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) channel.create(@random_user[:id]) - + # Send a message to create a thread message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) - + # Send a reply to create a thread channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) - + # Query threads with filter filter = { 'created_by_user_id' => { '$eq' => @random_user[:id] } } - + response = @client.thread.query_threads(filter, user_id: @random_user[:id]) - + # Verify the response expect(response).to include 'threads' expect(response['threads'].length).to be >= 1 - + # Clean up channel.delete end @@ -61,24 +61,24 @@ # Create a channel and send a message to create a thread channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) channel.create(@random_user[:id]) - + # Send a message to create a thread message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) - + # Send a reply to create a thread channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) - + # Query threads with sort sort = { 'created_at' => -1 } - + response = @client.thread.query_threads(sort: sort, user_id: @random_user[:id]) - + # Verify the response expect(response).to include 'threads' expect(response['threads'].length).to be >= 1 - + # Clean up channel.delete end @@ -87,30 +87,30 @@ # Create a channel and send a message to create a thread channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) channel.create(@random_user[:id]) - + # Send a message to create a thread message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) - + # Send a reply to create a thread channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) - + # Query threads with both filter and sort filter = { 'created_by_user_id' => { '$eq' => @random_user[:id] } } - + sort = { 'created_at' => -1 } - + response = @client.thread.query_threads(filter, sort: sort, user_id: @random_user[:id]) - + # Verify the response expect(response).to include 'threads' expect(response['threads'].length).to be >= 1 - + # Clean up channel.delete end end -end +end From 477c7e23c421115ba03f56b11875508bcafbf690 Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 12:53:25 +0200 Subject: [PATCH 03/12] Fix team based test --- spec/client_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index dd4ede4..3e0e866 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -168,10 +168,14 @@ def loop_times(times) user_id = SecureRandom.uuid @client.update_user({ id: user_id, name: 'Test User' }) + @client.update_user_partial({ + id: user_id, + set: { team: 'blue' } + }) + response = @client.update_user_partial({ id: user_id, set: { - team: 'blue', teams_role: { 'blue' => 'admin' } } }) From b0685a01f8c0d64fd9b31a90774a680123ca0120 Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 13:07:52 +0200 Subject: [PATCH 04/12] Fix team based test --- spec/client_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 3e0e866..b9fb8e5 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -170,7 +170,7 @@ def loop_times(times) @client.update_user_partial({ id: user_id, - set: { team: 'blue' } + teams: ['blue'], }) response = @client.update_user_partial({ From 9c9ddfe3b65b8a763dc851acf0e626db170ad4fb Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 13:08:15 +0200 Subject: [PATCH 05/12] Fix lint --- spec/client_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index b9fb8e5..87f1649 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -170,7 +170,7 @@ def loop_times(times) @client.update_user_partial({ id: user_id, - teams: ['blue'], + teams: ['blue'] }) response = @client.update_user_partial({ From fdadbeb841191a686f5e469f1bac11aaf18b4a56 Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 13:10:04 +0200 Subject: [PATCH 06/12] Fix team based test --- spec/client_spec.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 87f1649..55a3ed4 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -170,7 +170,9 @@ def loop_times(times) @client.update_user_partial({ id: user_id, - teams: ['blue'] + set: { + teams: ['blue'] + } }) response = @client.update_user_partial({ @@ -180,7 +182,7 @@ def loop_times(times) } }) - 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 From c5a04910478ebb25c264ddf0c2eb00be9f684679 Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 13:13:47 +0200 Subject: [PATCH 07/12] Fix test --- lib/stream-chat/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stream-chat/client.rb b/lib/stream-chat/client.rb index 054acbf..77471e6 100644 --- a/lib/stream-chat/client.rb +++ b/lib/stream-chat/client.rb @@ -936,7 +936,7 @@ def list_imports(options) sig { params(filter: StringKeyHash, sort: T.nilable(T::Hash[String, Integer]), options: T.untyped).returns(StreamChat::StreamResponse) } def query_threads(filter, sort, **options) - @thread.query_threads(filter, sort, **options) + @thread.query_threads(filter, sort: sort, **options) end private From 6ec3816d0b74c857089a314fe2fa2db3ac699677 Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 17:37:51 +0200 Subject: [PATCH 08/12] Make call query threads directly --- lib/stream-chat/client.rb | 14 ++--- lib/stream-chat/thread.rb | 41 -------------- spec/client_spec.rb | 84 +++++++++++++++++++++++++++ spec/thread_spec.rb | 116 -------------------------------------- 4 files changed, 91 insertions(+), 164 deletions(-) delete mode 100644 lib/stream-chat/thread.rb delete mode 100644 spec/thread_spec.rb diff --git a/lib/stream-chat/client.rb b/lib/stream-chat/client.rb index 77471e6..8d4944c 100644 --- a/lib/stream-chat/client.rb +++ b/lib/stream-chat/client.rb @@ -15,7 +15,6 @@ require 'stream-chat/util' require 'stream-chat/types' require 'stream-chat/moderation' -require 'stream-chat/thread' module StreamChat DEFAULT_BLOCKLIST = 'profanity_en_2020_v1' @@ -40,9 +39,6 @@ class Client sig { returns(Moderation) } attr_reader :moderation - sig { returns(Thread) } - attr_reader :thread - # initializes a Stream Chat API Client # # @param [string] api_key your application api_key @@ -73,7 +69,6 @@ def initialize(api_key, api_secret, timeout = nil, **options) end @conn = T.let(conn, Faraday::Connection) @moderation = T.let(Moderation.new(self), Moderation) - @thread = T.let(Thread.new(self), Thread) end # initializes a Stream Chat API Client from STREAM_KEY and STREAM_SECRET @@ -935,8 +930,13 @@ def list_imports(options) end sig { params(filter: StringKeyHash, sort: T.nilable(T::Hash[String, Integer]), options: T.untyped).returns(StreamChat::StreamResponse) } - def query_threads(filter, sort, **options) - @thread.query_threads(filter, sort: sort, **options) + 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 diff --git a/lib/stream-chat/thread.rb b/lib/stream-chat/thread.rb deleted file mode 100644 index fb88132..0000000 --- a/lib/stream-chat/thread.rb +++ /dev/null @@ -1,41 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -require 'stream-chat/client' -require 'stream-chat/errors' -require 'stream-chat/util' -require 'stream-chat/types' - -module StreamChat - class Thread - extend T::Sig - - sig { returns(StreamChat::Client) } - attr_reader :client - - sig { params(client: StreamChat::Client).void } - def initialize(client) - @client = client - end - - # Queries threads based on filter conditions and sort parameters. - # - # The queryThreads endpoint allows you to list and paginate threads. The - # endpoint supports filtering on numerous criteria and sorting by various fields. - # This endpoint is useful for displaying threads in a chat application. - # - # @param [StringKeyHash] filter MongoDB-style filter conditions - # @param [T.nilable(T::Hash[String, Integer])] sort Sort parameters - # @param [T.untyped] options Additional options like limit, offset, next, etc. - # @return [StreamChat::StreamResponse] - 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) - }) - - @client.post('threads', data: params) - end - end -end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 55a3ed4..3b0bc30 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -966,4 +966,88 @@ def loop_times(times) end end end + + describe '#query_threads' do + it 'queries threads with filter' do + # Create a channel and send a message to create a thread + channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) + channel.create(@random_user[:id]) + + # Send a message to create a thread + message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) + + # Send a reply to create a thread + channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) + + # Query threads with filter + filter = { + 'created_by_user_id' => { '$eq' => @random_user[:id] } + } + + response = @client.query_threads(filter, user_id: @random_user[:id]) + + # Verify the response + expect(response).to include 'threads' + expect(response['threads'].length).to be >= 1 + + # Clean up + channel.delete + end + + it 'queries threads with sort' do + # Create a channel and send a message to create a thread + channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) + channel.create(@random_user[:id]) + + # Send a message to create a thread + message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) + + # Send a reply to create a thread + channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) + + # Query threads with sort + sort = { + 'created_at' => -1 + } + + response = @client.query_threads(sort: sort, user_id: @random_user[:id]) + + # Verify the response + expect(response).to include 'threads' + expect(response['threads'].length).to be >= 1 + + # Clean up + channel.delete + end + + it 'queries threads with both filter and sort' do + # Create a channel and send a message to create a thread + channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) + channel.create(@random_user[:id]) + + # Send a message to create a thread + message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) + + # Send a reply to create a thread + channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) + + # Query threads with both filter and sort + filter = { + 'created_by_user_id' => { '$eq' => @random_user[:id] } + } + + sort = { + 'created_at' => -1 + } + + response = @client.query_threads(filter, sort: sort, user_id: @random_user[:id]) + + # Verify the response + expect(response).to include 'threads' + expect(response['threads'].length).to be >= 1 + + # Clean up + channel.delete + end + end end diff --git a/spec/thread_spec.rb b/spec/thread_spec.rb deleted file mode 100644 index 9871bab..0000000 --- a/spec/thread_spec.rb +++ /dev/null @@ -1,116 +0,0 @@ -# frozen_string_literal: true - -require 'stream-chat' -require 'faraday' -require 'securerandom' - -describe StreamChat::Thread do - before(:all) do - @client = StreamChat::Client.from_env - @created_users = [] - end - - before(:each) do - @random_user = { id: SecureRandom.uuid } - @created_users.push(@random_user[:id]) - @client.upsert_users([@random_user]) - end - - after(:all) do - curr_idx = 0 - batch_size = 25 - - slice = @created_users.slice(0, batch_size) - - while !slice.nil? && !slice.empty? - @client.delete_users(slice, user: StreamChat::HARD_DELETE, messages: StreamChat::HARD_DELETE) - - curr_idx += batch_size - slice = @created_users.slice(curr_idx, batch_size) - end - end - - describe '#query_threads' do - it 'queries threads with filter' do - # Create a channel and send a message to create a thread - channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) - channel.create(@random_user[:id]) - - # Send a message to create a thread - message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) - - # Send a reply to create a thread - channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) - - # Query threads with filter - filter = { - 'created_by_user_id' => { '$eq' => @random_user[:id] } - } - - response = @client.thread.query_threads(filter, user_id: @random_user[:id]) - - # Verify the response - expect(response).to include 'threads' - expect(response['threads'].length).to be >= 1 - - # Clean up - channel.delete - end - - it 'queries threads with sort' do - # Create a channel and send a message to create a thread - channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) - channel.create(@random_user[:id]) - - # Send a message to create a thread - message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) - - # Send a reply to create a thread - channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) - - # Query threads with sort - sort = { - 'created_at' => -1 - } - - response = @client.thread.query_threads(sort: sort, user_id: @random_user[:id]) - - # Verify the response - expect(response).to include 'threads' - expect(response['threads'].length).to be >= 1 - - # Clean up - channel.delete - end - - it 'queries threads with both filter and sort' do - # Create a channel and send a message to create a thread - channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) - channel.create(@random_user[:id]) - - # Send a message to create a thread - message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) - - # Send a reply to create a thread - channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) - - # Query threads with both filter and sort - filter = { - 'created_by_user_id' => { '$eq' => @random_user[:id] } - } - - sort = { - 'created_at' => -1 - } - - response = @client.thread.query_threads(filter, sort: sort, user_id: @random_user[:id]) - - # Verify the response - expect(response).to include 'threads' - expect(response['threads'].length).to be >= 1 - - # Clean up - channel.delete - end - end -end From 4b2fddc9495e48dcb8ee0bc46a14c11466e6776d Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 19:47:12 +0200 Subject: [PATCH 09/12] Fix test --- spec/client_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 3b0bc30..11763fa 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -1010,7 +1010,7 @@ def loop_times(times) 'created_at' => -1 } - response = @client.query_threads(sort: sort, user_id: @random_user[:id]) + response = @client.query_threads({}, sort: sort, user_id: @random_user[:id]) # Verify the response expect(response).to include 'threads' From 29ff3d13f6a3b480237e7d735075ba257d910a86 Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 19:56:23 +0200 Subject: [PATCH 10/12] Simplify test --- spec/client_spec.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 11763fa..8015899 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -168,16 +168,10 @@ def loop_times(times) user_id = SecureRandom.uuid @client.update_user({ id: user_id, name: 'Test User' }) - @client.update_user_partial({ - id: user_id, - set: { - teams: ['blue'] - } - }) - response = @client.update_user_partial({ id: user_id, set: { + teams: ['blue'], teams_role: { 'blue' => 'admin' } } }) From 127e93e228e8fba53f7b34fc828f9d0c7f8cf56f Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 20:01:00 +0200 Subject: [PATCH 11/12] Refactor test --- spec/client_spec.rb | 51 ++++++++++----------------------------------- 1 file changed, 11 insertions(+), 40 deletions(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 8015899..5da9d72 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -962,70 +962,45 @@ def loop_times(times) end describe '#query_threads' do - it 'queries threads with filter' do + before(:each) do # Create a channel and send a message to create a thread - channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) - channel.create(@random_user[:id]) + @thread_channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) + @thread_channel.create(@random_user[:id]) # Send a message to create a thread - message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) + @thread_message = @thread_channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) # Send a reply to create a thread - channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) + @thread_channel.send_message({ text: 'Thread reply', parent_id: @thread_message['message']['id'] }, @random_user[:id]) + end - # Query threads with filter + after(:each) do + @thread_channel.delete + end + + it 'queries threads with filter' do filter = { 'created_by_user_id' => { '$eq' => @random_user[:id] } } response = @client.query_threads(filter, user_id: @random_user[:id]) - # Verify the response expect(response).to include 'threads' expect(response['threads'].length).to be >= 1 - - # Clean up - channel.delete end it 'queries threads with sort' do - # Create a channel and send a message to create a thread - channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) - channel.create(@random_user[:id]) - - # Send a message to create a thread - message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) - - # Send a reply to create a thread - channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) - - # Query threads with sort sort = { 'created_at' => -1 } response = @client.query_threads({}, sort: sort, user_id: @random_user[:id]) - # Verify the response expect(response).to include 'threads' expect(response['threads'].length).to be >= 1 - - # Clean up - channel.delete end it 'queries threads with both filter and sort' do - # Create a channel and send a message to create a thread - channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { test: true }) - channel.create(@random_user[:id]) - - # Send a message to create a thread - message = channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) - - # Send a reply to create a thread - channel.send_message({ text: 'Thread reply', parent_id: message['message']['id'] }, @random_user[:id]) - - # Query threads with both filter and sort filter = { 'created_by_user_id' => { '$eq' => @random_user[:id] } } @@ -1036,12 +1011,8 @@ def loop_times(times) response = @client.query_threads(filter, sort: sort, user_id: @random_user[:id]) - # Verify the response expect(response).to include 'threads' expect(response['threads'].length).to be >= 1 - - # Clean up - channel.delete end end end From 2fbf49e9ed8cb4a30b4fa74fde5408715ee4af69 Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Tue, 15 Apr 2025 20:03:45 +0200 Subject: [PATCH 12/12] Refactor test --- spec/client_spec.rb | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 5da9d72..b0e5dc9 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -962,28 +962,33 @@ def loop_times(times) end describe '#query_threads' do - before(:each) 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(@random_user[:id]) + @thread_channel.create(@thread_test_user[:id]) # Send a message to create a thread - @thread_message = @thread_channel.send_message({ text: 'Thread parent message' }, @random_user[:id]) + @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'] }, @random_user[:id]) + @thread_channel.send_message({ text: 'Thread reply', parent_id: @thread_message['message']['id'] }, @thread_test_user[:id]) end - after(:each) do + 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' => @random_user[:id] } + 'created_by_user_id' => { '$eq' => @thread_test_user[:id] } } - response = @client.query_threads(filter, user_id: @random_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 @@ -994,7 +999,7 @@ def loop_times(times) 'created_at' => -1 } - response = @client.query_threads({}, sort: sort, user_id: @random_user[:id]) + response = @client.query_threads({}, sort: sort, user_id: @thread_test_user[:id]) expect(response).to include 'threads' expect(response['threads'].length).to be >= 1 @@ -1002,14 +1007,14 @@ def loop_times(times) it 'queries threads with both filter and sort' do filter = { - 'created_by_user_id' => { '$eq' => @random_user[:id] } + 'created_by_user_id' => { '$eq' => @thread_test_user[:id] } } sort = { 'created_at' => -1 } - response = @client.query_threads(filter, sort: sort, user_id: @random_user[:id]) + 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