2021-04-08 23:44:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-06-16 16:38:01 +00:00
|
|
|
"strconv"
|
2021-04-22 14:02:16 +00:00
|
|
|
"time"
|
2021-04-08 23:44:25 +00:00
|
|
|
|
|
|
|
"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,
|
2021-06-16 16:38:01 +00:00
|
|
|
"amount": demoGetParamInt(r, "amount", 500),
|
|
|
|
"message": demoGetParamStr(r, "message", "ShowLove500 Thanks for the Stream! myuserHype"),
|
|
|
|
"total_amount": demoGetParamInt(r, "total_amount", 1337),
|
2021-04-08 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 17:13:08 +00:00
|
|
|
case msgTypeDonation:
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"name": demoIssuer,
|
2021-06-16 16:38:01 +00:00
|
|
|
"amount": demoGetParamFloat(r, "amount", 6.66),
|
|
|
|
"message": demoGetParamStr(r, "message", "You rock!"),
|
2021-05-04 17:13:08 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 14:02:16 +00:00
|
|
|
case msgTypeFollow:
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"from": demoIssuer,
|
|
|
|
"followed_at": time.Now(),
|
|
|
|
}
|
|
|
|
|
2021-04-08 23:44:25 +00:00
|
|
|
case msgTypeHost:
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"from": demoIssuer,
|
2021-06-16 16:38:01 +00:00
|
|
|
"viewerCount": demoGetParamInt(r, "viewerCount", 5),
|
2021-04-08 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case msgTypeRaid:
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"from": demoIssuer,
|
2021-06-16 16:38:01 +00:00
|
|
|
"viewerCount": demoGetParamInt(r, "viewerCount", 5),
|
2021-04-08 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case msgTypeSub:
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"from": demoIssuer,
|
|
|
|
"is_resub": false,
|
2021-05-27 22:29:50 +00:00
|
|
|
"message": "",
|
2021-06-16 16:38:01 +00:00
|
|
|
"paid_for": demoGetParamInt(r, "paid_for", 1),
|
|
|
|
"streak": demoGetParamInt(r, "streak", 1),
|
|
|
|
"tier": demoGetParamStr(r, "tier", "1000"),
|
|
|
|
"total": 1,
|
2021-04-08 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "resub": // Execption to the known types to trigger resubs
|
|
|
|
event = msgTypeSub
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"from": demoIssuer,
|
|
|
|
"is_resub": true,
|
2021-06-16 16:38:01 +00:00
|
|
|
"message": demoGetParamStr(r, "message", "Already 12 months! PogChamp"),
|
|
|
|
"paid_for": demoGetParamInt(r, "paid_for", 1),
|
|
|
|
"streak": demoGetParamInt(r, "streak", 12),
|
|
|
|
"tier": demoGetParamStr(r, "tier", "1000"),
|
|
|
|
"total": demoGetParamInt(r, "total", 12),
|
2021-04-08 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case msgTypeSubGift:
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"from": demoIssuer,
|
2021-06-16 16:38:01 +00:00
|
|
|
"is_anon": demoGetParamStr(r, "is_anon", "false") == "true",
|
|
|
|
"gift_to": demoGetParamStr(r, "gift_to", "Tester"),
|
|
|
|
"paid_for": demoGetParamInt(r, "paid_for", 1),
|
|
|
|
"streak": demoGetParamInt(r, "streak", 12),
|
|
|
|
"tier": demoGetParamStr(r, "tier", "1000"),
|
|
|
|
"total": demoGetParamInt(r, "total", 12),
|
2021-04-08 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
http.Error(w, "Event not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-09 16:39:30 +00:00
|
|
|
if err := subscriptions.SendAllSockets(event, data, false, false); err != nil {
|
2021-04-08 23:44:25 +00:00
|
|
|
http.Error(w, errors.Wrap(err, "send to sockets").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
}
|
2021-06-16 16:38:01 +00:00
|
|
|
|
|
|
|
func demoGetParamFloat(r *http.Request, key string, fallback float64) float64 {
|
|
|
|
v := r.FormValue(key)
|
|
|
|
if v == "" {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
vi, err := strconv.ParseFloat(v, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return vi
|
|
|
|
}
|
|
|
|
|
|
|
|
func demoGetParamInt(r *http.Request, key string, fallback int) int {
|
|
|
|
v := r.FormValue(key)
|
|
|
|
if v == "" {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
vi, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return vi
|
|
|
|
}
|
|
|
|
|
|
|
|
func demoGetParamStr(r *http.Request, key, fallback string) string {
|
|
|
|
v := r.FormValue(key)
|
|
|
|
if v == "" {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|