From 7126f6d7b75c1f5ec7dc5331d2ff90d0ce384fe2 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Wed, 22 Mar 2023 00:12:06 +0100 Subject: [PATCH] [eventsub] Switch to v2 follows topic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit as Twitch changed the policy which applications can use the v1 endpoint > Update (2023-02-23): The deprecated “Get Users Follows” API > endpoint and channel.follow (v1) EventSub subscription type now > have limited access to minimize usage in new development leading up > to the August 3 decommission date. The API endpoint is accessible > to client IDs that have made requests on or before February 17, > 2023. The v1 EventSub subscription type is accessible to client IDs > that had an active subscription on February 17, 2023. > (https://discuss.dev.twitch.tv/t/follows-endpoints-and-eventsub-subscription-type-are-now-available-in-open-beta/43322) Signed-off-by: Knut Ahlers --- pkg/twitch/eventsub.go | 23 +++++++++++++---------- twitchWatcher.go | 6 +++--- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/pkg/twitch/eventsub.go b/pkg/twitch/eventsub.go index 82c60a6..bed568f 100644 --- a/pkg/twitch/eventsub.go +++ b/pkg/twitch/eventsub.go @@ -297,7 +297,7 @@ func (e *EventSubClient) HandleEventsubPush(w http.ResponseWriter, r *http.Reque e.subscriptionsLock.RLock() defer e.subscriptionsLock.RUnlock() - cacheKey := strings.Join([]string{message.Subscription.Type, condHash}, "::") + cacheKey := strings.Join([]string{message.Subscription.Type, message.Subscription.Version, condHash}, "::") reg, ok := e.subscriptions[cacheKey] if !ok { @@ -336,8 +336,9 @@ func (e *EventSubClient) PreFetchSubscriptions(ctx context.Context) error { // so we should be able to deregister it without causing any // trouble logger := log.WithFields(log.Fields{ - "id": sub.ID, - "topic": sub.Type, + "id": sub.ID, + "topic": sub.Type, + "version": sub.Version, }) logger.Debug("Removing deprecated EventSub subscription") if err = e.twitchClient.deleteEventSubSubscription(ctx, sub.ID); err != nil { @@ -359,9 +360,10 @@ func (e *EventSubClient) PreFetchSubscriptions(ctx context.Context) error { log.WithFields(log.Fields{ "condition": sub.Condition, "type": sub.Type, + "version": sub.Version, }).Debug("found existing eventsub subscription") - cacheKey := strings.Join([]string{sub.Type, condHash}, "::") + cacheKey := strings.Join([]string{sub.Type, sub.Version, condHash}, "::") e.subscriptions[cacheKey] = ®isteredSubscription{ Type: sub.Type, Callbacks: map[string]func(json.RawMessage) error{}, @@ -383,8 +385,12 @@ func (e *EventSubClient) RegisterEventSubHooks(event, version string, condition } var ( - cacheKey = strings.Join([]string{event, condHash}, "::") - logger = log.WithField("event", event) + cacheKey = strings.Join([]string{event, version, condHash}, "::") + logger = log.WithFields(log.Fields{ + "condition": condition, + "type": event, + "version": version, + }) ) e.subscriptionsLock.RLock() @@ -404,10 +410,7 @@ func (e *EventSubClient) RegisterEventSubHooks(event, version string, condition return func() { e.unregisterCallback(cacheKey, cbKey) }, nil } - log.WithFields(log.Fields{ - "condition": condition, - "type": event, - }).Debug("registering new eventsub subscription") + logger.Debug("registering new eventsub subscription") // Register subscriptions ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout) diff --git a/twitchWatcher.go b/twitchWatcher.go index 3366f89..3ca53d0 100644 --- a/twitchWatcher.go +++ b/twitchWatcher.go @@ -125,9 +125,9 @@ func (t *twitchWatcher) getTopicRegistrations(userID string) []topicRegistration }, { Topic: twitch.EventSubEventTypeChannelFollow, - Version: twitch.EventSubTopicVersion1, // DEPRECATED, to be removed August 3, 2023 - Condition: twitch.EventSubCondition{BroadcasterUserID: userID}, - RequiredScopes: nil, // Switch to []string{twitch.ScopeModeratorReadFollowers} after August 3, 2023 + Version: twitch.EventSubTopicVersion2, + Condition: twitch.EventSubCondition{BroadcasterUserID: userID, ModeratorUserID: userID}, + RequiredScopes: []string{twitch.ScopeModeratorReadFollowers}, Hook: t.handleEventSubChannelFollow, }, {