2022-08-13 06:11:36 +00:00
|
|
|
package photo
|
|
|
|
|
|
|
|
import (
|
2022-08-21 22:40:24 +00:00
|
|
|
"fmt"
|
2022-08-13 06:11:36 +00:00
|
|
|
"net/http"
|
2023-06-26 05:55:07 +00:00
|
|
|
"path/filepath"
|
2022-08-13 06:11:36 +00:00
|
|
|
"strconv"
|
2024-03-16 05:02:24 +00:00
|
|
|
"strings"
|
2022-08-13 06:11:36 +00:00
|
|
|
|
2024-03-15 06:08:14 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/chat"
|
2024-01-06 03:08:44 +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"
|
|
|
|
pphoto "code.nonshy.com/nonshy/website/pkg/photo"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2022-08-13 06:11:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Edit controller (/photo/edit?id=N) to change properties about your picture.
|
|
|
|
func Edit() http.HandlerFunc {
|
|
|
|
// Reuse the upload page but with an EditPhoto variable.
|
|
|
|
tmpl := templates.Must("photo/upload.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Query params.
|
|
|
|
photoID, err := strconv.Atoi(r.FormValue("id"))
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Photo 'id' parameter required.")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find this photo by ID.
|
|
|
|
photo, err := models.GetPhoto(uint64(photoID))
|
|
|
|
if err != nil {
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Unexpected error: couldn't get CurrentUser")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
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
|
|
|
// In case an admin is editing this photo: remember the HTTP request current user,
|
|
|
|
// before the currentUser may be set to the photo's owner below.
|
|
|
|
var requestUser = currentUser
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
// Do we have permission for this photo?
|
2023-12-21 22:37:16 +00:00
|
|
|
if photo.UserID != currentUser.ID {
|
2024-05-27 20:02:05 +00:00
|
|
|
if !currentUser.HasAdminScope(config.ScopePhotoModerator) {
|
2023-12-21 22:37:16 +00:00
|
|
|
templates.ForbiddenPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the owner of this photo and assume currentUser is them for the remainder
|
|
|
|
// of this controller.
|
|
|
|
if user, err := models.GetUser(photo.UserID); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get the owner User for this photo!")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
currentUser = user
|
|
|
|
}
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 03:08:44 +00:00
|
|
|
// Is the user throttled for Site Gallery photo uploads?
|
|
|
|
var SiteGalleryThrottled = models.IsSiteGalleryThrottled(currentUser, photo)
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
// Are we saving the changes?
|
|
|
|
if r.Method == http.MethodPost {
|
2024-09-20 02:30:02 +00:00
|
|
|
// Record if this change is going to make them a Shy Account.
|
|
|
|
var wasShy = currentUser.IsShy()
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
var (
|
2024-03-16 05:02:24 +00:00
|
|
|
caption = strings.TrimSpace(r.FormValue("caption"))
|
|
|
|
altText = strings.TrimSpace(r.FormValue("alt_text"))
|
2022-08-13 06:11:36 +00:00
|
|
|
isExplicit = r.FormValue("explicit") == "true"
|
|
|
|
isGallery = r.FormValue("gallery") == "true"
|
2024-07-07 21:00:58 +00:00
|
|
|
isPinned = r.FormValue("pinned") == "true"
|
2023-03-17 03:04:43 +00:00
|
|
|
visibility = models.PhotoVisibility(r.FormValue("visibility"))
|
2022-08-13 06:11:36 +00:00
|
|
|
|
|
|
|
// Profile pic fields
|
|
|
|
setProfilePic = r.FormValue("intent") == "profile-pic"
|
|
|
|
crop = pphoto.ParseCropCoords(r.FormValue("crop"))
|
2023-03-17 03:04:43 +00:00
|
|
|
|
2024-08-10 18:54:37 +00:00
|
|
|
// Are we GOING private?
|
2023-03-17 03:04:43 +00:00
|
|
|
goingPrivate = visibility == models.PhotoPrivate && visibility != photo.Visibility
|
2024-10-02 03:44:11 +00:00
|
|
|
|
|
|
|
// Is the user fighting an 'Explicit' tag added by the community?
|
|
|
|
isFightingExplicitFlag = photo.Flagged && photo.Explicit && !isExplicit
|
2022-08-13 06:11:36 +00:00
|
|
|
)
|
|
|
|
|
2024-03-16 05:02:24 +00:00
|
|
|
if len(altText) > config.AltTextMaxLength {
|
|
|
|
altText = altText[:config.AltTextMaxLength]
|
|
|
|
}
|
|
|
|
|
2024-01-06 03:08:44 +00:00
|
|
|
// Respect the Site Gallery throttle in case the user is messing around.
|
|
|
|
if SiteGalleryThrottled {
|
|
|
|
isGallery = false
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Diff for the ChangeLog.
|
|
|
|
diffs := []models.FieldDiff{
|
|
|
|
models.NewFieldDiff("Caption", photo.Caption, caption),
|
|
|
|
models.NewFieldDiff("Explicit", photo.Explicit, isExplicit),
|
|
|
|
models.NewFieldDiff("Gallery", photo.Gallery, isGallery),
|
2024-07-07 21:00:58 +00:00
|
|
|
models.NewFieldDiff("Pinned", photo.Pinned, isPinned),
|
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
|
|
|
models.NewFieldDiff("Visibility", photo.Visibility, visibility),
|
|
|
|
}
|
|
|
|
|
2024-10-02 06:13:09 +00:00
|
|
|
// Admin label options.
|
|
|
|
if requestUser.HasAdminScope(config.ScopePhotoModerator) {
|
|
|
|
var adminLabel string
|
|
|
|
if labels, ok := r.PostForm["admin_label"]; ok {
|
|
|
|
adminLabel = strings.Join(labels, ",")
|
|
|
|
}
|
|
|
|
diffs = append(diffs,
|
|
|
|
models.NewFieldDiff("Admin Label", photo.AdminLabel, adminLabel),
|
|
|
|
)
|
|
|
|
|
|
|
|
photo.AdminLabel = adminLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Admin label: forced explicit?
|
|
|
|
if photo.HasAdminLabelForceExplicit() {
|
|
|
|
isExplicit = true
|
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
photo.Caption = caption
|
2024-03-16 05:02:24 +00:00
|
|
|
photo.AltText = altText
|
2022-08-13 06:11:36 +00:00
|
|
|
photo.Explicit = isExplicit
|
|
|
|
photo.Gallery = isGallery
|
2024-07-07 21:00:58 +00:00
|
|
|
photo.Pinned = isPinned
|
2023-03-17 03:04:43 +00:00
|
|
|
photo.Visibility = visibility
|
2022-08-13 06:11:36 +00:00
|
|
|
|
2023-06-26 05:55:07 +00:00
|
|
|
// Can not use a GIF as profile pic.
|
|
|
|
if setProfilePic && filepath.Ext(photo.Filename) == ".gif" {
|
|
|
|
session.FlashError(w, r, "You can not use a GIF as your profile picture.")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
// Are we cropping ourselves a new profile pic?
|
|
|
|
log.Error("Profile pic? %+v and crop is: %+v", setProfilePic, crop)
|
|
|
|
if setProfilePic && crop != nil && len(crop) >= 4 {
|
|
|
|
cropFilename, err := pphoto.ReCrop(photo.Filename, crop[0], crop[1], crop[2], crop[3])
|
|
|
|
log.Error("ReCrop got: %s, %s", cropFilename, err)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't re-crop for profile picture: %s", err)
|
|
|
|
} else {
|
|
|
|
// If there was an old profile pic, remove it from disk.
|
|
|
|
if photo.CroppedFilename != "" {
|
|
|
|
pphoto.Delete(photo.CroppedFilename)
|
|
|
|
}
|
|
|
|
photo.CroppedFilename = cropFilename
|
|
|
|
log.Warn("HERE WE SET (%s) ON PHOTO (%+v)", cropFilename, photo)
|
|
|
|
}
|
2023-06-26 05:55:07 +00:00
|
|
|
} else {
|
|
|
|
setProfilePic = false
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 03:44:11 +00:00
|
|
|
// If the user is fighting a recent Explicit flag from the community.
|
|
|
|
if isFightingExplicitFlag {
|
|
|
|
|
|
|
|
// Notify the admin (unless we are an admin).
|
|
|
|
if !requestUser.IsAdmin {
|
|
|
|
fb := &models.Feedback{
|
|
|
|
Intent: "report",
|
|
|
|
Subject: "Explicit photo flag dispute",
|
|
|
|
UserID: currentUser.ID,
|
|
|
|
TableName: "photos",
|
|
|
|
TableID: photo.ID,
|
|
|
|
Message: "A user's photo was recently **flagged by the community** as Explicit, and its owner " +
|
|
|
|
"has **removed** the Explicit setting.\n\n" +
|
|
|
|
"Please check out the photo below and verify what its Explicit setting should be:",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := models.CreateFeedback(fb); err != nil {
|
|
|
|
log.Error("Couldn't save feedback from user updating their DOB: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow this change but clear the Flagged status.
|
|
|
|
photo.Flagged = false
|
2024-10-02 06:13:09 +00:00
|
|
|
|
|
|
|
// Clear the notification about this.
|
|
|
|
models.RemoveSpecificNotification(currentUser.ID, models.NotificationExplicitPhoto, "photos", photo.ID)
|
2024-10-02 03:44:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
if err := photo.Save(); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't save photo: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set their profile pic to this one.
|
2022-08-14 00:42:42 +00:00
|
|
|
if setProfilePic {
|
|
|
|
currentUser.ProfilePhoto = *photo
|
|
|
|
log.Error("Set user ProfilePhotoID=%d", photo.ID)
|
|
|
|
if err := currentUser.Save(); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't save user: %s", err)
|
|
|
|
}
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flash success.
|
|
|
|
session.Flash(w, r, "Photo settings updated!")
|
|
|
|
|
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, requestUser, "photos", photo.ID, "Updated the photo's settings.", diffs)
|
|
|
|
|
2024-03-15 06:08:14 +00:00
|
|
|
// Maybe kick them from the chat if this photo save makes them a Shy Account.
|
2024-09-20 02:30:02 +00:00
|
|
|
currentUser.FlushCaches()
|
|
|
|
if !wasShy && currentUser.IsShy() {
|
|
|
|
if _, err := chat.MaybeDisconnectUser(currentUser); err != nil {
|
|
|
|
log.Error("chat.MaybeDisconnectUser(%s#%d): %s", currentUser.Username, currentUser.ID, err)
|
|
|
|
}
|
2024-03-15 06:08:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 03:04:43 +00:00
|
|
|
// If this picture has moved to Private, revoke any notification we gave about it before.
|
2024-08-10 18:54:37 +00:00
|
|
|
if goingPrivate {
|
2023-09-15 00:40:12 +00:00
|
|
|
log.Info("The picture is GOING PRIVATE (to %s), revoke any notifications about it", photo.Visibility)
|
2023-03-17 03:04:43 +00:00
|
|
|
models.RemoveNotification("photos", photo.ID)
|
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
// Return the user to their gallery.
|
2024-02-11 00:17:15 +00:00
|
|
|
templates.Redirect(w, "/u/"+currentUser.Username+"/photos")
|
2022-08-13 06:11:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var vars = map[string]interface{}{
|
2024-01-06 03:08:44 +00:00
|
|
|
"EditPhoto": photo,
|
|
|
|
"SiteGalleryThrottled": SiteGalleryThrottled,
|
|
|
|
"SiteGalleryThrottleLimit": config.SiteGalleryRateLimitMax,
|
2024-10-02 06:13:09 +00:00
|
|
|
|
|
|
|
// Available admin labels enum.
|
|
|
|
"RequestUser": requestUser,
|
|
|
|
"AvailableAdminLabels": config.AdminLabelPhotoOptions,
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete controller (/photo/Delete?id=N) to change properties about your picture.
|
2024-10-05 04:17:20 +00:00
|
|
|
//
|
|
|
|
// DEPRECATED: send them to the batch-edit endpoint.
|
2022-08-13 06:11:36 +00:00
|
|
|
func Delete() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-10-05 04:17:20 +00:00
|
|
|
templates.Redirect(w, fmt.Sprintf("/photo/batch-edit?intent=delete&id=%s", r.FormValue("id")))
|
2022-08-13 06:11:36 +00:00
|
|
|
})
|
|
|
|
}
|