Truncate location precision on RefreshGeoIP()
This commit is contained in:
parent
6dc84bed33
commit
3f500cd019
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue
Block a user