Skip to content

Commit b519e60

Browse files
authored
Feature: Search improvements (#54)
1 parent 897e26f commit b519e60

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

lib/GetStream/StreamChat/Client.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,13 +877,28 @@ public function search($filterConditions, $query, $options=null)
877877
if ($options === null) {
878878
$options = [];
879879
}
880+
881+
if (array_key_exists('offset', $options) && $options['offset'] > 0) {
882+
if (array_key_exists('next', $options) || array_key_exists('sort', $options)) {
883+
throw new StreamException("Cannot use offset with next or sort parameters");
884+
}
885+
}
886+
880887
$options['filter_conditions'] = $filterConditions;
881888
if (is_string($query)) {
882889
$options['query'] = $query;
883890
} else {
884891
$options['message_filter_conditions'] = $query;
885892
}
886893

894+
$sortFields = [];
895+
if (array_key_exists('sort', $options)) {
896+
$sort = $options['sort'];
897+
foreach ($sort as $k => $v) {
898+
$sortFields[] = ["field" => $k, "direction" => $v];
899+
}
900+
}
901+
$options['sort'] = $sortFields;
887902
return $this->get("search", ["payload" => json_encode($options)]);
888903
}
889904

tests/integration/IntegrationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,55 @@ public function testSearch()
690690
$this->assertSame(count($response['results']), 1);
691691
}
692692

693+
public function testSearchOffsetAndSortFails()
694+
{
695+
$this->expectException(\GetStream\StreamChat\StreamException::class);
696+
$query = "supercalifragilisticexpialidocious";
697+
$this->client->search(
698+
["type" => "messaging"],
699+
$query,
700+
["sort" => [["created_at"=>-1]], "offset" => 1]
701+
);
702+
}
703+
704+
public function testSearchOffsetAndNextFails()
705+
{
706+
$this->expectException(\GetStream\StreamChat\StreamException::class);
707+
$query = "supercalifragilisticexpialidocious";
708+
$this->client->search(
709+
["type" => "messaging"],
710+
$query,
711+
["next" => $query, "offset" => 1]
712+
);
713+
}
714+
715+
716+
public function testSearchWithSort()
717+
{
718+
$this->markTestSkipped();
719+
$user = $this->getUser();
720+
$channel = $this->getChannel();
721+
$query = "supercalifragilisticexpialidocious";
722+
$channel->sendMessage(["text" => "How many syllables are there in " . $query . "?"], $user["id"]);
723+
$channel->sendMessage(["text" => "Does ". $query . " count as one or two?"], $user["id"]);
724+
$response = $this->client->search(
725+
["type" => "messaging"],
726+
$query,
727+
["sort"=> [["created_at"=> -1]], "limit" => 1]
728+
);
729+
// searches all channels so make sure at least one is found
730+
$this->assertTrue(count($response['results']) >= 1);
731+
$this->assertTrue(strpos($response['results'][0]['message']['text'], $query)!==false);
732+
$response = $this->client->search(
733+
["type" => "messaging"],
734+
$query,
735+
["limit" => 1, "next"=> $response['next']]
736+
);
737+
$this->assertTrue(count($response['results']) >= 1);
738+
$this->assertTrue(strpos($response['results'][0]['message']['text'], $query)!==false);
739+
}
740+
741+
693742
public function testGetMessage()
694743
{
695744
$user = $this->getUser();

0 commit comments

Comments
 (0)