2017-05-10 04:03:25 +02:00
package router
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-10 23:53:25 +02:00
"github.com/ewhal/nyaa/db"
2017-05-10 20:42:20 +02:00
"github.com/ewhal/nyaa/model"
2017-05-10 21:29:59 +02:00
"github.com/ewhal/nyaa/service"
2017-05-10 05:24:18 +02:00
"github.com/ewhal/nyaa/service/comment"
2017-05-10 20:42:20 +02:00
"github.com/ewhal/nyaa/service/report"
2017-05-10 05:24:18 +02:00
"github.com/ewhal/nyaa/service/torrent"
2017-05-10 05:54:12 +02:00
"github.com/ewhal/nyaa/service/user"
form "github.com/ewhal/nyaa/service/user/form"
"github.com/ewhal/nyaa/service/user/permission"
2017-05-10 05:24:18 +02:00
"github.com/ewhal/nyaa/util/languages"
2017-05-10 19:17:07 +02:00
"github.com/ewhal/nyaa/util/log"
"github.com/ewhal/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-13 17:29:21 +02:00
type ReassignForm struct {
AssignTo uint
By string
Data string
Torrents [ ] uint
}
func ( f * ReassignForm ) ExtractInfo ( r * http . Request ) error {
f . By = r . FormValue ( "by" )
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" )
torrent_id , err := strconv . ParseUint ( tmp , 10 , 0 )
if err != nil {
return fmt . Errorf ( "Couldn't parse number on line %d" , i + 1 )
}
f . Torrents = append ( f . Torrents , uint ( torrent_id ) )
}
}
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
}
func ( f * ReassignForm ) ExecuteAction ( ) ( int , error ) {
2017-05-13 17:36:50 +02:00
2017-05-13 17:29:21 +02:00
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
for _ , torrent_id := range toBeChanged {
torrent , err2 := torrentService . GetRawTorrentById ( torrent_id )
if err2 == nil {
torrent . UploaderID = f . AssignTo
db . ORM . Save ( & torrent )
num += 1
}
}
// TODO: clean shit from user_uploads_old if needed
return num , nil
}
2017-05-10 05:24:18 +02:00
func IndexModPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
offset := 10
2017-05-10 05:24:18 +02:00
2017-05-10 16:43:50 +02:00
torrents , _ , _ := torrentService . GetAllTorrents ( offset , 0 )
users , _ := userService . RetrieveUsersForAdmin ( offset , 0 )
comments , _ := commentService . GetAllComments ( offset , 0 , "" , "" )
2017-05-10 20:42:20 +02:00
torrentReports , _ , _ := reportService . GetAllTorrentReports ( offset , 0 )
2017-05-10 05:54:12 +02:00
languages . SetTranslationFromRequest ( panelIndex , r , "en-us" )
2017-05-14 13:01:59 +02:00
search := NewSearchForm ( )
search . ShowItemsPerPage = false
htv := PanelIndexVbs { torrents , model . TorrentReportsToJSON ( torrentReports ) , users , comments , search , currentUser , r . URL }
2017-05-13 00:31:27 +02:00
err := panelIndex . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err )
2017-05-10 08:18:58 +02:00
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
2017-05-10 05:54:12 +02:00
}
2017-05-10 05:24:18 +02:00
}
2017-05-10 20:42:20 +02:00
2017-05-10 05:24:18 +02:00
func TorrentsListPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
2017-05-10 16:43:50 +02:00
vars := mux . Vars ( r )
2017-05-10 19:17:07 +02:00
page := vars [ "page" ]
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 05:54:12 +02:00
offset := 100
2017-05-10 05:24:18 +02:00
2017-05-10 19:17:07 +02:00
searchParam , torrents , _ , err := search . SearchByQuery ( r , pagenum )
2017-05-10 17:37:49 +02:00
searchForm := SearchForm {
SearchParam : searchParam ,
Category : searchParam . Category . String ( ) ,
HideAdvancedSearch : false ,
}
2017-05-10 05:54:12 +02:00
languages . SetTranslationFromRequest ( panelTorrentList , r , "en-us" )
2017-05-10 19:17:07 +02:00
htv := PanelTorrentListVbs { torrents , searchForm , Navigation { int ( searchParam . Max ) , offset , pagenum , "mod_tlist_page" } , currentUser , r . URL }
2017-05-10 17:37:49 +02:00
err = panelTorrentList . ExecuteTemplate ( w , "admin_index.html" , htv )
2017-05-10 21:09:37 +02:00
log . CheckError ( err )
2017-05-10 08:18:58 +02:00
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
2017-05-10 05:54:12 +02:00
}
2017-05-10 05:24:18 +02:00
}
2017-05-10 20:42:20 +02:00
func TorrentReportListPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
if userPermission . HasAdmin ( currentUser ) {
vars := mux . Vars ( r )
page := vars [ "page" ]
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
}
}
offset := 100
torrentReports , nbReports , _ := reportService . GetAllTorrentReports ( offset , ( pagenum - 1 ) * offset )
reportJSON := model . TorrentReportsToJSON ( torrentReports )
2017-05-10 21:09:37 +02:00
languages . SetTranslationFromRequest ( panelTorrentReportList , r , "en-us" )
2017-05-10 20:42:20 +02:00
htv := PanelTorrentReportListVbs { reportJSON , NewSearchForm ( ) , Navigation { nbReports , offset , pagenum , "mod_trlist_page" } , currentUser , r . URL }
err = panelTorrentReportList . ExecuteTemplate ( w , "admin_index.html" , htv )
2017-05-10 21:09:37 +02:00
log . CheckError ( err )
2017-05-10 20:42:20 +02:00
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
}
}
2017-05-10 05:24:18 +02:00
func UsersListPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
2017-05-10 16:43:50 +02:00
vars := mux . Vars ( r )
2017-05-10 19:17:07 +02:00
page := vars [ "page" ]
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 05:54:12 +02:00
offset := 100
2017-05-10 05:24:18 +02:00
2017-05-10 19:17:07 +02:00
users , nbUsers := userService . RetrieveUsersForAdmin ( offset , ( pagenum - 1 ) * offset )
2017-05-10 05:54:12 +02:00
languages . SetTranslationFromRequest ( panelUserList , r , "en-us" )
2017-05-10 19:17:07 +02:00
htv := PanelUserListVbs { users , NewSearchForm ( ) , Navigation { nbUsers , offset , pagenum , "mod_ulist_page" } , currentUser , r . URL }
err = panelUserList . ExecuteTemplate ( w , "admin_index.html" , htv )
2017-05-10 21:09:37 +02:00
log . CheckError ( err )
2017-05-10 08:18:58 +02:00
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
2017-05-10 05:54:12 +02:00
}
2017-05-10 05:24:18 +02:00
}
2017-05-10 20:42:20 +02:00
2017-05-10 04:03:25 +02:00
func CommentsListPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-10 05:24:18 +02:00
currentUser := GetUser ( r )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
2017-05-10 16:43:50 +02:00
vars := mux . Vars ( r )
2017-05-10 19:17:07 +02:00
page := vars [ "page" ]
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 05:54:12 +02:00
offset := 100
2017-05-10 15:08:38 +02:00
userid := r . URL . Query ( ) . Get ( "userid" )
var conditions string
var values [ ] interface { }
2017-05-10 19:17:07 +02:00
if userid != "" {
2017-05-10 15:08:38 +02:00
conditions = "user_id = ?"
values = append ( values , userid )
}
2017-05-10 19:17:07 +02:00
comments , nbComments := commentService . GetAllComments ( offset , ( pagenum - 1 ) * offset , conditions , values ... )
2017-05-10 05:54:12 +02:00
languages . SetTranslationFromRequest ( panelCommentList , r , "en-us" )
2017-05-10 19:17:07 +02:00
htv := PanelCommentListVbs { comments , NewSearchForm ( ) , Navigation { nbComments , offset , pagenum , "mod_clist_page" } , currentUser , r . URL }
err = panelCommentList . ExecuteTemplate ( w , "admin_index.html" , htv )
2017-05-10 21:09:37 +02:00
log . CheckError ( err )
2017-05-10 08:18:58 +02:00
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
2017-05-10 05:54:12 +02:00
}
2017-05-10 04:03:25 +02:00
}
2017-05-11 01:46:50 +02:00
2017-05-10 04:03:25 +02:00
func TorrentEditModPanel ( w http . ResponseWriter , r * http . Request ) {
2017-05-10 05:24:18 +02:00
currentUser := GetUser ( r )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
id := r . URL . Query ( ) . Get ( "id" )
torrent , _ := torrentService . GetTorrentById ( id )
languages . SetTranslationFromRequest ( panelTorrentEd , r , "en-us" )
2017-05-10 23:53:25 +02:00
torrentJson := torrent . ToJSON ( )
uploadForm := NewUploadForm ( )
uploadForm . Name = torrentJson . Name
uploadForm . Category = torrentJson . Category + "_" + torrentJson . SubCategory
uploadForm . Status = torrentJson . Status
uploadForm . Description = string ( torrentJson . Description )
2017-05-11 00:02:36 +02:00
htv := PanelTorrentEdVbs { uploadForm , NewSearchForm ( ) , currentUser , form . NewErrors ( ) , form . NewInfos ( ) , r . URL }
2017-05-10 07:56:02 +02:00
err := panelTorrentEd . ExecuteTemplate ( w , "admin_index.html" , htv )
2017-05-10 21:09:37 +02:00
log . CheckError ( err )
2017-05-10 23:53:25 +02:00
2017-05-10 08:18:58 +02:00
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
2017-05-10 05:54:12 +02:00
}
2017-05-10 05:24:18 +02:00
}
2017-05-11 01:46:50 +02:00
2017-05-10 05:24:18 +02:00
func TorrentPostEditModPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
2017-05-11 01:46:50 +02:00
if ! userPermission . HasAdmin ( currentUser ) {
http . Error ( w , "admins only" , http . StatusForbidden )
2017-05-10 23:53:25 +02:00
return
}
var uploadForm UploadForm
id := r . URL . Query ( ) . Get ( "id" )
err := form . NewErrors ( )
infos := form . NewInfos ( )
torrent , _ := torrentService . GetTorrentById ( id )
2017-05-11 12:49:05 +02: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-11 01:46:50 +02:00
err [ "errors" ] = append ( err [ "errors" ] , "Failed to update torrent!" )
}
2017-05-11 12:49:05 +02:00
if len ( err ) == 0 {
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-11 01:46:50 +02:00
torrent . Description = uploadForm . Description
2017-05-11 12:49:05 +02:00
torrent . Uploader = nil // GORM will create a new user otherwise (wtf?!)
2017-05-11 01:46:50 +02:00
db . ORM . Save ( & torrent )
infos [ "infos" ] = append ( infos [ "infos" ] , "Torrent details updated." )
2017-05-10 05:54:12 +02:00
}
2017-05-10 05:24:18 +02:00
}
2017-05-11 01:46:50 +02:00
languages . SetTranslationFromRequest ( panelTorrentEd , r , "en-us" )
htv := PanelTorrentEdVbs { uploadForm , NewSearchForm ( ) , currentUser , err , infos , r . URL }
2017-05-13 00:31:27 +02:00
err_ := panelTorrentEd . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err_ )
2017-05-10 05:24:18 +02:00
}
2017-05-10 04:03:25 +02:00
func CommentDeleteModPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
2017-05-10 05:24:18 +02:00
id := r . URL . Query ( ) . Get ( "id" )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
_ = form . NewErrors ( )
2017-05-10 05:24:18 +02:00
_ , _ = userService . DeleteComment ( id )
2017-05-10 09:10:23 +02:00
url , _ := Router . Get ( "mod_clist" ) . URL ( )
2017-05-10 04:03:25 +02:00
http . Redirect ( w , r , url . String ( ) + "?deleted" , http . StatusSeeOther )
2017-05-10 08:18:58 +02:00
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
2017-05-10 04:03:25 +02:00
}
}
2017-05-10 22:05:56 +02:00
2017-05-10 04:03:25 +02:00
func TorrentDeleteModPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
2017-05-10 05:24:18 +02:00
id := r . URL . Query ( ) . Get ( "id" )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
_ = form . NewErrors ( )
2017-05-10 05:24:18 +02:00
_ , _ = torrentService . DeleteTorrent ( id )
2017-05-10 21:29:59 +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 )
}
2017-05-10 09:10:23 +02:00
url , _ := Router . Get ( "mod_tlist" ) . URL ( )
2017-05-10 04:03:25 +02:00
http . Redirect ( w , r , url . String ( ) + "?deleted" , http . StatusSeeOther )
2017-05-10 08:18:58 +02:00
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
2017-05-10 04:03:25 +02:00
}
2017-05-10 19:17:07 +02:00
}
2017-05-10 22:05:56 +02:00
func TorrentReportDeleteModPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
if userPermission . HasAdmin ( currentUser ) {
id := r . URL . Query ( ) . Get ( "id" )
fmt . Println ( id )
idNum , _ := strconv . ParseUint ( id , 10 , 64 )
_ = form . NewErrors ( )
_ , _ = reportService . DeleteTorrentReport ( uint ( idNum ) )
url , _ := Router . Get ( "mod_trlist" ) . URL ( )
http . Redirect ( w , r , url . String ( ) + "?deleted" , http . StatusSeeOther )
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
}
}
2017-05-13 17:29:21 +02:00
func TorrentReassignModPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
if ! userPermission . HasAdmin ( currentUser ) {
http . Error ( w , "admins only" , http . StatusForbidden )
return
}
languages . SetTranslationFromRequest ( panelTorrentReassign , r , "en-us" )
htv := PanelTorrentReassignVbs { ReassignForm { } , NewSearchForm ( ) , currentUser , form . NewErrors ( ) , form . NewInfos ( ) , r . URL }
err := panelTorrentReassign . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err )
}
func TorrentPostReassignModPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
if ! userPermission . HasAdmin ( currentUser ) {
http . Error ( w , "admins only" , http . StatusForbidden )
return
}
var rForm ReassignForm
err := form . NewErrors ( )
infos := form . NewInfos ( )
err2 := rForm . ExtractInfo ( r )
if err2 != nil {
err [ "errors" ] = append ( err [ "errors" ] , err2 . Error ( ) )
} else {
count , err2 := rForm . ExecuteAction ( )
if err2 != nil {
err [ "errors" ] = append ( err [ "errors" ] , "Something went wrong" )
} else {
infos [ "infos" ] = append ( infos [ "infos" ] , fmt . Sprintf ( "%d torrents updated." , count ) )
}
}
htv := PanelTorrentReassignVbs { rForm , NewSearchForm ( ) , currentUser , err , infos , r . URL }
err_ := panelTorrentReassign . ExecuteTemplate ( w , "admin_index.html" , htv )
log . CheckError ( err_ )
}