Truncate location precision on RefreshGeoIP()

face-detect
Noah Petherbridge 2023-08-20 10:25:13 -07:00
parent 6dc84bed33
commit 3f500cd019
1 changed files with 9 additions and 2 deletions

View File

@ -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.