Skip to content

Commit 4fadc8b

Browse files
[CHA-794] Add Query Threads (#138)
* Add Query Threads --------- Co-authored-by: Lennart <[email protected]>
1 parent 33ad9a6 commit 4fadc8b

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

lib/GetStream/StreamChat/Client.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,43 @@ public function queryChannels(array $filterConditions, ?array $sort = null, ?arr
886886
return $this->post("channels", $options);
887887
}
888888

889+
/** Queries threads.
890+
* You can query threads based on built-in fields as well as any custom field you add to threads.
891+
* Multiple filters can be combined, each filter can use its comparison (equality, inequality, greater than, greater or equal, etc.).
892+
* You can find the complete list of supported operators in the query syntax section of the docs.
893+
* @link https://getstream.io/chat/docs/php/threads/#filtering-and-sorting-threads
894+
* @throws StreamException
895+
*/
896+
public function queryThreads(array $filter, ?array $sort = null, ?array $options = null): StreamResponse
897+
{
898+
if ($options === null) {
899+
$options = [];
900+
}
901+
902+
$sortFields = [];
903+
if ($sort !== null) {
904+
foreach ($sort as $k => $v) {
905+
$sortFields[] = ["field" => $k, "direction" => $v];
906+
}
907+
}
908+
909+
910+
if (!empty($filter)) {
911+
$filterObject = (object)$filter;
912+
$options["filter"] = $filterObject;
913+
} else {
914+
$options["filter"] = null;
915+
}
916+
917+
if (!empty($sortFields)) {
918+
$options["sort"] = $sortFields;
919+
} else {
920+
$options["sort"] = null;
921+
}
922+
923+
return $this->post("threads", $options);
924+
}
925+
889926
/** Creates a channel type.
890927
* @link https://getstream.io/chat/docs/php/channel_features/?language=php
891928
* @throws StreamException

tests/integration/IntegrationTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,102 @@ public function testExportUsers()
15301530
$this->assertSame($response["status"], "completed");
15311531
}
15321532

1533+
public function testQueryThreadsWithFilter()
1534+
{
1535+
// Create a thread by sending a message with a parent_id
1536+
$parentMessage = $this->channel->sendMessage(["text" => "Parent message"], $this->user1["id"]);
1537+
$threadMessage = $this->channel->sendMessage(
1538+
["text" => "Thread message", "parent_id" => $parentMessage["message"]["id"]],
1539+
$this->user2["id"]
1540+
);
1541+
1542+
// Query threads with filter
1543+
$response = $this->client->queryThreads(
1544+
["parent_message_id" => ['$eq' => $parentMessage["message"]["id"]]],
1545+
null,
1546+
["user_id" => $this->user1["id"]]
1547+
);
1548+
1549+
// Verify the response
1550+
$this->assertTrue(array_key_exists("threads", (array)$response));
1551+
$this->assertGreaterThanOrEqual(1, count($response["threads"]));
1552+
}
1553+
1554+
public function testQueryThreadsWithSort()
1555+
{
1556+
// Create multiple threads
1557+
$parentMessage1 = $this->channel->sendMessage(["text" => "Parent message 1"], $this->user1["id"]);
1558+
$threadMessage1 = $this->channel->sendMessage(
1559+
["text" => "Thread message 1", "parent_id" => $parentMessage1["message"]["id"]],
1560+
$this->user2["id"]
1561+
);
1562+
1563+
$parentMessage2 = $this->channel->sendMessage(["text" => "Parent message 2"], $this->user1["id"]);
1564+
$threadMessage2 = $this->channel->sendMessage(
1565+
["text" => "Thread message 2", "parent_id" => $parentMessage2["message"]["id"]],
1566+
$this->user2["id"]
1567+
);
1568+
1569+
// Query threads with sort
1570+
$response = $this->client->queryThreads(
1571+
[],
1572+
["created_at" => -1],
1573+
["user_id" => $this->user1["id"]]
1574+
);
1575+
1576+
// Verify the response
1577+
$this->assertTrue(array_key_exists("threads", (array)$response));
1578+
$this->assertGreaterThanOrEqual(2, count($response["threads"]));
1579+
}
1580+
1581+
public function testQueryThreadsWithFilterAndSort()
1582+
{
1583+
// Create multiple threads
1584+
$parentMessage1 = $this->channel->sendMessage(["text" => "Parent message 1"], $this->user1["id"]);
1585+
$threadMessage1 = $this->channel->sendMessage(
1586+
["text" => "Thread message 1", "parent_id" => $parentMessage1["message"]["id"]],
1587+
$this->user2["id"]
1588+
);
1589+
1590+
$parentMessage2 = $this->channel->sendMessage(["text" => "Parent message 2"], $this->user1["id"]);
1591+
$threadMessage2 = $this->channel->sendMessage(
1592+
["text" => "Thread message 2", "parent_id" => $parentMessage2["message"]["id"]],
1593+
$this->user2["id"]
1594+
);
1595+
1596+
// Query threads with both filter and sort
1597+
$response = $this->client->queryThreads(
1598+
["created_by_user_id" => ['$eq' => $this->user2["id"]]],
1599+
["created_at" => -1],
1600+
["user_id" => $this->user1["id"]]
1601+
);
1602+
1603+
// Verify the response
1604+
$this->assertTrue(array_key_exists("threads", (array)$response));
1605+
$this->assertGreaterThanOrEqual(2, count($response["threads"]));
1606+
}
1607+
1608+
public function testQueryThreadsWithoutFilterAndSort()
1609+
{
1610+
// Create a thread by sending a message with a parent_id
1611+
$parentMessage = $this->channel->sendMessage(["text" => "Parent message for no filter test"], $this->user1["id"]);
1612+
$threadMessage = $this->channel->sendMessage(
1613+
["text" => "Thread message for no filter test", "parent_id" => $parentMessage["message"]["id"]],
1614+
$this->user2["id"]
1615+
);
1616+
1617+
// Query threads without filter and sort parameters
1618+
$response = $this->client->queryThreads(
1619+
[], // Empty filter
1620+
null, // No sort
1621+
["user_id" => $this->user1["id"]] // Only providing user_id in options
1622+
);
1623+
1624+
// Verify the response
1625+
$this->assertTrue(array_key_exists("threads", (array)$response));
1626+
$this->assertGreaterThanOrEqual(1, count($response["threads"]));
1627+
}
1628+
15331629
public function testCreateDraft()
15341630
{
15351631
$message = ["text" => "This is a draft message"];

0 commit comments

Comments
 (0)