[eventsub] Switch to v2 follows topic

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 <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-03-22 00:12:06 +01:00
parent f066b3fd2a
commit 7126f6d7b7
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
2 changed files with 16 additions and 13 deletions

View file

@ -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] = &registeredSubscription{
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)

View file

@ -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,
},
{