2022-08-27 18:42:48 +00:00
|
|
|
package comment
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Subscription endpoint - to opt in or out of comment thread subscriptions.
|
|
|
|
func Subscription() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Query params.
|
|
|
|
var (
|
|
|
|
tableName = r.FormValue("table_name")
|
|
|
|
tableID uint64
|
|
|
|
subscribe = r.FormValue("subscribe") == "true"
|
|
|
|
fromURL = r.FormValue("next") // what page to send back to
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse the table ID param.
|
|
|
|
if idStr := r.FormValue("table_id"); idStr == "" {
|
|
|
|
session.FlashError(w, r, "Comment table ID required.")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
} else {
|
2024-08-08 06:05:23 +00:00
|
|
|
// Is the table_id expected to be a username?
|
|
|
|
switch tableName {
|
|
|
|
case "friend.photos":
|
|
|
|
// Special "Friend uploaded a new photo" opt-out.
|
|
|
|
if user, err := models.FindUser(idStr); err != nil {
|
|
|
|
session.FlashError(w, r, "Username not found!")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
tableID = user.ID
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Integer IDs in all other cases.
|
|
|
|
if idInt, err := strconv.Atoi(idStr); err != nil {
|
|
|
|
session.FlashError(w, r, "Comment table ID invalid.")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
tableID = uint64(idInt)
|
|
|
|
}
|
2022-08-27 18:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect URL must be relative.
|
|
|
|
if !strings.HasPrefix(fromURL, "/") {
|
|
|
|
// Maybe it's URL encoded?
|
|
|
|
fromURL, _ = url.QueryUnescape(fromURL)
|
|
|
|
if !strings.HasPrefix(fromURL, "/") {
|
|
|
|
fromURL = "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate everything else.
|
2024-08-08 06:05:23 +00:00
|
|
|
if _, ok := models.SubscribableTables[tableName]; !ok {
|
2022-08-27 18:42:48 +00:00
|
|
|
session.FlashError(w, r, "You can not comment on that.")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get current user: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:05:23 +00:00
|
|
|
// Language to use in the flash messages.
|
|
|
|
var kind = "comments"
|
|
|
|
if tableName == "friend.photos" {
|
|
|
|
kind = "new photo uploads"
|
|
|
|
}
|
|
|
|
|
2022-08-27 18:42:48 +00:00
|
|
|
// Get their subscription.
|
|
|
|
sub, err := models.GetSubscription(currentUser, tableName, tableID)
|
|
|
|
if err != nil {
|
|
|
|
// If they want to subscribe, insert their row.
|
|
|
|
if subscribe {
|
|
|
|
if _, err := models.SubscribeTo(currentUser, tableName, tableID); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't create subscription: %s", err)
|
|
|
|
} else {
|
2024-08-08 06:05:23 +00:00
|
|
|
session.Flash(w, r, "You will now be notified about %s on this page.", kind)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// An explicit subscribe=false, may be a preemptive opt-out as in
|
|
|
|
// friend new photo notifications.
|
|
|
|
if _, err := models.UnsubscribeTo(currentUser, tableName, tableID); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't create subscription: %s", err)
|
|
|
|
} else {
|
|
|
|
session.Flash(w, r, "You will no longer be notified about %s on this page.", kind)
|
2022-08-27 18:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Toggle it.
|
|
|
|
sub.Subscribed = subscribe
|
|
|
|
if err := sub.Save(); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't save your subscription settings: %s", err)
|
|
|
|
} else {
|
|
|
|
if subscribe {
|
2024-08-08 06:05:23 +00:00
|
|
|
session.Flash(w, r, "You will now be notified about %s on this page.", kind)
|
2022-08-27 18:42:48 +00:00
|
|
|
} else {
|
2024-08-08 06:05:23 +00:00
|
|
|
session.Flash(w, r, "You will no longer be notified about new %s on this page.", kind)
|
2022-08-27 18:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
templates.Redirect(w, fromURL)
|
|
|
|
})
|
|
|
|
}
|