Skip to content

Commit 3aa4b6c

Browse files
feat: implemented message reminders feature
1 parent d3e6bff commit 3aa4b6c

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed

lib/GetStream/StreamChat/Client.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,4 +1598,73 @@ public function unreadCountsBatch(array $userIds): StreamResponse
15981598
{
15991599
return $this->post("unread_batch", ["user_ids" => $userIds]);
16001600
}
1601+
1602+
/**
1603+
* Creates a reminder for a message.
1604+
*
1605+
* @param string $messageId The ID of the message to create a reminder for
1606+
* @param string $userId The ID of the user creating the reminder
1607+
* @param DateTime|null $remindAt When to remind the user (optional)
1608+
* @return StreamResponse API response
1609+
* @throws StreamException
1610+
*/
1611+
public function createReminder(string $messageId, string $userId, ?DateTime $remindAt = null): StreamResponse
1612+
{
1613+
$data = ['user_id' => $userId];
1614+
if ($remindAt instanceof DateTime) {
1615+
$data['remind_at'] = $remindAt->format(DateTime::RFC3339);
1616+
}
1617+
return $this->post("messages/{$messageId}/reminders", $data);
1618+
}
1619+
1620+
/**
1621+
* Updates a reminder for a message.
1622+
*
1623+
* @param string $messageId The ID of the message with the reminder
1624+
* @param string $userId The ID of the user who owns the reminder
1625+
* @param DateTime|null $remindAt When to remind the user (optional)
1626+
* @return StreamResponse API response
1627+
* @throws StreamException
1628+
*/
1629+
public function updateReminder(string $messageId, string $userId, ?DateTime $remindAt = null): StreamResponse
1630+
{
1631+
$data = ['user_id' => $userId];
1632+
if ($remindAt instanceof DateTime) {
1633+
$data['remind_at'] = $remindAt->format(DateTime::RFC3339);
1634+
}
1635+
return $this->patch("messages/{$messageId}/reminders", $data);
1636+
}
1637+
1638+
/**
1639+
* Deletes a reminder for a message.
1640+
*
1641+
* @param string $messageId The ID of the message with the reminder
1642+
* @param string $userId The ID of the user who owns the reminder
1643+
* @return StreamResponse API response
1644+
* @throws StreamException
1645+
*/
1646+
public function deleteReminder(string $messageId, string $userId): StreamResponse
1647+
{
1648+
return $this->delete("messages/{$messageId}/reminders", ['user_id' => $userId]);
1649+
}
1650+
1651+
/**
1652+
* Queries reminders based on filter conditions.
1653+
*
1654+
* @param string $userId The ID of the user whose reminders to query
1655+
* @param array $filterConditions Conditions to filter reminders
1656+
* @param array|null $sort Sort parameters (default: [['field' => 'remind_at', 'direction' => 1]])
1657+
* @param array $options Additional query options like limit, offset
1658+
* @return StreamResponse API response with reminders
1659+
* @throws StreamException
1660+
*/
1661+
public function queryReminders(string $userId, array $filterConditions = [], ?array $sort = null, array $options = []): StreamResponse
1662+
{
1663+
$params = array_merge($options, [
1664+
'filter_conditions' => $filterConditions,
1665+
'sort' => $sort ?? [['field' => 'remind_at', 'direction' => 1]],
1666+
'user_id' => $userId
1667+
]);
1668+
return $this->post('reminders/query', $params);
1669+
}
16011670
}

