From 3f500cd0190ef775f7cf2e96afb63ec5fc731898 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 20 Aug 2023 10:25:13 -0700 Subject: [PATCH] Truncate location precision on RefreshGeoIP() --- pkg/models/user_location.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/models/user_location.go b/pkg/models/user_location.go index b6da197..cc6835b 100644 --- a/pkg/models/user_location.go +++ b/pkg/models/user_location.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "net/http" + "strconv" "code.nonshy.com/nonshy/website/pkg/geoip" "code.nonshy.com/nonshy/website/pkg/log" @@ -51,8 +52,8 @@ func RefreshGeoIP(userID uint64, r *http.Request) (*UserLocation, error) { loc := GetUserLocation(userID) if loc.Source == LocationSourceGeoIP { if insights, err := geoip.GetRequestInsights(r); err == nil { - loc.Latitude = insights.Latitude - loc.Longitude = insights.Longitude + loc.Latitude = truncate(insights.Latitude) + loc.Longitude = truncate(insights.Longitude) return loc, loc.Save() } else { 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 } +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. func MapDistances(currentUser *User, others []*User) DistanceMap { // Get all the distances we can.