Skip to content

Commit 4a59142

Browse files
committed
Added condition to /eventsub/subscriptions addressing #227
1 parent a3fa945 commit 4a59142

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

internal/events/websocket/mock_server/close_messages.go

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ type CloseMessage struct {
66
}
77

88
var (
9+
closeClientDisconnected = &CloseMessage{
10+
code: 1000,
11+
message: "client disconnected",
12+
}
13+
914
closeInternalServerError = &CloseMessage{
1015
code: 4000,
1116
message: "internal server error",

internal/events/websocket/mock_server/manager.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ import (
2424

2525
type ServerManager struct {
2626
serverList *util.List[WebSocketServer]
27-
reconnectTesting bool
28-
primaryServer string
29-
ip string
30-
port int
31-
debugEnabled bool
32-
strictMode bool
33-
sslEnabled bool
34-
protocolHttp string
35-
protocolWs string
27+
reconnectTesting bool // Indicates if the server is in the process of running a simulation server reconnect/restart
28+
primaryServer string // The current primary server by its ID. This should be in serverList
29+
ip string // IP the server will bind to
30+
port int // Port the server will bind to
31+
debugEnabled bool // Indicates if the server was started with --debug
32+
strictMode bool // Indicates if the server was started with --require-subscriptions
33+
sslEnabled bool // Indicates if the server was started with --ssl
34+
protocolHttp string // String for the HTTP protocol URIs (http or https)
35+
protocolWs string // String for the WS protocol URIs (ws or wss)
3636
}
3737

3838
var serverManager *ServerManager
@@ -269,7 +269,7 @@ func subscriptionPageHandlerGet(w http.ResponseWriter, r *http.Request) {
269269
Status: subscription.Status,
270270
Type: subscription.Type,
271271
Version: subscription.Version,
272-
Condition: EmptyStruct{},
272+
Condition: subscription.Conditions,
273273
CreatedAt: subscription.CreatedAt,
274274
Transport: SubscriptionTransport{
275275
Method: "websocket",
@@ -378,6 +378,7 @@ func subscriptionPageHandlerPost(w http.ResponseWriter, r *http.Request) {
378378
CreatedAt: time.Now().UTC().Format(time.RFC3339Nano),
379379
Status: STATUS_ENABLED, // https://dev.twitch.tv/docs/api/reference/#get-eventsub-subscriptions
380380
SessionClientName: clientName,
381+
Conditions: body.Condition,
381382
}
382383

383384
var subs []Subscription
@@ -403,7 +404,7 @@ func subscriptionPageHandlerPost(w http.ResponseWriter, r *http.Request) {
403404
Status: subscription.Status,
404405
Type: subscription.Type,
405406
Version: subscription.Version,
406-
Condition: EmptyStruct{},
407+
Condition: subscription.Conditions,
407408
CreatedAt: subscription.CreatedAt,
408409
Transport: SubscriptionTransport{
409410
Method: "websocket",

internal/events/websocket/mock_server/server.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
223223
log.Printf("read err [%v]: %v", client.clientName, err)
224224

225225
ws.muClients.Lock()
226-
client.CloseWithReason(closeNetworkError)
227-
ws.handleClientConnectionClose(client, closeNetworkError)
226+
client.CloseWithReason(closeClientDisconnected)
227+
ws.handleClientConnectionClose(client, closeClientDisconnected)
228228
ws.muClients.Unlock()
229229
break
230230
}
@@ -488,9 +488,9 @@ func (ws *WebSocketServer) handleClientConnectionClose(client *Client, closeReas
488488
if ws.Status == 2 {
489489
ws.muSubscriptions.Lock()
490490
subscriptions := ws.Subscriptions[client.clientName]
491-
for _, subscription := range subscriptions {
492-
if subscription.Status == STATUS_ENABLED {
493-
subscription.Status = getStatusFromCloseMessage(closeReason)
491+
for i := range subscriptions {
492+
if subscriptions[i].Status == STATUS_ENABLED {
493+
subscriptions[i].Status = getStatusFromCloseMessage(closeReason)
494494
}
495495
}
496496
ws.Subscriptions[client.clientName] = subscriptions

internal/events/websocket/mock_server/subscription.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package mock_server
22

3+
import "github.com/twitchdev/twitch-cli/internal/models"
4+
35
type Subscription struct {
46
SubscriptionID string // Random GUID for the subscription
57
ClientID string // Client ID included in headers
@@ -8,14 +10,16 @@ type Subscription struct {
810
CreatedAt string // Timestamp of when the subscription was created
911
Status string // Status of the subscription
1012
SessionClientName string // Client name of the session this is associated with.
13+
14+
Conditions models.EventsubCondition // Values of the subscription's condition object
1115
}
1216

1317
// Request - POST /eventsub/subscriptions
1418
type SubscriptionPostRequest struct {
15-
Type string `json:"type"`
16-
Version string `json:"version"`
17-
Condition interface{} `json:"condition"`
19+
Type string `json:"type"`
20+
Version string `json:"version"`
1821

22+
Condition models.EventsubCondition `json:"condition"`
1923
Transport SubscriptionPostRequestTransport `json:"transport"`
2024
}
2125

@@ -44,8 +48,8 @@ type SubscriptionPostSuccessResponseBody struct {
4448
CreatedAt string `json:"created_at"`
4549
Cost int `json:"cost"`
4650

47-
Condition EmptyStruct `json:"condition"`
48-
Transport SubscriptionTransport `json:"transport"`
51+
Condition models.EventsubCondition `json:"condition"`
52+
Transport SubscriptionTransport `json:"transport"`
4953
}
5054

5155
// Response (Error) - POST /eventsub/subscriptions
@@ -69,7 +73,7 @@ type SubscriptionGetSuccessResponse struct {
6973
type SubscriptionTransport struct {
7074
Method string `json:"method"`
7175
SessionID string `json:"session_id"`
72-
ConnectedAt string `json:"connected_at"`
76+
ConnectedAt string `json:"connected_at,omitempty"`
7377
}
7478

7579
// Cross-usage
@@ -112,6 +116,8 @@ func getStatusFromCloseMessage(reason *CloseMessage) string {
112116
code := reason.code
113117

114118
switch code {
119+
case 1000:
120+
return STATUS_WEBSOCKET_DISCONNECTED
115121
case 4000:
116122
return STATUS_INTERNAL_ERROR
117123
case 4001:

0 commit comments

Comments
 (0)