Skip to content

Commit

Permalink
Set created_at properly, depending on if the server uses strict mode …
Browse files Browse the repository at this point in the history
…or not. Fixes #264
  • Loading branch information
Xemdo committed Sep 9, 2023
1 parent 73c0259 commit 874212d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/events/websocket/mock_server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Client struct {
clientName string // Unique name for the client. Not the Client ID.
conn *websocket.Conn
mutex sync.Mutex
ConnectedAtTimestamp string
ConnectedAtTimestamp string // RFC3339Nano timestamp indicating when the client connected to the server
connectionUrl string

mustSubscribeTimer *time.Timer
Expand Down
5 changes: 1 addition & 4 deletions internal/events/websocket/mock_server/rpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ func RPCFireEventSubHandler(args rpc.RPCArgs) rpc.RPCResponse {
}
}

clientName, exists := args.Variables["ClientName"]
if !exists {

}
clientName := args.Variables["ClientName"]
if sessionRegex.MatchString(clientName) {
// Users can include the full session_id given in the response. If they do, subtract it to just the client name
clientName = sessionRegex.FindAllStringSubmatch(clientName, -1)[0][2]
Expand Down
22 changes: 17 additions & 5 deletions internal/events/websocket/mock_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
const KEEPALIVE_TIMEOUT_SECONDS = 10

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

Upgrader websocket.Upgrader

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

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

// Change payload's subscription.created_at to contain the correct timestamp -- https://github.com/twitchdev/twitch-cli/issues/264
if ws.StrictMode {
// When running WITH --require-subscription, created_at will be set to the time the subscription was created using the mock EventSub REST endpoint
eventObj.Subscription.CreatedAt = subscriptionCreatedAtTimestamp
} else {
// When running WITHOUT --require-subscription, created_at will be set to the time the client connected
// This is because without --require-subscription the server "grants" access to all event subscriptions at the moment the client is connected
eventObj.Subscription.CreatedAt = client.ConnectedAtTimestamp
}

// Build notification message
notificationMsg, err := json.Marshal(
NotificationMessage{
Expand Down

0 comments on commit 874212d

Please sign in to comment.