From 7c5bae175bd43889b717ccaba3d338e2b60aec36 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Wed, 10 May 2017 05:24:18 +0200 Subject: [PATCH] Shitty Moderation page No HTML done, could'nt test with nyaa.exe :/ 1 page with overview of torrents, comments, users 1 page list comments 1 page list torrents 1 page list users 1 route delete torreny 1 route delete comment Users can be deleted and edited by their route user_profile_edit (I think) --- router/modpanel.go | 125 +++++++++++++++++++++++++------ router/router.go | 23 ++++++ router/templateVariables.go | 19 +++++ service/comment/comment.go | 6 +- service/torrent/form/form.go | 11 +++ service/torrent/torrent.go | 11 ++- service/user/user.go | 14 +--- templates/admin/commentlist.html | 8 ++ templates/admin/torrentlist.html | 8 ++ templates/admin_index.html | 42 +++++++++++ 10 files changed, 230 insertions(+), 37 deletions(-) create mode 100644 service/torrent/form/form.go create mode 100644 templates/admin/commentlist.html create mode 100644 templates/admin/torrentlist.html create mode 100644 templates/admin_index.html diff --git a/router/modpanel.go b/router/modpanel.go index fbc0e951..970e5c20 100644 --- a/router/modpanel.go +++ b/router/modpanel.go @@ -3,55 +3,138 @@ package router import ( - "fmt" "net/http" "strconv" "html/template" + "path/filepath" - "github.com/ewhal/nyaa/model" + "github.com/ewhal/nyaa/service/comment" "github.com/ewhal/nyaa/service/user" "github.com/ewhal/nyaa/service/user/permission" - // "github.com/ewhal/nyaa/util/languages" - // "github.com/ewhal/nyaa/util/modelHelper" - "github.com/gorilla/mux" + form "github.com/ewhal/nyaa/service/user/form" + "github.com/ewhal/nyaa/service/torrent" + "github.com/ewhal/nyaa/service/torrent/form" + "github.com/ewhal/nyaa/util/languages" + "github.com/ewhal/nyaa/util/modelHelper" ) -var panelCommentList *template.Template +var panelIndex, panelTorrentList, panelUserList, panelCommentList, panelTorrentEd *template.Template func init() { + 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"))) panelCommentList = template.Must(template.New("commentlist").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/commentlist.html"))) + 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"))) } -func IndexModPanel(w http.ResponseWriter, r *http.Request) {} -func TorrentsListPanel(w http.ResponseWriter, r *http.Request) {} -func UsersListPanel(w http.ResponseWriter, r *http.Request) {} -func CommentsListPanel(w http.ResponseWriter, r *http.Request) { +func IndexModPanel(w http.ResponseWriter, r *http.Request) { + currentUser := GetUser(r) + if (userPermission.HasAdmin(currentUser)) { + offset := 10 + + 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) +} + +} +func TorrentsListPanel(w http.ResponseWriter, r *http.Request) { + currentUser := GetUser(r) + if (userPermission.HasAdmin(currentUser)) { page,_ := strconv.Atoi(r.URL.Query().Get("p")) offset := 100 - comments := commentService.GetAllTorrents(page*offset, offset) + torrents, _, _ := torrentService.GetAllTorrents(page*offset, offset) + languages.SetTranslationFromRequest(panelTorrentList, r, "en-us") + htv := PanelTorrentListVbs{torrents} + _= panelTorrentList.ExecuteTemplate(w, "admin_index.html", htv) +} +} +func UsersListPanel(w http.ResponseWriter, r *http.Request) { + currentUser := GetUser(r) + if (userPermission.HasAdmin(currentUser)) { + page,_ := strconv.Atoi(r.URL.Query().Get("p")) + offset := 100 + + users := userService.RetrieveUsersForAdmin(page*offset, offset) + languages.SetTranslationFromRequest(panelUserList, r, "en-us") + htv := PanelUserListVbs{users} + _ = panelUserList.ExecuteTemplate(w, "admin_index.html", htv) +} +} +func CommentsListPanel(w http.ResponseWriter, r *http.Request) { + currentUser := GetUser(r) + if (userPermission.HasAdmin(currentUser)) { + page,_ := strconv.Atoi(r.URL.Query().Get("p")) + offset := 100 + + comments := commentService.GetAllComments(page*offset, offset) languages.SetTranslationFromRequest(panelCommentList, r, "en-us") htv := PanelCommentListVbs{comments} - err := panelCommentList.ExecuteTemplate(w, "index.html", htv) + _= panelCommentList.ExecuteTemplate(w, "admin_index.html", htv) +} } func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) { - // Todo + currentUser := GetUser(r) + if (userPermission.HasAdmin(currentUser)) { + id := r.URL.Query().Get("id") + torrent, _ := torrentService.GetTorrentById(id) + languages.SetTranslationFromRequest(panelTorrentEd, r, "en-us") + htv := PanelTorrentEdVbs{torrent} + _= panelTorrentEd.ExecuteTemplate(w, "admin_index.html", htv) } + +} +func TorrentPostEditModPanel(w http.ResponseWriter, r *http.Request) { + currentUser := GetUser(r) + 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") + } + } + } + languages.SetTranslationFromRequest(panelTorrentEd, r, "en-us") + htv := PanelTorrentEdVbs{torrent} + _ = panelTorrentEd.ExecuteTemplate(w, "admin_index.html", htv) +} +} + func CommentDeleteModPanel(w http.ResponseWriter, r *http.Request) { currentUser := GetUser(r) - if (HasAdmin(currentUser)) { - err := form.NewErrors() - _, _ := userService.DeleteComment(id) - url, _ := Router.Get("mod_comment_list") + id := r.URL.Query().Get("id") + + if (userPermission.HasAdmin(currentUser)) { + _= form.NewErrors() + _, _ = userService.DeleteComment(id) + url, _ := Router.Get("mod_comment_list").URL() http.Redirect(w, r, url.String()+"?deleted", http.StatusSeeOther) } } func TorrentDeleteModPanel(w http.ResponseWriter, r *http.Request) { currentUser := GetUser(r) - if (HasAdmin(currentUser)) { - err := form.NewErrors() - _, _ := torrentService.DeleteTorrent(id) - url, _ := Router.Get("mod_torrent_list") + id := r.URL.Query().Get("id") + if (userPermission.HasAdmin(currentUser)) { + _= form.NewErrors() + _, _ = torrentService.DeleteTorrent(id) + url, _ := Router.Get("mod_torrent_list").URL() http.Redirect(w, r, url.String()+"?deleted", http.StatusSeeOther) } } \ No newline at end of file diff --git a/router/router.go b/router/router.go index 32a97dcd..e35623ae 100644 --- a/router/router.go +++ b/router/router.go @@ -39,6 +39,18 @@ func init() { gzipUserFollowHandler := handlers.CompressHandler(http.HandlerFunc(UserFollowHandler)) gzipUserProfileFormHandler := handlers.CompressHandler(http.HandlerFunc(UserProfileFormHandler)) + + gzipIndexModPanel := handlers.CompressHandler(http.HandlerFunc(IndexModPanel)) + gzipTorrentsListPanel := handlers.CompressHandler(http.HandlerFunc(TorrentsListPanel)) + gzipUsersListPanel := handlers.CompressHandler(http.HandlerFunc(UsersListPanel)) + gzipCommentsListPanel := handlers.CompressHandler(http.HandlerFunc(CommentsListPanel)) + gzipTorrentEditModPanel := handlers.CompressHandler(http.HandlerFunc(TorrentEditModPanel)) + gzipTorrentPostEditModPanel := handlers.CompressHandler(http.HandlerFunc(TorrentPostEditModPanel)) + gzipCommentDeleteModPanel := handlers.CompressHandler(http.HandlerFunc(CommentDeleteModPanel)) + gzipTorrentDeleteModPanel := handlers.CompressHandler(http.HandlerFunc(TorrentDeleteModPanel)) + + + Router = mux.NewRouter() // Routes @@ -68,6 +80,17 @@ func init() { Router.Handle("/user/{id}/{username}", gzipUserProfileHandler).Name("user_profile").Methods("GET") Router.Handle("/user/{id}/{username}/follow", gzipUserFollowHandler).Name("user_follow").Methods("GET") Router.Handle("/user/{id}/{username}", gzipUserProfileFormHandler).Name("user_profile").Methods("POST") + + Router.Handle("/mod/", gzipIndexModPanel).Name("mod_index") + Router.Handle("/mod/torrents", gzipTorrentsListPanel).Name("mod_tlist") + Router.Handle("/mod/users", gzipUsersListPanel).Name("mod_ulist") + Router.Handle("/mod/comments", gzipCommentsListPanel).Name("mod_clist") + Router.Handle("/mod/torrent/", gzipTorrentEditModPanel).Name("mod_tedit") + Router.Handle("/mod/torrent/", gzipTorrentPostEditModPanel).Name("mod_ptedit") + Router.Handle("/mod/torrent/delete", gzipCommentDeleteModPanel).Name("mod_tdelete") + Router.Handle("/mod/comment/delete", gzipTorrentDeleteModPanel).Name("mod_cdelete") + + Router.PathPrefix("/captcha").Methods("GET").HandlerFunc(captcha.ServeFiles) Router.NotFoundHandler = http.HandlerFunc(NotFoundHandler) diff --git a/router/templateVariables.go b/router/templateVariables.go index 8d8761e5..82bbef4d 100644 --- a/router/templateVariables.go +++ b/router/templateVariables.go @@ -113,6 +113,25 @@ type UploadTemplateVariables struct { Route *mux.Route } +type PanelIndexVbs struct { + Torrents []model.Torrents + Users []model.User + Comments []model.Comment +} + +type PanelTorrentListVbs struct { + Torrents []model.Torrents +} +type PanelUserListVbs struct { + Users []model.User +} +type PanelCommentListVbs struct { + Comments []model.Comment +} +type PanelTorrentEdVbs struct { + Torrent model.Torrents +} + /* * Variables used by the upper ones */ diff --git a/service/comment/comment.go b/service/comment/comment.go index 336b7cb2..3fac559d 100644 --- a/service/comment/comment.go +++ b/service/comment/comment.go @@ -1,16 +1,12 @@ package commentService import ( - "net/http" - "github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/model" - "github.com/ewhal/nyaa/service/user/permission" - "github.com/ewhal/nyaa/util/modelHelper" ) -func GetAllComments(limit int, offset int) { +func GetAllComments(limit int, offset int) []model.Comment{ var comments []model.Comment db.ORM.Limit(limit).Offset(offset).Preload("Uploader").Find(&comments) return comments diff --git a/service/torrent/form/form.go b/service/torrent/form/form.go new file mode 100644 index 00000000..fb194c7c --- /dev/null +++ b/service/torrent/form/form.go @@ -0,0 +1,11 @@ +package torrentform + +type PanelPost struct { + Name string `form:"name" needed:"true" len_min:"3" len_max:"20"` + Hash string `form:"hash" needed:"true"` + Category int `form:"cat" needed:"true"` + Sub_Category int `form:"subcat"` + Status string `form:"status" needed:"true"` + Description string `form:"desc"` + WebsiteLink string `form:"website"` +} \ No newline at end of file diff --git a/service/torrent/torrent.go b/service/torrent/torrent.go index 6a83451e..d598d17f 100644 --- a/service/torrent/torrent.go +++ b/service/torrent/torrent.go @@ -4,6 +4,7 @@ import ( "errors" "strconv" "strings" + "net/http" "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/db" @@ -177,7 +178,7 @@ func CreateWhereParams(conditions string, params ...string) WhereParams { return whereParams } -func DeleteTorrent(id string) { +func DeleteTorrent(id string) (int, error) { var torrent model.Torrents if db.ORM.First(&torrent, id).RecordNotFound() { return http.StatusNotFound, errors.New("Torrent is not found.") @@ -185,5 +186,13 @@ func DeleteTorrent(id string) { if db.ORM.Delete(&torrent).Error != nil { return http.StatusInternalServerError, errors.New("Torrent is not deleted.") } + return http.StatusOK, nil +} + +func UpdateTorrent(torrent model.Torrents) (int, error) { + if db.ORM.Save(torrent).Error != nil { + return http.StatusInternalServerError, errors.New("Torrent is not updated.") + } + return http.StatusOK, nil } \ No newline at end of file diff --git a/service/user/user.go b/service/user/user.go index 7c6b8399..61881d17 100644 --- a/service/user/user.go +++ b/service/user/user.go @@ -271,16 +271,10 @@ func RetrieveUserForAdmin(id string) (model.User, int, error) { } // RetrieveUsersForAdmin retrieves users for an administrator. -func RetrieveUsersForAdmin() []model.User { +func RetrieveUsersForAdmin(limit int, offset int) []model.User { var users []model.User - var userArr []model.User - db.ORM.Find(&users) - for _, user := range users { - db.ORM.Model(&user) - db.ORM.Model(&user).Related("Torrents").Related("Likings").Related("Liked").Find(&model.Torrents{}) - userArr = append(userArr, user) - } - return userArr + db.ORM.Preload("Torrents").Find(&users).Limit(limit).Offset(offset) + return users } // CreateUserAuthentication creates user authentication. @@ -307,7 +301,7 @@ func RemoveFollow(user *model.User, follower *model.User) { } } -func DeleteComment(id string) { +func DeleteComment(id string) (int, error) { var comment model.Comment if db.ORM.First(&comment, id).RecordNotFound() { return http.StatusNotFound, errors.New("Comment is not found.") diff --git a/templates/admin/commentlist.html b/templates/admin/commentlist.html new file mode 100644 index 00000000..28c781c2 --- /dev/null +++ b/templates/admin/commentlist.html @@ -0,0 +1,8 @@ +{{block "content"}} + +{{ range .Comments}} + + +{{end}} +
{{ .Content }}Delete
+{{end}} \ No newline at end of file diff --git a/templates/admin/torrentlist.html b/templates/admin/torrentlist.html new file mode 100644 index 00000000..0e2fb458 --- /dev/null +++ b/templates/admin/torrentlist.html @@ -0,0 +1,8 @@ +{{block "content"}} + +{{ range .Torrents}} + + +{{end}} +
{{ .Name }}Delete
+{{end}} \ No newline at end of file diff --git a/templates/admin_index.html b/templates/admin_index.html new file mode 100644 index 00000000..4ff1b519 --- /dev/null +++ b/templates/admin_index.html @@ -0,0 +1,42 @@ + + + + + + + + Nyaa Pantsu - {{block "title" .}}{{ T "error_404" }}{{end}} + + + + + + + + + + + + + + + + + + +
+ {{block "content" .}}{{end}} +
+ + + + + + + + {{block "js_footer" .}}{{end}} + +