Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/controllers/torrent/comment.go
akuma06 2c8344faf9 Controllers are now separated
They are in different folders and all loaded in controllers/router.go.
This means that you only have to create a folder with a router.go file and import this folder in main router.go to add a handler.
2017-07-16 17:14:21 +02:00

54 lignes
1,4 Kio
Go

package torrentController
import (
"net/http"
"strconv"
"strings"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/controllers/router"
"github.com/NyaaPantsu/nyaa/models/comments"
"github.com/NyaaPantsu/nyaa/models/torrents"
"github.com/NyaaPantsu/nyaa/utils/captcha"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/sanitize"
"github.com/gin-gonic/gin"
)
// PostCommentHandler : Controller for posting a comment
func PostCommentHandler(c *gin.Context) {
id, _ := strconv.ParseInt(c.Param("id"), 10, 32)
torrent, err := torrents.FindByID(uint(id))
if err != nil {
c.Status(http.StatusNotFound)
return
}
currentUser := router.GetUser(c)
messages := msg.GetMessages(c)
if currentUser.NeedsCaptcha() {
userCaptcha := captcha.Extract(c)
if !captcha.Authenticate(userCaptcha) {
messages.AddErrorT("errors", "bad_captcha")
}
}
content := sanitize.Sanitize(c.PostForm("comment"), "comment")
if strings.TrimSpace(content) == "" {
messages.AddErrorT("errors", "comment_empty")
}
if len(content) > config.Get().CommentLength {
messages.AddErrorT("errors", "comment_toolong")
}
if !messages.HasErrors() {
_, err := comments.Create(content, torrent, currentUser)
if err != nil {
messages.Error(err)
}
}
url := "/view/" + strconv.FormatUint(uint64(torrent.ID), 10)
c.Redirect(302, url)
}