2017-05-10 04:03:25 +02:00
package router
import (
2017-05-24 01:03:03 +02:00
"encoding/json"
2017-05-10 22:29:59 +03:00
"fmt"
2017-05-10 20:17:07 +03:00
"html"
2017-05-09 23:54:12 -04: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-05-17 15:58:40 +10:00
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/service"
2017-06-05 15:19:25 +02:00
"github.com/NyaaPantsu/nyaa/service/api"
2017-05-17 15:58:40 +10:00
"github.com/NyaaPantsu/nyaa/service/comment"
"github.com/NyaaPantsu/nyaa/service/report"
"github.com/NyaaPantsu/nyaa/service/torrent"
"github.com/NyaaPantsu/nyaa/service/user"
2017-05-20 14:05:26 +02:00
"github.com/NyaaPantsu/nyaa/service/user/permission"
2017-05-24 20:23:54 +02:00
"github.com/NyaaPantsu/nyaa/util/categories"
2017-05-17 15:58:40 +10:00
"github.com/NyaaPantsu/nyaa/util/log"
2017-05-20 13:45:15 +02:00
msg "github.com/NyaaPantsu/nyaa/util/messages"
2017-05-17 15:58:40 +10:00
"github.com/NyaaPantsu/nyaa/util/search"
2017-05-10 16:43:50 +02:00
"github.com/gorilla/mux"
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-05-13 17:29:21 +02:00
func ( f * ReassignForm ) ExtractInfo ( r * http . Request ) error {
f . By = r . FormValue ( "by" )
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-13 17:29:21 +02:00
if f . By != "olduser" && f . By != "torrentid" {
return fmt . Errorf ( "what?" )
}
f . Data = strings . Trim ( r . FormValue ( "data" ) , " \r\n" )
if f . By == "olduser" {
if f . Data == "" {
return fmt . Errorf ( "No username given" )
} else if strings . Contains ( f . Data , "\n" ) {
return fmt . Errorf ( "More than one username given" )
}
} else if f . By == "torrentid" {
if f . Data == "" {
return fmt . Errorf ( "No IDs given" )
}
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 {
return fmt . Errorf ( "Couldn't parse number on line %d" , i + 1 )
}
2017-05-25 21:54:58 +02:00
f . Torrents = append ( f . Torrents , uint ( torrentID ) )
2017-05-13 17:29:21 +02:00
}
}
tmp := r . FormValue ( "to" )
parsed , err := strconv . ParseUint ( tmp , 10 , 0 )
if err != nil {
return err
}
f . AssignTo = uint ( parsed )
2017-05-13 17:36:50 +02:00
_ , _ , _ , _ , err = userService . RetrieveUser ( r , tmp )
if err != nil {
return fmt . Errorf ( "User to assign to doesn't exist" )
}
2017-05-13 17:29:21 +02:00
return nil
}
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" {
toBeChanged , err = userService . RetrieveOldUploadsByUsername ( f . Data )
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-05-26 12:12:52 +02:00
torrent , err2 := torrentService . GetRawTorrentByID ( torrentID )
2017-05-13 17:29:21 +02:00
if err2 == nil {
torrent . UploaderID = f . AssignTo
2017-05-25 02:19:05 +02:00
db . ORM . Model ( & torrent ) . UpdateColumn ( & torrent )
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
// newPanelSearchForm : Helper that creates a search form without items/page field
// these need to be used when the templateVariables don't include `navigation`
func newPanelSearchForm ( ) searchForm {
form := newSearchForm ( )
2017-05-14 13:23:27 +02:00
form . ShowItemsPerPage = false
return form
}
2017-05-25 21:54:58 +02:00
//
func newPanelCommonVariables ( r * http . Request ) commonTemplateVariables {
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 21:54:58 +02:00
common := newCommonVariables ( r )
common . Search = newPanelSearchForm ( )
2017-05-21 04:10:19 -03:00
return common
}
2017-05-25 21:54:58 +02:00
// IndexModPanel : Controller for showing index page of Mod Panel
2017-05-10 05:24:18 +02:00
func IndexModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-19 19:10:16 -04:00
offset := 10
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-19 19:10:16 -04:00
torrents , _ , _ := torrentService . GetAllTorrents ( offset , 0 )
users , _ := userService . RetrieveUsersForAdmin ( offset , 0 )
comments , _ := commentService . GetAllComments ( offset , 0 , "" , "" )
torrentReports , _ , _ := reportService . GetAllTorrentReports ( offset , 0 )
2017-05-25 21:54:58 +02:00
htv := panelIndexVbs { newPanelCommonVariables ( r ) , torrents , model . TorrentReportsToJSON ( torrentReports ) , users , comments }
2017-05-19 19:10:16 -04:00
err := panelIndex . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err )
2017-05-10 05:24:18 +02:00
}
2017-05-10 21:42:20 +03:00
2017-05-25 21:54:58 +02:00
// TorrentsListPanel : Controller for listing torrents, can accept common search arguments
2017-05-10 05:24:18 +02:00
func TorrentsListPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-19 19:10:16 -04:00
vars := mux . Vars ( r )
page := vars [ "page" ]
2017-05-25 02:19:05 +02:00
messages := msg . GetMessages ( r )
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 02:19:05 +02:00
deleted := r . URL . Query ( ) [ "deleted" ]
unblocked := r . URL . Query ( ) [ "unblocked" ]
blocked := r . URL . Query ( ) [ "blocked" ]
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-19 19:10:16 -04:00
var err error
pagenum := 1
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
2017-05-10 20:17:07 +03:00
}
2017-05-25 21:54:58 +02:00
}
2017-05-10 05:24:18 +02:00
2017-05-25 21:54:58 +02:00
searchParam , torrents , count , err := search . SearchByQueryWithUser ( r , pagenum )
searchForm := searchForm {
SearchParam : searchParam ,
Category : searchParam . Category . String ( ) ,
ShowItemsPerPage : true ,
}
2017-05-19 19:10:16 -04:00
2017-05-25 21:54:58 +02:00
common := newCommonVariables ( r )
common . Navigation = navigation { count , int ( searchParam . Max ) , pagenum , "mod_tlist_page" }
2017-05-21 04:10:19 -03:00
common . Search = searchForm
2017-05-25 21:54:58 +02:00
ptlv := modelListVbs { common , torrents , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-19 19:10:16 -04:00
err = panelTorrentList . ExecuteTemplate ( w , "admin_index.html" , ptlv )
log . CheckError ( err )
2017-05-10 05:24:18 +02:00
}
2017-05-10 21:42:20 +03:00
2017-05-25 21:54:58 +02:00
// TorrentReportListPanel : Controller for listing torrent reports, can accept pages
2017-05-10 21:42:20 +03:00
func TorrentReportListPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-19 19:10:16 -04:00
vars := mux . Vars ( r )
page := vars [ "page" ]
2017-05-25 21:54:58 +02:00
messages := msg . GetMessages ( r )
2017-05-19 19:10:16 -04:00
pagenum := 1
2017-05-25 21:54:58 +02:00
offset := 100
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 21:54:58 +02:00
var err error
2017-05-19 19:10:16 -04:00
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
2017-05-10 21:42:20 +03:00
}
2017-05-19 19:10:16 -04:00
}
2017-05-10 21:42:20 +03:00
2017-05-19 19:10:16 -04:00
torrentReports , nbReports , _ := reportService . GetAllTorrentReports ( offset , ( pagenum - 1 ) * offset )
2017-05-10 21:42:20 +03:00
2017-05-19 19:10:16 -04:00
reportJSON := model . TorrentReportsToJSON ( torrentReports )
2017-05-25 21:54:58 +02:00
common := newCommonVariables ( r )
common . Navigation = navigation { nbReports , offset , pagenum , "mod_trlist_page" }
ptrlv := modelListVbs { common , reportJSON , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-19 19:10:16 -04:00
err = panelTorrentReportList . ExecuteTemplate ( w , "admin_index.html" , ptrlv )
log . CheckError ( err )
2017-05-10 21:42:20 +03:00
}
2017-05-25 21:54:58 +02:00
// UsersListPanel : Controller for listing users, can accept pages
2017-05-10 05:24:18 +02:00
func UsersListPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-19 19:10:16 -04:00
vars := mux . Vars ( r )
page := vars [ "page" ]
pagenum := 1
2017-05-25 21:54:58 +02:00
offset := 100
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 21:54:58 +02:00
var err error
messages := msg . GetMessages ( r )
2017-05-19 19:10:16 -04:00
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2017-05-09 23:54:12 -04:00
}
2017-05-19 19:10:16 -04:00
users , nbUsers := userService . RetrieveUsersForAdmin ( offset , ( pagenum - 1 ) * offset )
2017-05-25 21:54:58 +02:00
common := newCommonVariables ( r )
common . Navigation = navigation { nbUsers , offset , pagenum , "mod_ulist_page" }
htv := modelListVbs { common , users , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-19 19:10:16 -04:00
err = panelUserList . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err )
2017-05-10 05:24:18 +02:00
}
2017-05-10 21:42:20 +03:00
2017-05-25 21:54:58 +02:00
// CommentsListPanel : Controller for listing comments, can accept pages and userID
2017-05-10 04:03:25 +02:00
func CommentsListPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-19 19:10:16 -04:00
vars := mux . Vars ( r )
page := vars [ "page" ]
pagenum := 1
2017-05-25 21:54:58 +02:00
offset := 100
userid := r . URL . Query ( ) . Get ( "userid" )
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 21:54:58 +02:00
var err error
messages := msg . GetMessages ( r )
2017-05-19 19:10:16 -04:00
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if ! log . CheckError ( err ) {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
}
var conditions string
var values [ ] interface { }
if userid != "" {
conditions = "user_id = ?"
values = append ( values , userid )
2017-05-09 23:54:12 -04:00
}
2017-05-10 04:03:25 +02:00
2017-05-19 19:10:16 -04:00
comments , nbComments := commentService . GetAllComments ( offset , ( pagenum - 1 ) * offset , conditions , values ... )
2017-05-25 21:54:58 +02:00
common := newCommonVariables ( r )
common . Navigation = navigation { nbComments , offset , pagenum , "mod_clist_page" }
htv := modelListVbs { common , comments , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-19 19:10:16 -04:00
err = panelCommentList . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err )
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-05-10 04:03:25 +02:00
func TorrentEditModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-19 19:10:16 -04:00
id := r . URL . Query ( ) . Get ( "id" )
2017-05-26 12:12:52 +02:00
torrent , _ := torrentService . GetTorrentByID ( id )
2017-05-25 21:54:58 +02:00
messages := msg . GetMessages ( r )
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 21:54:58 +02:00
torrentJSON := torrent . ToJSON ( )
2017-06-05 15:19:25 +02:00
uploadForm := apiService . 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 )
htv := formTemplateVariables { newPanelCommonVariables ( r ) , uploadForm , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-19 19:10:16 -04:00
err := panelTorrentEd . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err )
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-05-10 05:24:18 +02:00
func TorrentPostEditModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-06-05 15:19:25 +02:00
var uploadForm apiService . TorrentRequest
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-10 23:53:25 +02:00
id := r . URL . Query ( ) . Get ( "id" )
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-26 12:12:52 +02:00
torrent , _ := torrentService . GetTorrentByID ( id )
2017-05-11 06:49:05 -04:00
if torrent . ID > 0 {
2017-05-11 01:46:50 +02:00
errUp := uploadForm . ExtractEditInfo ( r )
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 06:49:05 -04:00
torrent . Name = uploadForm . Name
torrent . Category = uploadForm . CategoryID
2017-05-11 01:46:50 +02:00
torrent . SubCategory = uploadForm . SubCategoryID
2017-05-11 06:49:05 -04:00
torrent . Status = uploadForm . Status
2017-05-27 20:33:40 +02:00
torrent . Hidden = uploadForm . Hidden
2017-05-20 21:10:59 -05:00
torrent . WebsiteLink = uploadForm . WebsiteLink
2017-05-11 01:46:50 +02:00
torrent . Description = uploadForm . Description
2017-05-25 02:19:05 +02:00
// torrent.Uploader = nil // GORM will create a new user otherwise (wtf?!)
db . ORM . Model ( & torrent ) . UpdateColumn ( & torrent )
2017-05-23 04:05:33 +02:00
messages . AddInfoT ( "infos" , "torrent_updated" )
2017-05-09 23:54:12 -04:00
}
2017-05-10 05:24:18 +02:00
}
2017-05-25 21:54:58 +02:00
htv := formTemplateVariables { newPanelCommonVariables ( r ) , uploadForm , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
err := panelTorrentEd . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err )
2017-05-10 05:24:18 +02:00
}
2017-05-25 21:54:58 +02:00
// CommentDeleteModPanel : Controller for deleting a comment
2017-05-10 04:03:25 +02:00
func CommentDeleteModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-10 05:24:18 +02:00
id := r . URL . Query ( ) . Get ( "id" )
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-26 12:12:52 +02:00
_ , _ = commentService . DeleteComment ( id )
2017-05-19 19:10:16 -04:00
url , _ := Router . Get ( "mod_clist" ) . URL ( )
http . Redirect ( w , r , url . String ( ) + "?deleted" , http . StatusSeeOther )
2017-05-10 04:03:25 +02:00
}
2017-05-10 23:05:56 +03:00
2017-05-25 21:54:58 +02:00
// TorrentDeleteModPanel : Controller for deleting a torrent
2017-05-10 04:03:25 +02:00
func TorrentDeleteModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-10 05:24:18 +02:00
id := r . URL . Query ( ) . Get ( "id" )
2017-05-25 02:19:05 +02:00
definitely := r . URL . Query ( ) [ "definitely" ]
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 02:19:05 +02:00
var returnRoute string
if definitely != nil {
_ , _ = torrentService . DefinitelyDeleteTorrent ( id )
//delete reports of torrent
whereParams := serviceBase . CreateWhereParams ( "torrent_id = ?" , id )
reports , _ , _ := reportService . GetTorrentReportsOrderBy ( & whereParams , "" , 0 , 0 )
for _ , report := range reports {
reportService . DeleteDefinitelyTorrentReport ( report . ID )
}
returnRoute = "mod_tlist_deleted"
} else {
_ , _ = torrentService . DeleteTorrent ( id )
2017-05-19 19:10:16 -04:00
2017-05-25 02:19:05 +02:00
//delete reports of torrent
whereParams := serviceBase . CreateWhereParams ( "torrent_id = ?" , id )
reports , _ , _ := reportService . GetTorrentReportsOrderBy ( & whereParams , "" , 0 , 0 )
for _ , report := range reports {
reportService . DeleteTorrentReport ( report . ID )
}
returnRoute = "mod_tlist"
2017-05-10 04:03:25 +02:00
}
2017-05-25 02:19:05 +02:00
url , _ := Router . Get ( returnRoute ) . URL ( )
2017-05-19 19:10:16 -04:00
http . Redirect ( w , r , url . String ( ) + "?deleted" , http . StatusSeeOther )
2017-05-10 20:17:07 +03:00
}
2017-05-10 23:05:56 +03:00
2017-05-25 21:54:58 +02:00
// TorrentReportDeleteModPanel : Controller for deleting a torrent report
2017-05-10 23:05:56 +03:00
func TorrentReportDeleteModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-19 19:10:16 -04:00
id := r . URL . Query ( ) . Get ( "id" )
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-19 19:10:16 -04:00
fmt . Println ( id )
idNum , _ := strconv . ParseUint ( id , 10 , 64 )
_ , _ = reportService . DeleteTorrentReport ( uint ( idNum ) )
url , _ := Router . Get ( "mod_trlist" ) . URL ( )
http . Redirect ( w , r , url . String ( ) + "?deleted" , http . StatusSeeOther )
2017-05-10 23:05:56 +03: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-05-13 17:29:21 +02:00
func TorrentReassignModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-21 20:20:40 +02:00
messages := msg . GetMessages ( r )
2017-05-25 21:54:58 +02:00
htv := formTemplateVariables { newPanelCommonVariables ( r ) , ReassignForm { } , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-13 17:29:21 +02:00
err := panelTorrentReassign . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err )
}
2017-05-25 21:54:58 +02:00
// TorrentPostReassignModPanel : Controller for reassigning a torrent, after POST request
2017-05-13 17:29:21 +02:00
func TorrentPostReassignModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-13 17:29:21 +02:00
var rForm ReassignForm
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-13 17:29:21 +02:00
err2 := rForm . ExtractInfo ( r )
if err2 != nil {
2017-05-21 19:38:39 +02:00
messages . ImportFromError ( "errors" , err2 )
2017-05-13 17:29:21 +02:00
} else {
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-05-25 21:54:58 +02:00
htv := formTemplateVariables { newPanelCommonVariables ( r ) , rForm , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
err := panelTorrentReassign . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err )
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-05-20 13:45:15 +02:00
func TorrentsPostListPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-20 13:45:15 +02:00
torrentManyAction ( r )
TorrentsListPanel ( w , r )
}
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 :
* status = 0 | 1 | 2 | 3 | 4 according to config / torrent . go ( can be omitted if action = delete | owner | category | multiple )
* 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-05-25 21:54:58 +02:00
func APIMassMod ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-24 01:03:03 +02:00
torrentManyAction ( r )
messages := msg . GetMessages ( r ) // new util for errors and infos
2017-05-25 21:54:58 +02:00
var apiJSON [ ] byte
w . Header ( ) . Set ( "Content-Type" , "application/json" )
2017-05-24 01:03:03 +02:00
if ! messages . HasErrors ( ) {
2017-05-24 22:15:38 +02:00
mapOk := map [ string ] interface { } { "ok" : true , "infos" : messages . GetAllInfos ( ) [ "infos" ] }
2017-05-25 21:54:58 +02:00
apiJSON , _ = json . Marshal ( mapOk )
2017-05-24 01:03:03 +02:00
} else { // We need to show error messages
2017-05-24 22:15:38 +02:00
mapNotOk := map [ string ] interface { } { "ok" : false , "errors" : messages . GetAllErrors ( ) [ "errors" ] }
2017-05-25 21:54:58 +02:00
apiJSON , _ = json . Marshal ( mapNotOk )
2017-05-24 01:03:03 +02:00
}
2017-05-25 21:54:58 +02:00
w . Write ( apiJSON )
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-05-25 02:19:05 +02:00
func DeletedTorrentsModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 02:19:05 +02:00
vars := mux . Vars ( r )
page := vars [ "page" ]
messages := msg . GetMessages ( r ) // new util for errors and infos
deleted := r . URL . Query ( ) [ "deleted" ]
unblocked := r . URL . Query ( ) [ "unblocked" ]
blocked := r . URL . Query ( ) [ "blocked" ]
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 ) {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2017-05-25 21:54:58 +02:00
}
2017-05-25 02:19:05 +02:00
2017-05-25 21:54:58 +02:00
searchParam , torrents , count , err := search . SearchByQueryDeleted ( r , pagenum )
searchForm := searchForm {
SearchParam : searchParam ,
Category : searchParam . Category . String ( ) ,
ShowItemsPerPage : true ,
}
2017-05-25 02:19:05 +02:00
2017-05-25 21:54:58 +02:00
common := newCommonVariables ( r )
common . Navigation = navigation { count , int ( searchParam . Max ) , pagenum , "mod_tlist_page" }
2017-05-25 02:19:05 +02:00
common . Search = searchForm
2017-05-25 21:54:58 +02:00
ptlv := modelListVbs { common , torrents , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-25 02:19:05 +02:00
err = panelTorrentList . ExecuteTemplate ( w , "admin_index.html" , ptlv )
log . CheckError ( err )
}
2017-05-25 21:54:58 +02:00
// DeletedTorrentsPostPanel : Controller for viewing deleted torrents after a mass update, accept common search arguments
2017-05-25 02:19:05 +02:00
func DeletedTorrentsPostPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 02:19:05 +02:00
torrentManyAction ( r )
DeletedTorrentsModPanel ( w , r )
}
2017-05-25 21:54:58 +02:00
// TorrentBlockModPanel : Controller to lock torrents, redirecting to previous page
2017-05-25 02:19:05 +02:00
func TorrentBlockModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 02:19:05 +02:00
id := r . URL . Query ( ) . Get ( "id" )
torrent , _ , _ := torrentService . ToggleBlockTorrent ( id )
var returnRoute , action string
if torrent . IsDeleted ( ) {
returnRoute = "mod_tlist_deleted"
} else {
returnRoute = "mod_tlist"
}
if torrent . IsBlocked ( ) {
action = "blocked"
} else {
action = "unblocked"
}
url , _ := Router . Get ( returnRoute ) . URL ( )
http . Redirect ( w , r , url . String ( ) + "?" + action , http . StatusSeeOther )
}
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
* /
func torrentManyAction ( r * http . Request ) {
2017-05-27 11:50:31 +10:00
defer r . Body . Close ( )
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-20 13:45:15 +02:00
r . ParseForm ( )
torrentsSelected := r . Form [ "torrent_id" ] // should be []string
action := r . FormValue ( "action" )
2017-05-24 20:23:54 +02:00
status , _ := strconv . Atoi ( r . FormValue ( "status" ) )
owner , _ := strconv . Atoi ( r . FormValue ( "owner" ) )
category := r . FormValue ( "category" )
withReport , _ := strconv . ParseBool ( r . FormValue ( "withreport" ) )
2017-05-20 13:45:15 +02:00
messages := msg . GetMessages ( r ) // new util 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-05-24 20:23:54 +02:00
if action == "status" && r . FormValue ( "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-05-24 20:23:54 +02:00
if action == "owner" && r . FormValue ( "owner" ) == "" { // We need to check the form value, not the int one because renchon is 0
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-05-24 21:16:15 +02:00
if r . FormValue ( "withreport" ) == "" { // Default behavior for withreport
withReport = false
}
2017-05-31 04:21:57 +02:00
if ! config . Conf . 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-05-25 21:54:58 +02:00
if ! userPermission . HasAdmin ( currentUser ) {
2017-05-24 20:23:54 +02:00
if r . FormValue ( "status" ) != "" { // Condition to check if a user try to change torrent status without having the right permission
if ( status == model . TorrentStatusTrusted && ! currentUser . IsTrusted ( ) ) || status == model . TorrentStatusAPlus || status == 0 {
status = model . TorrentStatusNormal
}
}
if r . FormValue ( "owner" ) != "" { // Only admins can change owner of torrents
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
}
if r . FormValue ( "owner" ) != "" && userPermission . HasAdmin ( currentUser ) { // We check that the user given exist and if not we return an error
_ , _ , errorUser := userService . RetrieveUserForAdmin ( strconv . Itoa ( owner ) )
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" )
}
if ! categories . CategoryExists ( category ) {
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-05-26 12:12:52 +02:00
torrent , _ := torrentService . GetTorrentByID ( torrentID )
2017-05-20 13:45:15 +02:00
if torrent . ID > 0 && userPermission . CurrentOrAdmin ( currentUser , 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 */
if r . FormValue ( "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-05-24 20:23:54 +02:00
if r . FormValue ( "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-05-25 02:19:05 +02:00
db . ORM . Unscoped ( ) . Model ( & torrent ) . UpdateColumn ( & torrent )
2017-05-24 20:23:54 +02:00
} else if action == "delete" {
2017-06-02 04:51:44 +02:00
if status == model . TorrentStatusBlocked { // Then we should lock torrents before deleting them
torrent . Status = status
messages . AddInfoTf ( "infos" , "torrent_moved" , torrent . Name )
db . ORM . Unscoped ( ) . Model ( & torrent ) . UpdateColumn ( & torrent ) // We must save it here and soft delete it after
}
2017-05-25 21:54:58 +02:00
_ , err = torrentService . DeleteTorrent ( torrentID )
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-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-05-25 21:54:58 +02:00
whereParams := serviceBase . CreateWhereParams ( "torrent_id = ?" , torrentID )
2017-05-24 21:16:15 +02:00
reports , _ , _ := reportService . GetTorrentReportsOrderBy ( & whereParams , "" , 0 , 0 )
for _ , report := range reports {
reportService . DeleteTorrentReport ( report . ID )
}
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-20 19:38:28 -03:00
}