Skip to content

Commit d0246a4

Browse files
[CHA-13] Pinning / archiving / partial channel member update (#126)
* feat: pin / unpin channels * feat: test php v8.4 as well * fix: linting * fix: disable 8.4 for now * fix: debugging * feat: changed ci * feat: add test * fix: test * feat: added updateMemberPartial * feat: adding test
1 parent adce931 commit d0246a4

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ jobs:
1313
strategy:
1414
max-parallel: 1
1515
matrix:
16-
php-versions: ['8.1', '8.2', '8.3']
16+
php-versions: ['8.1', '8.2', '8.3', '8.4']
1717
steps:
1818
- name: Checkout
1919
uses: actions/checkout@v3
2020
with:
2121
fetch-depth: 0 # gives the commit message linter access to all previous commits
2222

23-
- name: Commit lint
24-
if: ${{ matrix.php-versions == '8.1' }}
25-
uses: wagoid/commitlint-github-action@v4
26-
2723
- name: Setup PHP
2824
uses: shivammathur/setup-php@v2
2925
with:

lib/GetStream/StreamChat/Channel.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ public function unmute(string $userId): StreamResponse
546546
}
547547

548548
/** Pins the channel for the user.
549+
* @link https://getstream.io/chat/docs/php/channel_update/#pinning-a-channel?language=php
549550
* @throws StreamException
550551
*/
551552
public function pin(string $userId): StreamResponse
@@ -565,6 +566,7 @@ public function pin(string $userId): StreamResponse
565566

566567

567568
/** Unpins the channel for the user.
569+
* @link https://getstream.io/chat/docs/php/channel_update/#pinning-a-channel?language=php
568570
* @throws StreamException
569571
*/
570572
public function unpin(string $userId): StreamResponse
@@ -583,6 +585,7 @@ public function unpin(string $userId): StreamResponse
583585
}
584586

585587
/** Archives the channel for the user.
588+
* @link https://getstream.io/chat/docs/php/channel_update/#archiving-a-channel?language=php
586589
* @throws StreamException
587590
*/
588591
public function archive(string $userId): StreamResponse
@@ -601,6 +604,7 @@ public function archive(string $userId): StreamResponse
601604
}
602605

603606
/** Unarchives the channel for the user.
607+
* @link https://getstream.io/chat/docs/php/channel_update/#archiving-a-channel?language=php
604608
* @throws StreamException
605609
*/
606610
public function unarchive(string $userId): StreamResponse
@@ -617,4 +621,23 @@ public function unarchive(string $userId): StreamResponse
617621

618622
return $this->client->patch($this->getUrl() . "/member/" . urlencode($userId), $payload);
619623
}
624+
625+
/** Update channel member partially.
626+
* @link https://getstream.io/chat/docs/php/channel_member/#update-channel-members?language=php
627+
* @throws StreamException
628+
*/
629+
public function updateMemberPartial(string $userId, ?array $set = null, ?array $unset = null): StreamResponse
630+
{
631+
if (empty($userId)) {
632+
throw new StreamException("user ID must be not empty");
633+
}
634+
if ($set === null && $unset === null) {
635+
throw new StreamException("set or unset is required");
636+
}
637+
$update = [
638+
"set" => $set,
639+
"unset" => $unset
640+
];
641+
return $this->client->patch($this->getUrl() . "/member/" . urlencode($userId), $update);
642+
}
620643
}

