From bc44b0df06266eba3725b8ad183f13d323a17a7f Mon Sep 17 00:00:00 2001 From: Rafael Marinho Date: Wed, 3 Sep 2025 15:25:16 +0200 Subject: [PATCH 1/8] [CHA-1226]: support delivery receipts --- lib/GetStream/StreamChat/Channel.php | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/GetStream/StreamChat/Channel.php b/lib/GetStream/StreamChat/Channel.php index ac5b0e2..68adab0 100644 --- a/lib/GetStream/StreamChat/Channel.php +++ b/lib/GetStream/StreamChat/Channel.php @@ -354,6 +354,38 @@ public function markRead(string $userId, ?array $data = null): StreamResponse return $this->client->post($this->getUrl() . "/read", $payload); } + /** Send the mark delivered event for this user, only works if + * the `delivery_receipts` setting is enabled. + * + * @param string $userId The user ID sending the delivery receipt + * @param array|null $data Optional data for the mark delivered request. Expected structure: + * [ + * 'channel_delivered_message' => [ + * 'channel_id' => 'message_id' + * ], + * 'client_id' => 'optional_client_id', + * 'connection_id' => 'optional_connection_id', + * 'user' => [/* user data */], + * 'user_id' => 'optional_user_id' + * ] + * @return StreamResponse + * @throws StreamException + * @link https://getstream.io/chat/docs/php/send_message/?language=php + */ + public function markDelivered(string $userId, ?array $data = null): StreamResponse + { + if ($data === null) { + $data = []; + } + + // Note: In PHP, we don't have access to the current user's privacy settings + // through the client like in JavaScript. The delivery receipts check should + // be handled on the client side before calling this method. + + $payload = Channel::addUser($data, $userId); + return $this->client->post("channels/delivered", $payload); + } + /** List the message replies for a parent message. * @link https://getstream.io/chat/docs/php/threads/?language=php * @throws StreamException From 9bad5a09735f3ba2a77badd312a5aa77187050c5 Mon Sep 17 00:00:00 2001 From: Rafael Marinho Date: Fri, 12 Sep 2025 15:39:47 +0200 Subject: [PATCH 2/8] refactor --- examples/mark_delivered_example.php | 61 ++++++++++++ tests/unit/MarkDeliveredTest.php | 139 ++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 examples/mark_delivered_example.php create mode 100644 tests/unit/MarkDeliveredTest.php diff --git a/examples/mark_delivered_example.php b/examples/mark_delivered_example.php new file mode 100644 index 0000000..83f71b9 --- /dev/null +++ b/examples/mark_delivered_example.php @@ -0,0 +1,61 @@ +Channel('messaging', 'channel_id_here'); + +// Example 1: Basic usage with just user ID +try { + $response = $channel->markDelivered('user_id_here'); + echo "Delivery receipt sent successfully\n"; + echo "Response status: " . $response->getStatusCode() . "\n"; +} catch (Exception $e) { + echo "Error sending delivery receipt: " . $e->getMessage() . "\n"; +} + +// Example 2: Advanced usage with custom data +try { + $data = [ + 'channel_delivered_message' => [ + 'messaging:channel_id_here' => 'message_id_here' + ], + 'client_id' => 'web_client_123', + 'connection_id' => 'connection_456' + ]; + + $response = $channel->markDelivered('user_id_here', $data); + echo "Delivery receipt with custom data sent successfully\n"; + echo "Response status: " . $response->getStatusCode() . "\n"; +} catch (Exception $e) { + echo "Error sending delivery receipt with custom data: " . $e->getMessage() . "\n"; +} + +// Example 3: Using with multiple channels +try { + $data = [ + 'channel_delivered_message' => [ + 'messaging:channel_1' => 'message_1_id', + 'messaging:channel_2' => 'message_2_id', + 'team:team_channel' => 'team_message_id' + ] + ]; + + $response = $channel->markDelivered('user_id_here', $data); + echo "Multi-channel delivery receipt sent successfully\n"; + echo "Response status: " . $response->getStatusCode() . "\n"; +} catch (Exception $e) { + echo "Error sending multi-channel delivery receipt: " . $e->getMessage() . "\n"; +} + +echo "\nNote: The delivery_receipts setting must be enabled for the user\n"; +echo "for this functionality to work properly.\n"; + diff --git a/tests/unit/MarkDeliveredTest.php b/tests/unit/MarkDeliveredTest.php new file mode 100644 index 0000000..6e54f71 --- /dev/null +++ b/tests/unit/MarkDeliveredTest.php @@ -0,0 +1,139 @@ +client = $this->createMock(Client::class); + $this->channel = new Channel($this->client, 'messaging', 'test_channel_id'); + } + + public function testMarkDeliveredBasic() + { + $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); + + $this->client->expects($this->once()) + ->method('post') + ->with( + 'channels/delivered', + ['user_id' => 'test_user_id'] + ) + ->willReturn($expectedResponse); + + $response = $this->channel->markDelivered('test_user_id'); + + $this->assertInstanceOf(StreamResponse::class, $response); + $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); + } + + public function testMarkDeliveredWithData() + { + $data = [ + 'channel_delivered_message' => [ + 'messaging:test_channel_id' => 'test_message_id' + ], + 'client_id' => 'test_client' + ]; + + $expectedPayload = array_merge($data, ['user_id' => 'test_user_id']); + $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); + + $this->client->expects($this->once()) + ->method('post') + ->with( + 'channels/delivered', + $expectedPayload + ) + ->willReturn($expectedResponse); + + $response = $this->channel->markDelivered('test_user_id', $data); + + $this->assertInstanceOf(StreamResponse::class, $response); + $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); + } + + public function testMarkDeliveredWithNullData() + { + $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); + + $this->client->expects($this->once()) + ->method('post') + ->with( + 'channels/delivered', + ['user_id' => 'test_user_id'] + ) + ->willReturn($expectedResponse); + + $response = $this->channel->markDelivered('test_user_id', null); + + $this->assertInstanceOf(StreamResponse::class, $response); + $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); + } + + public function testMarkDeliveredWithEmptyData() + { + $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); + + $this->client->expects($this->once()) + ->method('post') + ->with( + 'channels/delivered', + ['user_id' => 'test_user_id'] + ) + ->willReturn($expectedResponse); + + $response = $this->channel->markDelivered('test_user_id', []); + + $this->assertInstanceOf(StreamResponse::class, $response); + $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); + } + + public function testMarkDeliveredWithMultipleChannels() + { + $data = [ + 'channel_delivered_message' => [ + 'messaging:channel_1' => 'message_1_id', + 'messaging:channel_2' => 'message_2_id', + 'team:team_channel' => 'team_message_id' + ] + ]; + + $expectedPayload = array_merge($data, ['user_id' => 'test_user_id']); + $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); + + $this->client->expects($this->once()) + ->method('post') + ->with( + 'channels/delivered', + $expectedPayload + ) + ->willReturn($expectedResponse); + + $response = $this->channel->markDelivered('test_user_id', $data); + + $this->assertInstanceOf(StreamResponse::class, $response); + $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); + } + + private function createMockResponse() + { + $mock = $this->createMock(\Psr\Http\Message\ResponseInterface::class); + $mock->method('getStatusCode')->willReturn(200); + $mock->method('getHeaders')->willReturn([]); + $mock->method('hasHeader')->willReturn(false); + return $mock; + } +} + From 59c2458fdf99fe5cc223869b9e74e4da42ce3c7d Mon Sep 17 00:00:00 2001 From: Rafael Marinho Date: Sat, 4 Oct 2025 09:45:41 +0200 Subject: [PATCH 3/8] refactor --- examples/mark_delivered_example.php | 61 ----------- lib/GetStream/StreamChat/Channel.php | 32 ------ lib/GetStream/StreamChat/Client.php | 15 +++ tests/integration/IntegrationTest.php | 18 ++++ tests/unit/MarkDeliveredTest.php | 139 -------------------------- 5 files changed, 33 insertions(+), 232 deletions(-) delete mode 100644 examples/mark_delivered_example.php delete mode 100644 tests/unit/MarkDeliveredTest.php diff --git a/examples/mark_delivered_example.php b/examples/mark_delivered_example.php deleted file mode 100644 index 83f71b9..0000000 --- a/examples/mark_delivered_example.php +++ /dev/null @@ -1,61 +0,0 @@ -Channel('messaging', 'channel_id_here'); - -// Example 1: Basic usage with just user ID -try { - $response = $channel->markDelivered('user_id_here'); - echo "Delivery receipt sent successfully\n"; - echo "Response status: " . $response->getStatusCode() . "\n"; -} catch (Exception $e) { - echo "Error sending delivery receipt: " . $e->getMessage() . "\n"; -} - -// Example 2: Advanced usage with custom data -try { - $data = [ - 'channel_delivered_message' => [ - 'messaging:channel_id_here' => 'message_id_here' - ], - 'client_id' => 'web_client_123', - 'connection_id' => 'connection_456' - ]; - - $response = $channel->markDelivered('user_id_here', $data); - echo "Delivery receipt with custom data sent successfully\n"; - echo "Response status: " . $response->getStatusCode() . "\n"; -} catch (Exception $e) { - echo "Error sending delivery receipt with custom data: " . $e->getMessage() . "\n"; -} - -// Example 3: Using with multiple channels -try { - $data = [ - 'channel_delivered_message' => [ - 'messaging:channel_1' => 'message_1_id', - 'messaging:channel_2' => 'message_2_id', - 'team:team_channel' => 'team_message_id' - ] - ]; - - $response = $channel->markDelivered('user_id_here', $data); - echo "Multi-channel delivery receipt sent successfully\n"; - echo "Response status: " . $response->getStatusCode() . "\n"; -} catch (Exception $e) { - echo "Error sending multi-channel delivery receipt: " . $e->getMessage() . "\n"; -} - -echo "\nNote: The delivery_receipts setting must be enabled for the user\n"; -echo "for this functionality to work properly.\n"; - diff --git a/lib/GetStream/StreamChat/Channel.php b/lib/GetStream/StreamChat/Channel.php index 68adab0..ac5b0e2 100644 --- a/lib/GetStream/StreamChat/Channel.php +++ b/lib/GetStream/StreamChat/Channel.php @@ -354,38 +354,6 @@ public function markRead(string $userId, ?array $data = null): StreamResponse return $this->client->post($this->getUrl() . "/read", $payload); } - /** Send the mark delivered event for this user, only works if - * the `delivery_receipts` setting is enabled. - * - * @param string $userId The user ID sending the delivery receipt - * @param array|null $data Optional data for the mark delivered request. Expected structure: - * [ - * 'channel_delivered_message' => [ - * 'channel_id' => 'message_id' - * ], - * 'client_id' => 'optional_client_id', - * 'connection_id' => 'optional_connection_id', - * 'user' => [/* user data */], - * 'user_id' => 'optional_user_id' - * ] - * @return StreamResponse - * @throws StreamException - * @link https://getstream.io/chat/docs/php/send_message/?language=php - */ - public function markDelivered(string $userId, ?array $data = null): StreamResponse - { - if ($data === null) { - $data = []; - } - - // Note: In PHP, we don't have access to the current user's privacy settings - // through the client like in JavaScript. The delivery receipts check should - // be handled on the client side before calling this method. - - $payload = Channel::addUser($data, $userId); - return $this->client->post("channels/delivered", $payload); - } - /** List the message replies for a parent message. * @link https://getstream.io/chat/docs/php/threads/?language=php * @throws StreamException diff --git a/lib/GetStream/StreamChat/Client.php b/lib/GetStream/StreamChat/Client.php index 3cd67cd..35323ed 100644 --- a/lib/GetStream/StreamChat/Client.php +++ b/lib/GetStream/StreamChat/Client.php @@ -1748,4 +1748,19 @@ public function updateUserActiveLiveLocation(string $userId, array $location): S $params = ["user_id" => $userId]; return $this->put("users/live_locations", $location, $params); } + + /** + * Mark messages as delivered + * @param string $userId The user ID + * @param array $latestDeliveredMessages Array of DeliveredMessageConfirmation objects + * @throws StreamException + */ + public function markDelivered(string $userId, array $latestDeliveredMessages): StreamResponse + { + $data = [ + 'latest_delivered_messages' => $latestDeliveredMessages + ]; + $params = ["user_id" => $userId]; + return $this->post("channels/delivered", $lastDeliveredMessages, $params); + } } diff --git a/tests/integration/IntegrationTest.php b/tests/integration/IntegrationTest.php index 34f4b61..a989116 100644 --- a/tests/integration/IntegrationTest.php +++ b/tests/integration/IntegrationTest.php @@ -1828,4 +1828,22 @@ public function testSharedLocations() $this->assertEquals(-118.2437, $newUserLocations["active_live_locations"][0]["longitude"]); $this->assertEquals('test-device-123', $newUserLocations["active_live_locations"][0]["created_by_device_id"]); } + + public function testMarkDelivered() + { + // Send a message first + $message = $this->channel->sendMessage(["text" => "Test message for delivery receipt"], $this->user1["id"]); + + // Mark the message as delivered + $latestDeliveredMessages = [ + [ + 'cid' => $this->channel->getCID(), + 'id' => $message["message"]["id"] + ] + ]; + + $response = $this->client->markDelivered($this->user1["id"], $latestDeliveredMessages); + + $this->assertInstanceOf(\GetStream\StreamChat\StreamResponse::class, $response); + } } diff --git a/tests/unit/MarkDeliveredTest.php b/tests/unit/MarkDeliveredTest.php deleted file mode 100644 index 6e54f71..0000000 --- a/tests/unit/MarkDeliveredTest.php +++ /dev/null @@ -1,139 +0,0 @@ -client = $this->createMock(Client::class); - $this->channel = new Channel($this->client, 'messaging', 'test_channel_id'); - } - - public function testMarkDeliveredBasic() - { - $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); - - $this->client->expects($this->once()) - ->method('post') - ->with( - 'channels/delivered', - ['user_id' => 'test_user_id'] - ) - ->willReturn($expectedResponse); - - $response = $this->channel->markDelivered('test_user_id'); - - $this->assertInstanceOf(StreamResponse::class, $response); - $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); - } - - public function testMarkDeliveredWithData() - { - $data = [ - 'channel_delivered_message' => [ - 'messaging:test_channel_id' => 'test_message_id' - ], - 'client_id' => 'test_client' - ]; - - $expectedPayload = array_merge($data, ['user_id' => 'test_user_id']); - $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); - - $this->client->expects($this->once()) - ->method('post') - ->with( - 'channels/delivered', - $expectedPayload - ) - ->willReturn($expectedResponse); - - $response = $this->channel->markDelivered('test_user_id', $data); - - $this->assertInstanceOf(StreamResponse::class, $response); - $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); - } - - public function testMarkDeliveredWithNullData() - { - $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); - - $this->client->expects($this->once()) - ->method('post') - ->with( - 'channels/delivered', - ['user_id' => 'test_user_id'] - ) - ->willReturn($expectedResponse); - - $response = $this->channel->markDelivered('test_user_id', null); - - $this->assertInstanceOf(StreamResponse::class, $response); - $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); - } - - public function testMarkDeliveredWithEmptyData() - { - $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); - - $this->client->expects($this->once()) - ->method('post') - ->with( - 'channels/delivered', - ['user_id' => 'test_user_id'] - ) - ->willReturn($expectedResponse); - - $response = $this->channel->markDelivered('test_user_id', []); - - $this->assertInstanceOf(StreamResponse::class, $response); - $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); - } - - public function testMarkDeliveredWithMultipleChannels() - { - $data = [ - 'channel_delivered_message' => [ - 'messaging:channel_1' => 'message_1_id', - 'messaging:channel_2' => 'message_2_id', - 'team:team_channel' => 'team_message_id' - ] - ]; - - $expectedPayload = array_merge($data, ['user_id' => 'test_user_id']); - $expectedResponse = new StreamResponse(['status' => 'success'], $this->createMockResponse()); - - $this->client->expects($this->once()) - ->method('post') - ->with( - 'channels/delivered', - $expectedPayload - ) - ->willReturn($expectedResponse); - - $response = $this->channel->markDelivered('test_user_id', $data); - - $this->assertInstanceOf(StreamResponse::class, $response); - $this->assertEquals(['status' => 'success'], $response->getArrayCopy()); - } - - private function createMockResponse() - { - $mock = $this->createMock(\Psr\Http\Message\ResponseInterface::class); - $mock->method('getStatusCode')->willReturn(200); - $mock->method('getHeaders')->willReturn([]); - $mock->method('hasHeader')->willReturn(false); - return $mock; - } -} - From ca49bcfb283a3142059222f137a6d79a6db89a83 Mon Sep 17 00:00:00 2001 From: Rafael Marinho Date: Sat, 4 Oct 2025 09:48:20 +0200 Subject: [PATCH 4/8] fix typo --- lib/GetStream/StreamChat/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/GetStream/StreamChat/Client.php b/lib/GetStream/StreamChat/Client.php index 35323ed..1a22355 100644 --- a/lib/GetStream/StreamChat/Client.php +++ b/lib/GetStream/StreamChat/Client.php @@ -1761,6 +1761,6 @@ public function markDelivered(string $userId, array $latestDeliveredMessages): S 'latest_delivered_messages' => $latestDeliveredMessages ]; $params = ["user_id" => $userId]; - return $this->post("channels/delivered", $lastDeliveredMessages, $params); + return $this->post("channels/delivered", $latestDeliveredMessages, $params); } } From bf2c0a069567e64bef6968f27e5e8a4686a55bf9 Mon Sep 17 00:00:00 2001 From: Rafael Marinho Date: Sat, 4 Oct 2025 09:53:28 +0200 Subject: [PATCH 5/8] fix typo --- lib/GetStream/StreamChat/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/GetStream/StreamChat/Client.php b/lib/GetStream/StreamChat/Client.php index 1a22355..085d987 100644 --- a/lib/GetStream/StreamChat/Client.php +++ b/lib/GetStream/StreamChat/Client.php @@ -1757,9 +1757,9 @@ public function updateUserActiveLiveLocation(string $userId, array $location): S */ public function markDelivered(string $userId, array $latestDeliveredMessages): StreamResponse { - $data = [ + $data = { 'latest_delivered_messages' => $latestDeliveredMessages - ]; + }; $params = ["user_id" => $userId]; return $this->post("channels/delivered", $latestDeliveredMessages, $params); } From 3ee7ebeb81dc761424360b44dc36881f77b79bed Mon Sep 17 00:00:00 2001 From: Rafael Marinho Date: Mon, 6 Oct 2025 07:37:32 +0200 Subject: [PATCH 6/8] fix --- lib/GetStream/StreamChat/Client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/GetStream/StreamChat/Client.php b/lib/GetStream/StreamChat/Client.php index 085d987..0ccd851 100644 --- a/lib/GetStream/StreamChat/Client.php +++ b/lib/GetStream/StreamChat/Client.php @@ -1757,10 +1757,10 @@ public function updateUserActiveLiveLocation(string $userId, array $location): S */ public function markDelivered(string $userId, array $latestDeliveredMessages): StreamResponse { - $data = { + $data = [ 'latest_delivered_messages' => $latestDeliveredMessages - }; + ]; $params = ["user_id" => $userId]; - return $this->post("channels/delivered", $latestDeliveredMessages, $params); + return $this->post("channels/delivered", $data, $params); } } From dffe21c169f24b96c4ff9f825220e11bde58efc2 Mon Sep 17 00:00:00 2001 From: Rafael Marinho Date: Mon, 6 Oct 2025 07:45:41 +0200 Subject: [PATCH 7/8] fix webhook v2 tests --- tests/integration/IntegrationTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/integration/IntegrationTest.php b/tests/integration/IntegrationTest.php index a989116..e86b419 100644 --- a/tests/integration/IntegrationTest.php +++ b/tests/integration/IntegrationTest.php @@ -157,6 +157,22 @@ public function testUpdateAppSettings() { $response = $this->client->getAppSettings(); $settings = $response['app']; + + unset($settings['webhook_url']); + unset($settings['webhook_events']); + unset($settings['sqs_url']); + unset($settings['sqs_key']); + unset($settings['sqs_secret']); + unset($settings['sns_topic_arn']); + unset($settings['sns_key']); + unset($settings['sns_secret']); + unset($settings['async_moderation_config']); + + // Add new event_hooks format if needed + if (!isset($settings['event_hooks'])) { + $settings['event_hooks'] = []; + } + $response = $this->client->updateAppSettings($settings); $this->assertTrue(array_key_exists("duration", (array)$response)); } From 58923a550ba09a4f1e4ae3a86733f03a0a05b35f Mon Sep 17 00:00:00 2001 From: Aditya Alif Nugraha Date: Mon, 6 Oct 2025 10:13:17 +0200 Subject: [PATCH 8/8] Fix query threads tests (#146) --- tests/integration/IntegrationTest.php | 68 ++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/tests/integration/IntegrationTest.php b/tests/integration/IntegrationTest.php index e86b419..f9ff52d 100644 --- a/tests/integration/IntegrationTest.php +++ b/tests/integration/IntegrationTest.php @@ -1550,9 +1550,17 @@ public function testExportUsers() public function testQueryThreadsWithFilter() { + // Create a fresh channel with both users as members + $testChannel = $this->client->Channel( + "messaging", + $this->generateGuid(), + ["test" => true, "language" => "php"] + ); + $testChannel->create($this->user1["id"], [$this->user1["id"], $this->user2["id"]]); + // Create a thread by sending a message with a parent_id - $parentMessage = $this->channel->sendMessage(["text" => "Parent message"], $this->user1["id"]); - $threadMessage = $this->channel->sendMessage( + $parentMessage = $testChannel->sendMessage(["text" => "Parent message"], $this->user1["id"]); + $threadMessage = $testChannel->sendMessage( ["text" => "Thread message", "parent_id" => $parentMessage["message"]["id"]], $this->user2["id"] ); @@ -1567,19 +1575,30 @@ public function testQueryThreadsWithFilter() // Verify the response $this->assertTrue(array_key_exists("threads", (array)$response)); $this->assertGreaterThanOrEqual(1, count($response["threads"])); + + // Cleanup + $testChannel->delete(); } public function testQueryThreadsWithSort() { + // Create a fresh channel with both users as members + $testChannel = $this->client->Channel( + "messaging", + $this->generateGuid(), + ["test" => true, "language" => "php"] + ); + $testChannel->create($this->user1["id"], [$this->user1["id"], $this->user2["id"]]); + // Create multiple threads - $parentMessage1 = $this->channel->sendMessage(["text" => "Parent message 1"], $this->user1["id"]); - $threadMessage1 = $this->channel->sendMessage( + $parentMessage1 = $testChannel->sendMessage(["text" => "Parent message 1"], $this->user1["id"]); + $threadMessage1 = $testChannel->sendMessage( ["text" => "Thread message 1", "parent_id" => $parentMessage1["message"]["id"]], $this->user2["id"] ); - $parentMessage2 = $this->channel->sendMessage(["text" => "Parent message 2"], $this->user1["id"]); - $threadMessage2 = $this->channel->sendMessage( + $parentMessage2 = $testChannel->sendMessage(["text" => "Parent message 2"], $this->user1["id"]); + $threadMessage2 = $testChannel->sendMessage( ["text" => "Thread message 2", "parent_id" => $parentMessage2["message"]["id"]], $this->user2["id"] ); @@ -1594,19 +1613,30 @@ public function testQueryThreadsWithSort() // Verify the response $this->assertTrue(array_key_exists("threads", (array)$response)); $this->assertGreaterThanOrEqual(2, count($response["threads"])); + + // Cleanup + $testChannel->delete(); } public function testQueryThreadsWithFilterAndSort() { + // Create a fresh channel with both users as members + $testChannel = $this->client->Channel( + "messaging", + $this->generateGuid(), + ["test" => true, "language" => "php"] + ); + $testChannel->create($this->user1["id"], [$this->user1["id"], $this->user2["id"]]); + // Create multiple threads - $parentMessage1 = $this->channel->sendMessage(["text" => "Parent message 1"], $this->user1["id"]); - $threadMessage1 = $this->channel->sendMessage( + $parentMessage1 = $testChannel->sendMessage(["text" => "Parent message 1"], $this->user1["id"]); + $threadMessage1 = $testChannel->sendMessage( ["text" => "Thread message 1", "parent_id" => $parentMessage1["message"]["id"]], $this->user2["id"] ); - $parentMessage2 = $this->channel->sendMessage(["text" => "Parent message 2"], $this->user1["id"]); - $threadMessage2 = $this->channel->sendMessage( + $parentMessage2 = $testChannel->sendMessage(["text" => "Parent message 2"], $this->user1["id"]); + $threadMessage2 = $testChannel->sendMessage( ["text" => "Thread message 2", "parent_id" => $parentMessage2["message"]["id"]], $this->user2["id"] ); @@ -1621,13 +1651,24 @@ public function testQueryThreadsWithFilterAndSort() // Verify the response $this->assertTrue(array_key_exists("threads", (array)$response)); $this->assertGreaterThanOrEqual(2, count($response["threads"])); + + // Cleanup + $testChannel->delete(); } public function testQueryThreadsWithoutFilterAndSort() { + // Create a fresh channel with both users as members + $testChannel = $this->client->Channel( + "messaging", + $this->generateGuid(), + ["test" => true, "language" => "php"] + ); + $testChannel->create($this->user1["id"], [$this->user1["id"], $this->user2["id"]]); + // Create a thread by sending a message with a parent_id - $parentMessage = $this->channel->sendMessage(["text" => "Parent message for no filter test"], $this->user1["id"]); - $threadMessage = $this->channel->sendMessage( + $parentMessage = $testChannel->sendMessage(["text" => "Parent message for no filter test"], $this->user1["id"]); + $threadMessage = $testChannel->sendMessage( ["text" => "Thread message for no filter test", "parent_id" => $parentMessage["message"]["id"]], $this->user2["id"] ); @@ -1642,6 +1683,9 @@ public function testQueryThreadsWithoutFilterAndSort() // Verify the response $this->assertTrue(array_key_exists("threads", (array)$response)); $this->assertGreaterThanOrEqual(1, count($response["threads"])); + + // Cleanup + $testChannel->delete(); } public function testCreateDraft()