Fix country flag with code for chat room

face-detect
Noah Petherbridge 2023-08-05 20:11:55 -07:00
parent 0c5db949aa
commit 79a6b8048c
2 changed files with 16 additions and 1 deletions

View File

@ -98,7 +98,7 @@ func Landing() http.HandlerFunc {
}
// Country flag emoji.
emoji, err := geoip.GetRequestCountryFlag(r)
emoji, err := geoip.GetRequestCountryFlagWithCode(r)
if err != nil {
emoji, err = geoip.CountryFlagEmojiWithCode("US")
if err != nil {

View File

@ -36,6 +36,21 @@ func GetRequestCountryFlag(r *http.Request) (string, error) {
return CountryFlagEmoji(city.Country.IsoCode)
}
// GetRequestCountryFlagWithCode returns the flag joined with the country code by a space (like CountryFlagEmojiWithCode).
func GetRequestCountryFlagWithCode(r *http.Request) (string, error) {
city, err := GetRequestCity(r)
if err != nil {
// If the remote addr is localhost (local dev testing), default to US flag.
if addr := utility.IPAddress(r); addr == "127.0.0.1" || addr == "::1" {
return CountryFlagEmojiWithCode("US")
}
return "", err
}
return CountryFlagEmojiWithCode(city.Country.IsoCode)
}
// GetCity queries the GeoIP database for city information for an IP address.
func GetCity(ip net.IP) (*geoip2.City, error) {
db, err := geoip2.Open(config.GeoIPPath)