tests/integration/IntegrationTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,4 +1300,87 @@ public function testUnreadCountsBatch()
13001300
$this->assertNotEmpty($resp["counts_by_user"][$this->user1["id"]]["total_unread_threads_count"]);
13011301
$this->assertEquals(1, $resp["counts_by_user"][$this->user1["id"]]["total_unread_threads_count"]);
13021302
}
1303+
1304+
public function testChannelPin()
1305+
{
1306+
$this->channel->addMembers([$this->user1["id"]]);
1307+
$this->channel->addMembers([$this->user2["id"]]);
1308+
1309+
// Pin the channel
1310+
$now = new \DateTime();
1311+
$response = $this->channel->pin($this->user1["id"]);
1312+
$this->assertNotNull($response["channel_member"]["pinned_at"]);
1313+
$this->assertGreaterThanOrEqual($now->getTimestamp(), strtotime($response["channel_member"]["pinned_at"]));
1314+
1315+
// Query for pinned channel
1316+
$response = $this->client->queryChannels([
1317+
"pinned" => true,
1318+
"cid" => $this->channel->getCID(),
1319+
], null, [
1320+
"user_id" => $this->user1["id"]
1321+
]);
1322+
$this->assertCount(1, $response["channels"]);
1323+
$this->assertEquals($this->channel->getCID(), $response["channels"][0]["channel"]["cid"]);
1324+
1325+
// Unpin the channel
1326+
$response = $this->channel->unpin($this->user1["id"]);
1327+
$this->assertArrayNotHasKey("pinned_at", $response["channel_member"]);
1328+
1329+
// Query for unpinned channel
1330+
$response = $this->client->queryChannels([
1331+
"pinned" => false,
1332+
"cid" => $this->channel->getCID(),
1333+
], null, [
1334+
"user_id" => $this->user1["id"]
1335+
]);
1336+
$this->assertCount(1, $response["channels"]);
1337+
$this->assertEquals($this->channel->getCID(), $response["channels"][0]["channel"]["cid"]);
1338+
}
1339+
1340+
public function testChannelArchive()
1341+
{
1342+
$this->channel->addMembers([$this->user1["id"]]);
1343+
$this->channel->addMembers([$this->user2["id"]]);
1344+
1345+
// Archive the channel
1346+
$now = new \DateTime();
1347+
$response = $this->channel->archive($this->user1["id"]);
1348+
$this->assertNotNull($response["channel_member"]["archived_at"]);
1349+
$this->assertGreaterThanOrEqual($now->getTimestamp(), strtotime($response["channel_member"]["archived_at"]));
1350+
1351+
// Query for archived channel
1352+
$response = $this->client->queryChannels([
1353+
"archived" => true,
1354+
"cid" => $this->channel->getCID(),
1355+
], null, [
1356+
"user_id" => $this->user1["id"]
1357+
]);
1358+
$this->assertCount(1, $response["channels"]);
1359+
$this->assertEquals($this->channel->getCID(), $response["channels"][0]["channel"]["cid"]);
1360+
1361+
// Unarchive the channel
1362+
$response = $this->channel->unarchive($this->user1["id"]);
1363+
$this->assertArrayNotHasKey("archived_at", $response["channel_member"]);
1364+
1365+
// Query for unarchived channel
1366+
$response = $this->client->queryChannels([
1367+
"archived" => false,
1368+
"cid" => $this->channel->getCID(),
1369+
], null, [
1370+
"user_id" => $this->user1["id"]
1371+
]);
1372+
$this->assertCount(1, $response["channels"]);
1373+
$this->assertEquals($this->channel->getCID(), $response["channels"][0]["channel"]["cid"]);
1374+
}
1375+
1376+
public function testChannelUpdateMemberPartial()
1377+
{
1378+
$this->channel->addMembers([$this->user1["id"]]);
1379+
$response = $this->channel->updateMemberPartial($this->user1["id"], ["hat" => "blue"]);
1380+
$this->assertEquals("blue", $response["channel_member"]["hat"]);
1381+
1382+
$response = $this->channel->updateMemberPartial($this->user1["id"], ["color" => "red"], ["hat"]);
1383+
$this->assertEquals("red", $response["channel_member"]["color"]);
1384+
$this->assertArrayNotHasKey("hat", $response["channel_member"]);
1385+
}
13031386
}

0 commit comments

Comments
 (0)