2022-08-14 05:44:57 +00:00
|
|
|
package friend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2023-10-28 21:34:35 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2024-07-21 02:44:22 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/webpush"
|
2022-08-14 05:44:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddFriend controller to send a friend request.
|
|
|
|
func AddFriend() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// POST only.
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
session.FlashError(w, r, "Unacceptable Request Method")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Form fields
|
|
|
|
var (
|
|
|
|
username = strings.ToLower(r.PostFormValue("username"))
|
|
|
|
verdict = r.PostFormValue("verdict")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get the current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get CurrentUser: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the target user.
|
|
|
|
user, err := models.FindUser(username)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "User Not Found")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't friend yourself.
|
|
|
|
if currentUser.ID == user.ID {
|
|
|
|
session.FlashError(w, r, "You can't send a friend request to yourself!")
|
|
|
|
templates.Redirect(w, "/u/"+username)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we adding, or rejecting+removing?
|
|
|
|
if verdict == "reject" || verdict == "remove" {
|
|
|
|
err := models.RemoveFriend(currentUser.ID, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Failed to remove friend: %s", err)
|
|
|
|
templates.Redirect(w, "/u/"+username)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-11 04:47:38 +00:00
|
|
|
// Revoke any friends-only photo notifications they had received before.
|
2024-01-11 06:25:50 +00:00
|
|
|
if err := models.RevokeFriendPhotoNotifications(currentUser, user); err != nil {
|
|
|
|
log.Error("Couldn't revoke friend photo notifications between %s and %s: %s", currentUser.Username, user.Username, err)
|
2024-01-11 04:47:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
var message string
|
|
|
|
if verdict == "reject" {
|
|
|
|
message = fmt.Sprintf("Friend request from %s has been rejected.", username)
|
|
|
|
} else {
|
|
|
|
message = fmt.Sprintf("Removed friendship with %s.", username)
|
|
|
|
}
|
|
|
|
|
|
|
|
session.Flash(w, r, message)
|
|
|
|
if verdict == "reject" {
|
|
|
|
templates.Redirect(w, "/friends?view=requests")
|
Change Logs
* Add a ChangeLog table to collect historic updates to various database tables.
* Created, Updated (with field diffs) and Deleted actions are logged, as well
as certification photo approves/denies.
* Specific items added to the change log:
* When a user photo is marked Explicit by an admin
* When users block/unblock each other
* When photo comments are posted, edited, and deleted
* When forums are created, edited, and deleted
* When forum comments are created, edited and deleted
* When a new forum thread is created
* When a user uploads or removes their own certification photo
* When an admin approves or rejects a certification photo
* When a user uploads, modifies or deletes their gallery photos
* When a friend request is sent
* When a friend request is accepted, ignored, or rejected
* When a friendship is removed
2024-02-26 01:03:36 +00:00
|
|
|
|
|
|
|
// Log the change.
|
|
|
|
models.LogDeleted(currentUser, nil, "friends", user.ID, "Rejected friend request from "+user.Username+".", nil)
|
|
|
|
} else {
|
|
|
|
// Log the change.
|
|
|
|
models.LogDeleted(currentUser, nil, "friends", user.ID, "Removed friendship with "+user.Username+".", nil)
|
2022-08-14 05:44:57 +00:00
|
|
|
}
|
|
|
|
templates.Redirect(w, "/friends")
|
2023-10-23 02:57:18 +00:00
|
|
|
return
|
|
|
|
} else if verdict == "ignore" {
|
|
|
|
if err := models.IgnoreFriendRequest(currentUser, user); err != nil {
|
|
|
|
session.FlashError(w, r, "Error marking the friend request as ignored: %s", err)
|
|
|
|
} else {
|
|
|
|
session.Flash(w, r, "You have ignored the friend request from %s.", username)
|
|
|
|
}
|
|
|
|
templates.Redirect(w, "/friends")
|
Change Logs
* Add a ChangeLog table to collect historic updates to various database tables.
* Created, Updated (with field diffs) and Deleted actions are logged, as well
as certification photo approves/denies.
* Specific items added to the change log:
* When a user photo is marked Explicit by an admin
* When users block/unblock each other
* When photo comments are posted, edited, and deleted
* When forums are created, edited, and deleted
* When forum comments are created, edited and deleted
* When a new forum thread is created
* When a user uploads or removes their own certification photo
* When an admin approves or rejects a certification photo
* When a user uploads, modifies or deletes their gallery photos
* When a friend request is sent
* When a friend request is accepted, ignored, or rejected
* When a friendship is removed
2024-02-26 01:03:36 +00:00
|
|
|
|
|
|
|
// Log the change.
|
|
|
|
models.LogUpdated(currentUser, nil, "friends", user.ID, "Ignored the friend request from "+user.Username+".", nil)
|
2023-10-23 02:57:18 +00:00
|
|
|
return
|
2022-08-14 05:44:57 +00:00
|
|
|
} else {
|
|
|
|
// Post the friend request.
|
|
|
|
if err := models.AddFriend(currentUser.ID, user.ID); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't send friend request: %s.", err)
|
|
|
|
} else {
|
|
|
|
if verdict == "approve" {
|
2022-08-26 03:36:59 +00:00
|
|
|
// Notify the requestor they'd been approved.
|
2023-10-28 21:34:35 +00:00
|
|
|
if !user.NotificationOptOut(config.NotificationOptOutFriendRequestAccepted) {
|
|
|
|
notif := &models.Notification{
|
|
|
|
UserID: user.ID,
|
|
|
|
AboutUser: *currentUser,
|
|
|
|
Type: models.NotificationFriendApproved,
|
|
|
|
}
|
|
|
|
if err := models.CreateNotification(notif); err != nil {
|
|
|
|
log.Error("Couldn't create approved notification: %s", err)
|
|
|
|
}
|
2022-08-26 03:36:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
session.Flash(w, r, "You accepted the friend request from %s!", username)
|
|
|
|
templates.Redirect(w, "/friends?view=requests")
|
Change Logs
* Add a ChangeLog table to collect historic updates to various database tables.
* Created, Updated (with field diffs) and Deleted actions are logged, as well
as certification photo approves/denies.
* Specific items added to the change log:
* When a user photo is marked Explicit by an admin
* When users block/unblock each other
* When photo comments are posted, edited, and deleted
* When forums are created, edited, and deleted
* When forum comments are created, edited and deleted
* When a new forum thread is created
* When a user uploads or removes their own certification photo
* When an admin approves or rejects a certification photo
* When a user uploads, modifies or deletes their gallery photos
* When a friend request is sent
* When a friend request is accepted, ignored, or rejected
* When a friendship is removed
2024-02-26 01:03:36 +00:00
|
|
|
|
|
|
|
// Log the change.
|
|
|
|
models.LogUpdated(currentUser, nil, "friends", user.ID, "Accepted friend request from "+user.Username+".", nil)
|
2022-08-14 05:44:57 +00:00
|
|
|
return
|
Change Logs
* Add a ChangeLog table to collect historic updates to various database tables.
* Created, Updated (with field diffs) and Deleted actions are logged, as well
as certification photo approves/denies.
* Specific items added to the change log:
* When a user photo is marked Explicit by an admin
* When users block/unblock each other
* When photo comments are posted, edited, and deleted
* When forums are created, edited, and deleted
* When forum comments are created, edited and deleted
* When a new forum thread is created
* When a user uploads or removes their own certification photo
* When an admin approves or rejects a certification photo
* When a user uploads, modifies or deletes their gallery photos
* When a friend request is sent
* When a friend request is accepted, ignored, or rejected
* When a friendship is removed
2024-02-26 01:03:36 +00:00
|
|
|
} else {
|
|
|
|
// Log the change.
|
|
|
|
models.LogCreated(currentUser, "friends", user.ID, "Sent a friend request to "+user.Username+".")
|
2024-07-21 02:44:22 +00:00
|
|
|
|
|
|
|
// Send a push notification to the recipient.
|
|
|
|
go func() {
|
|
|
|
// Opted out of this one?
|
|
|
|
if user.GetProfileField(config.PushNotificationOptOutFriends) == "true" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Try and send Web Push notification about new Friend Request to: %s", user.Username)
|
|
|
|
webpush.SendNotification(user, webpush.Payload{
|
|
|
|
Topic: "friend",
|
|
|
|
Title: "New Friend Request!",
|
|
|
|
Body: fmt.Sprintf("%s wants to be your friend on %s.", currentUser.Username, config.Title),
|
|
|
|
})
|
|
|
|
}()
|
2022-08-14 05:44:57 +00:00
|
|
|
}
|
|
|
|
session.Flash(w, r, "Friend request sent!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
templates.Redirect(w, "/u/"+username)
|
|
|
|
})
|
|
|
|
}
|