Skip to content

Commit 874212d

Browse files
committed
Set created_at properly, depending on if the server uses strict mode or not. Fixes #264
1 parent 73c0259 commit 874212d

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

internal/events/websocket/mock_server/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Client struct {
1111
clientName string // Unique name for the client. Not the Client ID.
1212
conn *websocket.Conn
1313
mutex sync.Mutex
14-
ConnectedAtTimestamp string
14+
ConnectedAtTimestamp string // RFC3339Nano timestamp indicating when the client connected to the server
1515
connectionUrl string
1616

1717
mustSubscribeTimer *time.Timer

internal/events/websocket/mock_server/rpc_handler.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ func RPCFireEventSubHandler(args rpc.RPCArgs) rpc.RPCResponse {
117117
}
118118
}
119119

120-
clientName, exists := args.Variables["ClientName"]
121-
if !exists {
122-
123-
}
120+
clientName := args.Variables["ClientName"]
124121
if sessionRegex.MatchString(clientName) {
125122
// Users can include the full session_id given in the response. If they do, subtract it to just the client name
126123
clientName = sessionRegex.FindAllStringSubmatch(clientName, -1)[0][2]

internal/events/websocket/mock_server/server.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919
const KEEPALIVE_TIMEOUT_SECONDS = 10
2020

2121
type WebSocketServer struct {
22-
ServerId string // Int representing the ID of the server
23-
//ConnectionUrl string // Server's url for people to connect to. Used for messaging in reconnect testing
24-
DebugEnabled bool // Display debug messages; --debug
25-
StrictMode bool // Force stricter production-like qualities; --strict
26-
Upgrader websocket.Upgrader
22+
ServerId string // Int representing the ID of the server
23+
DebugEnabled bool // Display debug messages; --debug
24+
StrictMode bool // Force stricter production-like qualities; --strict
25+
26+
Upgrader websocket.Upgrader
2727

2828
Clients *util.List[Client] // All connected clients
2929
muClients sync.Mutex // Mutex for WebSocketServer.Clients
@@ -428,6 +428,7 @@ func (ws *WebSocketServer) HandleRPCEventSubForwarding(eventsubBody string, clie
428428
}
429429

430430
// Check for subscriptions when running with --require-subscription
431+
subscriptionCreatedAtTimestamp := "" // Used below if in strict mode
431432
if ws.StrictMode {
432433
found := false
433434
for _, clientSubscriptions := range ws.Subscriptions {
@@ -438,6 +439,7 @@ func (ws *WebSocketServer) HandleRPCEventSubForwarding(eventsubBody string, clie
438439
for _, sub := range clientSubscriptions {
439440
if sub.SessionClientName == client.clientName && sub.Type == eventObj.Subscription.Type && sub.Version == eventObj.Subscription.Version {
440441
found = true
442+
subscriptionCreatedAtTimestamp = sub.CreatedAt
441443
}
442444
}
443445
}
@@ -450,6 +452,16 @@ func (ws *WebSocketServer) HandleRPCEventSubForwarding(eventsubBody string, clie
450452
// Change payload's subscription.transport.session_id to contain the correct Session ID
451453
eventObj.Subscription.Transport.SessionID = fmt.Sprintf("%v_%v", ws.ServerId, client.clientName)
452454

455+
// Change payload's subscription.created_at to contain the correct timestamp -- https://github.com/twitchdev/twitch-cli/issues/264
456+
if ws.StrictMode {
457+
// When running WITH --require-subscription, created_at will be set to the time the subscription was created using the mock EventSub REST endpoint
458+
eventObj.Subscription.CreatedAt = subscriptionCreatedAtTimestamp
459+
} else {
460+
// When running WITHOUT --require-subscription, created_at will be set to the time the client connected
461+
// This is because without --require-subscription the server "grants" access to all event subscriptions at the moment the client is connected
462+
eventObj.Subscription.CreatedAt = client.ConnectedAtTimestamp
463+
}
464+
453465
// Build notification message
454466
notificationMsg, err := json.Marshal(
455467
NotificationMessage{

0 commit comments

Comments
 (0)