-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatTestCase.php
More file actions
1431 lines (1264 loc) · 51 KB
/
ChatTestCase.php
File metadata and controls
1431 lines (1264 loc) · 51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace GetStream\Tests\Integration;
use GetStream\Client;
use GetStream\ClientBuilder;
use GetStream\GeneratedModels;
use GetStream\StreamResponse;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
/**
* Base test case for Chat integration tests.
*
* Provides helper methods for creating/cleaning up test users, channels, and messages.
* Follows the patterns established in getstream-go's chat_test_helpers_test.go.
*
* Subclasses can override sharedUserCount() to create shared users once per class
* instead of per-test, dramatically reducing API calls and rate limiting.
*/
#[Group('integration')]
abstract class ChatTestCase extends TestCase
{
protected Client $client;
/** @var string[] User IDs created during the test, cleaned up in tearDown */
protected array $createdUserIDs = [];
/** @var array{type: string, id: string}[] Channels created during the test, cleaned up in tearDown */
protected array $createdChannels = [];
// =========================================================================
// Video API Wrappers
// =========================================================================
/** @var string[] Call type names created during the test, cleaned up in tearDown */
protected array $createdCallTypes = [];
/** @var array{type: string, id: string}[] Calls created during the test, cleaned up in tearDown */
protected array $createdCalls = [];
/** @var string[] External storage names created during the test, cleaned up in tearDown */
protected array $createdExternalStorages = [];
// ------------------------------------------------------------------
// Class-level shared user management (created once per test class)
// ------------------------------------------------------------------
/** @var array<string, Client> Class-level shared client keyed by class name */
private static array $classClients = [];
/** @var array<string, string[]> Class-level shared user IDs keyed by class name */
private static array $classUserIDs = [];
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
$count = static::sharedUserCount();
if ($count <= 0) {
return;
}
$client = ClientBuilder::fromEnv()->build();
self::$classClients[static::class] = $client;
$users = [];
$ids = [];
for ($i = 0; $i < $count; $i++) {
$id = 'test-user-' . uniqid();
$ids[] = $id;
$users[$id] = new GeneratedModels\UserRequest(
id: $id,
name: 'Shared User ' . $id,
role: 'user',
);
}
$client->updateUsers(new GeneratedModels\UpdateUsersRequest(users: $users));
self::$classUserIDs[static::class] = $ids;
}
public static function tearDownAfterClass(): void
{
$class = static::class;
$ids = self::$classUserIDs[$class] ?? [];
$client = self::$classClients[$class] ?? null;
if (!empty($ids) && $client !== null) {
self::deleteUsersHardWithRetry($client, $ids);
}
unset(self::$classClients[$class], self::$classUserIDs[$class]);
parent::tearDownAfterClass();
}
// ------------------------------------------------------------------
// Per-test setup / teardown
// ------------------------------------------------------------------
protected function setUp(): void
{
// Reuse class-level client if available, otherwise create a new one
$this->client = self::$classClients[static::class] ?? ClientBuilder::fromEnv()->build();
}
protected function tearDown(): void
{
// Hard-delete channels first (they reference users)
foreach (array_reverse($this->createdChannels) as $ch) {
try {
$this->deleteChannel($ch['type'], $ch['id'], hardDelete: true);
} catch (\Exception $e) {
// Ignore cleanup errors
}
}
// Hard-delete per-test users (not shared ones)
if (!empty($this->createdUserIDs)) {
$this->deleteUsersWithRetry($this->createdUserIDs);
}
parent::tearDown();
}
/**
* Override to declare how many shared users this test class needs.
* Return 0 (default) to opt-out and create users per-test instead.
*/
protected static function sharedUserCount(): int
{
return 0;
}
/**
* Get the class-level shared user IDs.
*
* @return string[]
*/
protected function getSharedUserIDs(): array
{
return self::$classUserIDs[static::class] ?? [];
}
// =========================================================================
// Test Helpers
// =========================================================================
/**
* Create n test users with unique IDs.
*
* @return string[] Array of created user IDs
*/
protected function createTestUsers(int $n): array
{
$users = [];
$ids = [];
for ($i = 0; $i < $n; $i++) {
$id = 'test-user-' . uniqid();
$ids[] = $id;
$users[$id] = new GeneratedModels\UserRequest(
id: $id,
name: 'Test User ' . $id,
role: 'user',
);
}
$response = $this->client->updateUsers(new GeneratedModels\UpdateUsersRequest(
users: $users,
));
$this->assertResponseSuccess($response, 'create test users');
// Track for cleanup
array_push($this->createdUserIDs, ...$ids);
return $ids;
}
/**
* Create a messaging channel with a unique ID.
*
* @return array{0: string, 1: string} [channelType, channelID]
*/
protected function createTestChannel(string $creatorID): array
{
$channelType = 'messaging';
$channelID = 'test-ch-' . uniqid();
$response = $this->getOrCreateChannel($channelType, $channelID, new GeneratedModels\ChannelGetOrCreateRequest(
data: new GeneratedModels\ChannelInput(
createdByID: $creatorID,
),
));
$this->assertResponseSuccess($response, 'create test channel');
// Track for cleanup
$this->createdChannels[] = ['type' => $channelType, 'id' => $channelID];
return [$channelType, $channelID];
}
/**
* Create a messaging channel with members.
*
* @param string[] $memberIDs
*
* @return array{0: string, 1: string} [channelType, channelID]
*/
protected function createTestChannelWithMembers(string $creatorID, array $memberIDs): array
{
$channelType = 'messaging';
$channelID = 'test-ch-' . uniqid();
$members = array_map(
static fn (string $id) => new GeneratedModels\ChannelMemberRequest(userID: $id),
$memberIDs,
);
$response = $this->getOrCreateChannel($channelType, $channelID, new GeneratedModels\ChannelGetOrCreateRequest(
data: new GeneratedModels\ChannelInput(
createdByID: $creatorID,
members: $members,
),
));
$this->assertResponseSuccess($response, 'create test channel with members');
// Track for cleanup
$this->createdChannels[] = ['type' => $channelType, 'id' => $channelID];
return [$channelType, $channelID];
}
/**
* Send a test message to a channel.
*
* @return string The message ID
*/
protected function sendTestMessage(string $channelType, string $channelID, string $userID, string $text): string
{
$response = $this->sendMessage($channelType, $channelID, new GeneratedModels\SendMessageRequest(
message: new GeneratedModels\MessageRequest(
text: $text,
userID: $userID,
),
));
$this->assertResponseSuccess($response, 'send test message');
$data = $response->getData();
self::assertNotNull($data->message, 'Message should not be null in response');
self::assertNotEmpty($data->message->id, 'Message ID should not be empty');
return $data->message->id;
}
/**
* Retry a callable until it succeeds or max attempts exhausted.
* Useful for eventually consistent API operations.
*
* @template T
*
* @param callable(): T $fn
* @param int $sleepMs milliseconds to wait between attempts
*
* @return T
*/
protected function retryUntilSuccess(callable $fn, int $maxAttempts = 5, int $sleepMs = 500): mixed
{
$lastException = new \RuntimeException('retryUntilSuccess: no attempts made');
for ($i = 0; $i < $maxAttempts; $i++) {
try {
return $fn();
} catch (\Exception $e) {
$lastException = $e;
usleep($sleepMs * 1000);
}
}
throw $lastException;
}
/**
* Delete users with retry logic for rate limiting.
* Delegates to the shared static implementation so both per-test and
* class-level teardowns use the same jitter + backoff strategy.
*
* @param string[] $userIDs
*/
protected function deleteUsersWithRetry(array $userIDs): void
{
self::deleteUsersHardWithRetry($this->client, $userIDs);
}
/**
* Poll an async task until completed or failed.
* Uses adaptive backoff: 100ms → 200ms → 400ms → 800ms → 1s (cap), up to 60 attempts (~60s max).
*/
protected function waitForTask(string $taskID): GeneratedModels\GetTaskResponse
{
$maxAttempts = 60;
for ($i = 0; $i < $maxAttempts; $i++) {
$response = $this->client->getTask($taskID);
$this->assertResponseSuccess($response, 'get task status');
$data = $response->getData();
if ($data->status === 'completed' || $data->status === 'failed') {
return $data;
}
// Adaptive backoff: 100ms, 200ms, 400ms, 800ms, then cap at 1s
$sleepMs = min(100 << min($i, 10), 1000);
usleep($sleepMs * 1000);
}
self::fail("Task {$taskID} did not complete after {$maxAttempts} attempts");
}
/**
* Generate a random alphanumeric string.
*/
protected function randomString(int $length = 12): string
{
return substr(bin2hex(random_bytes((int) ceil($length / 2))), 0, $length);
}
// =========================================================================
// Channel API Wrappers (since PHP SDK lacks a ChatClient)
// These wrap $client->makeRequest() for channel-level operations.
// =========================================================================
/**
* @return StreamResponse<GeneratedModels\ChannelStateResponse>
*/
protected function getOrCreateChannel(string $type, string $id, GeneratedModels\ChannelGetOrCreateRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/{$id}/query", [], $request),
GeneratedModels\ChannelStateResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\SendMessageResponse>
*/
protected function sendMessage(string $type, string $id, GeneratedModels\SendMessageRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/{$id}/message", [], $request),
GeneratedModels\SendMessageResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\DeleteChannelResponse>
*/
protected function deleteChannel(string $type, string $id, bool $hardDelete = false): StreamResponse
{
$queryParams = [];
if ($hardDelete) {
$queryParams['hard_delete'] = 'true';
}
return StreamResponse::fromJson(
$this->client->makeRequest('DELETE', "/api/v2/chat/channels/{$type}/{$id}", $queryParams),
GeneratedModels\DeleteChannelResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\QueryChannelsResponse>
*/
protected function queryChannels(GeneratedModels\QueryChannelsRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/chat/channels', [], $request),
GeneratedModels\QueryChannelsResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UpdateChannelResponse>
*/
protected function updateChannel(string $type, string $id, GeneratedModels\UpdateChannelRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/{$id}", [], $request),
GeneratedModels\UpdateChannelResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UpdateChannelPartialResponse>
*/
protected function updateChannelPartial(string $type, string $id, GeneratedModels\UpdateChannelPartialRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('PATCH', "/api/v2/chat/channels/{$type}/{$id}", [], $request),
GeneratedModels\UpdateChannelPartialResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\DeleteChannelsResponse>
*/
protected function deleteChannelsBatch(GeneratedModels\DeleteChannelsRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/chat/channels/delete', [], $request),
GeneratedModels\DeleteChannelsResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\HideChannelResponse>
*/
protected function hideChannel(string $type, string $id, GeneratedModels\HideChannelRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/{$id}/hide", [], $request),
GeneratedModels\HideChannelResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\ShowChannelResponse>
*/
protected function showChannel(string $type, string $id, GeneratedModels\ShowChannelRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/{$id}/show", [], $request),
GeneratedModels\ShowChannelResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\TruncateChannelResponse>
*/
protected function truncateChannel(string $type, string $id, GeneratedModels\TruncateChannelRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/{$id}/truncate", [], $request),
GeneratedModels\TruncateChannelResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\MarkReadResponse>
*/
protected function markRead(string $type, string $id, GeneratedModels\MarkReadRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/{$id}/read", [], $request),
GeneratedModels\MarkReadResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\Response>
*/
protected function markUnread(string $type, string $id, GeneratedModels\MarkUnreadRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/{$id}/unread", [], $request),
GeneratedModels\Response::class,
);
}
/**
* @return StreamResponse<GeneratedModels\MuteChannelResponse>
*/
protected function muteChannel(GeneratedModels\MuteChannelRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/chat/moderation/mute/channel', [], $request),
GeneratedModels\MuteChannelResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UnmuteResponse>
*/
protected function unmuteChannel(GeneratedModels\UnmuteChannelRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/chat/moderation/unmute/channel', [], $request),
GeneratedModels\UnmuteResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UpdateMemberPartialResponse>
*/
protected function updateMemberPartial(string $type, string $id, string $userID, GeneratedModels\UpdateMemberPartialRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('PATCH', "/api/v2/chat/channels/{$type}/{$id}/member", ['user_id' => $userID], $request),
GeneratedModels\UpdateMemberPartialResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\MembersResponse>
*/
protected function queryMembers(GeneratedModels\QueryMembersPayload $payload): StreamResponse
{
$queryParams = ['payload' => json_encode($payload->toArray())];
return StreamResponse::fromJson(
$this->client->makeRequest('GET', '/api/v2/chat/members', $queryParams),
GeneratedModels\MembersResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\EventResponse>
*/
protected function sendEvent(string $type, string $id, GeneratedModels\SendEventRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/{$id}/event", [], $request),
GeneratedModels\EventResponse::class,
);
}
/**
* Create a distinct channel (without explicit ID).
*
* @return StreamResponse<GeneratedModels\ChannelStateResponse>
*/
protected function getOrCreateDistinctChannel(string $type, GeneratedModels\ChannelGetOrCreateRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/channels/{$type}/query", [], $request),
GeneratedModels\ChannelStateResponse::class,
);
}
// =========================================================================
// Message API Wrappers
// =========================================================================
/**
* @return StreamResponse<GeneratedModels\GetMessageResponse>
*/
protected function getMessage(string $messageID): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', "/api/v2/chat/messages/{$messageID}"),
GeneratedModels\GetMessageResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\GetManyMessagesResponse>
*/
protected function getManyMessages(string $type, string $id, array $messageIDs): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', "/api/v2/chat/channels/{$type}/{$id}/messages", ['ids' => implode(',', $messageIDs)]),
GeneratedModels\GetManyMessagesResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UpdateMessageResponse>
*/
protected function updateMessage(string $messageID, GeneratedModels\UpdateMessageRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/messages/{$messageID}", [], $request),
GeneratedModels\UpdateMessageResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UpdateMessagePartialResponse>
*/
protected function updateMessagePartial(string $messageID, GeneratedModels\UpdateMessagePartialRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('PUT', "/api/v2/chat/messages/{$messageID}", [], $request),
GeneratedModels\UpdateMessagePartialResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\DeleteMessageResponse>
*/
protected function deleteMessageApi(string $messageID, bool $hard = false, ?string $deletedBy = null, bool $deleteForMe = false): StreamResponse
{
$queryParams = [];
if ($hard) {
$queryParams['hard'] = 'true';
}
if ($deletedBy !== null) {
$queryParams['deleted_by'] = $deletedBy;
}
if ($deleteForMe) {
$queryParams['delete_for_me'] = 'true';
}
return StreamResponse::fromJson(
$this->client->makeRequest('DELETE', "/api/v2/chat/messages/{$messageID}", $queryParams),
GeneratedModels\DeleteMessageResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\MessageActionResponse>
*/
protected function translateMessage(string $messageID, GeneratedModels\TranslateMessageRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/messages/{$messageID}/translate", [], $request),
GeneratedModels\MessageActionResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\GetRepliesResponse>
*/
protected function getReplies(string $parentID, array $queryParams = []): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', "/api/v2/chat/messages/{$parentID}/replies", $queryParams),
GeneratedModels\GetRepliesResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\SearchResponse>
*/
protected function searchMessages(GeneratedModels\SearchPayload $payload): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', '/api/v2/chat/search', ['payload' => json_encode($payload->toArray())]),
GeneratedModels\SearchResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\MessageActionResponse>
*/
protected function commitMessage(string $messageID): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/messages/{$messageID}/commit", [], new GeneratedModels\CommitMessageRequest()),
GeneratedModels\MessageActionResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UndeleteMessageResponse>
*/
protected function undeleteMessage(string $messageID, GeneratedModels\UndeleteMessageRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/messages/{$messageID}/undelete", [], $request),
GeneratedModels\UndeleteMessageResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\QueryMessageHistoryResponse>
*/
protected function queryMessageHistory(GeneratedModels\QueryMessageHistoryRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/chat/messages/history', [], $request),
GeneratedModels\QueryMessageHistoryResponse::class,
);
}
// =========================================================================
// Reaction API Wrappers
// =========================================================================
/**
* @return StreamResponse<GeneratedModels\SendReactionResponse>
*/
protected function sendReaction(string $messageID, GeneratedModels\SendReactionRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/messages/{$messageID}/reaction", [], $request),
GeneratedModels\SendReactionResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\GetReactionsResponse>
*/
protected function getReactions(string $messageID, int $limit = 0, int $offset = 0): StreamResponse
{
$queryParams = [];
if ($limit > 0) {
$queryParams['limit'] = (string) $limit;
}
if ($offset > 0) {
$queryParams['offset'] = (string) $offset;
}
return StreamResponse::fromJson(
$this->client->makeRequest('GET', "/api/v2/chat/messages/{$messageID}/reactions", $queryParams),
GeneratedModels\GetReactionsResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\DeleteReactionResponse>
*/
protected function deleteReaction(string $messageID, string $reactionType, string $userID): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('DELETE', "/api/v2/chat/messages/{$messageID}/reaction/{$reactionType}", ['user_id' => $userID]),
GeneratedModels\DeleteReactionResponse::class,
);
}
// =========================================================================
// Poll API Wrappers
// =========================================================================
/**
* @return StreamResponse<GeneratedModels\PollResponse>
*/
protected function createPoll(GeneratedModels\CreatePollRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/polls', [], $request),
GeneratedModels\PollResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\PollResponse>
*/
protected function getPoll(string $pollID): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', "/api/v2/polls/{$pollID}"),
GeneratedModels\PollResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\QueryPollsResponse>
*/
protected function queryPolls(GeneratedModels\QueryPollsRequest $request, ?string $userID = null): StreamResponse
{
$queryParams = [];
if ($userID !== null) {
$queryParams['user_id'] = $userID;
}
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/polls/query', $queryParams, $request),
GeneratedModels\QueryPollsResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\Response>
*/
protected function deletePoll(string $pollID, ?string $userID = null): StreamResponse
{
$queryParams = [];
if ($userID !== null) {
$queryParams['user_id'] = $userID;
}
return StreamResponse::fromJson(
$this->client->makeRequest('DELETE', "/api/v2/polls/{$pollID}", $queryParams),
GeneratedModels\Response::class,
);
}
/**
* @return StreamResponse<GeneratedModels\PollVoteResponse>
*/
protected function castPollVote(string $messageID, string $pollID, GeneratedModels\CastPollVoteRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', "/api/v2/chat/messages/{$messageID}/polls/{$pollID}/vote", [], $request),
GeneratedModels\PollVoteResponse::class,
);
}
// =========================================================================
// Command API Wrappers
// =========================================================================
/**
* @return StreamResponse<GeneratedModels\CreateCommandResponse>
*/
protected function createCommand(GeneratedModels\CreateCommandRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/chat/commands', [], $request),
GeneratedModels\CreateCommandResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\GetCommandResponse>
*/
protected function getCommand(string $name): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', "/api/v2/chat/commands/{$name}"),
GeneratedModels\GetCommandResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\ListCommandsResponse>
*/
protected function listCommands(): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', '/api/v2/chat/commands'),
GeneratedModels\ListCommandsResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UpdateCommandResponse>
*/
protected function updateCommand(string $name, GeneratedModels\UpdateCommandRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('PUT', "/api/v2/chat/commands/{$name}", [], $request),
GeneratedModels\UpdateCommandResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\DeleteCommandResponse>
*/
protected function deleteCommandApi(string $name): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('DELETE', "/api/v2/chat/commands/{$name}"),
GeneratedModels\DeleteCommandResponse::class,
);
}
// =========================================================================
// Channel Type API Wrappers
// =========================================================================
/**
* @return StreamResponse<GeneratedModels\CreateChannelTypeResponse>
*/
protected function createChannelType(GeneratedModels\CreateChannelTypeRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/chat/channeltypes', [], $request),
GeneratedModels\CreateChannelTypeResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\GetChannelTypeResponse>
*/
protected function getChannelType(string $name): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', "/api/v2/chat/channeltypes/{$name}"),
GeneratedModels\GetChannelTypeResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\ListChannelTypesResponse>
*/
protected function listChannelTypes(): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', '/api/v2/chat/channeltypes'),
GeneratedModels\ListChannelTypesResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UpdateChannelTypeResponse>
*/
protected function updateChannelType(string $name, GeneratedModels\UpdateChannelTypeRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('PUT', "/api/v2/chat/channeltypes/{$name}", [], $request),
GeneratedModels\UpdateChannelTypeResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\Response>
*/
protected function deleteChannelType(string $name): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('DELETE', "/api/v2/chat/channeltypes/{$name}"),
GeneratedModels\Response::class,
);
}
// =========================================================================
// Ban / Mute API Wrappers (Moderation)
// =========================================================================
/**
* @return StreamResponse<GeneratedModels\BanResponse>
*/
protected function banUser(GeneratedModels\BanRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/moderation/ban', [], $request),
GeneratedModels\BanResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\Response>
*/
protected function unbanUser(string $targetUserID, string $channelCid = ''): StreamResponse
{
$queryParams = ['target_user_id' => $targetUserID];
if ($channelCid !== '') {
$queryParams['channel_cid'] = $channelCid;
}
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/moderation/unban', $queryParams, new GeneratedModels\UnbanRequest()),
GeneratedModels\Response::class,
);
}
/**
* @return StreamResponse<GeneratedModels\QueryBannedUsersResponse>
*/
protected function queryBannedUsers(GeneratedModels\QueryBannedUsersPayload $payload): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('GET', '/api/v2/chat/query_banned_users', ['payload' => json_encode($payload->toArray())]),
GeneratedModels\QueryBannedUsersResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\MuteResponse>
*/
protected function muteUser(GeneratedModels\MuteRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/moderation/mute', [], $request),
GeneratedModels\MuteResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\UnmuteResponse>
*/
protected function unmuteUser(GeneratedModels\UnmuteRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/moderation/unmute', [], $request),
GeneratedModels\UnmuteResponse::class,
);
}
// =========================================================================
// Flag API Wrappers
// =========================================================================
/**
* @return StreamResponse<GeneratedModels\FlagResponse>
*/
protected function flagContent(GeneratedModels\FlagRequest $request): StreamResponse
{
return StreamResponse::fromJson(
$this->client->makeRequest('POST', '/api/v2/moderation/flag', [], $request),
GeneratedModels\FlagResponse::class,
);
}
/**
* @return StreamResponse<GeneratedModels\QueryMessageFlagsResponse>
*/
protected function queryMessageFlags(GeneratedModels\QueryMessageFlagsPayload $payload): StreamResponse
{
$queryParams = ['payload' => json_encode($payload->toArray())];
return StreamResponse::fromJson(
$this->client->makeRequest('GET', '/api/v2/chat/moderation/flags/message', $queryParams),
GeneratedModels\QueryMessageFlagsResponse::class,
);
}
// =========================================================================
// Export Channels API Wrapper