6edc68fe22
* Torrent Mass Edit Api (WIP) * Torrents can be deleted in mass from frontend with api post request * Torrents status can be edited from frontend with api post request -- Look to function doc for more info on how to use it It is a WIP so it might not work =D * Finished Mass mod Api As per suggestion of @yiiTT in #720, I added: * Changing torrents category * Deletion of reports with deletion of a torrent * Changing owner of multiple torrents Commit also add some new translation strings. * Make some changes * Reports can now be cleared for the torrents selected without having to delete them * Users with no admin rights can't delete reports * Fix moveto to status moveto deprecated in api * Tested and works! Changes: * Updates only the colomns of torrent table * Moved categories config in config/torrents.go * Forgot this file in last commit * Less useless queries The use of Save makes it that users are created and updates also all the associatiated models. Better to just update the colomns needed (less useless queries) * Some Updates * Added a new status of 5 for locking torrents * Modifying the list torrents view for using it in deleted torrents view * Added function to get deleted torrents * Torrents (and reports) can be definitely deleted * Some new translation string * Fixing * fix 2 * Added upload check for locked torrents If a user owns a torrent, has deleted it and try to repload it. As long as it has not been locked, he can. * Fixing wrong condition in isdeleted * Finished * Info messages on success when deletes or lock * Fixed double deleted_at is Null * Added Link to view of deleted torrents * Added new translation string
228 lignes
6,9 Kio
Go
228 lignes
6,9 Kio
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/NyaaPantsu/nyaa/db"
|
|
"github.com/NyaaPantsu/nyaa/model"
|
|
"github.com/NyaaPantsu/nyaa/service"
|
|
"github.com/NyaaPantsu/nyaa/service/captcha"
|
|
"github.com/NyaaPantsu/nyaa/service/notifier"
|
|
"github.com/NyaaPantsu/nyaa/service/report"
|
|
"github.com/NyaaPantsu/nyaa/service/torrent"
|
|
"github.com/NyaaPantsu/nyaa/service/user/permission"
|
|
"github.com/NyaaPantsu/nyaa/util"
|
|
"github.com/NyaaPantsu/nyaa/util/languages"
|
|
"github.com/NyaaPantsu/nyaa/util/log"
|
|
"github.com/NyaaPantsu/nyaa/util/filelist"
|
|
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)
|
|
user := GetUser(r)
|
|
|
|
if r.URL.Query()["success"] != nil {
|
|
messages.AddInfo("infos", "Torrent uploaded successfully!")
|
|
}
|
|
|
|
torrent, err := torrentService.GetTorrentById(id)
|
|
|
|
if r.URL.Query()["notif"] != nil {
|
|
notifierService.ToggleReadNotification(torrent.Identifier(), user.ID)
|
|
}
|
|
|
|
if err != nil {
|
|
NotFoundHandler(w, r)
|
|
return
|
|
}
|
|
b := torrent.ToJSON()
|
|
folder := filelist.FileListToFolder(torrent.FileList)
|
|
captchaID := ""
|
|
if userPermission.NeedsCaptcha(user) {
|
|
captchaID = captcha.GetID()
|
|
}
|
|
htv := ViewTemplateVariables{NewCommonVariables(r), b, folder, captchaID, messages.GetAllErrors(), messages.GetAllInfos()}
|
|
|
|
err = viewTemplate.ExecuteTemplate(w, "index.html", htv)
|
|
if err != nil {
|
|
log.Errorf("ViewHandler(): %s", err)
|
|
}
|
|
}
|
|
|
|
func ViewHeadHandler(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.ParseInt(vars["id"], 10, 32)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
_, err = torrentService.GetRawTorrentById(uint(id))
|
|
|
|
if err != nil {
|
|
NotFoundHandler(w, r)
|
|
return
|
|
}
|
|
|
|
w.Write(nil)
|
|
}
|
|
|
|
func PostCommentHandler(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id := vars["id"]
|
|
|
|
torrent, err := torrentService.GetTorrentById(id)
|
|
if err != nil {
|
|
NotFoundHandler(w, r)
|
|
return
|
|
}
|
|
|
|
currentUser := GetUser(r)
|
|
messages := msg.GetMessages(r)
|
|
|
|
if userPermission.NeedsCaptcha(currentUser) {
|
|
userCaptcha := captcha.Extract(r)
|
|
if !captcha.Authenticate(userCaptcha) {
|
|
messages.AddErrorT("errors", "bad_captcha")
|
|
}
|
|
}
|
|
content := util.Sanitize(r.FormValue("comment"), "comment")
|
|
|
|
if strings.TrimSpace(content) == "" {
|
|
messages.AddErrorT("errors", "comment_empty")
|
|
}
|
|
if !messages.HasErrors() {
|
|
userID := currentUser.ID
|
|
comment := model.Comment{TorrentID: torrent.ID, UserID: userID, Content: content, CreatedAt: time.Now()}
|
|
err := db.ORM.Create(&comment).Error
|
|
comment.Torrent = &torrent
|
|
|
|
url, err := Router.Get("view_torrent").URL("id", strconv.FormatUint(uint64(torrent.ID), 10))
|
|
torrent.Uploader.ParseSettings()
|
|
if torrent.Uploader.Settings.Get("new_comment") {
|
|
T, _, _ := languages.TfuncAndLanguageWithFallback(torrent.Uploader.Language, torrent.Uploader.Language) // We need to send the notification to every user in their language
|
|
notifierService.NotifyUser(torrent.Uploader, comment.Identifier(), fmt.Sprintf(T("new_comment_on_torrent"), torrent.Name), url.String(), torrent.Uploader.Settings.Get("new_comment_email"))
|
|
}
|
|
|
|
if err != nil {
|
|
messages.ImportFromError("errors", err)
|
|
}
|
|
}
|
|
ViewHandler(w, r)
|
|
}
|
|
|
|
func ReportTorrentHandler(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id := vars["id"]
|
|
messages := msg.GetMessages(r)
|
|
currentUser := GetUser(r)
|
|
if userPermission.NeedsCaptcha(currentUser) {
|
|
userCaptcha := captcha.Extract(r)
|
|
if !captcha.Authenticate(userCaptcha) {
|
|
messages.AddErrorT("errors", "bad_captcha")
|
|
}
|
|
}
|
|
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(),
|
|
}
|
|
|
|
err = db.ORM.Create(&report).Error
|
|
if err != nil {
|
|
messages.ImportFromError("errors", err)
|
|
}
|
|
}
|
|
ViewHandler(w, r)
|
|
}
|
|
|
|
func TorrentEditUserPanel(w http.ResponseWriter, r *http.Request) {
|
|
id := r.URL.Query().Get("id")
|
|
torrent, _ := torrentService.GetTorrentById(id)
|
|
messages := msg.GetMessages(r)
|
|
currentUser := GetUser(r)
|
|
if userPermission.CurrentOrAdmin(currentUser, torrent.UploaderID) {
|
|
uploadForm := NewUploadForm()
|
|
uploadForm.Name = torrent.Name
|
|
uploadForm.Category = strconv.Itoa(torrent.Category) + "_" + strconv.Itoa(torrent.SubCategory)
|
|
uploadForm.Remake = torrent.Status == model.TorrentStatusRemake
|
|
uploadForm.WebsiteLink = string(torrent.WebsiteLink)
|
|
uploadForm.Description = string(torrent.Description)
|
|
htv := UserTorrentEdVbs{NewCommonVariables(r), uploadForm, messages.GetAllErrors(), messages.GetAllInfos()}
|
|
err := userTorrentEd.ExecuteTemplate(w, "index.html", htv)
|
|
log.CheckError(err)
|
|
} else {
|
|
NotFoundHandler(w, r)
|
|
}
|
|
}
|
|
|
|
func TorrentPostEditUserPanel(w http.ResponseWriter, r *http.Request) {
|
|
var uploadForm UploadForm
|
|
id := r.URL.Query().Get("id")
|
|
messages := msg.GetMessages(r)
|
|
torrent, _ := torrentService.GetTorrentById(id)
|
|
currentUser := GetUser(r)
|
|
if torrent.ID > 0 && userPermission.CurrentOrAdmin(currentUser, torrent.UploaderID) {
|
|
errUp := uploadForm.ExtractEditInfo(r)
|
|
if errUp != nil {
|
|
messages.AddErrorT("errors", "fail_torrent_update")
|
|
}
|
|
if !messages.HasErrors() {
|
|
status := model.TorrentStatusNormal
|
|
uploadForm.Remake = r.FormValue(UploadFormRemake) == "on"
|
|
if uploadForm.Remake { // overrides trusted
|
|
status = model.TorrentStatusRemake
|
|
} else if currentUser.IsTrusted() {
|
|
status = model.TorrentStatusTrusted
|
|
}
|
|
// update some (but not all!) values
|
|
torrent.Name = uploadForm.Name
|
|
torrent.Category = uploadForm.CategoryID
|
|
torrent.SubCategory = uploadForm.SubCategoryID
|
|
torrent.Status = status
|
|
torrent.WebsiteLink = uploadForm.WebsiteLink
|
|
torrent.Description = uploadForm.Description
|
|
// torrent.Uploader = nil // GORM will create a new user otherwise (wtf?!)
|
|
db.ORM.Model(&torrent).UpdateColumn(&torrent)
|
|
messages.AddInfoT("infos", "torrent_updated")
|
|
}
|
|
htv := UserTorrentEdVbs{NewCommonVariables(r), uploadForm, messages.GetAllErrors(), messages.GetAllInfos()}
|
|
err_ := userTorrentEd.ExecuteTemplate(w, "index.html", htv)
|
|
log.CheckError(err_)
|
|
} else {
|
|
NotFoundHandler(w, r)
|
|
}
|
|
}
|
|
|
|
func TorrentDeleteUserPanel(w http.ResponseWriter, r *http.Request) {
|
|
id := r.URL.Query().Get("id")
|
|
currentUser := GetUser(r)
|
|
torrent, _ := torrentService.GetTorrentById(id)
|
|
if userPermission.CurrentOrAdmin(currentUser, torrent.UploaderID) {
|
|
_, err := torrentService.DeleteTorrent(id)
|
|
if err == nil {
|
|
//delete reports of torrent
|
|
whereParams := serviceBase.CreateWhereParams("torrent_id = ?", id)
|
|
reports, _, _ := reportService.GetTorrentReportsOrderBy(&whereParams, "", 0, 0)
|
|
for _, report := range reports {
|
|
reportService.DeleteTorrentReport(report.ID)
|
|
}
|
|
}
|
|
url, _ := Router.Get("home").URL()
|
|
http.Redirect(w, r, url.String()+"?deleted", http.StatusSeeOther)
|
|
} else {
|
|
NotFoundHandler(w, r)
|
|
}
|
|
}
|