From 8754ed85925256ac28852472693c792f1f963f60 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Wed, 26 Jun 2024 21:27:03 -0700 Subject: [PATCH] Search users by "Liked" --- pkg/controller/account/search.go | 14 +++++++++++++- pkg/controller/api/likes.go | 1 - pkg/models/user.go | 15 +++++++++++++++ web/static/js/slim-forms.js | 18 ++++++++++++++++++ web/templates/account/search.html | 23 +++++++++++++++++++++-- web/templates/base.html | 1 + 6 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 web/static/js/slim-forms.js diff --git a/pkg/controller/account/search.go b/pkg/controller/account/search.go index 68a67f9..0e4d51f 100644 --- a/pkg/controller/account/search.go +++ b/pkg/controller/account/search.go @@ -39,6 +39,7 @@ func Search() http.HandlerFunc { maritalStatus = r.FormValue("marital_status") hereFor = r.FormValue("here_for") friendSearch = r.FormValue("friends") == "true" + likedSearch = r.FormValue("liked") == "true" sort = r.FormValue("sort") sortOK bool ) @@ -110,6 +111,7 @@ func Search() http.HandlerFunc { IsDisabled: isCertified == "disabled", IsAdmin: isCertified == "admin", Friends: friendSearch, + Liked: likedSearch, AgeMin: ageMin, AgeMax: ageMax, }, pager) @@ -122,8 +124,16 @@ func Search() http.HandlerFunc { // Collect usernames to map to chat online status. var usernames = []string{} + var userIDs = []uint64{} for _, user := range users { usernames = append(usernames, user.Username) + userIDs = append(userIDs, user.ID) + } + + // User IDs of these I have "Liked" + likedIDs, err := models.LikedIDs(currentUser, "users", userIDs) + if err != nil { + log.Error("LikedIDs: %s", err) } var vars = map[string]interface{}{ @@ -142,6 +152,7 @@ func Search() http.HandlerFunc { "AgeMin": ageMin, "AgeMax": ageMax, "FriendSearch": friendSearch, + "LikedSearch": likedSearch, "Sort": sort, // Photo counts mapped to users @@ -150,8 +161,9 @@ func Search() http.HandlerFunc { // Map Shy Account badges for these results "ShyMap": models.MapShyAccounts(users), - // Map friendships to these users. + // Map friendships and likes to these users. "FriendMap": models.MapFriends(currentUser, users), + "LikedMap": models.MapLikes(currentUser, "users", likedIDs), // Users on the chat room map. "UserOnChatMap": worker.GetChatStatistics().MapUsersOnline(usernames), diff --git a/pkg/controller/api/likes.go b/pkg/controller/api/likes.go index 3e626f4..dc97c1b 100644 --- a/pkg/controller/api/likes.go +++ b/pkg/controller/api/likes.go @@ -127,7 +127,6 @@ func Likes() http.HandlerFunc { log.Error("subject is users, find %d", tableID) if user, err := models.GetUser(tableID); err == nil { targetUser = user - log.Warn("found user %s", targetUser.Username) // Blocking safety check: if either user blocks the other, liking is not allowed. if models.IsBlocking(currentUser.ID, user.ID) { diff --git a/pkg/models/user.go b/pkg/models/user.go index bc1ff34..76022ac 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -267,6 +267,7 @@ type UserSearch struct { IsDisabled bool IsAdmin bool // search for admin users Friends bool + Liked bool AgeMin int AgeMax int } @@ -451,6 +452,20 @@ func SearchUsers(user *User, search *UserSearch, pager *Pagination) ([]*User, er ) } + if search.Liked { + wheres = append(wheres, ` + EXISTS ( + SELECT 1 FROM likes + WHERE user_id = ? + AND table_name = 'users' + AND table_id = users.id + ) + `) + placeholders = append(placeholders, + user.ID, + ) + } + if search.AgeMin > 0 { date := time.Now().AddDate(-search.AgeMin, 0, 0) wheres = append(wheres, ` diff --git a/web/static/js/slim-forms.js b/web/static/js/slim-forms.js new file mode 100644 index 0000000..5348bbc --- /dev/null +++ b/web/static/js/slim-forms.js @@ -0,0 +1,18 @@ +/* Slim "GET" forms: an onSubmit handler that trims empty query parameters. */ +document.addEventListener('DOMContentLoaded', (e) => { + (document.querySelectorAll("form") || []).forEach(form => { + + // Find forms with method="GET" + let method = form.method || "GET"; + if (method.toUpperCase() !== "GET") { + return; + }; + + // Trim their empty parameters. + form.addEventListener("submit", (e) => { + for (let member of form.elements) { + if (!member.value) member.disabled = true; + } + }) + }); +}); \ No newline at end of file diff --git a/web/templates/account/search.html b/web/templates/account/search.html index abef4e4..4c5a94f 100644 --- a/web/templates/account/search.html +++ b/web/templates/account/search.html @@ -230,7 +230,7 @@
- + +
@@ -287,13 +295,24 @@ {{if $Root.FriendMap.Get .ID}} -
+
Friends
{{end}} + + + {{$LikedStats := $Root.LikedMap.Get .ID}} + {{if $LikedStats.UserLikes}} +
+ + + Liked + +
+ {{end}}

diff --git a/web/templates/base.html b/web/templates/base.html index c39e937..e13a305 100644 --- a/web/templates/base.html +++ b/web/templates/base.html @@ -386,6 +386,7 @@ + {{template "scripts" .}}