2017-06-28 13:42:38 +02:00
package controllers
2017-05-10 04:03:25 +02:00
import (
2017-05-10 21:29:59 +02:00
"fmt"
2017-05-10 19:17:07 +02:00
"html"
2017-05-10 05:54:12 +02:00
"net/http"
"strconv"
2017-05-13 17:29:21 +02:00
"strings"
2017-05-10 04:03:25 +02:00
2017-05-20 13:45:15 +02:00
"github.com/NyaaPantsu/nyaa/config"
2017-06-29 13:15:23 +02:00
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/models/activities"
"github.com/NyaaPantsu/nyaa/models/comments"
"github.com/NyaaPantsu/nyaa/models/reports"
"github.com/NyaaPantsu/nyaa/models/torrents"
2017-07-02 23:53:23 +02:00
"github.com/NyaaPantsu/nyaa/models/users"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/categories"
"github.com/NyaaPantsu/nyaa/utils/cookies"
"github.com/NyaaPantsu/nyaa/utils/log"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/search"
2017-07-02 23:53:23 +02:00
"github.com/NyaaPantsu/nyaa/utils/search/structs"
"github.com/NyaaPantsu/nyaa/utils/upload"
"github.com/NyaaPantsu/nyaa/utils/validator/torrent"
2017-06-28 13:42:38 +02:00
"github.com/gin-gonic/gin"
2017-05-10 04:03:25 +02:00
)
2017-05-25 21:54:58 +02:00
// ReassignForm : Structure for reassign Form used by the reassign page
2017-05-13 17:29:21 +02:00
type ReassignForm struct {
AssignTo uint
By string
Data string
Torrents [ ] uint
}
2017-05-25 21:54:58 +02:00
// ExtractInfo : Function to assign values from request to ReassignForm
2017-06-28 13:42:38 +02:00
func ( f * ReassignForm ) ExtractInfo ( c * gin . Context ) bool {
f . By = c . PostForm ( "by" )
messages := msg . GetMessages ( c )
2017-05-13 17:29:21 +02:00
if f . By != "olduser" && f . By != "torrentid" {
2017-06-28 13:42:38 +02:00
messages . AddErrorTf ( "errors" , "no_action_exist" , f . By )
return false
2017-05-13 17:29:21 +02:00
}
2017-06-28 13:42:38 +02:00
f . Data = strings . Trim ( c . PostForm ( "data" ) , " \r\n" )
2017-05-13 17:29:21 +02:00
if f . By == "olduser" {
if f . Data == "" {
2017-06-28 13:42:38 +02:00
messages . AddErrorT ( "errors" , "user_not_found" )
return false
2017-05-13 17:29:21 +02:00
} else if strings . Contains ( f . Data , "\n" ) {
2017-06-28 13:42:38 +02:00
messages . AddErrorT ( "errors" , "multiple_username_error" )
return false
2017-05-13 17:29:21 +02:00
}
} else if f . By == "torrentid" {
if f . Data == "" {
2017-06-28 13:42:38 +02:00
messages . AddErrorT ( "errors" , "no_id_given" )
return false
2017-05-13 17:29:21 +02:00
}
splitData := strings . Split ( f . Data , "\n" )
for i , tmp := range splitData {
tmp = strings . Trim ( tmp , " \r" )
2017-05-25 21:54:58 +02:00
torrentID , err := strconv . ParseUint ( tmp , 10 , 0 )
2017-05-13 17:29:21 +02:00
if err != nil {
2017-06-28 13:42:38 +02:00
messages . AddErrorTf ( "errors" , "parse_error_line" , i + 1 )
return false // TODO: Shouldn't it continue to parse the rest and display the errored lines?
2017-05-13 17:29:21 +02:00
}
2017-05-25 21:54:58 +02:00
f . Torrents = append ( f . Torrents , uint ( torrentID ) )
2017-05-13 17:29:21 +02:00
}
}
2017-06-28 13:42:38 +02:00
tmpID := c . PostForm ( "to" )
2017-07-02 23:53:23 +02:00
parsed , err := strconv . ParseUint ( tmpID , 10 , 32 )
2017-05-13 17:29:21 +02:00
if err != nil {
2017-06-28 13:42:38 +02:00
messages . Error ( err )
return false
2017-05-13 17:29:21 +02:00
}
f . AssignTo = uint ( parsed )
2017-07-02 23:53:23 +02:00
_ , _ , _ , _ , err = cookies . RetrieveUserFromRequest ( c , uint ( parsed ) )
2017-05-13 17:36:50 +02:00
if err != nil {
2017-06-28 13:42:38 +02:00
messages . AddErrorTf ( "errors" , "no_user_found_id" , int ( parsed ) )
return false
2017-05-13 17:36:50 +02:00
}
2017-05-13 17:29:21 +02:00
2017-06-28 13:42:38 +02:00
return true
2017-05-13 17:29:21 +02:00
}
2017-05-25 21:54:58 +02:00
// ExecuteAction : Function for applying the changes from ReassignForm
2017-05-13 17:29:21 +02:00
func ( f * ReassignForm ) ExecuteAction ( ) ( int , error ) {
var toBeChanged [ ] uint
var err error
if f . By == "olduser" {
2017-07-02 23:53:23 +02:00
toBeChanged , err = users . FindOldUploadsByUsername ( f . Data )
2017-05-13 17:29:21 +02:00
if err != nil {
return 0 , err
}
} else if f . By == "torrentid" {
toBeChanged = f . Torrents
}
num := 0
2017-05-25 21:54:58 +02:00
for _ , torrentID := range toBeChanged {
2017-07-02 23:53:23 +02:00
torrent , err2 := torrents . FindRawByID ( torrentID )
2017-05-13 17:29:21 +02:00
if err2 == nil {
torrent . UploaderID = f . AssignTo
2017-07-02 23:53:23 +02:00
torrent . Update ( true )
2017-05-25 21:54:58 +02:00
num ++
2017-05-13 17:29:21 +02:00
}
}
return num , nil
}
2017-05-25 21:54:58 +02:00
// IndexModPanel : Controller for showing index page of Mod Panel
2017-06-28 13:42:38 +02:00
func IndexModPanel ( c * gin . Context ) {
2017-05-20 01:10:16 +02:00
offset := 10
2017-07-02 23:53:23 +02:00
torrents , _ , _ := torrents . FindAll ( offset , 0 )
users , _ := users . FindUsersForAdmin ( offset , 0 )
comments , _ := comments . FindAll ( offset , 0 , "" , "" )
torrentReports , _ , _ := reports . GetAll ( offset , 0 )
2017-05-20 01:10:16 +02:00
2017-07-01 23:09:35 +02:00
panelAdminTemplate ( c , torrents , models . TorrentReportsToJSON ( torrentReports ) , users , comments )
2017-05-10 05:24:18 +02:00
}
2017-05-10 20:42:20 +02:00
2017-05-25 21:54:58 +02:00
// TorrentsListPanel : Controller for listing torrents, can accept common search arguments
2017-06-28 13:42:38 +02:00
func TorrentsListPanel ( c * gin . Context ) {
page := c . Param ( "page" )
messages := msg . GetMessages ( c )
deleted := c . Request . URL . Query ( ) [ "deleted" ]
unblocked := c . Request . URL . Query ( ) [ "unblocked" ]
blocked := c . Request . URL . Query ( ) [ "blocked" ]
2017-05-25 02:19:05 +02:00
if deleted != nil {
messages . AddInfoTf ( "infos" , "torrent_deleted" , "" )
}
if blocked != nil {
messages . AddInfoT ( "infos" , "torrent_blocked" )
}
if unblocked != nil {
messages . AddInfoT ( "infos" , "torrent_unblocked" )
}
2017-05-10 05:24:18 +02:00
2017-05-20 01:10:16 +02:00
var err error
pagenum := 1
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
2017-06-28 13:42:38 +02:00
c . AbortWithError ( http . StatusInternalServerError , err )
2017-05-20 01:10:16 +02:00
return
2017-05-10 19:17:07 +02:00
}
2017-05-25 21:54:58 +02:00
}
2017-05-10 05:24:18 +02:00
2017-07-05 13:33:12 +02:00
searchParam , torrents , count , err := search . ByQueryWithUser ( c , pagenum )
2017-06-14 12:10:03 +02:00
if err != nil {
2017-06-28 13:42:38 +02:00
messages . Error ( err )
2017-06-14 12:10:03 +02:00
}
2017-06-18 00:30:12 +02:00
category := ""
if len ( searchParam . Category ) > 0 {
category = searchParam . Category [ 0 ] . String ( )
}
2017-05-25 21:54:58 +02:00
searchForm := searchForm {
2017-07-02 23:53:23 +02:00
TorrentParam : searchParam ,
2017-06-18 00:30:12 +02:00
Category : category ,
2017-05-25 21:54:58 +02:00
ShowItemsPerPage : true ,
}
2017-05-20 01:10:16 +02:00
2017-07-07 00:27:08 +02:00
nav := navigation { count , int ( searchParam . Max ) , pagenum , "mod/torrents/p" }
2017-06-28 13:42:38 +02:00
modelList ( c , "admin/torrentlist.jet.html" , torrents , nav , searchForm )
2017-05-10 05:24:18 +02:00
}
2017-05-10 20:42:20 +02:00
2017-05-25 21:54:58 +02:00
// TorrentReportListPanel : Controller for listing torrent reports, can accept pages
2017-06-28 13:42:38 +02:00
func TorrentReportListPanel ( c * gin . Context ) {
page := c . Param ( "page" )
2017-05-20 01:10:16 +02:00
pagenum := 1
2017-05-25 21:54:58 +02:00
offset := 100
var err error
2017-05-20 01:10:16 +02:00
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
2017-06-28 13:42:38 +02:00
c . AbortWithError ( http . StatusInternalServerError , err )
2017-05-20 01:10:16 +02:00
return
2017-05-10 20:42:20 +02:00
}
2017-05-20 01:10:16 +02:00
}
2017-05-10 20:42:20 +02:00
2017-07-02 23:53:23 +02:00
torrentReports , nbReports , _ := reports . GetAll ( offset , ( pagenum - 1 ) * offset )
2017-05-10 20:42:20 +02:00
2017-07-01 23:09:35 +02:00
reportJSON := models . TorrentReportsToJSON ( torrentReports )
2017-07-07 00:27:08 +02:00
nav := navigation { nbReports , offset , pagenum , "mod/reports/p" }
2017-06-28 13:42:38 +02:00
modelList ( c , "admin/torrent_report.jet.html" , reportJSON , nav , newSearchForm ( c ) )
2017-05-10 20:42:20 +02:00
}
2017-05-25 21:54:58 +02:00
// UsersListPanel : Controller for listing users, can accept pages
2017-06-28 13:42:38 +02:00
func UsersListPanel ( c * gin . Context ) {
page := c . Param ( "page" )
2017-05-20 01:10:16 +02:00
pagenum := 1
2017-05-25 21:54:58 +02:00
offset := 100
var err error
2017-05-20 01:10:16 +02:00
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
2017-06-28 13:42:38 +02:00
c . AbortWithError ( http . StatusInternalServerError , err )
2017-05-20 01:10:16 +02:00
return
}
2017-05-10 05:54:12 +02:00
}
2017-05-20 01:10:16 +02:00
2017-07-02 23:53:23 +02:00
users , nbUsers := users . FindUsersForAdmin ( offset , ( pagenum - 1 ) * offset )
2017-07-07 00:27:08 +02:00
nav := navigation { nbUsers , offset , pagenum , "mod/users/p" }
2017-06-28 13:42:38 +02:00
modelList ( c , "admin/userlist.jet.html" , users , nav , newSearchForm ( c ) )
2017-05-10 05:24:18 +02:00
}
2017-05-10 20:42:20 +02:00
2017-05-25 21:54:58 +02:00
// CommentsListPanel : Controller for listing comments, can accept pages and userID
2017-06-28 13:42:38 +02:00
func CommentsListPanel ( c * gin . Context ) {
page := c . Param ( "page" )
2017-05-20 01:10:16 +02:00
pagenum := 1
2017-05-25 21:54:58 +02:00
offset := 100
2017-06-28 13:42:38 +02:00
userid := c . Query ( "userid" )
2017-05-25 21:54:58 +02:00
var err error
2017-05-20 01:10:16 +02:00
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
2017-06-28 13:42:38 +02:00
c . AbortWithError ( http . StatusInternalServerError , err )
2017-05-20 01:10:16 +02:00
return
}
}
var conditions string
var values [ ] interface { }
if userid != "" {
conditions = "user_id = ?"
values = append ( values , userid )
2017-05-10 05:54:12 +02:00
}
2017-05-10 04:03:25 +02:00
2017-07-02 23:53:23 +02:00
comments , nbComments := comments . FindAll ( offset , ( pagenum - 1 ) * offset , conditions , values ... )
2017-07-07 00:27:08 +02:00
nav := navigation { nbComments , offset , pagenum , "mod/comments/p" }
2017-06-28 13:42:38 +02:00
modelList ( c , "admin/commentlist.jet.html" , comments , nav , newSearchForm ( c ) )
2017-05-10 04:03:25 +02:00
}
2017-05-11 01:46:50 +02:00
2017-05-25 21:54:58 +02:00
// TorrentEditModPanel : Controller for editing a torrent after GET request
2017-06-28 13:42:38 +02:00
func TorrentEditModPanel ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
torrent , _ := torrents . FindByID ( uint ( id ) )
2017-05-25 21:54:58 +02:00
torrentJSON := torrent . ToJSON ( )
2017-07-02 23:53:23 +02:00
uploadForm := upload . NewTorrentRequest ( )
2017-05-25 21:54:58 +02:00
uploadForm . Name = torrentJSON . Name
uploadForm . Category = torrentJSON . Category + "_" + torrentJSON . SubCategory
uploadForm . Status = torrentJSON . Status
2017-05-27 20:33:40 +02:00
uploadForm . Hidden = torrent . Hidden
2017-05-25 21:54:58 +02:00
uploadForm . WebsiteLink = string ( torrentJSON . WebsiteLink )
uploadForm . Description = string ( torrentJSON . Description )
2017-07-02 23:53:23 +02:00
uploadForm . Languages = torrent . Languages
2017-06-28 13:42:38 +02:00
formTemplate ( c , "admin/paneltorrentedit.jet.html" , uploadForm )
2017-05-10 05:24:18 +02:00
}
2017-05-11 01:46:50 +02:00
2017-05-25 21:54:58 +02:00
// TorrentPostEditModPanel : Controller for editing a torrent after POST request
2017-06-28 13:42:38 +02:00
func TorrentPostEditModPanel ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
var uploadForm torrentValidator . TorrentRequest
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
2017-06-28 13:42:38 +02:00
messages := msg . GetMessages ( c )
2017-07-02 23:53:23 +02:00
torrent , _ := torrents . FindByID ( uint ( id ) )
2017-05-11 12:49:05 +02:00
if torrent . ID > 0 {
2017-07-02 23:53:23 +02:00
errUp := upload . ExtractEditInfo ( c , & uploadForm )
2017-05-10 23:53:25 +02:00
if errUp != nil {
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "fail_torrent_update" )
2017-05-11 01:46:50 +02:00
}
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
2017-05-11 01:46:50 +02:00
// update some (but not all!) values
2017-05-11 12:49:05 +02:00
torrent . Name = uploadForm . Name
torrent . Category = uploadForm . CategoryID
2017-05-11 01:46:50 +02:00
torrent . SubCategory = uploadForm . SubCategoryID
2017-05-11 12:49:05 +02:00
torrent . Status = uploadForm . Status
2017-05-27 20:33:40 +02:00
torrent . Hidden = uploadForm . Hidden
2017-05-21 04:10:59 +02:00
torrent . WebsiteLink = uploadForm . WebsiteLink
2017-05-11 01:46:50 +02:00
torrent . Description = uploadForm . Description
2017-07-02 23:53:23 +02:00
torrent . Languages = uploadForm . Languages
_ , err := torrent . UpdateUnscope ( )
2017-05-23 04:05:33 +02:00
messages . AddInfoT ( "infos" , "torrent_updated" )
2017-06-15 04:44:46 +02:00
if err == nil { // We only log edit torrent for admins
2017-07-02 23:53:23 +02:00
_ , username := torrents . HideUser ( torrent . UploaderID , torrent . Uploader . Username , torrent . Hidden )
activities . Log ( & models . User { } , torrent . Identifier ( ) , "edit" , "torrent_edited_by" , strconv . Itoa ( int ( torrent . ID ) ) , username , getUser ( c ) . Username )
2017-06-15 04:44:46 +02:00
}
2017-05-10 05:54:12 +02:00
}
2017-05-10 05:24:18 +02:00
}
2017-06-28 13:42:38 +02:00
formTemplate ( c , "admin/paneltorrentedit.jet.html" , uploadForm )
2017-05-10 05:24:18 +02:00
}
2017-05-25 21:54:58 +02:00
// CommentDeleteModPanel : Controller for deleting a comment
2017-06-28 13:42:38 +02:00
func CommentDeleteModPanel ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
comment , _ , err := comments . Delete ( uint ( id ) )
2017-06-15 04:44:46 +02:00
if err == nil {
2017-07-02 23:53:23 +02:00
activities . Log ( & models . User { } , comment . Identifier ( ) , "delete" , "comment_deleted_by" , strconv . Itoa ( int ( comment . ID ) ) , comment . User . Username , getUser ( c ) . Username )
2017-06-15 04:44:46 +02:00
}
2017-06-28 13:42:38 +02:00
c . Redirect ( http . StatusSeeOther , "/mod/comments?deleted" )
2017-05-10 04:03:25 +02:00
}
2017-05-10 22:05:56 +02:00
2017-05-25 21:54:58 +02:00
// TorrentDeleteModPanel : Controller for deleting a torrent
2017-06-28 13:42:38 +02:00
func TorrentDeleteModPanel ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
2017-06-28 13:42:38 +02:00
definitely := c . Request . URL . Query ( ) [ "definitely" ]
2017-07-02 23:53:23 +02:00
var returnRoute = "/mod/torrents"
torrent , errFind := torrents . FindByID ( uint ( id ) )
if errFind == nil {
var err error
if definitely != nil {
_ , _ , err = torrent . DefinitelyDelete ( )
//delete reports of torrent
whereParams := structs . CreateWhereParams ( "torrent_id = ?" , id )
reports , _ , _ := reports . FindOrderBy ( & whereParams , "" , 0 , 0 )
for _ , report := range reports {
report . Delete ( true )
}
returnRoute = "/mod/torrents/deleted"
} else {
_ , _ , err = torrent . Delete ( false )
2017-05-20 01:10:16 +02:00
2017-07-02 23:53:23 +02:00
//delete reports of torrent
whereParams := structs . CreateWhereParams ( "torrent_id = ?" , id )
reports , _ , _ := reports . FindOrderBy ( & whereParams , "" , 0 , 0 )
for _ , report := range reports {
report . Delete ( false )
}
}
if err == nil {
_ , username := torrents . HideUser ( torrent . UploaderID , torrent . Uploader . Username , torrent . Hidden )
activities . Log ( & models . User { } , torrent . Identifier ( ) , "delete" , "torrent_deleted_by" , strconv . Itoa ( int ( torrent . ID ) ) , username , getUser ( c ) . Username )
2017-05-25 02:19:05 +02:00
}
2017-06-15 04:44:46 +02:00
}
2017-06-28 13:42:38 +02:00
c . Redirect ( http . StatusSeeOther , returnRoute + "?deleted" )
2017-05-10 19:17:07 +02:00
}
2017-05-10 22:05:56 +02:00
2017-05-25 21:54:58 +02:00
// TorrentReportDeleteModPanel : Controller for deleting a torrent report
2017-06-28 13:42:38 +02:00
func TorrentReportDeleteModPanel ( c * gin . Context ) {
id := c . Query ( "id" )
2017-05-20 01:10:16 +02:00
fmt . Println ( id )
idNum , _ := strconv . ParseUint ( id , 10 , 64 )
2017-07-02 23:53:23 +02:00
_ , _ , _ = reports . Delete ( uint ( idNum ) )
2017-06-15 04:44:46 +02:00
/ * If we need to log report delete activity
if err == nil {
2017-07-01 23:09:35 +02:00
activity . Log ( & models . User { } , torrent . Identifier ( ) , "delete" , "torrent_report_deleted_by" , strconv . Itoa ( int ( report . ID ) ) , getUser ( c ) . Username )
2017-06-15 04:44:46 +02:00
}
* /
2017-06-28 13:42:38 +02:00
c . Redirect ( http . StatusSeeOther , "/mod/reports?deleted" )
2017-05-10 22:05:56 +02:00
}
2017-05-13 17:29:21 +02:00
2017-05-25 21:54:58 +02:00
// TorrentReassignModPanel : Controller for reassigning a torrent, after GET request
2017-06-28 13:42:38 +02:00
func TorrentReassignModPanel ( c * gin . Context ) {
formTemplate ( c , "admin/reassign.jet.html" , ReassignForm { } )
2017-05-13 17:29:21 +02:00
}
2017-05-25 21:54:58 +02:00
// TorrentPostReassignModPanel : Controller for reassigning a torrent, after POST request
2017-06-28 13:42:38 +02:00
func TorrentPostReassignModPanel ( c * gin . Context ) {
2017-05-13 17:29:21 +02:00
var rForm ReassignForm
2017-06-28 13:42:38 +02:00
messages := msg . GetMessages ( c )
2017-05-13 17:29:21 +02:00
2017-06-28 13:42:38 +02:00
if rForm . ExtractInfo ( c ) {
2017-05-13 17:29:21 +02:00
count , err2 := rForm . ExecuteAction ( )
if err2 != nil {
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "something_went_wrong" )
2017-05-13 17:29:21 +02:00
} else {
2017-05-23 04:05:33 +02:00
messages . AddInfoTf ( "infos" , "nb_torrents_updated" , count )
2017-05-13 17:29:21 +02:00
}
}
2017-06-28 13:42:38 +02:00
formTemplate ( c , "admin/reassign.jet.html" , rForm )
2017-05-13 17:29:21 +02:00
}
2017-05-20 13:45:15 +02:00
2017-05-25 21:54:58 +02:00
// TorrentsPostListPanel : Controller for listing torrents, after POST request when mass update
2017-06-28 13:42:38 +02:00
func TorrentsPostListPanel ( c * gin . Context ) {
torrentManyAction ( c )
TorrentsListPanel ( c )
2017-05-20 13:45:15 +02:00
}
2017-05-25 21:54:58 +02:00
// APIMassMod : This function is used on the frontend for the mass
/ * Query is : action = status | delete | owner | category | multiple
2017-05-24 20:23:54 +02:00
* Needed : torrent_id [ ] Ids of torrents in checkboxes of name torrent_id
2017-05-25 21:54:58 +02:00
*
2017-05-24 20:23:54 +02:00
* Needed on context :
2017-07-04 02:07:25 +02:00
* status = 0 | 1 | 2 | 3 | 4 according to config / find . go ( can be omitted if action = delete | owner | category | multiple )
2017-05-24 20:23:54 +02:00
* owner is the User ID of the new owner of the torrents ( can be omitted if action = delete | status | category | multiple )
* category is the category string ( eg . 1_3 ) of the new category of the torrents ( can be omitted if action = delete | status | owner | multiple )
2017-05-24 21:16:15 +02:00
*
* withreport is the bool to enable torrent reports deletion ( can be omitted )
2017-05-24 20:23:54 +02:00
*
2017-05-25 21:54:58 +02:00
* In case of action = multiple , torrents can be at the same time changed status , owner and category
2017-05-24 01:03:03 +02:00
* /
2017-06-28 13:42:38 +02:00
func APIMassMod ( c * gin . Context ) {
torrentManyAction ( c )
2017-07-02 16:54:55 +02:00
messages := msg . GetMessages ( c ) // new utils for errors and infos
2017-06-28 13:42:38 +02:00
c . Header ( "Content-Type" , "application/json" )
2017-05-25 21:54:58 +02:00
2017-06-28 13:42:38 +02:00
var mapOk map [ string ] interface { }
2017-05-24 01:03:03 +02:00
if ! messages . HasErrors ( ) {
2017-06-28 13:42:38 +02:00
mapOk = map [ string ] interface { } { "ok" : true , "infos" : messages . GetAllInfos ( ) [ "infos" ] }
2017-05-24 01:03:03 +02:00
} else { // We need to show error messages
2017-06-28 13:42:38 +02:00
mapOk = map [ string ] interface { } { "ok" : false , "errors" : messages . GetAllErrors ( ) [ "errors" ] }
2017-05-24 01:03:03 +02:00
}
2017-06-28 13:42:38 +02:00
c . JSON ( http . StatusOK , mapOk )
2017-05-24 01:03:03 +02:00
}
2017-05-20 13:45:15 +02:00
2017-05-25 21:54:58 +02:00
// DeletedTorrentsModPanel : Controller for viewing deleted torrents, accept common search arguments
2017-06-28 13:42:38 +02:00
func DeletedTorrentsModPanel ( c * gin . Context ) {
page := c . Param ( "page" )
2017-07-02 16:54:55 +02:00
messages := msg . GetMessages ( c ) // new utils for errors and infos
2017-06-28 13:42:38 +02:00
deleted := c . Request . URL . Query ( ) [ "deleted" ]
unblocked := c . Request . URL . Query ( ) [ "unblocked" ]
blocked := c . Request . URL . Query ( ) [ "blocked" ]
2017-05-25 02:19:05 +02:00
if deleted != nil {
messages . AddInfoT ( "infos" , "torrent_deleted_definitely" )
}
if blocked != nil {
messages . AddInfoT ( "infos" , "torrent_blocked" )
}
if unblocked != nil {
messages . AddInfoT ( "infos" , "torrent_unblocked" )
}
var err error
pagenum := 1
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
2017-06-28 13:42:38 +02:00
c . AbortWithError ( http . StatusInternalServerError , err )
2017-05-25 02:19:05 +02:00
return
}
2017-05-25 21:54:58 +02:00
}
2017-05-25 02:19:05 +02:00
2017-07-05 13:33:12 +02:00
searchParam , torrents , count , err := search . ByQueryDeleted ( c , pagenum )
2017-06-14 12:10:03 +02:00
if err != nil {
2017-06-28 13:42:38 +02:00
messages . Error ( err )
2017-06-14 12:10:03 +02:00
}
2017-06-18 00:30:12 +02:00
category := ""
if len ( searchParam . Category ) > 0 {
category = searchParam . Category [ 0 ] . String ( )
}
2017-05-25 21:54:58 +02:00
searchForm := searchForm {
2017-07-02 23:53:23 +02:00
TorrentParam : searchParam ,
2017-06-18 00:30:12 +02:00
Category : category ,
2017-05-25 21:54:58 +02:00
ShowItemsPerPage : true ,
}
2017-05-25 02:19:05 +02:00
2017-07-07 00:27:08 +02:00
nav := navigation { count , int ( searchParam . Max ) , pagenum , "mod/torrents/deleted/p" }
2017-06-28 13:42:38 +02:00
search := searchForm
modelList ( c , "admin/torrentlist.jet.html" , torrents , nav , search )
2017-05-25 02:19:05 +02:00
}
2017-05-25 21:54:58 +02:00
// DeletedTorrentsPostPanel : Controller for viewing deleted torrents after a mass update, accept common search arguments
2017-06-28 13:42:38 +02:00
func DeletedTorrentsPostPanel ( c * gin . Context ) {
torrentManyAction ( c )
DeletedTorrentsModPanel ( c )
2017-05-25 02:19:05 +02:00
}
2017-05-25 21:54:58 +02:00
// TorrentBlockModPanel : Controller to lock torrents, redirecting to previous page
2017-06-28 13:42:38 +02:00
func TorrentBlockModPanel ( c * gin . Context ) {
2017-07-02 23:53:23 +02:00
id , _ := strconv . ParseInt ( c . Query ( "id" ) , 10 , 32 )
torrent , _ , err := torrents . ToggleBlock ( uint ( id ) )
2017-05-25 02:19:05 +02:00
var returnRoute , action string
if torrent . IsDeleted ( ) {
2017-06-28 13:42:38 +02:00
returnRoute = "/mod/torrents/deleted"
2017-05-25 02:19:05 +02:00
} else {
2017-06-28 13:42:38 +02:00
returnRoute = "/mod/torrents"
2017-05-25 02:19:05 +02:00
}
if torrent . IsBlocked ( ) {
action = "blocked"
} else {
action = "unblocked"
}
2017-06-15 04:44:46 +02:00
if err == nil {
2017-07-02 23:53:23 +02:00
_ , username := torrents . HideUser ( torrent . UploaderID , torrent . Uploader . Username , torrent . Hidden )
activities . Log ( & models . User { } , torrent . Identifier ( ) , action , "torrent_" + action + "_by" , strconv . Itoa ( int ( torrent . ID ) ) , username , getUser ( c ) . Username )
2017-06-15 04:44:46 +02:00
}
2017-06-28 13:42:38 +02:00
c . Redirect ( http . StatusSeeOther , returnRoute + "?" + action )
2017-05-25 02:19:05 +02:00
}
2017-05-20 13:45:15 +02:00
/ *
* Controller to modify multiple torrents and can be used by the owner of the torrent or admin
* /
2017-06-28 13:42:38 +02:00
func torrentManyAction ( c * gin . Context ) {
currentUser := getUser ( c )
torrentsSelected := c . PostFormArray ( "torrent_id" ) // should be []string
action := c . PostForm ( "action" )
status , _ := strconv . Atoi ( c . PostForm ( "status" ) )
owner , _ := strconv . Atoi ( c . PostForm ( "owner" ) )
category := c . PostForm ( "category" )
withReport , _ := strconv . ParseBool ( c . DefaultPostForm ( "withreport" , "false" ) )
2017-07-02 16:54:55 +02:00
messages := msg . GetMessages ( c ) // new utils for errors and infos
2017-05-24 20:23:54 +02:00
catID , subCatID := - 1 , - 1
var err error
2017-05-20 13:45:15 +02:00
if action == "" {
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "no_action_selected" )
2017-05-20 13:45:15 +02:00
}
2017-06-28 13:42:38 +02:00
if action == "status" && c . PostForm ( "status" ) == "" { // We need to check the form value, not the int one because hidden is 0
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "no_move_location_selected" )
2017-05-20 13:45:15 +02:00
}
2017-06-28 13:42:38 +02:00
if action == "owner" && c . PostForm ( "owner" ) == "" { // We need to check the form value, not the int one because renchon is 0
2017-05-24 20:23:54 +02:00
messages . AddErrorT ( "errors" , "no_owner_selected" )
}
2017-05-25 21:54:58 +02:00
if action == "category" && category == "" {
2017-05-24 20:23:54 +02:00
messages . AddErrorT ( "errors" , "no_category_selected" )
}
2017-05-20 13:45:15 +02:00
if len ( torrentsSelected ) == 0 {
2017-05-23 04:05:33 +02:00
messages . AddErrorT ( "errors" , "select_one_element" )
2017-05-20 13:45:15 +02:00
}
2017-05-24 20:23:54 +02:00
2017-07-10 14:11:05 +02:00
if ! config . Get ( ) . Torrents . Status [ status ] { // Check if the status exist
2017-05-24 22:15:38 +02:00
messages . AddErrorTf ( "errors" , "no_status_exist" , status )
status = - 1
}
2017-07-02 23:53:23 +02:00
if ! currentUser . HasAdmin ( ) {
2017-06-28 13:42:38 +02:00
if c . PostForm ( "status" ) != "" { // Condition to check if a user try to change torrent status without having the right permission
2017-07-01 23:09:35 +02:00
if ( status == models . TorrentStatusTrusted && ! currentUser . IsTrusted ( ) ) || status == models . TorrentStatusAPlus || status == 0 {
status = models . TorrentStatusNormal
2017-05-24 20:23:54 +02:00
}
}
2017-06-28 13:42:38 +02:00
if c . PostForm ( "owner" ) != "" { // Only admins can change owner of torrents
2017-05-24 20:23:54 +02:00
owner = - 1
}
2017-05-24 21:16:15 +02:00
withReport = false // Users should not be able to remove reports
2017-05-24 20:23:54 +02:00
}
2017-07-02 23:53:23 +02:00
if c . PostForm ( "owner" ) != "" && currentUser . HasAdmin ( ) { // We check that the user given exist and if not we return an error
_ , _ , errorUser := users . FindForAdmin ( uint ( owner ) )
2017-05-24 20:23:54 +02:00
if errorUser != nil {
messages . AddErrorTf ( "errors" , "no_user_found_id" , owner )
2017-05-24 22:15:38 +02:00
owner = - 1
2017-05-24 20:23:54 +02:00
}
}
if category != "" {
catsSplit := strings . Split ( category , "_" )
// need this to prevent out of index panics
if len ( catsSplit ) == 2 {
catID , err = strconv . Atoi ( catsSplit [ 0 ] )
if err != nil {
messages . AddErrorT ( "errors" , "invalid_torrent_category" )
}
subCatID , err = strconv . Atoi ( catsSplit [ 1 ] )
if err != nil {
messages . AddErrorT ( "errors" , "invalid_torrent_category" )
}
2017-07-06 21:53:13 +02:00
if ! categories . Exists ( category ) {
2017-05-24 20:23:54 +02:00
messages . AddErrorT ( "errors" , "invalid_torrent_category" )
}
}
}
2017-05-20 13:45:15 +02:00
if ! messages . HasErrors ( ) {
2017-05-25 21:54:58 +02:00
for _ , torrentID := range torrentsSelected {
2017-07-02 23:53:23 +02:00
id , _ := strconv . Atoi ( torrentID )
torrent , _ := torrents . FindByID ( uint ( id ) )
if torrent . ID > 0 && currentUser . CurrentOrAdmin ( torrent . UploaderID ) {
2017-05-24 20:23:54 +02:00
if action == "status" || action == "multiple" || action == "category" || action == "owner" {
/* If we don't delete, we make changes according to the form posted and we save at the end */
2017-06-28 13:42:38 +02:00
if c . PostForm ( "status" ) != "" && status != - 1 {
2017-05-25 21:54:58 +02:00
torrent . Status = status
messages . AddInfoTf ( "infos" , "torrent_moved" , torrent . Name )
2017-05-20 13:45:15 +02:00
}
2017-06-28 13:42:38 +02:00
if c . PostForm ( "owner" ) != "" && owner != - 1 {
2017-05-25 21:54:58 +02:00
torrent . UploaderID = uint ( owner )
messages . AddInfoTf ( "infos" , "torrent_owner_changed" , torrent . Name )
2017-05-24 20:23:54 +02:00
}
if category != "" && catID != - 1 && subCatID != - 1 {
2017-05-25 21:54:58 +02:00
torrent . Category = catID
torrent . SubCategory = subCatID
messages . AddInfoTf ( "infos" , "torrent_category_changed" , torrent . Name )
2017-05-24 20:23:54 +02:00
}
/* Changes are done, we save */
2017-07-02 23:53:23 +02:00
_ , err := torrent . UpdateUnscope ( )
2017-06-15 04:44:46 +02:00
if err == nil {
2017-07-02 23:53:23 +02:00
_ , username := torrents . HideUser ( torrent . UploaderID , torrent . Uploader . Username , torrent . Hidden )
activities . Log ( & models . User { } , torrent . Identifier ( ) , "edited" , "torrent_edited_by" , strconv . Itoa ( int ( torrent . ID ) ) , username , getUser ( c ) . Username )
2017-06-15 04:44:46 +02:00
}
2017-05-24 20:23:54 +02:00
} else if action == "delete" {
2017-07-01 23:09:35 +02:00
if status == models . TorrentStatusBlocked { // Then we should lock torrents before deleting them
2017-06-02 04:51:44 +02:00
torrent . Status = status
messages . AddInfoTf ( "infos" , "torrent_moved" , torrent . Name )
2017-07-02 23:53:23 +02:00
torrent . UpdateUnscope ( )
2017-06-02 04:51:44 +02:00
}
2017-07-02 23:53:23 +02:00
_ , _ , err = torrent . Delete ( false )
2017-05-20 13:45:15 +02:00
if err != nil {
2017-05-20 17:01:13 +02:00
messages . ImportFromError ( "errors" , err )
2017-05-20 13:45:15 +02:00
} else {
2017-05-23 04:05:33 +02:00
messages . AddInfoTf ( "infos" , "torrent_deleted" , torrent . Name )
2017-07-02 23:53:23 +02:00
_ , username := torrents . HideUser ( torrent . UploaderID , torrent . Uploader . Username , torrent . Hidden )
activities . Log ( & models . User { } , torrent . Identifier ( ) , "deleted" , "torrent_deleted_by" , strconv . Itoa ( int ( torrent . ID ) ) , username , getUser ( c ) . Username )
2017-05-20 13:45:15 +02:00
}
2017-05-24 20:23:54 +02:00
} else {
2017-05-23 04:05:33 +02:00
messages . AddErrorTf ( "errors" , "no_action_exist" , action )
2017-05-20 13:45:15 +02:00
}
2017-05-24 21:16:15 +02:00
if withReport {
2017-07-02 23:53:23 +02:00
whereParams := structs . CreateWhereParams ( "torrent_id = ?" , torrentID )
reports , _ , _ := reports . FindOrderBy ( & whereParams , "" , 0 , 0 )
2017-05-24 21:16:15 +02:00
for _ , report := range reports {
2017-07-02 23:53:23 +02:00
report . Delete ( false )
2017-05-24 21:16:15 +02:00
}
messages . AddInfoTf ( "infos" , "torrent_reports_deleted" , torrent . Name )
}
2017-05-20 13:45:15 +02:00
} else {
2017-05-25 21:54:58 +02:00
messages . AddErrorTf ( "errors" , "torrent_not_exist" , torrentID )
}
2017-05-20 13:45:15 +02:00
}
}
2017-05-21 00:38:28 +02:00
}