Bugfixes on user profile pages

This commit is contained in:
Noah Petherbridge 2023-10-23 21:55:55 -07:00
parent c701dd831f
commit 0143fd752f
5 changed files with 50 additions and 22 deletions

View File

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

View File

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

View File

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

View File

@ -44,7 +44,6 @@
<div class="field block">
<label class="label" for="dob">Date of birth:</label>
<input type="date" class="input"
placeholder="password"
name="dob"
id="dob"
required>

View File

@ -205,7 +205,7 @@
{{$Like := .LikeMap.Get .User.ID}}
<div class="column is-narrow has-text-centered">
<button type="button" class="button is-fullwidth nonshy-like-button"
data-table-name="users" data-table-id="{{.User.ID}}"
data-table-name="users" data-table-id="{{.User.Username}}"
title="Like this profile">
<span class="icon{{if $Like.UserLikes}} has-text-danger{{end}}"><i class="fa fa-heart"></i></span>
<span class="nonshy-likes">
@ -236,7 +236,7 @@
</div>
<div class="column is-narrow has-text-centered">
<a href="/contact?intent=report&subject=report.user&id={{.User.ID}}" class="button is-fullwidth">
<a href="/contact?intent=report&subject=report.user&id={{.User.Username}}" class="button is-fullwidth">
<span class="icon-text">
<span class="icon">
<i class="fa fa-flag"></i>