2022-08-14 05:44:57 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-08-14 21:40:57 +00:00
|
|
|
"strconv"
|
2022-08-14 05:44:57 +00:00
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
2023-08-20 04:09:23 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/geoip"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2022-08-14 05:44:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Search controller.
|
|
|
|
func Search() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("account/search.html")
|
2022-08-14 21:40:57 +00:00
|
|
|
|
|
|
|
// Whitelist for ordering options.
|
|
|
|
var sortWhitelist = []string{
|
|
|
|
"last_login_at desc",
|
|
|
|
"created_at desc",
|
|
|
|
"username",
|
2023-08-30 04:10:00 +00:00
|
|
|
"username desc",
|
2022-08-14 21:40:57 +00:00
|
|
|
"lower(name)",
|
2023-08-30 04:10:00 +00:00
|
|
|
"lower(name) desc",
|
2023-08-20 02:11:33 +00:00
|
|
|
"distance",
|
2022-08-14 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Search filters.
|
|
|
|
var (
|
|
|
|
isCertified = r.FormValue("certified")
|
2023-08-16 00:33:33 +00:00
|
|
|
username = r.FormValue("username") // username search
|
2022-08-14 05:44:57 +00:00
|
|
|
gender = r.FormValue("gender")
|
|
|
|
orientation = r.FormValue("orientation")
|
|
|
|
maritalStatus = r.FormValue("marital_status")
|
2023-08-30 04:10:00 +00:00
|
|
|
hereFor = r.FormValue("here_for")
|
2023-09-02 00:12:27 +00:00
|
|
|
friendSearch = r.FormValue("friends") == "true"
|
2022-08-14 21:40:57 +00:00
|
|
|
sort = r.FormValue("sort")
|
|
|
|
sortOK bool
|
|
|
|
ageMin int
|
|
|
|
ageMax int
|
2022-08-14 05:44:57 +00:00
|
|
|
)
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
ageMin, _ = strconv.Atoi(r.FormValue("age_min"))
|
|
|
|
ageMax, _ = strconv.Atoi(r.FormValue("age_max"))
|
|
|
|
if ageMin > ageMax {
|
|
|
|
ageMin, ageMax = ageMax, ageMin
|
|
|
|
}
|
|
|
|
|
2022-08-15 00:45:55 +00:00
|
|
|
// Get current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get current user!")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-20 04:09:23 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// Sort options.
|
|
|
|
for _, v := range sortWhitelist {
|
|
|
|
if sort == v {
|
|
|
|
sortOK = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sortOK {
|
|
|
|
sort = "last_login_at desc"
|
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
// Default
|
|
|
|
if isCertified == "" {
|
|
|
|
isCertified = "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
pager := &models.Pagination{
|
|
|
|
PerPage: config.PageSizeMemberSearch,
|
2022-08-14 21:40:57 +00:00
|
|
|
Sort: sort,
|
2022-08-14 05:44:57 +00:00
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
|
|
|
|
2022-09-09 04:42:20 +00:00
|
|
|
users, err := models.SearchUsers(currentUser, &models.UserSearch{
|
2023-08-16 00:33:33 +00:00
|
|
|
Username: username,
|
|
|
|
Gender: gender,
|
|
|
|
Orientation: orientation,
|
|
|
|
MaritalStatus: maritalStatus,
|
2023-08-30 04:10:00 +00:00
|
|
|
HereFor: hereFor,
|
2023-09-02 00:20:34 +00:00
|
|
|
Certified: isCertified == "true",
|
|
|
|
NotCertified: isCertified == "false",
|
2023-08-16 00:33:33 +00:00
|
|
|
InnerCircle: isCertified == "circle",
|
2023-09-02 00:12:27 +00:00
|
|
|
ShyAccounts: isCertified == "shy",
|
|
|
|
Friends: friendSearch,
|
2023-08-16 00:33:33 +00:00
|
|
|
AgeMin: ageMin,
|
|
|
|
AgeMax: ageMax,
|
2022-08-14 05:44:57 +00:00
|
|
|
}, pager)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't search users: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-08-20 04:09:23 +00:00
|
|
|
// Who's Nearby feature, get some data.
|
|
|
|
insights, _ := geoip.GetRequestInsights(r)
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
var vars = map[string]interface{}{
|
|
|
|
"Users": users,
|
|
|
|
"Pager": pager,
|
|
|
|
"Enum": config.ProfileEnums,
|
|
|
|
|
|
|
|
// Search filter values.
|
|
|
|
"Certified": isCertified,
|
|
|
|
"Gender": gender,
|
|
|
|
"Orientation": orientation,
|
|
|
|
"MaritalStatus": maritalStatus,
|
2023-08-30 04:10:00 +00:00
|
|
|
"HereFor": hereFor,
|
2022-08-14 05:44:57 +00:00
|
|
|
"EmailOrUsername": username,
|
2022-08-14 21:40:57 +00:00
|
|
|
"AgeMin": ageMin,
|
|
|
|
"AgeMax": ageMax,
|
2023-09-02 00:12:27 +00:00
|
|
|
"FriendSearch": friendSearch,
|
2022-08-14 21:40:57 +00:00
|
|
|
"Sort": sort,
|
2023-07-23 22:02:41 +00:00
|
|
|
|
|
|
|
// Photo counts mapped to users
|
|
|
|
"PhotoCountMap": models.MapPhotoCounts(users),
|
2023-08-20 02:11:33 +00:00
|
|
|
|
2023-09-02 00:12:27 +00:00
|
|
|
// Map Shy Account badges for these results
|
|
|
|
"ShyMap": models.MapShyAccounts(users),
|
|
|
|
|
|
|
|
// Map friendships to these users.
|
|
|
|
"FriendMap": models.MapFriends(currentUser, users),
|
|
|
|
|
2023-08-20 02:11:33 +00:00
|
|
|
// Current user's location setting.
|
2023-08-20 04:09:23 +00:00
|
|
|
"MyLocation": myLocation,
|
|
|
|
"GeoIPInsights": insights,
|
|
|
|
"DistanceMap": models.MapDistances(currentUser, users),
|
2022-08-14 05:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|