Repository navigation
Expand file tree
/
Copy pathWebhook.php
More file actions
904 lines (876 loc) · 50.7 KB
/
Copy pathWebhook.php
File metadata and controls
904 lines (876 loc) · 50.7 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
<?php
// Code generated by GetStream internal OpenAPI code generator. DO NOT EDIT.
namespace GetStream\Generated;
use GetStream\Exceptions\InvalidWebhookException;
use GetStream\Models\UnknownEvent;
use GetStream\GeneratedModels\ActivityAddedEvent;
use GetStream\GeneratedModels\ActivityDeletedEvent;
use GetStream\GeneratedModels\ActivityFeedbackEvent;
use GetStream\GeneratedModels\ActivityMarkEvent;
use GetStream\GeneratedModels\ActivityPinnedEvent;
use GetStream\GeneratedModels\ActivityReactionAddedEvent;
use GetStream\GeneratedModels\ActivityReactionDeletedEvent;
use GetStream\GeneratedModels\ActivityReactionUpdatedEvent;
use GetStream\GeneratedModels\ActivityRemovedFromFeedEvent;
use GetStream\GeneratedModels\ActivityRestoredEvent;
use GetStream\GeneratedModels\ActivityUnpinnedEvent;
use GetStream\GeneratedModels\ActivityUpdatedEvent;
use GetStream\GeneratedModels\AppealAcceptedEvent;
use GetStream\GeneratedModels\AppealCreatedEvent;
use GetStream\GeneratedModels\AppealRejectedEvent;
use GetStream\GeneratedModels\AsyncBulkImageModerationEvent;
use GetStream\GeneratedModels\AsyncExportChannelsEvent;
use GetStream\GeneratedModels\AsyncExportErrorEvent;
use GetStream\GeneratedModels\AsyncExportFeedsSuccessEvent;
use GetStream\GeneratedModels\AsyncExportLabelResultsEvent;
use GetStream\GeneratedModels\AsyncExportModerationLogsEvent;
use GetStream\GeneratedModels\AsyncExportReviewQueueEvent;
use GetStream\GeneratedModels\AsyncExportUsersEvent;
use GetStream\GeneratedModels\AsyncImportErrorEvent;
use GetStream\GeneratedModels\AsyncImportSuccessEvent;
use GetStream\GeneratedModels\BlockedUserEvent;
use GetStream\GeneratedModels\BookmarkAddedEvent;
use GetStream\GeneratedModels\BookmarkDeletedEvent;
use GetStream\GeneratedModels\BookmarkFolderDeletedEvent;
use GetStream\GeneratedModels\BookmarkFolderUpdatedEvent;
use GetStream\GeneratedModels\BookmarkUpdatedEvent;
use GetStream\GeneratedModels\CallAcceptedEvent;
use GetStream\GeneratedModels\CallClosedCaptionsFailedEvent;
use GetStream\GeneratedModels\CallClosedCaptionsStartedEvent;
use GetStream\GeneratedModels\CallClosedCaptionsStoppedEvent;
use GetStream\GeneratedModels\CallCreatedEvent;
use GetStream\GeneratedModels\CallDTMFEvent;
use GetStream\GeneratedModels\CallDeletedEvent;
use GetStream\GeneratedModels\CallEndedEvent;
use GetStream\GeneratedModels\CallFrameRecordingFailedEvent;
use GetStream\GeneratedModels\CallFrameRecordingFrameReadyEvent;
use GetStream\GeneratedModels\CallFrameRecordingStartedEvent;
use GetStream\GeneratedModels\CallFrameRecordingStoppedEvent;
use GetStream\GeneratedModels\CallHLSBroadcastingFailedEvent;
use GetStream\GeneratedModels\CallHLSBroadcastingStartedEvent;
use GetStream\GeneratedModels\CallHLSBroadcastingStoppedEvent;
use GetStream\GeneratedModels\CallLiveStartedEvent;
use GetStream\GeneratedModels\CallMemberAddedEvent;
use GetStream\GeneratedModels\CallMemberRemovedEvent;
use GetStream\GeneratedModels\CallMemberUpdatedEvent;
use GetStream\GeneratedModels\CallMemberUpdatedPermissionEvent;
use GetStream\GeneratedModels\CallMissedEvent;
use GetStream\GeneratedModels\CallModerationBlurEvent;
use GetStream\GeneratedModels\CallModerationWarningEvent;
use GetStream\GeneratedModels\CallNotificationEvent;
use GetStream\GeneratedModels\CallReactionEvent;
use GetStream\GeneratedModels\CallRecordingFailedEvent;
use GetStream\GeneratedModels\CallRecordingReadyEvent;
use GetStream\GeneratedModels\CallRecordingStartedEvent;
use GetStream\GeneratedModels\CallRecordingStoppedEvent;
use GetStream\GeneratedModels\CallRejectedEvent;
use GetStream\GeneratedModels\CallRingEvent;
use GetStream\GeneratedModels\CallRingSettledEvent;
use GetStream\GeneratedModels\CallRtmpBroadcastFailedEvent;
use GetStream\GeneratedModels\CallRtmpBroadcastStartedEvent;
use GetStream\GeneratedModels\CallRtmpBroadcastStoppedEvent;
use GetStream\GeneratedModels\CallSessionEndedEvent;
use GetStream\GeneratedModels\CallSessionParticipantCountsUpdatedEvent;
use GetStream\GeneratedModels\CallSessionParticipantJoinedEvent;
use GetStream\GeneratedModels\CallSessionParticipantLeftEvent;
use GetStream\GeneratedModels\CallSessionStartedEvent;
use GetStream\GeneratedModels\CallStatsReportReadyEvent;
use GetStream\GeneratedModels\CallTranscriptionFailedEvent;
use GetStream\GeneratedModels\CallTranscriptionReadyEvent;
use GetStream\GeneratedModels\CallTranscriptionStartedEvent;
use GetStream\GeneratedModels\CallTranscriptionStoppedEvent;
use GetStream\GeneratedModels\CallUpdatedEvent;
use GetStream\GeneratedModels\CallUserFeedbackSubmittedEvent;
use GetStream\GeneratedModels\CallUserMutedEvent;
use GetStream\GeneratedModels\CampaignCompletedEvent;
use GetStream\GeneratedModels\CampaignStartedEvent;
use GetStream\GeneratedModels\ChannelBatchCompletedEvent;
use GetStream\GeneratedModels\ChannelBatchStartedEvent;
use GetStream\GeneratedModels\ChannelCreatedEvent;
use GetStream\GeneratedModels\ChannelDeletedEvent;
use GetStream\GeneratedModels\ChannelForkedEvent;
use GetStream\GeneratedModels\ChannelFrozenEvent;
use GetStream\GeneratedModels\ChannelHiddenEvent;
use GetStream\GeneratedModels\ChannelMutedEvent;
use GetStream\GeneratedModels\ChannelRewoundEvent;
use GetStream\GeneratedModels\ChannelTruncatedEvent;
use GetStream\GeneratedModels\ChannelUnFrozenEvent;
use GetStream\GeneratedModels\ChannelUnmutedEvent;
use GetStream\GeneratedModels\ChannelUpdatedEvent;
use GetStream\GeneratedModels\ChannelVisibleEvent;
use GetStream\GeneratedModels\ClosedCaptionEvent;
use GetStream\GeneratedModels\CommentAddedEvent;
use GetStream\GeneratedModels\CommentDeletedEvent;
use GetStream\GeneratedModels\CommentFeedbackEvent;
use GetStream\GeneratedModels\CommentReactionAddedEvent;
use GetStream\GeneratedModels\CommentReactionDeletedEvent;
use GetStream\GeneratedModels\CommentReactionUpdatedEvent;
use GetStream\GeneratedModels\CommentRestoredEvent;
use GetStream\GeneratedModels\CommentUpdatedEvent;
use GetStream\GeneratedModels\CustomEvent;
use GetStream\GeneratedModels\CustomVideoEvent;
use GetStream\GeneratedModels\FeedCreatedEvent;
use GetStream\GeneratedModels\FeedDeletedEvent;
use GetStream\GeneratedModels\FeedGroupChangedEvent;
use GetStream\GeneratedModels\FeedGroupDeletedEvent;
use GetStream\GeneratedModels\FeedGroupRestoredEvent;
use GetStream\GeneratedModels\FeedMemberAddedEvent;
use GetStream\GeneratedModels\FeedMemberRemovedEvent;
use GetStream\GeneratedModels\FeedMemberUpdatedEvent;
use GetStream\GeneratedModels\FeedUpdatedEvent;
use GetStream\GeneratedModels\FlagUpdatedEvent;
use GetStream\GeneratedModels\FollowCreatedEvent;
use GetStream\GeneratedModels\FollowDeletedEvent;
use GetStream\GeneratedModels\FollowUpdatedEvent;
use GetStream\GeneratedModels\IngressErrorEvent;
use GetStream\GeneratedModels\IngressStartedEvent;
use GetStream\GeneratedModels\IngressStoppedEvent;
use GetStream\GeneratedModels\IngressStreamKeyRejectedEvent;
use GetStream\GeneratedModels\KickedUserEvent;
use GetStream\GeneratedModels\MaxStreakChangedEvent;
use GetStream\GeneratedModels\MemberAddedEvent;
use GetStream\GeneratedModels\MemberRemovedEvent;
use GetStream\GeneratedModels\MemberUpdatedEvent;
use GetStream\GeneratedModels\MessageDeletedEvent;
use GetStream\GeneratedModels\MessageFlaggedEvent;
use GetStream\GeneratedModels\MessageNewEvent;
use GetStream\GeneratedModels\MessageReadEvent;
use GetStream\GeneratedModels\MessageSchemaViolationEvent;
use GetStream\GeneratedModels\MessageUnblockedEvent;
use GetStream\GeneratedModels\MessageUndeletedEvent;
use GetStream\GeneratedModels\MessageUpdatedEvent;
use GetStream\GeneratedModels\ModerationAnalysisFailedEvent;
use GetStream\GeneratedModels\ModerationCheckCompletedEvent;
use GetStream\GeneratedModels\ModerationCustomActionEvent;
use GetStream\GeneratedModels\ModerationFlaggedEvent;
use GetStream\GeneratedModels\ModerationImageAnalysisCompleteEvent;
use GetStream\GeneratedModels\ModerationMarkReviewedEvent;
use GetStream\GeneratedModels\ModerationRulesTriggeredEvent;
use GetStream\GeneratedModels\ModerationTextAnalysisCompleteEvent;
use GetStream\GeneratedModels\NotificationFeedUpdatedEvent;
use GetStream\GeneratedModels\NotificationMarkUnreadEvent;
use GetStream\GeneratedModels\NotificationThreadMessageNewEvent;
use GetStream\GeneratedModels\PendingMessageEvent;
use GetStream\GeneratedModels\PermissionRequestEvent;
use GetStream\GeneratedModels\PollClosedEvent;
use GetStream\GeneratedModels\PollDeletedEvent;
use GetStream\GeneratedModels\PollUpdatedEvent;
use GetStream\GeneratedModels\PollVoteCastedEvent;
use GetStream\GeneratedModels\PollVoteChangedEvent;
use GetStream\GeneratedModels\PollVoteRemovedEvent;
use GetStream\GeneratedModels\ReactionDeletedEvent;
use GetStream\GeneratedModels\ReactionNewEvent;
use GetStream\GeneratedModels\ReactionSchemaViolationEvent;
use GetStream\GeneratedModels\ReactionUpdatedEvent;
use GetStream\GeneratedModels\ReminderCreatedEvent;
use GetStream\GeneratedModels\ReminderDeletedEvent;
use GetStream\GeneratedModels\ReminderNotificationEvent;
use GetStream\GeneratedModels\ReminderUpdatedEvent;
use GetStream\GeneratedModels\ReviewQueueItemNewEvent;
use GetStream\GeneratedModels\ReviewQueueItemUpdatedEvent;
use GetStream\GeneratedModels\ScheduledActivityFailedEvent;
use GetStream\GeneratedModels\ScheduledActivityPublishedEvent;
use GetStream\GeneratedModels\ScheduledMessageFailedEvent;
use GetStream\GeneratedModels\ScheduledMessageSentEvent;
use GetStream\GeneratedModels\StoriesFeedUpdatedEvent;
use GetStream\GeneratedModels\ThreadUpdatedEvent;
use GetStream\GeneratedModels\UnblockedUserEvent;
use GetStream\GeneratedModels\UpdatedCallPermissionsEvent;
use GetStream\GeneratedModels\UserBannedEvent;
use GetStream\GeneratedModels\UserClaimedEvent;
use GetStream\GeneratedModels\UserDeactivatedEvent;
use GetStream\GeneratedModels\UserDeletedEvent;
use GetStream\GeneratedModels\UserFlaggedEvent;
use GetStream\GeneratedModels\UserGroupCreatedEvent;
use GetStream\GeneratedModels\UserGroupDeletedEvent;
use GetStream\GeneratedModels\UserGroupMemberAddedEvent;
use GetStream\GeneratedModels\UserGroupMemberRemovedEvent;
use GetStream\GeneratedModels\UserGroupUpdatedEvent;
use GetStream\GeneratedModels\UserMessagesDeletedEvent;
use GetStream\GeneratedModels\UserMutedEvent;
use GetStream\GeneratedModels\UserReactivatedEvent;
use GetStream\GeneratedModels\UserUnbannedEvent;
use GetStream\GeneratedModels\UserUnmutedEvent;
use GetStream\GeneratedModels\UserUnreadReminderEvent;
use GetStream\GeneratedModels\UserUpdatedEvent;
/**
* Webhook utilities for Stream webhooks.
*/
class Webhook
{
/**
* gzip magic prefix (RFC 1952 §2.3.1). JSON cannot start with these bytes,
* so this gives unambiguous detection for Stream's always-JSON payloads.
*/
private const GZIP_MAGIC = "\x1F\x8B";
/**
* Caps the decompressed size of a gzip webhook body. gunzipPayload runs
* before the signature check, so without a cap an unsigned gzip bomb could
* exhaust memory. Raise it if your events are larger.
*/
public static int $maxPayloadBytes = 10 * 1024 * 1024;
// Webhook event type constants
public const EVENT_TYPE_WILDCARD = '*';
public const EVENT_TYPE_APPEAL_ACCEPTED = 'appeal.accepted';
public const EVENT_TYPE_APPEAL_CREATED = 'appeal.created';
public const EVENT_TYPE_APPEAL_REJECTED = 'appeal.rejected';
public const EVENT_TYPE_CALL_ACCEPTED = 'call.accepted';
public const EVENT_TYPE_CALL_BLOCKED_USER = 'call.blocked_user';
public const EVENT_TYPE_CALL_CLOSED_CAPTION = 'call.closed_caption';
public const EVENT_TYPE_CALL_CLOSED_CAPTIONS_FAILED = 'call.closed_captions_failed';
public const EVENT_TYPE_CALL_CLOSED_CAPTIONS_STARTED = 'call.closed_captions_started';
public const EVENT_TYPE_CALL_CLOSED_CAPTIONS_STOPPED = 'call.closed_captions_stopped';
public const EVENT_TYPE_CALL_CREATED = 'call.created';
public const EVENT_TYPE_CALL_DELETED = 'call.deleted';
public const EVENT_TYPE_CALL_DTMF = 'call.dtmf';
public const EVENT_TYPE_CALL_ENDED = 'call.ended';
public const EVENT_TYPE_CALL_FRAME_RECORDING_FAILED = 'call.frame_recording_failed';
public const EVENT_TYPE_CALL_FRAME_RECORDING_READY = 'call.frame_recording_ready';
public const EVENT_TYPE_CALL_FRAME_RECORDING_STARTED = 'call.frame_recording_started';
public const EVENT_TYPE_CALL_FRAME_RECORDING_STOPPED = 'call.frame_recording_stopped';
public const EVENT_TYPE_CALL_HLS_BROADCASTING_FAILED = 'call.hls_broadcasting_failed';
public const EVENT_TYPE_CALL_HLS_BROADCASTING_STARTED = 'call.hls_broadcasting_started';
public const EVENT_TYPE_CALL_HLS_BROADCASTING_STOPPED = 'call.hls_broadcasting_stopped';
public const EVENT_TYPE_CALL_KICKED_USER = 'call.kicked_user';
public const EVENT_TYPE_CALL_LIVE_STARTED = 'call.live_started';
public const EVENT_TYPE_CALL_MEMBER_ADDED = 'call.member_added';
public const EVENT_TYPE_CALL_MEMBER_REMOVED = 'call.member_removed';
public const EVENT_TYPE_CALL_MEMBER_UPDATED = 'call.member_updated';
public const EVENT_TYPE_CALL_MEMBER_UPDATED_PERMISSION = 'call.member_updated_permission';
public const EVENT_TYPE_CALL_MISSED = 'call.missed';
public const EVENT_TYPE_CALL_MODERATION_BLUR = 'call.moderation_blur';
public const EVENT_TYPE_CALL_MODERATION_WARNING = 'call.moderation_warning';
public const EVENT_TYPE_CALL_NOTIFICATION = 'call.notification';
public const EVENT_TYPE_CALL_PERMISSION_REQUEST = 'call.permission_request';
public const EVENT_TYPE_CALL_PERMISSIONS_UPDATED = 'call.permissions_updated';
public const EVENT_TYPE_CALL_REACTION_NEW = 'call.reaction_new';
public const EVENT_TYPE_CALL_RECORDING_FAILED = 'call.recording_failed';
public const EVENT_TYPE_CALL_RECORDING_READY = 'call.recording_ready';
public const EVENT_TYPE_CALL_RECORDING_STARTED = 'call.recording_started';
public const EVENT_TYPE_CALL_RECORDING_STOPPED = 'call.recording_stopped';
public const EVENT_TYPE_CALL_REJECTED = 'call.rejected';
public const EVENT_TYPE_CALL_RING = 'call.ring';
public const EVENT_TYPE_CALL_RING_SETTLED = 'call.ring_settled';
public const EVENT_TYPE_CALL_RTMP_BROADCAST_FAILED = 'call.rtmp_broadcast_failed';
public const EVENT_TYPE_CALL_RTMP_BROADCAST_STARTED = 'call.rtmp_broadcast_started';
public const EVENT_TYPE_CALL_RTMP_BROADCAST_STOPPED = 'call.rtmp_broadcast_stopped';
public const EVENT_TYPE_CALL_SESSION_ENDED = 'call.session_ended';
public const EVENT_TYPE_CALL_SESSION_PARTICIPANT_COUNT_UPDATED = 'call.session_participant_count_updated';
public const EVENT_TYPE_CALL_SESSION_PARTICIPANT_JOINED = 'call.session_participant_joined';
public const EVENT_TYPE_CALL_SESSION_PARTICIPANT_LEFT = 'call.session_participant_left';
public const EVENT_TYPE_CALL_SESSION_STARTED = 'call.session_started';
public const EVENT_TYPE_CALL_STATS_REPORT_READY = 'call.stats_report_ready';
public const EVENT_TYPE_CALL_TRANSCRIPTION_FAILED = 'call.transcription_failed';
public const EVENT_TYPE_CALL_TRANSCRIPTION_READY = 'call.transcription_ready';
public const EVENT_TYPE_CALL_TRANSCRIPTION_STARTED = 'call.transcription_started';
public const EVENT_TYPE_CALL_TRANSCRIPTION_STOPPED = 'call.transcription_stopped';
public const EVENT_TYPE_CALL_UNBLOCKED_USER = 'call.unblocked_user';
public const EVENT_TYPE_CALL_UPDATED = 'call.updated';
public const EVENT_TYPE_CALL_USER_FEEDBACK_SUBMITTED = 'call.user_feedback_submitted';
public const EVENT_TYPE_CALL_USER_MUTED = 'call.user_muted';
public const EVENT_TYPE_CAMPAIGN_COMPLETED = 'campaign.completed';
public const EVENT_TYPE_CAMPAIGN_STARTED = 'campaign.started';
public const EVENT_TYPE_CHANNEL_CREATED = 'channel.created';
public const EVENT_TYPE_CHANNEL_DELETED = 'channel.deleted';
public const EVENT_TYPE_CHANNEL_FORKED = 'channel.forked';
public const EVENT_TYPE_CHANNEL_FROZEN = 'channel.frozen';
public const EVENT_TYPE_CHANNEL_HIDDEN = 'channel.hidden';
public const EVENT_TYPE_CHANNEL_MAX_STREAK_CHANGED = 'channel.max_streak_changed';
public const EVENT_TYPE_CHANNEL_MUTED = 'channel.muted';
public const EVENT_TYPE_CHANNEL_REWOUND = 'channel.rewound';
public const EVENT_TYPE_CHANNEL_TRUNCATED = 'channel.truncated';
public const EVENT_TYPE_CHANNEL_UNFROZEN = 'channel.unfrozen';
public const EVENT_TYPE_CHANNEL_UNMUTED = 'channel.unmuted';
public const EVENT_TYPE_CHANNEL_UPDATED = 'channel.updated';
public const EVENT_TYPE_CHANNEL_VISIBLE = 'channel.visible';
public const EVENT_TYPE_CHANNEL_BATCH_UPDATE_COMPLETED = 'channel_batch_update.completed';
public const EVENT_TYPE_CHANNEL_BATCH_UPDATE_STARTED = 'channel_batch_update.started';
public const EVENT_TYPE_CUSTOM = 'custom';
public const EVENT_TYPE_EXPORT_BULK_IMAGE_MODERATION_ERROR = 'export.bulk_image_moderation.error';
public const EVENT_TYPE_EXPORT_BULK_IMAGE_MODERATION_SUCCESS = 'export.bulk_image_moderation.success';
public const EVENT_TYPE_EXPORT_CHANNELS_ERROR = 'export.channels.error';
public const EVENT_TYPE_EXPORT_CHANNELS_SUCCESS = 'export.channels.success';
public const EVENT_TYPE_EXPORT_LABEL_RESULTS_ERROR = 'export.label_results.error';
public const EVENT_TYPE_EXPORT_LABEL_RESULTS_SUCCESS = 'export.label_results.success';
public const EVENT_TYPE_EXPORT_MODERATION_LOGS_ERROR = 'export.moderation_logs.error';
public const EVENT_TYPE_EXPORT_MODERATION_LOGS_SUCCESS = 'export.moderation_logs.success';
public const EVENT_TYPE_EXPORT_REVIEW_QUEUE_ERROR = 'export.review_queue.error';
public const EVENT_TYPE_EXPORT_REVIEW_QUEUE_SUCCESS = 'export.review_queue.success';
public const EVENT_TYPE_EXPORT_USERS_ERROR = 'export.users.error';
public const EVENT_TYPE_EXPORT_USERS_SUCCESS = 'export.users.success';
public const EVENT_TYPE_FEEDS_ACTIVITY_ADDED = 'feeds.activity.added';
public const EVENT_TYPE_FEEDS_ACTIVITY_DELETED = 'feeds.activity.deleted';
public const EVENT_TYPE_FEEDS_ACTIVITY_FEEDBACK = 'feeds.activity.feedback';
public const EVENT_TYPE_FEEDS_ACTIVITY_MARKED = 'feeds.activity.marked';
public const EVENT_TYPE_FEEDS_ACTIVITY_PINNED = 'feeds.activity.pinned';
public const EVENT_TYPE_FEEDS_ACTIVITY_REACTION_ADDED = 'feeds.activity.reaction.added';
public const EVENT_TYPE_FEEDS_ACTIVITY_REACTION_DELETED = 'feeds.activity.reaction.deleted';
public const EVENT_TYPE_FEEDS_ACTIVITY_REACTION_UPDATED = 'feeds.activity.reaction.updated';
public const EVENT_TYPE_FEEDS_ACTIVITY_REMOVED_FROM_FEED = 'feeds.activity.removed_from_feed';
public const EVENT_TYPE_FEEDS_ACTIVITY_RESTORED = 'feeds.activity.restored';
public const EVENT_TYPE_FEEDS_ACTIVITY_UNPINNED = 'feeds.activity.unpinned';
public const EVENT_TYPE_FEEDS_ACTIVITY_UPDATED = 'feeds.activity.updated';
public const EVENT_TYPE_FEEDS_BOOKMARK_ADDED = 'feeds.bookmark.added';
public const EVENT_TYPE_FEEDS_BOOKMARK_DELETED = 'feeds.bookmark.deleted';
public const EVENT_TYPE_FEEDS_BOOKMARK_UPDATED = 'feeds.bookmark.updated';
public const EVENT_TYPE_FEEDS_BOOKMARK_FOLDER_DELETED = 'feeds.bookmark_folder.deleted';
public const EVENT_TYPE_FEEDS_BOOKMARK_FOLDER_UPDATED = 'feeds.bookmark_folder.updated';
public const EVENT_TYPE_FEEDS_COMMENT_ADDED = 'feeds.comment.added';
public const EVENT_TYPE_FEEDS_COMMENT_DELETED = 'feeds.comment.deleted';
public const EVENT_TYPE_FEEDS_COMMENT_FEEDBACK = 'feeds.comment.feedback';
public const EVENT_TYPE_FEEDS_COMMENT_REACTION_ADDED = 'feeds.comment.reaction.added';
public const EVENT_TYPE_FEEDS_COMMENT_REACTION_DELETED = 'feeds.comment.reaction.deleted';
public const EVENT_TYPE_FEEDS_COMMENT_REACTION_UPDATED = 'feeds.comment.reaction.updated';
public const EVENT_TYPE_FEEDS_COMMENT_RESTORED = 'feeds.comment.restored';
public const EVENT_TYPE_FEEDS_COMMENT_UPDATED = 'feeds.comment.updated';
public const EVENT_TYPE_FEEDS_EXPORT_ERROR = 'feeds.export.error';
public const EVENT_TYPE_FEEDS_EXPORT_SUCCESS = 'feeds.export.success';
public const EVENT_TYPE_FEEDS_FEED_CREATED = 'feeds.feed.created';
public const EVENT_TYPE_FEEDS_FEED_DELETED = 'feeds.feed.deleted';
public const EVENT_TYPE_FEEDS_FEED_UPDATED = 'feeds.feed.updated';
public const EVENT_TYPE_FEEDS_FEED_GROUP_CHANGED = 'feeds.feed_group.changed';
public const EVENT_TYPE_FEEDS_FEED_GROUP_DELETED = 'feeds.feed_group.deleted';
public const EVENT_TYPE_FEEDS_FEED_GROUP_RESTORED = 'feeds.feed_group.restored';
public const EVENT_TYPE_FEEDS_FEED_MEMBER_ADDED = 'feeds.feed_member.added';
public const EVENT_TYPE_FEEDS_FEED_MEMBER_REMOVED = 'feeds.feed_member.removed';
public const EVENT_TYPE_FEEDS_FEED_MEMBER_UPDATED = 'feeds.feed_member.updated';
public const EVENT_TYPE_FEEDS_FOLLOW_CREATED = 'feeds.follow.created';
public const EVENT_TYPE_FEEDS_FOLLOW_DELETED = 'feeds.follow.deleted';
public const EVENT_TYPE_FEEDS_FOLLOW_UPDATED = 'feeds.follow.updated';
public const EVENT_TYPE_FEEDS_NOTIFICATION_FEED_UPDATED = 'feeds.notification_feed.updated';
public const EVENT_TYPE_FEEDS_SCHEDULED_ACTIVITY_FAILED = 'feeds.scheduled_activity.failed';
public const EVENT_TYPE_FEEDS_SCHEDULED_ACTIVITY_PUBLISHED = 'feeds.scheduled_activity.published';
public const EVENT_TYPE_FEEDS_STORIES_FEED_UPDATED = 'feeds.stories_feed.updated';
public const EVENT_TYPE_FLAG_UPDATED = 'flag.updated';
public const EVENT_TYPE_IMPORT_ERROR = 'import.error';
public const EVENT_TYPE_IMPORT_SUCCESS = 'import.success';
public const EVENT_TYPE_INGRESS_ERROR = 'ingress.error';
public const EVENT_TYPE_INGRESS_STARTED = 'ingress.started';
public const EVENT_TYPE_INGRESS_STOPPED = 'ingress.stopped';
public const EVENT_TYPE_INGRESS_STREAM_KEY_REJECTED = 'ingress.stream_key.rejected';
public const EVENT_TYPE_MEMBER_ADDED = 'member.added';
public const EVENT_TYPE_MEMBER_REMOVED = 'member.removed';
public const EVENT_TYPE_MEMBER_UPDATED = 'member.updated';
public const EVENT_TYPE_MESSAGE_DELETED = 'message.deleted';
public const EVENT_TYPE_MESSAGE_FLAGGED = 'message.flagged';
public const EVENT_TYPE_MESSAGE_NEW = 'message.new';
public const EVENT_TYPE_MESSAGE_PENDING = 'message.pending';
public const EVENT_TYPE_MESSAGE_READ = 'message.read';
public const EVENT_TYPE_MESSAGE_SCHEMA_VIOLATION = 'message.schema_violation';
public const EVENT_TYPE_MESSAGE_UNBLOCKED = 'message.unblocked';
public const EVENT_TYPE_MESSAGE_UNDELETED = 'message.undeleted';
public const EVENT_TYPE_MESSAGE_UPDATED = 'message.updated';
public const EVENT_TYPE_MODERATION_ANALYSIS_FAILED = 'moderation.analysis.failed';
public const EVENT_TYPE_MODERATION_CUSTOM_ACTION = 'moderation.custom_action';
public const EVENT_TYPE_MODERATION_FLAGGED = 'moderation.flagged';
public const EVENT_TYPE_MODERATION_IMAGE_ANALYSIS_COMPLETE = 'moderation.image_analysis.complete';
public const EVENT_TYPE_MODERATION_MARK_REVIEWED = 'moderation.mark_reviewed';
public const EVENT_TYPE_MODERATION_TEXT_ANALYSIS_COMPLETE = 'moderation.text_analysis.complete';
public const EVENT_TYPE_MODERATION_CHECK_COMPLETED = 'moderation_check.completed';
public const EVENT_TYPE_MODERATION_RULE_TRIGGERED = 'moderation_rule.triggered';
public const EVENT_TYPE_NOTIFICATION_MARK_UNREAD = 'notification.mark_unread';
public const EVENT_TYPE_NOTIFICATION_REMINDER_DUE = 'notification.reminder_due';
public const EVENT_TYPE_NOTIFICATION_THREAD_MESSAGE_NEW = 'notification.thread_message_new';
public const EVENT_TYPE_POLL_CLOSED = 'poll.closed';
public const EVENT_TYPE_POLL_DELETED = 'poll.deleted';
public const EVENT_TYPE_POLL_UPDATED = 'poll.updated';
public const EVENT_TYPE_POLL_VOTE_CASTED = 'poll.vote_casted';
public const EVENT_TYPE_POLL_VOTE_CHANGED = 'poll.vote_changed';
public const EVENT_TYPE_POLL_VOTE_REMOVED = 'poll.vote_removed';
public const EVENT_TYPE_REACTION_DELETED = 'reaction.deleted';
public const EVENT_TYPE_REACTION_NEW = 'reaction.new';
public const EVENT_TYPE_REACTION_SCHEMA_VIOLATION = 'reaction.schema_violation';
public const EVENT_TYPE_REACTION_UPDATED = 'reaction.updated';
public const EVENT_TYPE_REMINDER_CREATED = 'reminder.created';
public const EVENT_TYPE_REMINDER_DELETED = 'reminder.deleted';
public const EVENT_TYPE_REMINDER_UPDATED = 'reminder.updated';
public const EVENT_TYPE_REVIEW_QUEUE_ITEM_NEW = 'review_queue_item.new';
public const EVENT_TYPE_REVIEW_QUEUE_ITEM_UPDATED = 'review_queue_item.updated';
public const EVENT_TYPE_SCHEDULED_MESSAGE_FAILED = 'scheduled_message.failed';
public const EVENT_TYPE_SCHEDULED_MESSAGE_SENT = 'scheduled_message.sent';
public const EVENT_TYPE_THREAD_UPDATED = 'thread.updated';
public const EVENT_TYPE_USER_BANNED = 'user.banned';
public const EVENT_TYPE_USER_CLAIMED = 'user.claimed';
public const EVENT_TYPE_USER_DEACTIVATED = 'user.deactivated';
public const EVENT_TYPE_USER_DELETED = 'user.deleted';
public const EVENT_TYPE_USER_FLAGGED = 'user.flagged';
public const EVENT_TYPE_USER_MESSAGES_DELETED = 'user.messages.deleted';
public const EVENT_TYPE_USER_MUTED = 'user.muted';
public const EVENT_TYPE_USER_REACTIVATED = 'user.reactivated';
public const EVENT_TYPE_USER_UNBANNED = 'user.unbanned';
public const EVENT_TYPE_USER_UNMUTED = 'user.unmuted';
public const EVENT_TYPE_USER_UNREAD_MESSAGE_REMINDER = 'user.unread_message_reminder';
public const EVENT_TYPE_USER_UPDATED = 'user.updated';
public const EVENT_TYPE_USER_GROUP_CREATED = 'user_group.created';
public const EVENT_TYPE_USER_GROUP_DELETED = 'user_group.deleted';
public const EVENT_TYPE_USER_GROUP_MEMBER_ADDED = 'user_group.member_added';
public const EVENT_TYPE_USER_GROUP_MEMBER_REMOVED = 'user_group.member_removed';
public const EVENT_TYPE_USER_GROUP_UPDATED = 'user_group.updated';
/**
* Extract the event type from a raw webhook payload.
*
* @param string|array $rawEvent The raw webhook payload
* @return string|null The event type string (e.g., "message.new"), or null if not found
*/
public static function getEventType($rawEvent): ?string
{
$data = self::parseToArray($rawEvent);
return $data['type'] ?? null;
}
/**
* Deserialize a raw webhook payload into a typed event object.
*
* Throws on unknown event types. For forward-compatible parsing that returns
* an UnknownEvent on unrecognized discriminators, use {@see self::parseEvent()}.
*
* @param string|array $rawEvent The raw webhook payload
* @return object A typed event object corresponding to the event type
* @throws \InvalidArgumentException if the event type is unknown or deserialization fails
*/
public static function parseWebhookEvent($rawEvent): object
{
$data = self::parseToArray($rawEvent);
if (!isset($data['type'])) {
throw new \InvalidArgumentException("Webhook payload missing 'type' field");
}
$eventType = $data['type'];
$eventClass = self::getEventClass($eventType);
if ($eventClass === null) {
throw new \InvalidArgumentException("Unknown webhook event type: {$eventType}");
}
try {
return $eventClass::fromJson($data);
} catch (\Throwable $e) {
throw new \InvalidArgumentException("Failed to deserialize webhook event: " . $e->getMessage(), 0, $e);
}
}
private static function parseToArray(mixed $rawEvent): array
{
if (is_array($rawEvent)) {
return $rawEvent;
}
if (is_string($rawEvent)) {
$decoded = json_decode($rawEvent, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \InvalidArgumentException('Invalid JSON in webhook payload: ' . json_last_error_msg());
}
return $decoded;
}
throw new \InvalidArgumentException('Webhook payload must be a string or array');
}
/**
* Map an event type discriminator to its generated event class.
* Returns null when the discriminator is not recognized; callers decide whether
* to throw (parseWebhookEvent) or fall back to UnknownEvent (parseEvent).
*/
private static function getEventClass(string $eventType): ?string
{
$eventClassMap = [
'*' => CustomEvent::class,
'appeal.accepted' => AppealAcceptedEvent::class,
'appeal.created' => AppealCreatedEvent::class,
'appeal.rejected' => AppealRejectedEvent::class,
'call.accepted' => CallAcceptedEvent::class,
'call.blocked_user' => BlockedUserEvent::class,
'call.closed_caption' => ClosedCaptionEvent::class,
'call.closed_captions_failed' => CallClosedCaptionsFailedEvent::class,
'call.closed_captions_started' => CallClosedCaptionsStartedEvent::class,
'call.closed_captions_stopped' => CallClosedCaptionsStoppedEvent::class,
'call.created' => CallCreatedEvent::class,
'call.deleted' => CallDeletedEvent::class,
'call.dtmf' => CallDTMFEvent::class,
'call.ended' => CallEndedEvent::class,
'call.frame_recording_failed' => CallFrameRecordingFailedEvent::class,
'call.frame_recording_ready' => CallFrameRecordingFrameReadyEvent::class,
'call.frame_recording_started' => CallFrameRecordingStartedEvent::class,
'call.frame_recording_stopped' => CallFrameRecordingStoppedEvent::class,
'call.hls_broadcasting_failed' => CallHLSBroadcastingFailedEvent::class,
'call.hls_broadcasting_started' => CallHLSBroadcastingStartedEvent::class,
'call.hls_broadcasting_stopped' => CallHLSBroadcastingStoppedEvent::class,
'call.kicked_user' => KickedUserEvent::class,
'call.live_started' => CallLiveStartedEvent::class,
'call.member_added' => CallMemberAddedEvent::class,
'call.member_removed' => CallMemberRemovedEvent::class,
'call.member_updated' => CallMemberUpdatedEvent::class,
'call.member_updated_permission' => CallMemberUpdatedPermissionEvent::class,
'call.missed' => CallMissedEvent::class,
'call.moderation_blur' => CallModerationBlurEvent::class,
'call.moderation_warning' => CallModerationWarningEvent::class,
'call.notification' => CallNotificationEvent::class,
'call.permission_request' => PermissionRequestEvent::class,
'call.permissions_updated' => UpdatedCallPermissionsEvent::class,
'call.reaction_new' => CallReactionEvent::class,
'call.recording_failed' => CallRecordingFailedEvent::class,
'call.recording_ready' => CallRecordingReadyEvent::class,
'call.recording_started' => CallRecordingStartedEvent::class,
'call.recording_stopped' => CallRecordingStoppedEvent::class,
'call.rejected' => CallRejectedEvent::class,
'call.ring' => CallRingEvent::class,
'call.ring_settled' => CallRingSettledEvent::class,
'call.rtmp_broadcast_failed' => CallRtmpBroadcastFailedEvent::class,
'call.rtmp_broadcast_started' => CallRtmpBroadcastStartedEvent::class,
'call.rtmp_broadcast_stopped' => CallRtmpBroadcastStoppedEvent::class,
'call.session_ended' => CallSessionEndedEvent::class,
'call.session_participant_count_updated' => CallSessionParticipantCountsUpdatedEvent::class,
'call.session_participant_joined' => CallSessionParticipantJoinedEvent::class,
'call.session_participant_left' => CallSessionParticipantLeftEvent::class,
'call.session_started' => CallSessionStartedEvent::class,
'call.stats_report_ready' => CallStatsReportReadyEvent::class,
'call.transcription_failed' => CallTranscriptionFailedEvent::class,
'call.transcription_ready' => CallTranscriptionReadyEvent::class,
'call.transcription_started' => CallTranscriptionStartedEvent::class,
'call.transcription_stopped' => CallTranscriptionStoppedEvent::class,
'call.unblocked_user' => UnblockedUserEvent::class,
'call.updated' => CallUpdatedEvent::class,
'call.user_feedback_submitted' => CallUserFeedbackSubmittedEvent::class,
'call.user_muted' => CallUserMutedEvent::class,
'campaign.completed' => CampaignCompletedEvent::class,
'campaign.started' => CampaignStartedEvent::class,
'channel.created' => ChannelCreatedEvent::class,
'channel.deleted' => ChannelDeletedEvent::class,
'channel.forked' => ChannelForkedEvent::class,
'channel.frozen' => ChannelFrozenEvent::class,
'channel.hidden' => ChannelHiddenEvent::class,
'channel.max_streak_changed' => MaxStreakChangedEvent::class,
'channel.muted' => ChannelMutedEvent::class,
'channel.rewound' => ChannelRewoundEvent::class,
'channel.truncated' => ChannelTruncatedEvent::class,
'channel.unfrozen' => ChannelUnFrozenEvent::class,
'channel.unmuted' => ChannelUnmutedEvent::class,
'channel.updated' => ChannelUpdatedEvent::class,
'channel.visible' => ChannelVisibleEvent::class,
'channel_batch_update.completed' => ChannelBatchCompletedEvent::class,
'channel_batch_update.started' => ChannelBatchStartedEvent::class,
'custom' => CustomVideoEvent::class,
'export.bulk_image_moderation.error' => AsyncExportErrorEvent::class,
'export.bulk_image_moderation.success' => AsyncBulkImageModerationEvent::class,
'export.channels.error' => AsyncExportErrorEvent::class,
'export.channels.success' => AsyncExportChannelsEvent::class,
'export.label_results.error' => AsyncExportErrorEvent::class,
'export.label_results.success' => AsyncExportLabelResultsEvent::class,
'export.moderation_logs.error' => AsyncExportErrorEvent::class,
'export.moderation_logs.success' => AsyncExportModerationLogsEvent::class,
'export.review_queue.error' => AsyncExportErrorEvent::class,
'export.review_queue.success' => AsyncExportReviewQueueEvent::class,
'export.users.error' => AsyncExportErrorEvent::class,
'export.users.success' => AsyncExportUsersEvent::class,
'feeds.activity.added' => ActivityAddedEvent::class,
'feeds.activity.deleted' => ActivityDeletedEvent::class,
'feeds.activity.feedback' => ActivityFeedbackEvent::class,
'feeds.activity.marked' => ActivityMarkEvent::class,
'feeds.activity.pinned' => ActivityPinnedEvent::class,
'feeds.activity.reaction.added' => ActivityReactionAddedEvent::class,
'feeds.activity.reaction.deleted' => ActivityReactionDeletedEvent::class,
'feeds.activity.reaction.updated' => ActivityReactionUpdatedEvent::class,
'feeds.activity.removed_from_feed' => ActivityRemovedFromFeedEvent::class,
'feeds.activity.restored' => ActivityRestoredEvent::class,
'feeds.activity.unpinned' => ActivityUnpinnedEvent::class,
'feeds.activity.updated' => ActivityUpdatedEvent::class,
'feeds.bookmark.added' => BookmarkAddedEvent::class,
'feeds.bookmark.deleted' => BookmarkDeletedEvent::class,
'feeds.bookmark.updated' => BookmarkUpdatedEvent::class,
'feeds.bookmark_folder.deleted' => BookmarkFolderDeletedEvent::class,
'feeds.bookmark_folder.updated' => BookmarkFolderUpdatedEvent::class,
'feeds.comment.added' => CommentAddedEvent::class,
'feeds.comment.deleted' => CommentDeletedEvent::class,
'feeds.comment.feedback' => CommentFeedbackEvent::class,
'feeds.comment.reaction.added' => CommentReactionAddedEvent::class,
'feeds.comment.reaction.deleted' => CommentReactionDeletedEvent::class,
'feeds.comment.reaction.updated' => CommentReactionUpdatedEvent::class,
'feeds.comment.restored' => CommentRestoredEvent::class,
'feeds.comment.updated' => CommentUpdatedEvent::class,
'feeds.export.error' => AsyncExportErrorEvent::class,
'feeds.export.success' => AsyncExportFeedsSuccessEvent::class,
'feeds.feed.created' => FeedCreatedEvent::class,
'feeds.feed.deleted' => FeedDeletedEvent::class,
'feeds.feed.updated' => FeedUpdatedEvent::class,
'feeds.feed_group.changed' => FeedGroupChangedEvent::class,
'feeds.feed_group.deleted' => FeedGroupDeletedEvent::class,
'feeds.feed_group.restored' => FeedGroupRestoredEvent::class,
'feeds.feed_member.added' => FeedMemberAddedEvent::class,
'feeds.feed_member.removed' => FeedMemberRemovedEvent::class,
'feeds.feed_member.updated' => FeedMemberUpdatedEvent::class,
'feeds.follow.created' => FollowCreatedEvent::class,
'feeds.follow.deleted' => FollowDeletedEvent::class,
'feeds.follow.updated' => FollowUpdatedEvent::class,
'feeds.notification_feed.updated' => NotificationFeedUpdatedEvent::class,
'feeds.scheduled_activity.failed' => ScheduledActivityFailedEvent::class,
'feeds.scheduled_activity.published' => ScheduledActivityPublishedEvent::class,
'feeds.stories_feed.updated' => StoriesFeedUpdatedEvent::class,
'flag.updated' => FlagUpdatedEvent::class,
'import.error' => AsyncImportErrorEvent::class,
'import.success' => AsyncImportSuccessEvent::class,
'ingress.error' => IngressErrorEvent::class,
'ingress.started' => IngressStartedEvent::class,
'ingress.stopped' => IngressStoppedEvent::class,
'ingress.stream_key.rejected' => IngressStreamKeyRejectedEvent::class,
'member.added' => MemberAddedEvent::class,
'member.removed' => MemberRemovedEvent::class,
'member.updated' => MemberUpdatedEvent::class,
'message.deleted' => MessageDeletedEvent::class,
'message.flagged' => MessageFlaggedEvent::class,
'message.new' => MessageNewEvent::class,
'message.pending' => PendingMessageEvent::class,
'message.read' => MessageReadEvent::class,
'message.schema_violation' => MessageSchemaViolationEvent::class,
'message.unblocked' => MessageUnblockedEvent::class,
'message.undeleted' => MessageUndeletedEvent::class,
'message.updated' => MessageUpdatedEvent::class,
'moderation.analysis.failed' => ModerationAnalysisFailedEvent::class,
'moderation.custom_action' => ModerationCustomActionEvent::class,
'moderation.flagged' => ModerationFlaggedEvent::class,
'moderation.image_analysis.complete' => ModerationImageAnalysisCompleteEvent::class,
'moderation.mark_reviewed' => ModerationMarkReviewedEvent::class,
'moderation.text_analysis.complete' => ModerationTextAnalysisCompleteEvent::class,
'moderation_check.completed' => ModerationCheckCompletedEvent::class,
'moderation_rule.triggered' => ModerationRulesTriggeredEvent::class,
'notification.mark_unread' => NotificationMarkUnreadEvent::class,
'notification.reminder_due' => ReminderNotificationEvent::class,
'notification.thread_message_new' => NotificationThreadMessageNewEvent::class,
'poll.closed' => PollClosedEvent::class,
'poll.deleted' => PollDeletedEvent::class,
'poll.updated' => PollUpdatedEvent::class,
'poll.vote_casted' => PollVoteCastedEvent::class,
'poll.vote_changed' => PollVoteChangedEvent::class,
'poll.vote_removed' => PollVoteRemovedEvent::class,
'reaction.deleted' => ReactionDeletedEvent::class,
'reaction.new' => ReactionNewEvent::class,
'reaction.schema_violation' => ReactionSchemaViolationEvent::class,
'reaction.updated' => ReactionUpdatedEvent::class,
'reminder.created' => ReminderCreatedEvent::class,
'reminder.deleted' => ReminderDeletedEvent::class,
'reminder.updated' => ReminderUpdatedEvent::class,
'review_queue_item.new' => ReviewQueueItemNewEvent::class,
'review_queue_item.updated' => ReviewQueueItemUpdatedEvent::class,
'scheduled_message.failed' => ScheduledMessageFailedEvent::class,
'scheduled_message.sent' => ScheduledMessageSentEvent::class,
'thread.updated' => ThreadUpdatedEvent::class,
'user.banned' => UserBannedEvent::class,
'user.claimed' => UserClaimedEvent::class,
'user.deactivated' => UserDeactivatedEvent::class,
'user.deleted' => UserDeletedEvent::class,
'user.flagged' => UserFlaggedEvent::class,
'user.messages.deleted' => UserMessagesDeletedEvent::class,
'user.muted' => UserMutedEvent::class,
'user.reactivated' => UserReactivatedEvent::class,
'user.unbanned' => UserUnbannedEvent::class,
'user.unmuted' => UserUnmutedEvent::class,
'user.unread_message_reminder' => UserUnreadReminderEvent::class,
'user.updated' => UserUpdatedEvent::class,
'user_group.created' => UserGroupCreatedEvent::class,
'user_group.deleted' => UserGroupDeletedEvent::class,
'user_group.member_added' => UserGroupMemberAddedEvent::class,
'user_group.member_removed' => UserGroupMemberRemovedEvent::class,
'user_group.updated' => UserGroupUpdatedEvent::class,
];
return $eventClassMap[$eventType] ?? null;
}
/**
* Verify the HMAC-SHA256 signature of a webhook payload.
*
* @param string $body The raw request body (already-decompressed)
* @param string $signature The signature from the X-Signature header
* @param string $secret Your webhook secret (found in the Stream Dashboard)
* @return bool true if the signature is valid, false otherwise
*/
public static function verifySignature(string $body, string $signature, string $secret): bool
{
$expected = hash_hmac('sha256', $body, $secret);
return hash_equals($signature, $expected);
}
/**
* Decompress the body if it is gzip-prefixed (first two bytes 0x1F 0x8B), else
* return it unchanged.
*
* Magic-byte detection is reliable for Stream payloads because Stream webhook
* bodies are always JSON, and JSON cannot start with 0x1F.
*
* @throws InvalidWebhookException if the body has the gzip magic prefix but
* isn't a valid gzip stream, or decompresses to more than
* {@see self::$maxPayloadBytes}
*/
public static function gunzipPayload(string $body): string
{
if (\strlen($body) < 2 || \substr($body, 0, 2) !== self::GZIP_MAGIC) {
return $body;
}
$limit = self::$maxPayloadBytes;
$ctx = \inflate_init(\ZLIB_ENCODING_GZIP);
if ($ctx === false) {
throw new InvalidWebhookException(InvalidWebhookException::GZIP_FAILED);
}
$result = '';
// Small input chunks bound how far one inflate_add call can overshoot the cap.
for ($i = 0, $n = \strlen($body); $i < $n; $i += 1024) {
$piece = @\inflate_add($ctx, \substr($body, $i, 1024));
if ($piece === false) {
throw new InvalidWebhookException(InvalidWebhookException::GZIP_FAILED);
}
$result .= $piece;
if (\strlen($result) > $limit) {
throw new InvalidWebhookException(InvalidWebhookException::GZIP_FAILED . ": payload exceeds {$limit} bytes");
}
// inflate_add restarts after stream end, so stop here to ignore trailing bytes like gzdecode.
if (\inflate_get_status($ctx) === \ZLIB_STREAM_END) {
break;
}
}
if (\inflate_get_status($ctx) !== \ZLIB_STREAM_END) {
throw new InvalidWebhookException(InvalidWebhookException::GZIP_FAILED);
}
return $result;
}
/**
* Decode an SQS Message Body: try base64 first, fall back to raw bytes if
* base64 fails, then gunzip if gzip-prefixed.
*
* Wire format (per CHA-3071): SQS bodies are raw JSON when
* enable_hook_payload_compression is off (today's default for all existing
* apps), and base64(gzip(json)) when it's on. This helper handles both:
* raw JSON starts with '{' which is not valid base64, so the base64 decode
* fails and we fall through to raw bytes, then {@see self::gunzipPayload()}'s
* magic-byte detection decides whether to decompress.
*
* {@see self::parseSqs()} sits on top of this and works transparently for
* both wire formats: no caller code change, no flag, no header.
*
* @throws InvalidWebhookException if gzip decompression fails (only when input has gzip magic prefix)
*/
public static function decodeSqsPayload(string $messageBody): string
{
$decoded = \base64_decode($messageBody, true);
if ($decoded === false) {
// Not base64, so treat input as raw bytes (uncompressed wire format).
$decoded = $messageBody;
}
return self::gunzipPayload($decoded);
}
/**
* Return the inner Message field when $body is a standard SNS notification
* envelope JSON; otherwise return $body unchanged so a pre-extracted Message
* string flows through.
*
* Heuristic: try to JSON-parse the input. If it yields an array with a
* string 'Message' field, that's the envelope shape; return the Message.
* Otherwise the input is presumed to BE the pre-extracted Message
* (base64-encoded bytes are not valid JSON, so this falls through cleanly).
*/
private static function unwrapSnsNotificationBody(string $body): string
{
$env = \json_decode($body, true);
if (\is_array($env) && isset($env['Message']) && \is_string($env['Message'])) {
return $env['Message'];
}
return $body;
}
/**
* Decode an SNS notification body. Accepts either:
* - a full SNS HTTP notification envelope JSON
* ({"Type":"Notification","Message":"<base64>",...}), or
* - a pre-extracted Message string (forwarded-through-SQS path).
* The inner payload is then base64-decoded and gunzipped via
* {@see self::decodeSqsPayload()}.
*
* @throws InvalidWebhookException
*/
public static function decodeSnsPayload(string $notificationBody): string
{
return self::decodeSqsPayload(self::unwrapSnsNotificationBody($notificationBody));
}
/**
* Parse a webhook payload and return the typed event for known discriminators
* or {@see UnknownEvent} for well-formed-but-unknown ones.
*
* Distinct from {@see self::parseWebhookEvent()}: parseEvent returns an
* UnknownEvent on unrecognized discriminators (forward-compat); parseWebhookEvent
* throws.
*
* @return object the typed event class or UnknownEvent
* @throws InvalidWebhookException for invalid JSON, missing/non-string type field,
* or known-type deserialization failure
*/
public static function parseEvent(string $payload): object
{
if ($payload === '') {
throw new InvalidWebhookException(InvalidWebhookException::INVALID_JSON . ': payload must not be empty');
}
$data = \json_decode($payload, true);
if (\json_last_error() !== \JSON_ERROR_NONE) {
throw new InvalidWebhookException(InvalidWebhookException::INVALID_JSON . ': failed to parse webhook payload: ' . \json_last_error_msg());
}
if (!\is_array($data)) {
throw new InvalidWebhookException(InvalidWebhookException::INVALID_JSON . ': webhook payload must be a JSON object');
}
$eventType = $data['type'] ?? null;
if (!\is_string($eventType) || $eventType === '') {
throw new InvalidWebhookException(InvalidWebhookException::INVALID_JSON . ": webhook payload missing 'type' string field");
}
$eventClass = self::getEventClass($eventType);
if ($eventClass === null) {
$createdRaw = $data['created_at'] ?? null;
$createdAt = null;
if (\is_string($createdRaw)) {
try {
$createdAt = new \DateTimeImmutable($createdRaw);
} catch (\Throwable) {
$createdAt = null;
}
}
return new UnknownEvent($eventType, $createdAt, $data);
}
try {
return $eventClass::fromJson($data);
} catch (\Throwable $e) {
throw new InvalidWebhookException(InvalidWebhookException::INVALID_JSON . ': failed to deserialize event: ' . $e->getMessage(), 0, $e);
}
}
/**
* HTTP composite: gunzip (if gzip-prefixed) -> verify HMAC-SHA256 -> parse.
*
* The signature header is X-Signature. The signature is HMAC-SHA256 of the
* *uncompressed* JSON body, hex-encoded. Magic-byte detection means callers
* can pass either the raw HTTP body or already-decompressed bytes; both work.
*
* @return object the typed event class or UnknownEvent
* @throws InvalidWebhookException for every failure mode: signature mismatch,
* gunzip failure, JSON parse, type dispatch, or event deserialization
* failure. Filter on the exception message (or the failure-mode
* class constants) to differentiate modes.
*/
public static function verifyAndParseWebhook(string $body, string $signature, string $secret): object
{
$payload = self::gunzipPayload($body);
if (!self::verifySignature($payload, $signature, $secret)) {
throw new InvalidWebhookException(InvalidWebhookException::SIGNATURE_MISMATCH);
}
return self::parseEvent($payload);
}
/**
* SQS composite: base64-decode -> gunzip (if gzip-prefixed) -> parse.
*
* The backend emits no signature attribute on SQS messages today; this helper
* therefore performs no signature verification. If a signed variant is added later,
* it'll be a separate function rather than retrofitting this signature.
*
* @return object the typed event class or UnknownEvent
* @throws InvalidWebhookException
*/
public static function parseSqs(string $messageBody): object
{
return self::parseEvent(self::decodeSqsPayload($messageBody));
}
/**
* SNS composite: parse SNS envelope -> base64-decode -> gunzip -> parse.
* Same no-signature posture as {@see self::parseSqs()}.
*
* @return object the typed event class or UnknownEvent
* @throws InvalidWebhookException
*/
public static function parseSns(string $notificationBody): object
{
return self::parseEvent(self::decodeSnsPayload($notificationBody));
}
}
// Canonical name `GetStream\Webhook` is declared as a real subclass in
// src/Webhook.php (emitted by webhook_shim.tpl). Static analyzers can see it
// directly; the runtime class inherits every static helper declared above.