go-twitch/eventsub/webhook/messages/marshal.go

273 lines
7.9 KiB
Go
Raw Permalink Normal View History

2024-03-05 12:14:18 -05:00
package messages
import (
"encoding/json"
"github.com/mitchellh/mapstructure"
"go.fifitido.net/twitch/eventsub/events"
)
func decode[T any](input map[string]any) (T, error) {
var result T
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Metadata: nil,
Result: &result,
TagName: "json",
})
if err != nil {
return result, err
}
if err := decoder.Decode(input); err != nil {
return result, err
}
return result, nil
}
var _ json.Unmarshaler = (*Notification)(nil)
func (m *Notification) UnmarshalJSON(data []byte) error {
var payload Notification
if err := json.Unmarshal(data, &payload); err != nil {
return err
}
m.Subscription = payload.Subscription
event := payload.Event.(map[string]any)
switch payload.Subscription.Type {
case "channel.update":
var err error
m.Event, err = decode[events.ChannelUpdateEvent](event)
return err
case "channel.follow":
var err error
m.Event, err = decode[events.ChannelFollowEvent](event)
return err
case "channel.ad_break.begin":
var err error
m.Event, err = decode[events.ChannelAdBreakBeginEvent](event)
return err
case "channel.chat.clear":
var err error
m.Event, err = decode[events.ChannelChatClearEvent](event)
return err
case "channel.chat.clear_user_messages":
var err error
m.Event, err = decode[events.ChannelChatClearUserMessagesEvent](event)
return err
case "channel.chat.message":
var err error
m.Event, err = decode[events.ChannelChatMessageEvent](event)
return err
case "channel.chat.message_delete":
var err error
m.Event, err = decode[events.ChannelChatMessageDeleteEvent](event)
return err
case "channel.chat.notification":
var err error
m.Event, err = decode[events.ChannelChatNotificationEvent](event)
return err
case "channel.chat_settings.update":
var err error
m.Event, err = decode[events.ChannelChatSettingsUpdateEvent](event)
return err
case "channel.subscribe":
var err error
m.Event, err = decode[events.ChannelSubscribeEvent](event)
return err
case "channel.subscription.end":
var err error
m.Event, err = decode[events.ChannelSubscriptionEndEvent](event)
return err
case "channel.subscription.gift":
var err error
m.Event, err = decode[events.ChannelSubscriptionGiftEvent](event)
return err
case "channel.subscription.message":
var err error
m.Event, err = decode[events.ChannelSubscriptionMessageEvent](event)
return err
case "channel.cheer":
var err error
m.Event, err = decode[events.ChannelCheerEvent](event)
return err
case "channel.raid":
var err error
m.Event, err = decode[events.ChannelRaidEvent](event)
return err
case "channel.ban":
var err error
m.Event, err = decode[events.ChannelBanEvent](event)
return err
case "channel.unban":
var err error
m.Event, err = decode[events.ChannelUnbanEvent](event)
return err
case "channel.moderator.add":
var err error
m.Event, err = decode[events.ChannelModeratorAddEvent](event)
return err
case "channel.moderator.remove":
var err error
m.Event, err = decode[events.ChannelModeratorRemoveEvent](event)
return err
case "channel.guest_star_session.begin":
var err error
m.Event, err = decode[events.ChannelGuestStarSessionBeginEvent](event)
return err
case "channel.guest_star_session.end":
var err error
m.Event, err = decode[events.ChannelGuestStarSessionEndEvent](event)
return err
case "channel.guest_star_guest.update":
var err error
m.Event, err = decode[events.ChannelGuestStarGuestUpdateEvent](event)
return err
case "channel.guest_star_settings.update":
var err error
m.Event, err = decode[events.ChannelGuestStarSettingsUpdateEvent](event)
return err
case "channel.channel_points_custom_reward.add":
var err error
m.Event, err = decode[events.ChannelPointsCustomRewardAddEvent](event)
return err
case "channel.channel_points_custom_reward.update":
var err error
m.Event, err = decode[events.ChannelPointsCustomRewardUpdateEvent](event)
return err
case "channel.channel_points_custom_reward.remove":
var err error
m.Event, err = decode[events.ChannelPointsCustomRewardRemoveEvent](event)
return err
case "channel.channel_points_custom_reward_redemption.add":
var err error
m.Event, err = decode[events.ChannelPointsCustomRewardRedemptionAddEvent](event)
return err
case "channel.channel_points_custom_reward_redemption.update":
var err error
m.Event, err = decode[events.ChannelPointsCustomRewardRedemptionUpdateEvent](event)
return err
case "channel.poll.begin":
var err error
m.Event, err = decode[events.ChannelPollBeginEvent](event)
return err
case "channel.poll.progress":
var err error
m.Event, err = decode[events.ChannelPollProgressEvent](event)
return err
case "channel.poll.end":
var err error
m.Event, err = decode[events.ChannelPollEndEvent](event)
return err
case "channel.prediction.begin":
var err error
m.Event, err = decode[events.ChannelPredictionBeginEvent](event)
return err
case "channel.prediction.progress":
var err error
m.Event, err = decode[events.ChannelPredictionProgressEvent](event)
return err
case "channel.prediction.lock":
var err error
m.Event, err = decode[events.ChannelPredictionLockEvent](event)
return err
case "channel.prediction.end":
var err error
m.Event, err = decode[events.ChannelPredictionEndEvent](event)
return err
case "channel.hype_train.begin":
var err error
m.Event, err = decode[events.HypeTrainBeginEvent](event)
return err
case "channel.hype_train.progress":
var err error
m.Event, err = decode[events.HypeTrainProgressEvent](event)
return err
case "channel.hype_train.end":
var err error
m.Event, err = decode[events.HypeTrainEndEvent](event)
return err
case "channel.charity_campaign.donate":
var err error
m.Event, err = decode[events.CharityDonationEvent](event)
return err
case "channel.charity_campaign.start":
var err error
m.Event, err = decode[events.CharityCampaignStartEvent](event)
return err
case "channel.charity_campaign.progress":
var err error
m.Event, err = decode[events.CharityCampaignProgressEvent](event)
return err
case "channel.charity_campaign.stop":
var err error
m.Event, err = decode[events.CharityCampaignStopEvent](event)
return err
case "channel.shield_mode.begin":
var err error
m.Event, err = decode[events.ShieldModeEvent](event)
return err
case "channel.shield_mode.end":
var err error
m.Event, err = decode[events.ShieldModeEvent](event)
return err
case "channel.shoutout.create":
var err error
m.Event, err = decode[events.ShoutoutCreateEvent](event)
return err
case "channel.shoutout.receive":
var err error
m.Event, err = decode[events.ShoutoutReceivedEvent](event)
return err
case "conduit.shard.disabled":
var err error
m.Event, err = decode[events.ConduitShardDisabledEvent](event)
return err
case "drop.entitlement.grant":
var err error
m.Event, err = decode[events.DropEntitlementGrantEvent](event)
return err
case "extension.bits_transaction.create":
var err error
m.Event, err = decode[events.ExtensionBitsTransactionCreateEvent](event)
return err
case "channel.goal.begin":
var err error
m.Event, err = decode[events.GoalsEvent](event)
return err
case "channel.goal.progress":
var err error
m.Event, err = decode[events.GoalsEvent](event)
return err
case "channel.goal.end":
var err error
m.Event, err = decode[events.GoalsEvent](event)
return err
case "stream.online":
var err error
m.Event, err = decode[events.StreamOnlineEvent](event)
return err
case "stream.offline":
var err error
m.Event, err = decode[events.StreamOfflineEvent](event)
return err
case "user.authorization.grant":
var err error
m.Event, err = decode[events.UserAuthorizationGrantEvent](event)
return err
case "user.authorization.revoke":
var err error
m.Event, err = decode[events.UserAuthorizationRevokeEvent](event)
return err
case "user.update":
var err error
m.Event, err = decode[events.UserUpdateEvent](event)
return err
}
return nil
}