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/router/view_torrent_handler.go

114 lignes
2,7 Kio
Go
Brut Vue normale Historique

package router
2017-05-06 10:36:37 +02:00
import (
"net/http"
"strconv"
2017-05-10 22:09:17 +02:00
"strings"
2017-05-08 19:26:29 +02:00
"time"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/service/captcha"
2017-05-21 01:06:40 +02:00
"github.com/NyaaPantsu/nyaa/service/notifier"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/service/torrent"
"github.com/NyaaPantsu/nyaa/service/user/permission"
"github.com/NyaaPantsu/nyaa/util/log"
msg "github.com/NyaaPantsu/nyaa/util/messages"
"github.com/gorilla/mux"
)
func ViewHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
messages := msg.GetMessages(r)
2017-05-21 01:06:40 +02:00
user := GetUser(r)
if (r.URL.Query()["success"] != nil) {
messages.AddInfo("infos", "Torrent uploaded successfully!")
}
torrent, err := torrentService.GetTorrentById(id)
2017-05-21 01:06:40 +02:00
if (r.URL.Query()["notif"] != nil) {
notifierService.ToggleReadNotification(torrent.Identifier(), user.ID)
}
if err != nil {
NotFoundHandler(w, r)
return
}
b := torrent.ToJSON()
captchaID := ""
if userPermission.NeedsCaptcha(user) {
captchaID = captcha.GetID()
}
htv := ViewTemplateVariables{NewCommonVariables(r), b, captchaID, messages.GetAllErrors(), messages.GetAllInfos()}
err = viewTemplate.ExecuteTemplate(w, "index.html", htv)
if err != nil {
log.Errorf("ViewHandler(): %s", err)
}
2017-05-06 10:36:37 +02:00
}
func PostCommentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
2017-05-08 13:18:52 +02:00
currentUser := GetUser(r)
2017-05-21 19:38:39 +02:00
messages := msg.GetMessages(r)
if userPermission.NeedsCaptcha(currentUser) {
userCaptcha := captcha.Extract(r)
if !captcha.Authenticate(userCaptcha) {
2017-05-21 19:38:39 +02:00
messages.AddError("errors", "Bad captcha!")
}
}
2017-05-08 06:58:21 +02:00
content := p.Sanitize(r.FormValue("comment"))
if strings.TrimSpace(content) == "" {
2017-05-21 19:38:39 +02:00
messages.AddError("errors", "Comment empty!")
}
2017-05-21 19:38:39 +02:00
if !messages.HasErrors() {
idNum, err := strconv.Atoi(id)
2017-05-21 19:38:39 +02:00
userID := currentUser.ID
comment := model.Comment{TorrentID: uint(idNum), UserID: userID, Content: content, CreatedAt: time.Now()}
2017-05-21 19:38:39 +02:00
err = db.ORM.Create(&comment).Error
if err != nil {
messages.ImportFromError("errors", err)
}
}
2017-05-21 19:38:39 +02:00
ViewHandler(w,r)
}
2017-05-10 10:17:21 +02:00
func ReportTorrentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
2017-05-21 19:38:39 +02:00
messages := msg.GetMessages(r)
2017-05-10 10:17:21 +02:00
currentUser := GetUser(r)
if userPermission.NeedsCaptcha(currentUser) {
userCaptcha := captcha.Extract(r)
if !captcha.Authenticate(userCaptcha) {
2017-05-21 19:38:39 +02:00
messages.AddError("errors", "Bad captcha!")
}
}
2017-05-21 19:38:39 +02:00
if !messages.HasErrors() {
idNum, err := strconv.Atoi(id)
userID := currentUser.ID
report := model.TorrentReport{
Description: r.FormValue("report_type"),
TorrentID: uint(idNum),
UserID: userID,
CreatedAt: time.Now(),
}
2017-05-10 10:17:21 +02:00
2017-05-21 19:38:39 +02:00
err = db.ORM.Create(&report).Error
if err != nil {
messages.ImportFromError("errors", err)
}
2017-05-10 10:17:21 +02:00
}
2017-05-21 19:38:39 +02:00
ViewHandler(w,r)
2017-05-10 10:17:21 +02:00
}