Skip to content

Commit ba99639

Browse files
authored
Merge pull request #19 from GetStream/query-members
add queryMembers implementation
2 parents bd059ca + f62fe75 commit ba99639

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/GetStream/StreamChat/Channel.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,37 @@ public function query($options)
182182
return $state;
183183
}
184184

185+
/**
186+
* @param array $filterConditions
187+
* @param array $sort
188+
* @param array $options
189+
* @return mixed
190+
* @throws StreamException
191+
*/
192+
public function queryMembers($filterConditions, $sort = null, $options = null)
193+
{
194+
if ($options === null) {
195+
$options = array();
196+
}
197+
$sortFields = [];
198+
if ($sort !== null) {
199+
foreach ($sort as $k => $v) {
200+
$sortFields[] = ["field" => $k, "direction" => $v];
201+
}
202+
}
203+
204+
if ($this->id !== null) {
205+
$options["id"] = $this->id;
206+
} else if ($this->customData !== null && is_array($this->customData["members"])) {
207+
// member based channel aka distinct channel
208+
$options["members"] = $this->customData["members"];
209+
}
210+
$options["type"] = $this->channelType;
211+
$options["filter_conditions"] = $filterConditions;
212+
$options["sort"] = $sortFields;
213+
return $this->client->get("members", ["payload" => json_encode($options)]);
214+
}
215+
185216
/**
186217
* @param array $channelData
187218
* @param string $updateMessage

tests/integration/IntegrationTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,52 @@ public function testQueryChannelsMembersIn()
304304
$this->assertSame(count($response["channels"][0]["members"]), 9);
305305
}
306306

307+
public function testQueryMembers()
308+
{
309+
$bob = ["id" => Uuid::uuid4()->toString(), "name" => "bob the builder"];
310+
$bobSponge = ["id" => Uuid::uuid4()->toString(), "name" => "bob the sponge"];
311+
$this->client->updateUsers([$bob, $bobSponge]);
312+
$channel = $this->client->Channel(
313+
"messaging",
314+
Uuid::uuid4()->toString(),
315+
["members" => [$bob["id"], $bobSponge["id"]]]
316+
);
317+
$channel->create($bob["id"]);
318+
319+
$response = $channel->queryMembers(["id" => $bob["id"]]);
320+
$this->assertSame(count($response["members"]), 1);
321+
$this->assertSame($response["members"][0]["user_id"], $bob["id"]);
322+
323+
$response = $channel->queryMembers(["name" => ['$autocomplete' => "bob"]], []);
324+
$this->assertSame(count($response["members"]), 2);
325+
326+
$response = $channel->queryMembers(["name" => ['$autocomplete' => "bob"]], [], ["limit" => 1]);
327+
$this->assertSame(count($response["members"]), 1);
328+
}
329+
330+
public function testQueryMembersMemberBasedChannel()
331+
{
332+
$bob = ["id" => Uuid::uuid4()->toString(), "name" => "bob the builder"];
333+
$bobSponge = ["id" => Uuid::uuid4()->toString(), "name" => "bob the sponge"];
334+
$this->client->updateUsers([$bob, $bobSponge]);
335+
$channel = $this->client->Channel(
336+
"messaging",
337+
null,
338+
["members" => [$bob["id"], $bobSponge["id"]]]
339+
);
340+
$channel->create($bob["id"]);
341+
342+
$response = $channel->queryMembers(["id" => $bob["id"]]);
343+
$this->assertSame(count($response["members"]), 1);
344+
$this->assertSame($response["members"][0]["user_id"], $bob["id"]);
345+
346+
$response = $channel->queryMembers(["name" => ['$autocomplete' => "bob"]], []);
347+
$this->assertSame(count($response["members"]), 2);
348+
349+
$response = $channel->queryMembers(["name" => ['$autocomplete' => "bob"]], [], ["limit" => 1]);
350+
$this->assertSame(count($response["members"]), 1);
351+
}
352+
307353
public function testDevices()
308354
{
309355
$user = $this->getUser();

0 commit comments

Comments
 (0)