From 0143fd752fcdd50c066a4959703e55c7c535dbdf Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Mon, 23 Oct 2023 21:55:55 -0700 Subject: [PATCH] Bugfixes on user profile pages --- pkg/controller/api/likes.go | 53 +++++++++++++++++++++-------- pkg/controller/index/contact.go | 10 ++++-- web/static/js/likes.js | 2 +- web/templates/account/age_gate.html | 3 +- web/templates/account/profile.html | 4 +-- 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/pkg/controller/api/likes.go b/pkg/controller/api/likes.go index b83f153..3309573 100644 --- a/pkg/controller/api/likes.go +++ b/pkg/controller/api/likes.go @@ -16,7 +16,7 @@ func Likes() http.HandlerFunc { // Request JSON schema. type Request struct { TableName string `json:"name"` - TableID uint64 `json:"id"` + TableID string `json:"id"` Unlike bool `json:"unlike,omitempty"` Referrer string `json:"page"` } @@ -61,6 +61,29 @@ func Likes() http.HandlerFunc { req.Referrer = "" } + // Is the ID an integer? + var tableID uint64 + if v, err := strconv.Atoi(req.TableID); err != nil { + // Non-integer must be usernames? + if req.TableName == "users" { + user, err := models.FindUser(req.TableID) + if err != nil { + SendJSON(w, http.StatusBadRequest, Response{ + Error: "User not found.", + }) + return + } + tableID = user.ID + } else { + SendJSON(w, http.StatusBadRequest, Response{ + Error: "Invalid ID.", + }) + return + } + } else { + tableID = uint64(v) + } + // Who do we notify about this like? var ( targetUser *models.User @@ -68,7 +91,7 @@ func Likes() http.HandlerFunc { ) switch req.TableName { case "photos": - if photo, err := models.GetPhoto(req.TableID); err == nil { + if photo, err := models.GetPhoto(tableID); err == nil { if user, err := models.GetUser(photo.UserID); err == nil { // Admin safety check: in case the admin clicked 'Like' on a friends-only or private // picture they shouldn't have been expected to see, do not log a like. @@ -92,11 +115,11 @@ func Likes() http.HandlerFunc { targetUser = user } } else { - log.Error("For like on photos table: didn't find photo %d: %s", req.TableID, err) + log.Error("For like on photos table: didn't find photo %d: %s", tableID, err) } case "users": - log.Error("subject is users, find %d", req.TableID) - if user, err := models.GetUser(req.TableID); err == nil { + 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) @@ -108,11 +131,11 @@ func Likes() http.HandlerFunc { return } } else { - log.Error("For like on users table: didn't find user %d: %s", req.TableID, err) + log.Error("For like on users table: didn't find user %d: %s", tableID, err) } case "comments": - log.Error("subject is comments, find %d", req.TableID) - if comment, err := models.GetComment(req.TableID); err == nil { + log.Error("subject is comments, find %d", tableID) + if comment, err := models.GetComment(tableID); err == nil { targetUser = &comment.User notificationMessage = comment.Message log.Warn("found user %s", targetUser.Username) @@ -125,7 +148,7 @@ func Likes() http.HandlerFunc { return } } else { - log.Error("For like on users table: didn't find user %d: %s", req.TableID, err) + log.Error("For like on users table: didn't find user %d: %s", tableID, err) } } @@ -139,7 +162,7 @@ func Likes() http.HandlerFunc { // Put in a like. if req.Unlike { - if err := models.Unlike(currentUser, req.TableName, req.TableID); err != nil { + if err := models.Unlike(currentUser, req.TableName, tableID); err != nil { SendJSON(w, http.StatusBadRequest, Response{ Error: fmt.Sprintf("Error unliking: %s", err), }) @@ -147,9 +170,9 @@ func Likes() http.HandlerFunc { } // Remove the target's notification about this like. - models.RemoveSpecificNotification(targetUser.ID, models.NotificationLike, req.TableName, req.TableID) + models.RemoveSpecificNotification(targetUser.ID, models.NotificationLike, req.TableName, tableID) } else { - if err := models.AddLike(currentUser, req.TableName, req.TableID); err != nil { + if err := models.AddLike(currentUser, req.TableName, tableID); err != nil { SendJSON(w, http.StatusBadRequest, Response{ Error: fmt.Sprintf("Error liking: %s", err), }) @@ -157,14 +180,14 @@ func Likes() http.HandlerFunc { } // Notify the recipient of the like. - log.Info("Added like on %s:%d, notifying owner %+v", req.TableName, req.TableID, targetUser) + log.Info("Added like on %s:%d, notifying owner %+v", req.TableName, tableID, targetUser) if targetUser != nil { notif := &models.Notification{ UserID: targetUser.ID, AboutUser: *currentUser, Type: models.NotificationLike, TableName: req.TableName, - TableID: req.TableID, + TableID: tableID, Message: notificationMessage, Link: req.Referrer, } @@ -177,7 +200,7 @@ func Likes() http.HandlerFunc { // Send success response. SendJSON(w, http.StatusOK, Response{ OK: true, - Likes: models.CountLikes(req.TableName, req.TableID), + Likes: models.CountLikes(req.TableName, tableID), }) }) } diff --git a/pkg/controller/index/contact.go b/pkg/controller/index/contact.go index fcb4088..705f57b 100644 --- a/pkg/controller/index/contact.go +++ b/pkg/controller/index/contact.go @@ -37,7 +37,13 @@ func Contact() http.HandlerFunc { ) // For report intents: ID of the user, photo, message, etc. - tableID, _ = strconv.Atoi(r.FormValue("id")) + tableID, err := strconv.Atoi(r.FormValue("id")) + if err != nil { + // The tableID is not an int - was it a username? + if user, err := models.FindUser(r.FormValue("id")); err == nil { + tableID = int(user.ID) + } + } if tableID > 0 { messageRequired = false } @@ -195,7 +201,7 @@ func Contact() http.HandlerFunc { var vars = map[string]interface{}{ "Intent": intent, - "TableID": tableID, + "TableID": r.FormValue("id"), "TableLabel": tableLabel, "Subject": subject, "PageTitle": title, diff --git a/web/static/js/likes.js b/web/static/js/likes.js index cdcbf13..9666561 100644 --- a/web/static/js/likes.js +++ b/web/static/js/likes.js @@ -35,7 +35,7 @@ document.addEventListener('DOMContentLoaded', () => { }, body: JSON.stringify({ "name": tableName, // TODO - "id": parseInt(tableID), + "id": ""+tableID, "unlike": !liking, "page": window.location.pathname + window.location.search + window.location.hash, }), diff --git a/web/templates/account/age_gate.html b/web/templates/account/age_gate.html index e169d8f..8af2eb3 100644 --- a/web/templates/account/age_gate.html +++ b/web/templates/account/age_gate.html @@ -44,7 +44,6 @@
@@ -101,4 +100,4 @@ }); }); -{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/account/profile.html b/web/templates/account/profile.html index 9c3758b..a7ae685 100644 --- a/web/templates/account/profile.html +++ b/web/templates/account/profile.html @@ -205,7 +205,7 @@ {{$Like := .LikeMap.Get .User.ID}}