Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHA-13] Pinning / archiving / partial channel member update #126

Merged
merged 11 commits into from
Dec 5, 2024
6 changes: 1 addition & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ jobs:
strategy:
max-parallel: 1
matrix:
php-versions: ['8.1', '8.2', '8.3']
php-versions: ['8.1', '8.2', '8.3', '8.4']
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # gives the commit message linter access to all previous commits

- name: Commit lint
if: ${{ matrix.php-versions == '8.1' }}
uses: wagoid/commitlint-github-action@v4

totalimmersion marked this conversation as resolved.
Show resolved Hide resolved
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand Down
23 changes: 23 additions & 0 deletions lib/GetStream/StreamChat/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ public function unmute(string $userId): StreamResponse
}

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


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

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

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

return $this->client->patch($this->getUrl() . "/member/" . urlencode($userId), $payload);
}

/** Update channel member partially.
* @link https://getstream.io/chat/docs/php/channel_member/#update-channel-members?language=php
* @throws StreamException
*/
public function updateMemberPartial(string $userId, ?array $set = null, ?array $unset = null): StreamResponse
{
if (empty($userId)) {
throw new StreamException("user ID must be not empty");
}
if ($set === null && $unset === null) {
throw new StreamException("set or unset is required");
}
$update = [
"set" => $set,
"unset" => $unset
];
return $this->client->patch($this->getUrl() . "/member/" . urlencode($userId), $update);
}
}
83 changes: 83 additions & 0 deletions tests/integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1300,4 +1300,87 @@ public function testUnreadCountsBatch()
$this->assertNotEmpty($resp["counts_by_user"][$this->user1["id"]]["total_unread_threads_count"]);
$this->assertEquals(1, $resp["counts_by_user"][$this->user1["id"]]["total_unread_threads_count"]);
}

public function testChannelPin()
{
$this->channel->addMembers([$this->user1["id"]]);
$this->channel->addMembers([$this->user2["id"]]);

// Pin the channel
$now = new \DateTime();
$response = $this->channel->pin($this->user1["id"]);
$this->assertNotNull($response["channel_member"]["pinned_at"]);
$this->assertGreaterThanOrEqual($now->getTimestamp(), strtotime($response["channel_member"]["pinned_at"]));

// Query for pinned channel
$response = $this->client->queryChannels([
"pinned" => true,
"cid" => $this->channel->getCID(),
], null, [
"user_id" => $this->user1["id"]
]);
$this->assertCount(1, $response["channels"]);
$this->assertEquals($this->channel->getCID(), $response["channels"][0]["channel"]["cid"]);

// Unpin the channel
$response = $this->channel->unpin($this->user1["id"]);
$this->assertArrayNotHasKey("pinned_at", $response["channel_member"]);

// Query for unpinned channel
$response = $this->client->queryChannels([
"pinned" => false,
"cid" => $this->channel->getCID(),
], null, [
"user_id" => $this->user1["id"]
]);
$this->assertCount(1, $response["channels"]);
$this->assertEquals($this->channel->getCID(), $response["channels"][0]["channel"]["cid"]);
}

public function testChannelArchive()
{
$this->channel->addMembers([$this->user1["id"]]);
$this->channel->addMembers([$this->user2["id"]]);

// Archive the channel
$now = new \DateTime();
$response = $this->channel->archive($this->user1["id"]);
$this->assertNotNull($response["channel_member"]["archived_at"]);
$this->assertGreaterThanOrEqual($now->getTimestamp(), strtotime($response["channel_member"]["archived_at"]));

// Query for archived channel
$response = $this->client->queryChannels([
"archived" => true,
"cid" => $this->channel->getCID(),
], null, [
"user_id" => $this->user1["id"]
]);
$this->assertCount(1, $response["channels"]);
$this->assertEquals($this->channel->getCID(), $response["channels"][0]["channel"]["cid"]);

// Unarchive the channel
$response = $this->channel->unarchive($this->user1["id"]);
$this->assertArrayNotHasKey("archived_at", $response["channel_member"]);

// Query for unarchived channel
$response = $this->client->queryChannels([
"archived" => false,
"cid" => $this->channel->getCID(),
], null, [
"user_id" => $this->user1["id"]
]);
$this->assertCount(1, $response["channels"]);
$this->assertEquals($this->channel->getCID(), $response["channels"][0]["channel"]["cid"]);
}

public function testChannelUpdateMemberPartial()
{
$this->channel->addMembers([$this->user1["id"]]);
$response = $this->channel->updateMemberPartial($this->user1["id"], ["hat" => "blue"]);
$this->assertEquals("blue", $response["channel_member"]["hat"]);

$response = $this->channel->updateMemberPartial($this->user1["id"], ["color" => "red"], ["hat"]);
$this->assertEquals("red", $response["channel_member"]["color"]);
$this->assertArrayNotHasKey("hat", $response["channel_member"]);
}
}
Loading