Skip to content

Commit

Permalink
Adds lobbycode endpoint for external integrations (Seb!)
Browse files Browse the repository at this point in the history
  • Loading branch information
denverquane committed Nov 29, 2020
1 parent 9ae19a9 commit 400b8a3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
83 changes: 83 additions & 0 deletions broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -292,10 +293,90 @@ func (broker *Broker) Start(port string) {
w.Write(jsonBytes)
})

router.HandleFunc("/lobbycode/{connectCode}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
conncode := vars["connectCode"]

if conncode == "" {
errorResponse(w)
return
}
cursor := uint64(0)
keys := []string{}
for len(keys) == 0 {
keys, cursor, err = broker.client.Scan(context.Background(), cursor, "automuteus:discord:*:"+conncode, 10).Result()
if cursor == 0 {
break
}
i := 0
for _, v := range keys {
if !strings.Contains(v, ":pointer:") {
keys[i] = v
i++
}
}
}

if err != nil || len(keys) == 0 {
log.Println(err)
w.WriteHeader(http.StatusNotFound)
return
}
key := keys[0]
res, err := broker.client.Get(context.Background(), key).Result()
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusNotFound)
return
} else {
jsonVars := map[string]interface{}{}
err := json.Unmarshal([]byte(res), &jsonVars)
if err != nil || jsonVars["amongUsData"] == nil {
errorResponse(w)
return
}

//this is some ugly casting
auData := jsonVars["amongUsData"].(map[string]interface{})

if auData["room"] == nil {
errorResponse(w)
return
}

r := Resp{Result: auData["room"].(string)}
jbytes, err := json.Marshal(r)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
w.Write(jbytes)
}
return
}
})
log.Printf("Message broker is running on port %s...\n", port)
log.Fatal(http.ListenAndServe(":"+port, router))
}

type Resp struct {
Result string `json:"result"`
}

func errorResponse(w http.ResponseWriter) {
w.WriteHeader(http.StatusBadRequest)
r := Resp{Result: "error"}
jbytes, err := json.Marshal(r)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
} else {
w.Write(jbytes)
}
return
}

func totalGuildsKey(version string) string {
return "automuteus:count:guilds:version-" + version
}
Expand All @@ -309,6 +390,8 @@ func commitKey() string {
return "automuteus:commit"
}

///////

func GetVersionAndCommit(client *redis.Client) (string, string) {
v, err := client.Get(ctx, versionKey()).Result()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions galactus/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const AckFromClientCapturesTimeout = time.Second
var DefaultIdentifyThresholds = discord.IdentifyThresholds{
HardWindow: time.Hour * 24,
HardThreshold: 950,
SoftWindow: time.Hour * 2,
SoftThreshold: 80,
SoftWindow: time.Hour * 12,
SoftThreshold: 500,
}

var ctx = context.Background()
Expand Down

0 comments on commit 400b8a3

Please sign in to comment.