From 6e62d3283234b3100481466501fb8ca22a566acb Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 9 Apr 2021 01:44:25 +0200 Subject: [PATCH] Add demo-alert endpoint Signed-off-by: Knut Ahlers --- api.go | 1 + demo.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 demo.go diff --git a/api.go b/api.go index 88ab67c..dc3e902 100644 --- a/api.go +++ b/api.go @@ -101,6 +101,7 @@ var upgrader = websocket.Upgrader{ func registerAPI(r *mux.Router) { r.HandleFunc("/api/custom-alert", handleCustomAlert).Methods(http.MethodPost) r.HandleFunc("/api/custom-event", handleCustomEvent).Methods(http.MethodPost) + r.HandleFunc("/api/demo/{event}", handleDemoAlert).Methods(http.MethodPut) r.HandleFunc("/api/follows/clear-last", handleSetLastFollower).Methods(http.MethodPut) r.HandleFunc("/api/follows/set-last/{name}", handleSetLastFollower).Methods(http.MethodPut) r.HandleFunc("/api/subscribe", handleUpdateSocket).Methods(http.MethodGet) diff --git a/demo.go b/demo.go new file mode 100644 index 0000000..ff279d9 --- /dev/null +++ b/demo.go @@ -0,0 +1,82 @@ +package main + +import ( + "net/http" + + "github.com/gorilla/mux" + "github.com/pkg/errors" +) + +const demoIssuer = "Twitch-Manager" + +func handleDemoAlert(w http.ResponseWriter, r *http.Request) { + var ( + vars = mux.Vars(r) + event = vars["event"] + data interface{} + ) + + switch event { + case msgTypeBits: + data = map[string]interface{}{ + "from": demoIssuer, + "amount": 5, + "total_amount": 1337, + } + + case msgTypeHost: + data = map[string]interface{}{ + "from": demoIssuer, + "viewerCount": 5, + } + + case msgTypeRaid: + data = map[string]interface{}{ + "from": demoIssuer, + "viewerCount": 5, + } + + case msgTypeSub: + data = map[string]interface{}{ + "from": demoIssuer, + "is_resub": false, + "paid_for": "1", + "streak": "1", + "tier": "1000", + "total": "1", + } + + case "resub": // Execption to the known types to trigger resubs + event = msgTypeSub + data = map[string]interface{}{ + "from": demoIssuer, + "is_resub": true, + "paid_for": "1", + "streak": "12", + "tier": "1000", + "total": "12", + } + + case msgTypeSubGift: + data = map[string]interface{}{ + "from": demoIssuer, + "is_anon": false, + "gift_to": "Tester", + "paid_for": 1, + "streak": 1, + "tier": "1000", + "total": 1, + } + + default: + http.Error(w, "Event not found", http.StatusNotFound) + return + } + + if err := subscriptions.SendAllSockets(event, data); err != nil { + http.Error(w, errors.Wrap(err, "send to sockets").Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusCreated) +}