Test update to chat country flags

This commit is contained in:
Noah Petherbridge 2023-08-15 20:56:22 -07:00
parent 1ee8acf060
commit ece83a0b23
2 changed files with 34 additions and 1 deletions

View File

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

View File

@ -51,6 +51,39 @@ func GetRequestCountryFlagWithCode(r *http.Request) (string, error) {
return CountryFlagEmojiWithCode(city.Country.IsoCode)
}
// GetChatFlagEmoji returns a specialized country flag emoji string for the BareRTC chat room.
//
// This will include the country flag emoji along with the country and territory/state name.
func GetChatFlagEmoji(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 and only "US" code.
if addr := utility.IPAddress(r); addr == "127.0.0.1" || addr == "::1" {
return CountryFlagEmojiWithCode("US")
}
return "", err
}
// Codes to attach (state, country, etc.)
emoji, err := CountryFlagEmoji(city.Country.IsoCode)
if err != nil {
return emoji, err
}
var flags = []string{
emoji,
city.Country.IsoCode,
}
if len(city.Subdivisions) > 0 {
for _, sub := range city.Subdivisions {
flags = append(flags, sub.IsoCode)
}
}
return strings.Join(flags, " "), nil
}
// 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)