2022-08-24 05:55:19 +00:00
|
|
|
package forum
|
|
|
|
|
|
|
|
import (
|
2022-10-21 04:02:30 +00:00
|
|
|
"bytes"
|
2022-08-24 05:55:19 +00:00
|
|
|
"fmt"
|
2022-10-21 04:02:30 +00:00
|
|
|
"io"
|
2022-08-24 05:55:19 +00:00
|
|
|
"net/http"
|
2022-10-21 04:02:30 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-08-24 05:55:19 +00:00
|
|
|
"strconv"
|
|
|
|
|
2022-12-15 06:57:06 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
2022-08-27 18:42:48 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/markdown"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
2022-10-21 04:02:30 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/photo"
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
2024-03-16 06:19:26 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/spam"
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2022-08-24 05:55:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewPost view.
|
|
|
|
func NewPost() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("forum/new_post.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Query params.
|
|
|
|
var (
|
|
|
|
fragment = r.FormValue("to") // forum to (new post)
|
|
|
|
toThreadID = r.FormValue("thread") // add reply to a thread ID
|
|
|
|
quoteCommentID = r.FormValue("quote") // add reply to thread while quoting a comment
|
|
|
|
editCommentID = r.FormValue("edit") // edit your comment
|
|
|
|
intent = r.FormValue("intent") // preview or submit
|
2022-10-21 04:02:30 +00:00
|
|
|
photoIntent = r.FormValue("photo_intent") // upload, remove photo attachment
|
|
|
|
photoID = r.FormValue("photo_id") // existing CommentPhoto ID
|
2022-08-24 05:55:19 +00:00
|
|
|
title = r.FormValue("title") // for new forum post only
|
|
|
|
message = r.PostFormValue("message") // comment body
|
2022-08-25 04:17:34 +00:00
|
|
|
isPinned = r.PostFormValue("pinned") == "true" // owners or admins only
|
2022-08-24 05:55:19 +00:00
|
|
|
isExplicit = r.PostFormValue("explicit") == "true" // for thread only
|
|
|
|
isNoReply = r.PostFormValue("noreply") == "true" // for thread only
|
|
|
|
isDelete = r.FormValue("delete") == "true" // delete comment (along with edit=$id)
|
|
|
|
forum *models.Forum
|
|
|
|
thread *models.Thread // if replying to a thread
|
|
|
|
comment *models.Comment // if editing a comment
|
2022-08-25 04:17:34 +00:00
|
|
|
|
|
|
|
// If we are modifying a comment (post) and it's the OG post of the
|
|
|
|
// thread, we show and accept the thread settings to be updated as
|
|
|
|
// well (pinned, explicit, noreply)
|
|
|
|
isOriginalComment bool
|
2022-10-21 04:02:30 +00:00
|
|
|
|
2024-11-28 19:21:06 +00:00
|
|
|
// If neither the forum nor thread are explicit, show a hint to the user not to
|
|
|
|
// share an explicit photo in their reply.
|
|
|
|
explicitPhotoAllowed bool
|
|
|
|
|
2022-12-15 06:57:06 +00:00
|
|
|
// Polls
|
2022-12-21 03:15:45 +00:00
|
|
|
pollOptions = []string{}
|
|
|
|
pollExpires = 3
|
|
|
|
pollMultipleChoice = r.FormValue("poll_multiple_choice") == "true"
|
|
|
|
isPoll bool
|
2022-12-15 06:57:06 +00:00
|
|
|
|
2022-10-21 04:02:30 +00:00
|
|
|
// Attached photo object.
|
|
|
|
commentPhoto *models.CommentPhoto
|
2022-08-24 05:55:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the forum itself.
|
|
|
|
if found, err := models.ForumByFragment(fragment); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't post to forum %s: not found.", fragment)
|
|
|
|
templates.Redirect(w, "/forum")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
forum = found
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we manipulating a reply to an existing thread?
|
|
|
|
if len(toThreadID) > 0 {
|
|
|
|
if i, err := strconv.Atoi(toThreadID); err == nil {
|
|
|
|
if found, err := models.GetThread(uint64(i)); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't find that thread ID!")
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/f/%s", forum.Fragment))
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
thread = found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-28 19:21:06 +00:00
|
|
|
// Would an explicit photo attachment be allowed?
|
|
|
|
if forum.Explicit || (thread != nil && thread.Explicit) {
|
|
|
|
explicitPhotoAllowed = true
|
|
|
|
}
|
|
|
|
|
2024-08-21 04:26:53 +00:00
|
|
|
// If the current user can moderate the forum thread, e.g. edit or delete posts.
|
|
|
|
// Admins can edit always, user owners of forums can only delete.
|
|
|
|
var canModerate = currentUser.HasAdminScope(config.ScopeForumModerator) ||
|
|
|
|
(forum.OwnerID == currentUser.ID && isDelete)
|
|
|
|
|
2022-10-21 04:02:30 +00:00
|
|
|
// Does the comment have an existing Photo ID?
|
|
|
|
if len(photoID) > 0 {
|
|
|
|
if i, err := strconv.Atoi(photoID); err == nil {
|
|
|
|
if found, err := models.GetCommentPhoto(uint64(i)); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't find comment photo ID #%d!", i)
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/f/%s", forum.Fragment))
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
commentPhoto = found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// Are we pre-filling the message with a quotation of an existing comment?
|
|
|
|
if len(quoteCommentID) > 0 {
|
|
|
|
if i, err := strconv.Atoi(quoteCommentID); err == nil {
|
|
|
|
if comment, err := models.GetComment(uint64(i)); err == nil {
|
2024-11-23 20:55:13 +00:00
|
|
|
|
|
|
|
// Prefill the message with the @mention and quoted post.
|
|
|
|
message = fmt.Sprintf(
|
|
|
|
"[@%s](/go/comment?id=%d)\n\n%s\n\n",
|
|
|
|
comment.User.Username,
|
|
|
|
comment.ID,
|
|
|
|
markdown.Quotify(comment.Message),
|
|
|
|
)
|
2022-08-24 05:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we editing or deleting our comment?
|
|
|
|
if len(editCommentID) > 0 {
|
|
|
|
if i, err := strconv.Atoi(editCommentID); err == nil {
|
|
|
|
if found, err := models.GetComment(uint64(i)); err == nil {
|
|
|
|
comment = found
|
|
|
|
|
|
|
|
// Verify that it is indeed OUR comment.
|
2024-08-21 04:26:53 +00:00
|
|
|
if currentUser.ID != comment.UserID && !canModerate {
|
2022-08-24 05:55:19 +00:00
|
|
|
templates.ForbiddenPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the form w/ the content of this message.
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
message = comment.Message
|
|
|
|
}
|
|
|
|
|
2022-10-21 04:02:30 +00:00
|
|
|
// Did this comment have a picture? Load it if so.
|
|
|
|
if photos, err := comment.GetPhotos(); err == nil && len(photos) > 0 {
|
|
|
|
commentPhoto = photos[0]
|
|
|
|
}
|
|
|
|
|
2022-08-25 04:17:34 +00:00
|
|
|
// Is this the OG thread of the post?
|
|
|
|
if thread.CommentID == comment.ID {
|
|
|
|
isOriginalComment = true
|
|
|
|
|
|
|
|
// Restore the checkbox option form values from thread settings.
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
isPinned = thread.Pinned
|
|
|
|
isExplicit = thread.Explicit
|
|
|
|
isNoReply = thread.NoReply
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// Are we DELETING this comment?
|
|
|
|
if isDelete {
|
2022-10-21 04:51:53 +00:00
|
|
|
// Is there a photo attachment? Remove it, too.
|
|
|
|
if commentPhoto != nil {
|
|
|
|
if err := photo.Delete(commentPhoto.Filename); err != nil {
|
|
|
|
session.FlashError(w, r, "Error removing the photo from disk: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := commentPhoto.Delete(); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't remove photo from DB: %s", err)
|
|
|
|
} else {
|
|
|
|
commentPhoto = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
if err := thread.DeleteReply(comment); err != nil {
|
|
|
|
session.FlashError(w, r, "Error deleting your post: %s", err)
|
|
|
|
} else {
|
|
|
|
session.Flash(w, r, "Your post has been deleted.")
|
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(&models.User{ID: comment.UserID}, currentUser, "comments", comment.ID, fmt.Sprintf(
|
|
|
|
"Deleted a forum comment on thread %d forum /f/%s", thread.ID, forum.Fragment,
|
|
|
|
), comment)
|
2022-08-24 05:55:19 +00:00
|
|
|
}
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/forum/thread/%d", thread.ID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Comment not found - show the Forbidden page anyway.
|
|
|
|
templates.ForbiddenPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Submitting the form.
|
|
|
|
if r.Method == http.MethodPost {
|
2024-03-16 06:19:26 +00:00
|
|
|
// Look for spammy links to video sites or things.
|
|
|
|
if err := spam.DetectSpamMessage(title + message); err != nil {
|
|
|
|
session.FlashError(w, r, err.Error())
|
|
|
|
if thread != nil {
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/forum/thread/%d", thread.ID))
|
|
|
|
} else if forum != nil {
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/f/%s", forum.Fragment))
|
|
|
|
} else {
|
|
|
|
templates.Redirect(w, "/forum")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-15 06:57:06 +00:00
|
|
|
// Polls: parse form parameters into a neat list of answers.
|
|
|
|
pollExpires, _ = strconv.Atoi(r.FormValue("poll_expires"))
|
2022-12-21 03:15:45 +00:00
|
|
|
var distinctPollChoices = map[string]interface{}{}
|
2022-12-15 06:57:06 +00:00
|
|
|
for i := 0; i < config.PollMaxAnswers; i++ {
|
|
|
|
if value := r.FormValue(fmt.Sprintf("answer%d", i)); value != "" {
|
|
|
|
pollOptions = append(pollOptions, value)
|
|
|
|
isPoll = len(pollOptions) >= 2
|
2022-12-21 03:15:45 +00:00
|
|
|
|
|
|
|
// Make sure every option is distinct!
|
|
|
|
if _, ok := distinctPollChoices[value]; ok {
|
|
|
|
session.FlashError(w, r, "Your poll options must all be unique! Duplicate option '%s' seen in your post!", value)
|
|
|
|
intent = "preview" // do not continue to submit
|
|
|
|
}
|
|
|
|
distinctPollChoices[value] = nil
|
2022-12-15 06:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 03:15:45 +00:00
|
|
|
// If only one poll option, warn about it.
|
|
|
|
if len(pollOptions) == 1 {
|
|
|
|
session.FlashError(w, r, "Your poll should have at least two choices.")
|
|
|
|
intent = "preview" // do not continue to submit
|
|
|
|
}
|
|
|
|
|
2022-10-21 04:02:30 +00:00
|
|
|
// Is a photo coming along?
|
|
|
|
if forum.PermitPhotos {
|
|
|
|
// Removing or replacing?
|
|
|
|
if photoIntent == "remove" || photoIntent == "replace" {
|
|
|
|
// Remove the attached photo.
|
|
|
|
if commentPhoto == nil {
|
|
|
|
session.FlashError(w, r, "Couldn't remove photo from post: no photo found!")
|
|
|
|
} else {
|
|
|
|
photo.Delete(commentPhoto.Filename)
|
|
|
|
if err := commentPhoto.Delete(); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't remove photo from DB: %s", err)
|
|
|
|
} else {
|
|
|
|
session.Flash(w, r, "Photo attachment %sd from this post.", photoIntent)
|
|
|
|
commentPhoto = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uploading a new picture?
|
|
|
|
if photoIntent == "upload" || photoIntent == "replace" {
|
|
|
|
log.Info("Receiving a photo upload for forum post")
|
|
|
|
|
|
|
|
// Get their file upload.
|
|
|
|
file, header, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Error receiving your file: %s", err)
|
|
|
|
templates.Redirect(w, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the file contents.
|
|
|
|
log.Debug("Receiving uploaded file (%d bytes): %s", header.Size, header.Filename)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, file)
|
|
|
|
|
|
|
|
filename, _, err := photo.UploadPhoto(photo.UploadConfig{
|
|
|
|
Extension: filepath.Ext(header.Filename),
|
|
|
|
Data: buf.Bytes(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Error in UploadPhoto: %s", err)
|
|
|
|
templates.Redirect(w, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the PhotoComment. If we don't have a Comment ID yet, let it be empty.
|
|
|
|
ptmpl := models.CommentPhoto{
|
|
|
|
Filename: filename,
|
2022-10-21 04:51:53 +00:00
|
|
|
UserID: currentUser.ID,
|
2022-10-21 04:02:30 +00:00
|
|
|
}
|
|
|
|
if comment != nil {
|
|
|
|
ptmpl.CommentID = comment.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the filesize.
|
|
|
|
if stat, err := os.Stat(photo.DiskPath(filename)); err == nil {
|
|
|
|
ptmpl.Filesize = stat.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create it in DB!
|
|
|
|
p, err := models.CreateCommentPhoto(ptmpl)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't create CommentPhoto in DB: %s", err)
|
|
|
|
} else {
|
|
|
|
log.Info("New photo! %+v", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
commentPhoto = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// Default intent is preview unless told to submit.
|
|
|
|
if intent == "submit" {
|
2022-10-21 04:02:30 +00:00
|
|
|
// A message OR a photo is required.
|
|
|
|
if forum.PermitPhotos && message == "" && commentPhoto == nil {
|
|
|
|
session.FlashError(w, r, "A message OR photo is required for this post.")
|
2022-10-21 04:07:21 +00:00
|
|
|
if thread != nil {
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/forum/thread/%d", thread.ID))
|
|
|
|
} else if forum != nil {
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/f/%s", forum.Fragment))
|
|
|
|
} else {
|
|
|
|
templates.Redirect(w, "/forum")
|
|
|
|
}
|
2022-10-21 04:02:30 +00:00
|
|
|
return
|
|
|
|
} else if !forum.PermitPhotos && message == "" {
|
|
|
|
session.FlashError(w, r, "A message is required for this post.")
|
2022-10-21 04:07:21 +00:00
|
|
|
if thread != nil {
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/forum/thread/%d", thread.ID))
|
|
|
|
} else if forum != nil {
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/f/%s", forum.Fragment))
|
|
|
|
} else {
|
|
|
|
templates.Redirect(w, "/forum")
|
|
|
|
}
|
2022-10-21 04:02:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// Are we modifying an existing comment?
|
|
|
|
if comment != nil {
|
|
|
|
comment.Message = message
|
2022-08-25 04:17:34 +00:00
|
|
|
|
|
|
|
// Can we update the thread props?
|
|
|
|
if isOriginalComment {
|
|
|
|
thread.Pinned = isPinned
|
|
|
|
thread.Explicit = isExplicit
|
|
|
|
thread.NoReply = isNoReply
|
|
|
|
if err := thread.Save(); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't save thread properties: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
if err := comment.Save(); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't save comment: %s", err)
|
|
|
|
} else {
|
|
|
|
session.Flash(w, r, "Comment 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(&models.User{ID: comment.UserID}, currentUser, "comments", comment.ID, fmt.Sprintf(
|
|
|
|
"Edited their comment on thread %d (in /f/%s):\n\n%s",
|
|
|
|
thread.ID,
|
|
|
|
forum.Fragment,
|
|
|
|
message,
|
|
|
|
), nil)
|
2022-08-24 05:55:19 +00:00
|
|
|
}
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/forum/thread/%d", thread.ID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we replying to an existing thread?
|
|
|
|
if thread != nil {
|
2022-10-21 04:02:30 +00:00
|
|
|
if reply, err := thread.Reply(currentUser, message); err != nil {
|
2022-08-24 05:55:19 +00:00
|
|
|
session.FlashError(w, r, "Couldn't add reply to thread: %s", err)
|
|
|
|
} else {
|
|
|
|
session.Flash(w, r, "Reply added to the thread!")
|
2022-08-27 18:42:48 +00:00
|
|
|
|
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.LogCreated(currentUser, "comments", reply.ID, fmt.Sprintf(
|
|
|
|
"Commented on thread %d:\n\n%s",
|
|
|
|
thread.ID,
|
|
|
|
message,
|
|
|
|
))
|
|
|
|
|
2022-10-21 04:02:30 +00:00
|
|
|
// If we're attaching a photo, link it to this reply CommentID.
|
|
|
|
if commentPhoto != nil {
|
|
|
|
commentPhoto.CommentID = reply.ID
|
|
|
|
if err := commentPhoto.Save(); err != nil {
|
|
|
|
log.Error("Couldn't save forum reply CommentPhoto.CommentID: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:40:40 +00:00
|
|
|
// What page number of the thread will this comment appear on?
|
|
|
|
// Get the last page of the thread, and if not 1, append the
|
|
|
|
// query string - so the notification might go to e.g.
|
|
|
|
// "/forum/thread/:id?page=4#p50" to link directly to page 4
|
|
|
|
// where comment 50 can be seen.
|
|
|
|
var queryString = ""
|
|
|
|
if lastPage := thread.Pages(); lastPage > 1 {
|
|
|
|
queryString = fmt.Sprintf("?page=%d", lastPage)
|
|
|
|
}
|
|
|
|
|
2023-09-17 06:07:32 +00:00
|
|
|
// Notify watchers about this new post. Filter by blocked user IDs.
|
|
|
|
for _, userID := range models.FilterBlockingUserIDs(currentUser, models.GetSubscribers("threads", thread.ID)) {
|
2022-08-27 18:42:48 +00:00
|
|
|
if userID == currentUser.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
notif := &models.Notification{
|
|
|
|
UserID: userID,
|
|
|
|
AboutUser: *currentUser,
|
|
|
|
Type: models.NotificationAlsoPosted,
|
|
|
|
TableName: "threads",
|
|
|
|
TableID: thread.ID,
|
|
|
|
Message: message,
|
2024-04-28 02:46:22 +00:00
|
|
|
Link: fmt.Sprintf("/go/comment?id=%d", reply.ID),
|
2022-08-27 18:42:48 +00:00
|
|
|
}
|
|
|
|
if err := models.CreateNotification(notif); err != nil {
|
|
|
|
log.Error("Couldn't create thread reply notification for subscriber %d: %s", userID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe the current user to further responses on this thread.
|
2023-10-28 21:34:35 +00:00
|
|
|
if !currentUser.NotificationOptOut(config.NotificationOptOutSubscriptions) {
|
|
|
|
if _, err := models.SubscribeTo(currentUser, "threads", thread.ID); err != nil {
|
|
|
|
log.Error("Couldn't subscribe user %d to forum thread %d: %s", currentUser.ID, thread.ID, err)
|
|
|
|
}
|
2022-08-27 18:42:48 +00:00
|
|
|
}
|
2023-06-16 02:40:40 +00:00
|
|
|
|
|
|
|
// Redirect the poster to the correct page number too.
|
|
|
|
templates.Redirect(w, fmt.Sprintf("/forum/thread/%d%s", thread.ID, queryString))
|
|
|
|
return
|
2022-08-24 05:55:19 +00:00
|
|
|
}
|
2023-06-16 02:40:40 +00:00
|
|
|
|
|
|
|
// Called on the error case that the post couldn't be created -
|
|
|
|
// probably should not happen.
|
2022-08-24 05:55:19 +00:00
|
|
|
templates.Redirect(w, fmt.Sprintf("/forum/thread/%d", thread.ID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new thread?
|
|
|
|
if thread, err := models.CreateThread(
|
|
|
|
currentUser,
|
|
|
|
forum.ID,
|
|
|
|
title,
|
|
|
|
message,
|
2022-08-25 04:17:34 +00:00
|
|
|
isPinned,
|
2022-08-24 05:55:19 +00:00
|
|
|
isExplicit,
|
|
|
|
isNoReply,
|
|
|
|
); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't create thread: %s", err)
|
|
|
|
} else {
|
|
|
|
session.Flash(w, r, "Thread created!")
|
2022-08-27 18:42:48 +00:00
|
|
|
|
2022-10-21 04:02:30 +00:00
|
|
|
// If we're attaching a photo, link it to this CommentID.
|
|
|
|
if commentPhoto != nil {
|
|
|
|
commentPhoto.CommentID = thread.CommentID
|
|
|
|
if err := commentPhoto.Save(); err != nil {
|
|
|
|
log.Error("Couldn't save forum post CommentPhoto.CommentID: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 06:57:06 +00:00
|
|
|
// Are we attaching a poll to this new thread?
|
|
|
|
if isPoll {
|
|
|
|
log.Info("It's a Poll! Options: %+v", pollOptions)
|
|
|
|
poll := models.CreatePoll(pollOptions, pollExpires)
|
2022-12-21 03:15:45 +00:00
|
|
|
poll.MultipleChoice = pollMultipleChoice
|
2022-12-15 06:57:06 +00:00
|
|
|
if err := poll.Save(); err != nil {
|
|
|
|
session.FlashError(w, r, "Error creating poll: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach it to this thread.
|
|
|
|
thread.PollID = &poll.ID
|
|
|
|
if err := thread.Save(); err != nil {
|
|
|
|
log.Error("Couldn't save PollID onto thread! %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 18:42:48 +00:00
|
|
|
// Subscribe the current user to responses on this thread.
|
2023-10-28 21:34:35 +00:00
|
|
|
if !currentUser.NotificationOptOut(config.NotificationOptOutSubscriptions) {
|
|
|
|
if _, err := models.SubscribeTo(currentUser, "threads", thread.ID); err != nil {
|
|
|
|
log.Error("Couldn't subscribe user %d to forum thread %d: %s", currentUser.ID, thread.ID, err)
|
|
|
|
}
|
2022-08-27 18:42:48 +00:00
|
|
|
}
|
|
|
|
|
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.LogCreated(currentUser, "threads", thread.ID, fmt.Sprintf(
|
|
|
|
"Started a new forum thread on forum /f/%s (%s)\n\n"+
|
|
|
|
"* Has poll? %v\n"+
|
|
|
|
"* Title: %s\n\n%s",
|
|
|
|
forum.Fragment,
|
|
|
|
forum.Title,
|
|
|
|
isPoll,
|
|
|
|
thread.Title,
|
|
|
|
message,
|
|
|
|
))
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
templates.Redirect(w, fmt.Sprintf("/forum/thread/%d", thread.ID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var vars = map[string]interface{}{
|
2024-11-28 19:21:06 +00:00
|
|
|
"Forum": forum,
|
|
|
|
"Thread": thread,
|
|
|
|
"Intent": intent,
|
|
|
|
"PostTitle": title,
|
|
|
|
"EditCommentID": editCommentID,
|
|
|
|
"EditThreadSettings": isOriginalComment,
|
|
|
|
"ExplicitPhotoAllowed": explicitPhotoAllowed,
|
|
|
|
"Message": message,
|
2022-08-25 04:17:34 +00:00
|
|
|
|
|
|
|
// Thread settings (for editing the original comment esp.)
|
|
|
|
"IsPinned": isPinned,
|
|
|
|
"IsExplicit": isExplicit,
|
|
|
|
"IsNoReply": isNoReply,
|
2022-10-21 04:02:30 +00:00
|
|
|
|
2022-12-15 06:57:06 +00:00
|
|
|
// Polls
|
2024-11-28 19:21:06 +00:00
|
|
|
"PollOptions": pollOptions,
|
|
|
|
"PollExpires": pollExpires,
|
|
|
|
"PollExpiresOptions": config.PollExpires,
|
2022-12-15 06:57:06 +00:00
|
|
|
|
2022-10-21 04:02:30 +00:00
|
|
|
// Attached photo.
|
|
|
|
"CommentPhoto": commentPhoto,
|
2022-08-24 05:55:19 +00:00
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|