mirror of
https://github.com/Xevion/banner.git
synced 2025-12-08 14:06:27 -06:00
Finish proper term archival check func
This commit is contained in:
5
api.go
5
api.go
@@ -24,6 +24,11 @@ type Pair struct {
|
|||||||
type BannerTerm Pair
|
type BannerTerm Pair
|
||||||
type Instructor Pair
|
type Instructor Pair
|
||||||
|
|
||||||
|
// Archived returns true if the term is in it's archival state (view only)
|
||||||
|
func (term BannerTerm) Archived() bool {
|
||||||
|
return strings.Contains(term.Description, "View Only")
|
||||||
|
}
|
||||||
|
|
||||||
// GetTerms retrieves and parses the term information for a given search term.
|
// GetTerms retrieves and parses the term information for a given search term.
|
||||||
// Ensure that the offset is greater than 0.
|
// Ensure that the offset is greater than 0.
|
||||||
func GetTerms(search string, offset int, max int) ([]BannerTerm, error) {
|
func GetTerms(search string, offset int, max int) ([]BannerTerm, error) {
|
||||||
|
|||||||
32
helpers.go
32
helpers.go
@@ -16,6 +16,7 @@ import (
|
|||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
log "github.com/rs/zerolog/log"
|
log "github.com/rs/zerolog/log"
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BuildRequestWithBody builds a request with the given method, path, parameters, and body
|
// BuildRequestWithBody builds a request with the given method, path, parameters, and body
|
||||||
@@ -369,9 +370,36 @@ func EncodeParams(params map[string]*[]string) string {
|
|||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add a function to check if a term is view-only
|
var terms []BannerTerm
|
||||||
|
var lastTermUpdate time.Time
|
||||||
|
|
||||||
|
// IsTermArchived checks if the given term is archived
|
||||||
|
// TODO: Add error, switch missing term logic to error
|
||||||
func IsTermArchived(term string) bool {
|
func IsTermArchived(term string) bool {
|
||||||
return false
|
// If the terms are not loaded, or the last update was more than 4 hours ago, update the terms
|
||||||
|
if len(terms) == 0 || time.Since(lastTermUpdate) > 4*time.Hour {
|
||||||
|
// Load the terms
|
||||||
|
var err error
|
||||||
|
terms, err = GetTerms("", 1, 10)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("Failed to get terms")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
lastTermUpdate = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the term is in the list of terms
|
||||||
|
bannerTerm, exists := lo.Find(terms, func(t BannerTerm) bool {
|
||||||
|
return t.Code == term
|
||||||
|
})
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
log.Warn().Str("term", term).Msg("Term does not exist")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return bannerTerm.Archived()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Point represents a point in 2D space
|
// Point represents a point in 2D space
|
||||||
|
|||||||
Reference in New Issue
Block a user