Truncate location precision on RefreshGeoIP()

This commit is contained in:
Noah Petherbridge 2023-08-20 10:25:13 -07:00
parent 6dc84bed33
commit 3f500cd019

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"math" "math"
"net/http" "net/http"
"strconv"
"code.nonshy.com/nonshy/website/pkg/geoip" "code.nonshy.com/nonshy/website/pkg/geoip"
"code.nonshy.com/nonshy/website/pkg/log" "code.nonshy.com/nonshy/website/pkg/log"
@ -51,8 +52,8 @@ func RefreshGeoIP(userID uint64, r *http.Request) (*UserLocation, error) {
loc := GetUserLocation(userID) loc := GetUserLocation(userID)
if loc.Source == LocationSourceGeoIP { if loc.Source == LocationSourceGeoIP {
if insights, err := geoip.GetRequestInsights(r); err == nil { if insights, err := geoip.GetRequestInsights(r); err == nil {
loc.Latitude = insights.Latitude loc.Latitude = truncate(insights.Latitude)
loc.Longitude = insights.Longitude loc.Longitude = truncate(insights.Longitude)
return loc, loc.Save() return loc, loc.Save()
} else { } else {
return loc, fmt.Errorf("didn't get insights: %s", err) return loc, fmt.Errorf("didn't get insights: %s", err)
@ -61,6 +62,12 @@ func RefreshGeoIP(userID uint64, r *http.Request) (*UserLocation, error) {
return loc, nil return loc, nil
} }
func truncate(f float64) float64 {
s := strconv.FormatFloat(f, 'f', 2, 64)
f, _ = strconv.ParseFloat(s, 64)
return f
}
// MapDistances computes human readable distances between you and the set of users. // MapDistances computes human readable distances between you and the set of users.
func MapDistances(currentUser *User, others []*User) DistanceMap { func MapDistances(currentUser *User, others []*User) DistanceMap {
// Get all the distances we can. // Get all the distances we can.