Skip to content

Commit

Permalink
feat: pin / unpin channels
Browse files Browse the repository at this point in the history
  • Loading branch information
totalimmersion committed Dec 4, 2024
1 parent 1098037 commit 747fb31
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
73 changes: 73 additions & 0 deletions lib/GetStream/StreamChat/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,76 @@ public function unmute(string $userId): StreamResponse
return $this->client->post("moderation/unmute/channel", $postData);
}
}

/** Pins the channel for the user.
* @throws StreamException
*/
public function pin(string $userId): StreamResponse
{
if (empty($userId)) {
throw new StreamException("user ID must be not empty");
}

$payload = [
"set" => [
"pinned" => true
]
];

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


/** Unpins the channel for the user.
* @throws StreamException
*/
public function unpin(string $userId): StreamResponse
{
if (empty($userId)) {
throw new StreamException("user ID must be not empty");
}

$payload = [
"set" => [
"pinned" => false
]
];

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

/** Archives the channel for the user.
* @throws StreamException
*/
public function pin(string $userId): StreamResponse
{
if (empty($userId)) {
throw new StreamException("user ID must be not empty");
}

$payload = [
"set" => [
"archived" => true
]
];

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

/** Unarchives the channel for the user.
* @throws StreamException
*/
public function pin(string $userId): StreamResponse
{
if (empty($userId)) {
throw new StreamException("user ID must be not empty");
}

$payload = [
"set" => [
"archived" => false
]
];

return $this->client->patch($this->getUrl() . "/member/" . urlencode($userId), $payload);
}
42 changes: 42 additions & 0 deletions tests/integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1300,4 +1300,46 @@ 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();
$member = $this->channel->pin($users[0]['id']);
$this->assertNotNull($member->channelMember->pinned_at);
$this->assertGreaterThanOrEqual($now->getTimestamp(), strtotime($member->channelMember->pinned_at));

// Query for pinned channel
$queryChannResp = $client->queryChannels([
'user_id' => $users[0]['id'],
'filter' => [
'pinned' => true,
'cid' => $this->channel->getCID(),
],
]);

$channels = $queryChannResp['channels'];
$this->assertCount(1, $channels);
$this->assertEquals($channels[0]['cid'], $channel->getCID());

// Unpin the channel
$member = $channel->unpin($users[0]['id']);
$this->assertNull($member->channelMember->pinned_at);

// Query for unpinned channel
$queryChannResp = $client->queryChannels([
'user_id' => $users[0]['id'],
'filter' => [
'pinned' => false,
'cid' => $this->channel->getCID(),
],
]);

$channels = $queryChannResp['channels'];
$this->assertCount(1, $channels);
$this->assertEquals($channels[0]['cid'], $channel->getCID());
}
}

0 comments on commit 747fb31

Please sign in to comment.