Validate location existence, validate code pattern

This commit is contained in:
2024-06-09 18:44:26 -05:00
parent 87e7da60fa
commit 92849b83ba
2 changed files with 16 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"regexp"
"strconv"
"time"
@@ -15,6 +16,8 @@ import (
// In order for the modal submission to be useful, the context for it's initial request must be stored.
var SubmissionContexts = timedmap.New(5 * time.Minute)
var codePattern = regexp.MustCompile(`^[a-zA-Z0-9]{4,12}$`)
var CodeCommandDefinition = &discordgo.ApplicationCommand{
Name: "code",
Description: "Set the guest code for a given location",
@@ -51,9 +54,12 @@ func CodeCommandHandler(session *discordgo.Session, interaction *discordgo.Inter
code := data.Options[1].StringValue()
userId, _ := strconv.Atoi(interaction.Member.User.ID)
// TODO: Validate that the location exists
// TODO: Validate that the code has no invalid characters
already_set := StoreCode(code, int64(location_id), user_id)
// Validate that the location exists
if !LocationExists(int64(locationId)) {
HandleError(session, interaction, nil, "The location provided does not exist.")
return
}
// Validate that the code has no invalid characters
if !codePattern.MatchString(code) {
HandleError(session, interaction, nil, "The code provided contains invalid characters.")

View File

@@ -4,6 +4,13 @@ import (
"fmt"
)
// LocationExists checks if a location identifier is valid (as known by the cache).
// Cache rarely will change, so this is a good way to check if a location is valid.
func LocationExists(location int64) bool {
_, ok := cachedLocationsMap[uint(location)]
return ok
}
func StoreCode(code string, location int64, member_id int) bool {
key := fmt.Sprintf("code:%d:%d", location, member_id)
already_set := db.Exists(key).Val() == 1