tests/integration/ReminderTest.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
declare(strict_types=0);
4+
5+
namespace GetStream\Integration;
6+
7+
use DateTime;
8+
use GetStream\StreamChat\Client;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class ReminderTest extends TestCase
12+
{
13+
/**
14+
* @var Client
15+
*/
16+
protected $client;
17+
18+
/**
19+
* @var array
20+
*/
21+
protected $user;
22+
23+
/**
24+
* @var \GetStream\StreamChat\Channel
25+
*/
26+
protected $channel;
27+
28+
/**
29+
* @var string
30+
*/
31+
protected $messageId;
32+
33+
protected function setUp(): void
34+
{
35+
// Set the base URL environment variable if STREAM_HOST is provided
36+
$baseURL = getenv('STREAM_HOST');
37+
if ($baseURL) {
38+
putenv("STREAM_BASE_CHAT_URL={$baseURL}");
39+
}
40+
41+
$this->client = new Client(getenv('STREAM_KEY'), getenv('STREAM_SECRET'));
42+
$this->user = $this->getUser();
43+
$this->channel = $this->getChannel();
44+
45+
// Create a message to use for reminders
46+
$message = [
47+
'text' => 'This is a test message for reminders'
48+
];
49+
$response = $this->channel->sendMessage($message, $this->user['id']);
50+
$this->messageId = $response['message']['id'];
51+
}
52+
53+
protected function tearDown(): void
54+
{
55+
try {
56+
$this->channel->delete();
57+
$this->client->deleteUser($this->user['id'], ["user" => "hard", "messages" => "hard"]);
58+
} catch (\Exception $e) {
59+
// We don't care about cleanup errors
60+
}
61+
}
62+
63+
private function getUser(): array
64+
{
65+
$userId = 'reminder-test-user-' . uniqid();
66+
$user = [
67+
'id' => $userId,
68+
'name' => 'Reminder Test User',
69+
];
70+
$this->client->upsertUser($user);
71+
return $user;
72+
}
73+
74+
public function getChannel(): \GetStream\StreamChat\Channel
75+
{
76+
$channelId = 'reminder-test-channel-' . uniqid();
77+
$channel = $this->client->Channel('messaging', $channelId);
78+
$channel->create($this->user['id']);
79+
return $channel;
80+
}
81+
82+
public function testCreateReminder()
83+
{
84+
$remindAt = new DateTime('+1 day');
85+
$response = $this->client->createReminder($this->messageId, $this->user['id'], $remindAt);
86+
87+
$this->assertArrayHasKey('reminder', $response);
88+
$this->assertEquals($this->messageId, $response['reminder']['message_id']);
89+
$this->assertEquals($this->user['id'], $response['reminder']['user_id']);
90+
$this->assertNotEmpty($response['reminder']['remind_at']);
91+
}
92+
93+
public function testCreateReminderWithoutRemindAt()
94+
{
95+
$response = $this->client->createReminder($this->messageId, $this->user['id']);
96+
97+
$this->assertArrayHasKey('reminder', $response);
98+
$this->assertEquals($this->messageId, $response['reminder']['message_id']);
99+
$this->assertEquals($this->user['id'], $response['reminder']['user_id']);
100+
}
101+
102+
public function testUpdateReminder()
103+
{
104+
// First create a reminder
105+
$this->client->createReminder($this->messageId, $this->user['id']);
106+
107+
// Then update it
108+
$newRemindAt = new DateTime('+2 days');
109+
$response = $this->client->updateReminder($this->messageId, $this->user['id'], $newRemindAt);
110+
111+
$this->assertArrayHasKey('reminder', $response);
112+
$this->assertEquals($this->messageId, $response['reminder']['message_id']);
113+
$this->assertEquals($this->user['id'], $response['reminder']['user_id']);
114+
$this->assertNotEmpty($response['reminder']['remind_at']);
115+
}
116+
117+
public function testDeleteReminder()
118+
{
119+
// First create a reminder
120+
$this->client->createReminder($this->messageId, $this->user['id']);
121+
122+
// Then delete it
123+
$response = $this->client->deleteReminder($this->messageId, $this->user['id']);
124+
125+
// The response is a StreamResponse object, so we'll just check that it exists
126+
$this->assertNotNull($response);
127+
$this->assertTrue(true); // If we got here, the test passed
128+
}
129+
130+
public function testQueryReminders()
131+
{
132+
// Create a reminder
133+
$remindAt = new DateTime('+1 day');
134+
$this->client->createReminder($this->messageId, $this->user['id'], $remindAt);
135+
136+
// Query reminders
137+
$response = $this->client->queryReminders($this->user['id']);
138+
139+
$this->assertArrayHasKey('reminders', $response);
140+
$this->assertGreaterThan(0, count($response['reminders']));
141+
142+
// Test with filter conditions
143+
$filterConditions = [
144+
'message_id' => $this->messageId
145+
];
146+
$response = $this->client->queryReminders($this->user['id'], $filterConditions);
147+
148+
$this->assertArrayHasKey('reminders', $response);
149+
$this->assertGreaterThan(0, count($response['reminders']));
150+
$this->assertEquals($this->messageId, $response['reminders'][0]['message_id']);
151+
}
152+
}

0 commit comments

Comments
 (0)