2017-05-10 04:03:25 +02:00
// hurry mod panel to get it faaaaaaaaaaaast
package router
import (
"html/template"
2017-05-10 05:54:12 +02:00
"net/http"
2017-05-10 05:24:18 +02:00
"path/filepath"
2017-05-10 05:54:12 +02:00
"strconv"
2017-05-10 07:47:01 +02:00
"fmt"
2017-05-10 04:03:25 +02:00
2017-05-10 05:24:18 +02:00
"github.com/ewhal/nyaa/service/comment"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/service/torrent/form"
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"
"github.com/ewhal/nyaa/util/modelHelper"
2017-05-10 04:03:25 +02:00
)
2017-05-10 05:24:18 +02:00
var panelIndex , panelTorrentList , panelUserList , panelCommentList , panelTorrentEd * template . Template
2017-05-10 05:54:12 +02:00
2017-05-10 04:03:25 +02:00
func init ( ) {
2017-05-10 05:24:18 +02:00
panelTorrentList = template . Must ( template . New ( "torrentlist" ) . Funcs ( FuncMap ) . ParseFiles ( filepath . Join ( TemplateDir , "admin_index.html" ) , filepath . Join ( TemplateDir , "admin/torrentlist.html" ) ) )
panelUserList = template . Must ( template . New ( "userlist" ) . Funcs ( FuncMap ) . ParseFiles ( filepath . Join ( TemplateDir , "admin_index.html" ) , filepath . Join ( TemplateDir , "admin/userlist.html" ) ) )
2017-05-10 04:03:25 +02:00
panelCommentList = template . Must ( template . New ( "commentlist" ) . Funcs ( FuncMap ) . ParseFiles ( filepath . Join ( TemplateDir , "admin_index.html" ) , filepath . Join ( TemplateDir , "admin/commentlist.html" ) ) )
2017-05-10 05:24:18 +02:00
panelIndex = template . Must ( template . New ( "indexPanel" ) . Funcs ( FuncMap ) . ParseFiles ( filepath . Join ( TemplateDir , "admin_index.html" ) , filepath . Join ( TemplateDir , "admin/panelindex.html" ) ) )
panelTorrentEd = template . Must ( template . New ( "indexPanel" ) . Funcs ( FuncMap ) . ParseFiles ( filepath . Join ( TemplateDir , "admin_index.html" ) , filepath . Join ( TemplateDir , "admin/paneltorrentedit.html" ) ) )
2017-05-10 04:03:25 +02:00
}
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 05:54:12 +02:00
torrents , _ , _ := torrentService . GetAllTorrents ( 0 , offset )
users := userService . RetrieveUsersForAdmin ( 0 , offset )
comments := commentService . GetAllComments ( 0 , offset )
languages . SetTranslationFromRequest ( panelIndex , r , "en-us" )
htv := PanelIndexVbs { torrents , users , comments }
_ = panelIndex . ExecuteTemplate ( w , "admin_index.html" , htv )
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
}
func TorrentsListPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
page , _ := strconv . Atoi ( r . URL . Query ( ) . Get ( "p" ) )
offset := 100
2017-05-10 05:24:18 +02:00
2017-05-10 07:47:01 +02:00
torrents , _ , _ := torrentService . GetAllTorrents ( offset , page * offset )
2017-05-10 05:54:12 +02:00
languages . SetTranslationFromRequest ( panelTorrentList , r , "en-us" )
htv := PanelTorrentListVbs { torrents }
2017-05-10 07:47:01 +02:00
err := panelTorrentList . ExecuteTemplate ( w , "admin_index.html" , htv )
fmt . Println ( 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
}
func UsersListPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
page , _ := strconv . Atoi ( r . URL . Query ( ) . Get ( "p" ) )
offset := 100
2017-05-10 05:24:18 +02:00
2017-05-10 07:56:02 +02:00
users := userService . RetrieveUsersForAdmin ( offset , page * offset )
2017-05-10 05:54:12 +02:00
languages . SetTranslationFromRequest ( panelUserList , r , "en-us" )
htv := PanelUserListVbs { users }
2017-05-10 07:56:02 +02:00
err := panelUserList . ExecuteTemplate ( w , "admin_index.html" , htv )
fmt . Println ( 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 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 ) {
page , _ := strconv . Atoi ( r . URL . Query ( ) . Get ( "p" ) )
offset := 100
2017-05-10 04:03:25 +02:00
2017-05-10 07:56:02 +02:00
comments := commentService . GetAllComments ( offset , page * offset )
2017-05-10 05:54:12 +02:00
languages . SetTranslationFromRequest ( panelCommentList , r , "en-us" )
htv := PanelCommentListVbs { comments }
2017-05-10 07:56:02 +02:00
err := panelCommentList . ExecuteTemplate ( w , "admin_index.html" , htv )
fmt . Println ( 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
}
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" )
htv := PanelTorrentEdVbs { torrent }
2017-05-10 07:56:02 +02:00
err := panelTorrentEd . ExecuteTemplate ( w , "admin_index.html" , htv )
fmt . Println ( 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
}
func TorrentPostEditModPanel ( w http . ResponseWriter , r * http . Request ) {
currentUser := GetUser ( r )
2017-05-10 05:54:12 +02:00
if userPermission . HasAdmin ( currentUser ) {
b := torrentform . PanelPost { }
err := form . NewErrors ( )
infos := form . NewInfos ( )
modelHelper . BindValueForm ( & b , r )
err = modelHelper . ValidateForm ( & b , err )
id := r . URL . Query ( ) . Get ( "id" )
torrent , _ := torrentService . GetTorrentById ( id )
if torrent . ID > 0 {
modelHelper . AssignValue ( & torrent , & b )
if len ( err ) == 0 {
_ , errorT := torrentService . UpdateTorrent ( torrent )
if errorT != nil {
err [ "errors" ] = append ( err [ "errors" ] , errorT . Error ( ) )
}
if len ( err ) == 0 {
infos [ "infos" ] = append ( infos [ "infos" ] , "torrent_updated" )
}
2017-05-10 05:24:18 +02:00
}
2017-05-10 05:54:12 +02:00
}
languages . SetTranslationFromRequest ( panelTorrentEd , r , "en-us" )
htv := PanelTorrentEdVbs { torrent }
_ = panelTorrentEd . ExecuteTemplate ( w , "admin_index.html" , htv )
2017-05-10 08:18:58 +02:00
} else {
http . Error ( w , "admins only" , http . StatusForbidden )
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
}
}
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 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 05:54:12 +02:00
}