Skip to content

Commit 5975dd9

Browse files
gumuzferhatelmas
andauthored
[CHAT-2597] Add shadow_ban, remove_shadow_ban, update_message_partial, pin_message & unpin_message methods (#66)
Co-authored-by: ferhat elmas <elmas.ferhat@gmail.com>
1 parent ceec689 commit 5975dd9

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

lib/GetStream/StreamChat/Client.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,36 @@ public function unbanUser($targetId, $options=null)
499499
return $this->delete("moderation/ban", $options);
500500
}
501501

502+
/**
503+
* @param string $targetId
504+
* @param array $options
505+
* @return mixed
506+
* @throws StreamException
507+
*/
508+
public function shadowBan($targetId, $options=null)
509+
{
510+
if ($options === null) {
511+
$options = [];
512+
}
513+
$options["shadow"] = true;
514+
return $this->banUser($targetId, $options);
515+
}
516+
517+
/**
518+
* @param string $targetId
519+
* @param array $options
520+
* @return mixed
521+
* @throws StreamException
522+
*/
523+
public function removeShadowBan($targetId, $options=null)
524+
{
525+
if ($options === null) {
526+
$options = [];
527+
}
528+
$options["shadow"] = true;
529+
return $this->unbanUser($targetId, $options);
530+
}
531+
502532
/**
503533
* @param string $messageId
504534
* @return mixed
@@ -624,6 +654,60 @@ public function markAllRead($userId)
624654
return $this->post("channels/read", $options);
625655
}
626656

657+
/**
658+
* @param string $messageId
659+
* @param string $userId
660+
* @param int $expiration
661+
* @return mixed
662+
* @throws StreamException
663+
*/
664+
public function pinMessage($messageId, $userId, $expiration=null)
665+
{
666+
$updates = [
667+
"set" => [
668+
"pinned" => true,
669+
"pin_expires" => $expiration,
670+
]
671+
];
672+
return $this->partialUpdateMessage($messageId, $updates, $userId);
673+
}
674+
675+
/**
676+
* @param string $messageId
677+
* @param string $userId
678+
* @return mixed
679+
* @throws StreamException
680+
*/
681+
public function unPinMessage($messageId, $userId)
682+
{
683+
$updates = [
684+
"set" => [
685+
"pinned" => false,
686+
]
687+
];
688+
return $this->partialUpdateMessage($messageId, $updates, $userId);
689+
}
690+
691+
/**
692+
* @param string $messageId
693+
* @param array $updates [set => [key => value], unset => [key]]
694+
* @param string $userId
695+
* @param array $options
696+
* @return mixed
697+
* @throws StreamException
698+
*/
699+
public function partialUpdateMessage($messageId, $updates, $userId=null, $options=null)
700+
{
701+
if ($options === null) {
702+
$options = [];
703+
}
704+
if ($userId !== null) {
705+
$options["user"] = ["id" => $userId];
706+
}
707+
$options = array_merge($options, $updates);
708+
return $this->put("messages/" .$messageId, $options);
709+
}
710+
627711
/**
628712
* @param array $message
629713
* @return mixed

tests/integration/IntegrationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,55 @@ public function testExportUser()
208208
$this->assertSame("Gandalf the Grey", $response["user"]["name"]);
209209
}
210210

211+
public function testShadowban()
212+
{
213+
$user1 = $this->getUser();
214+
$user2 = $this->getUser();
215+
$channel = $this->getChannel();
216+
217+
$response = $channel->sendMessage(["text" => "hello world"], $user1["id"]);
218+
$this->assertFalse($response["message"]["shadowed"]);
219+
$response = $this->client->getMessage($response["message"]["id"]);
220+
$this->assertFalse($response["message"]["shadowed"]);
221+
222+
$this->client->shadowBan($user1["id"], ["user_id" => $user2["id"]]);
223+
224+
$response = $channel->sendMessage(["text" => "hello world"], $user1["id"]);
225+
$this->assertFalse($response["message"]["shadowed"]);
226+
$response = $this->client->getMessage($response["message"]["id"]);
227+
$this->assertTrue($response["message"]["shadowed"]);
228+
229+
$this->client->removeShadowBan($user1["id"], ["user_id" => $user2["id"]]);
230+
231+
$response = $channel->sendMessage(["text" => "hello world"], $user1["id"]);
232+
$this->assertFalse($response["message"]["shadowed"]);
233+
$response = $this->client->getMessage($response["message"]["id"]);
234+
$this->assertFalse($response["message"]["shadowed"]);
235+
}
236+
237+
public function testPinMessage()
238+
{
239+
$user1 = $this->getUser();
240+
$user2 = $this->getUser();
241+
$channel = $this->getChannel();
242+
243+
$response = $channel->sendMessage(["text" => "hello world"], $user1["id"]);
244+
$this->assertNull($response["message"]["pinned_at"]);
245+
$this->assertNull($response["message"]["pinned_by"]);
246+
247+
$this->client->pinMessage($response["message"]["id"], $user2["id"]);
248+
249+
$response = $this->client->getMessage($response["message"]["id"]);
250+
$this->assertNotNull($response["message"]["pinned_at"]);
251+
$this->assertEquals($user2["id"], $response["message"]["pinned_by"]["id"]);
252+
253+
$this->client->unPinMessage($response["message"]["id"], $user2["id"]);
254+
255+
$response = $this->client->getMessage($response["message"]["id"]);
256+
$this->assertNull($response["message"]["pinned_at"]);
257+
$this->assertNull($response["message"]["pinned_by"]);
258+
}
259+
211260
public function testBanUser()
212261
{
213262
$user1 = $this->getUser();

0 commit comments

Comments
 (0)