Search users by "Liked"

This commit is contained in:
Noah Petherbridge 2024-06-26 21:27:03 -07:00
parent 72b6e2a616
commit 8754ed8592
6 changed files with 68 additions and 4 deletions

View File

@ -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),

View File

@ -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) {

View File

@ -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, `

View File

@ -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;
}
})
});
});

View File

@ -230,7 +230,7 @@
<div class="column px-1">
<div class="field">
<label class="label" for="friends">Friendship:</label>
<label class="label" for="friends">Miscellanous:</label>
<label class="checkbox">
<input type="checkbox"
name="friends"
@ -239,6 +239,14 @@
{{if .FriendSearch}}checked{{end}}>
Show only my friends
</label>
<label class="checkbox">
<input type="checkbox"
name="liked"
id="liked"
value="true"
{{if .LikedSearch}}checked{{end}}>
Show only my "Likes"
</label>
</div>
</div>
@ -287,13 +295,24 @@
<!-- Friendship badge -->
{{if $Root.FriendMap.Get .ID}}
<div class="has-text-centered">
<div>
<span class="is-size-7 has-text-warning">
<i class="fa fa-user-group" title="Friends"></i>
Friends
</span>
</div>
{{end}}
<!-- Liked badge -->
{{$LikedStats := $Root.LikedMap.Get .ID}}
{{if $LikedStats.UserLikes}}
<div>
<span class="is-size-7">
<i class="fa fa-user-group has-text-danger" title="Friends"></i>
Liked
</span>
</div>
{{end}}
</div>
<div class="media-content">
<p class="title is-4">

View File

@ -386,6 +386,7 @@
<script type="text/javascript" src="/static/js/likes.js?build={{.BuildHash}}"></script>
<script type="text/javascript" src="/static/js/vue-3.2.45.js"></script>
<script type="text/javascript" src="/static/js/htmx-1.9.12.min.js"></script>
<script type="text/javascript" src="/static/js/slim-forms.js?build={{.BuildHash}}"></script>
{{template "scripts" .}}
<!-- Likes modal -->