Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/GetStream/StreamChat/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,43 @@ public function queryChannels(array $filterConditions, ?array $sort = null, ?arr
return $this->post("channels", $options);
}

/** Queries threads.
* You can query threads based on built-in fields as well as any custom field you add to threads.
* Multiple filters can be combined, each filter can use its comparison (equality, inequality, greater than, greater or equal, etc.).
* You can find the complete list of supported operators in the query syntax section of the docs.
* @link https://getstream.io/chat/docs/php/threads/#filtering-and-sorting-threads
* @throws StreamException
*/
public function queryThreads(array $filter, ?array $sort = null, ?array $options = null): StreamResponse
{
if ($options === null) {
$options = [];
}

$sortFields = [];
if ($sort !== null) {
foreach ($sort as $k => $v) {
$sortFields[] = ["field" => $k, "direction" => $v];
}
}


if (!empty($filter)) {
$filterObject = (object)$filter;
$options["filter"] = $filterObject;
} else {
$options["filter"] = null;
}

if (!empty($sortFields)) {
$options["sort"] = $sortFields;
} else {
$options["sort"] = null;
}

return $this->post("threads", $options);
}

/** Creates a channel type.
* @link https://getstream.io/chat/docs/php/channel_features/?language=php
* @throws StreamException
Expand Down
96 changes: 96 additions & 0 deletions tests/integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,102 @@ public function testExportUsers()
$this->assertSame($response["status"], "completed");
}

public function testQueryThreadsWithFilter()
{
// 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(
["text" => "Thread message", "parent_id" => $parentMessage["message"]["id"]],
$this->user2["id"]
);

// Query threads with filter
$response = $this->client->queryThreads(
["parent_message_id" => ['$eq' => $parentMessage["message"]["id"]]],
null,
["user_id" => $this->user1["id"]]
);

// Verify the response
$this->assertTrue(array_key_exists("threads", (array)$response));
$this->assertGreaterThanOrEqual(1, count($response["threads"]));
}

public function testQueryThreadsWithSort()
{
// Create multiple threads
$parentMessage1 = $this->channel->sendMessage(["text" => "Parent message 1"], $this->user1["id"]);
$threadMessage1 = $this->channel->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(
["text" => "Thread message 2", "parent_id" => $parentMessage2["message"]["id"]],
$this->user2["id"]
);

// Query threads with sort
$response = $this->client->queryThreads(
[],
["created_at" => -1],
["user_id" => $this->user1["id"]]
);

// Verify the response
$this->assertTrue(array_key_exists("threads", (array)$response));
$this->assertGreaterThanOrEqual(2, count($response["threads"]));
}

public function testQueryThreadsWithFilterAndSort()
{
// Create multiple threads
$parentMessage1 = $this->channel->sendMessage(["text" => "Parent message 1"], $this->user1["id"]);
$threadMessage1 = $this->channel->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(
["text" => "Thread message 2", "parent_id" => $parentMessage2["message"]["id"]],
$this->user2["id"]
);

// Query threads with both filter and sort
$response = $this->client->queryThreads(
["created_by_user_id" => ['$eq' => $this->user2["id"]]],
["created_at" => -1],
["user_id" => $this->user1["id"]]
);

// Verify the response
$this->assertTrue(array_key_exists("threads", (array)$response));
$this->assertGreaterThanOrEqual(2, count($response["threads"]));
}

public function testQueryThreadsWithoutFilterAndSort()
{
// 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(
["text" => "Thread message for no filter test", "parent_id" => $parentMessage["message"]["id"]],
$this->user2["id"]
);

// Query threads without filter and sort parameters
$response = $this->client->queryThreads(
[], // Empty filter
null, // No sort
["user_id" => $this->user1["id"]] // Only providing user_id in options
);

// Verify the response
$this->assertTrue(array_key_exists("threads", (array)$response));
$this->assertGreaterThanOrEqual(1, count($response["threads"]));
}

public function testCreateDraft()
{
$message = ["text" => "This is a draft message"];
Expand Down