From a785b093e7046c6338b981cc5f0168b96e41d241 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sat, 19 Aug 2023 21:09:23 -0700 Subject: [PATCH] Tweaks to Who's Nearby --- pkg/controller/account/dashboard.go | 6 ++++- pkg/controller/account/search.go | 17 ++++++++++-- pkg/geoip/geoip.go | 10 +++++++ pkg/models/user_location.go | 8 +++--- web/templates/account/dashboard.html | 39 ++++++++++++++++++++++++++++ web/templates/account/search.html | 15 +++++++++-- web/templates/account/settings.html | 19 +++++++++++++- 7 files changed, 104 insertions(+), 10 deletions(-) diff --git a/pkg/controller/account/dashboard.go b/pkg/controller/account/dashboard.go index e0e1e20..5ac41d9 100644 --- a/pkg/controller/account/dashboard.go +++ b/pkg/controller/account/dashboard.go @@ -78,7 +78,8 @@ func Dashboard() http.HandlerFunc { // Geolocation/Who's Nearby: if the current user uses GeoIP, update // their coordinates now. - if err := models.RefreshGeoIP(currentUser.ID, r); err != nil { + myLocation, err := models.RefreshGeoIP(currentUser.ID, r) + if err != nil { log.Error("RefreshGeoIP: %s", err) } @@ -92,6 +93,9 @@ func Dashboard() http.HandlerFunc { "HasPublicPhoto": hasPublic, "PhotoLikeMap": models.MapLikes(currentUser, "photos", photoIDs), + + // Who's Nearby stats. + "MyLocation": myLocation, } if err := tmpl.Execute(w, r, vars); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/pkg/controller/account/search.go b/pkg/controller/account/search.go index 71a57fe..6d14695 100644 --- a/pkg/controller/account/search.go +++ b/pkg/controller/account/search.go @@ -5,6 +5,8 @@ import ( "strconv" "code.nonshy.com/nonshy/website/pkg/config" + "code.nonshy.com/nonshy/website/pkg/geoip" + "code.nonshy.com/nonshy/website/pkg/log" "code.nonshy.com/nonshy/website/pkg/models" "code.nonshy.com/nonshy/website/pkg/session" "code.nonshy.com/nonshy/website/pkg/templates" @@ -51,6 +53,13 @@ func Search() http.HandlerFunc { return } + // Geolocation/Who's Nearby: if the current user uses GeoIP, update + // their coordinates now. + myLocation, err := models.RefreshGeoIP(currentUser.ID, r) + if err != nil { + log.Error("RefreshGeoIP: %s", err) + } + // Sort options. for _, v := range sortWhitelist { if sort == v { @@ -87,6 +96,9 @@ func Search() http.HandlerFunc { session.FlashError(w, r, "Couldn't search users: %s", err) } + // Who's Nearby feature, get some data. + insights, _ := geoip.GetRequestInsights(r) + var vars = map[string]interface{}{ "Users": users, "Pager": pager, @@ -106,8 +118,9 @@ func Search() http.HandlerFunc { "PhotoCountMap": models.MapPhotoCounts(users), // Current user's location setting. - "MyLocation": models.GetUserLocation(currentUser.ID), - "DistanceMap": models.MapDistances(currentUser, users), + "MyLocation": myLocation, + "GeoIPInsights": insights, + "DistanceMap": models.MapDistances(currentUser, users), } if err := tmpl.Execute(w, r, vars); err != nil { diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index 4593962..d952e74 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -43,6 +43,16 @@ func (i Insights) String() string { return strings.Join(parts, "; ") } +// Short prints a short summary string of the insights. +func (i Insights) Short() string { + var parts = []string{ + i.CountryName, + strings.Join(i.Subdivisions, ", "), + i.City, + } + return strings.Join(parts, "; ") +} + // GetRequestInsights returns structured insights based on the current HTTP request. func GetRequestInsights(r *http.Request) (Insights, error) { var ( diff --git a/pkg/models/user_location.go b/pkg/models/user_location.go index 91e3980..b6da197 100644 --- a/pkg/models/user_location.go +++ b/pkg/models/user_location.go @@ -47,18 +47,18 @@ func (ul *UserLocation) Save() error { } // RefreshGeoIP will auto-update a user's location by GeoIP if that's their setting. -func RefreshGeoIP(userID uint64, r *http.Request) error { +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 - return loc.Save() + return loc, loc.Save() } else { - return fmt.Errorf("didn't get insights: %s", err) + return loc, fmt.Errorf("didn't get insights: %s", err) } } - return nil + return loc, nil } // MapDistances computes human readable distances between you and the set of users. diff --git a/web/templates/account/dashboard.html b/web/templates/account/dashboard.html index 6e0c25d..84ec34d 100644 --- a/web/templates/account/dashboard.html +++ b/web/templates/account/dashboard.html @@ -133,6 +133,45 @@ {{end}} + + {{if not .MyLocation.Source}} +
+
+

+ + New Feature: Who's Nearby? +

+
+ +
+

+ We've recently added a new feature! Who's Nearby can allow you to sort the + Member Directory by their distance away from you. +

+ +

+ First, you'll need to opt-in where your location is so that the site can know who's + nearby. Please visit your Location Settings page to choose + how you share your location -- you can even just drop a pin on a map anywhere you're comfortable + with! +

+ +

+ Then, you'll be able to sort the Member Directory by distance. + Only people whose location is known will show in the results. +

+ +

+ This feature is very privacy-conscious and you can turn it off again later, and we'll forget + any location data we had! For more information, see this forum thread. + This message will go away after you have set a location source + for your profile -- or after a few weeks when enough people have had a chance to hear about + the new feature! +

+
+
+ {{end}} +