diff --git a/config/default_config.yml b/config/default_config.yml index 919e222b..7c833de1 100644 --- a/config/default_config.yml +++ b/config/default_config.yml @@ -129,6 +129,11 @@ torrents: tags: # Torrent Tag Max weight for automatic system approval max_weight: 100.00 +# Array of tag types allowed for now + types: + - anidbid + - vndbid + - quality users: default_notifications_settings: {"new_torrent": true, "new_torrent_email": false, "new_comment": true, "new_comment_email": false, "new_responses": false, "new_responses_email": false, "new_follower": false, "new_follower_email": false, "followed": false, "followed_email": false} navigation: diff --git a/config/structs.go b/config/structs.go index 1ca64932..c21c76dd 100644 --- a/config/structs.go +++ b/config/structs.go @@ -47,7 +47,8 @@ type Config struct { } type Tags struct { - MaxWeight float64 `yaml:"max_weight,omitempty"` + MaxWeight float64 `yaml:"max_weight,omitempty"` + Types ArrayString `yaml:"types,omitempty"` } // WebAddressConfig : Config struct for web addresses @@ -102,8 +103,8 @@ type ScraperConfig struct { // TrackersConfig ; Config struct for Trackers type TrackersConfig struct { - Default []string `yaml:"default,flow,omitempty"` - NeededTrackers []int `yaml:"needed,flow,omitempty"` + Default ArrayString `yaml:"default,flow,omitempty"` + NeededTrackers []int `yaml:"needed,flow,omitempty"` } // TorrentsConfig : Config struct for Torrents @@ -111,9 +112,9 @@ type TorrentsConfig struct { Status []bool `yaml:"status,omitempty,omitempty"` SukebeiCategories map[string]string `yaml:"sukebei_categories,omitempty"` CleanCategories map[string]string `yaml:"clean_categories,omitempty"` - EnglishOnlyCategories []string `yaml:"english_only_categories,omitempty"` - NonEnglishOnlyCategories []string `yaml:"non_english_only_categories,omitempty"` - AdditionalLanguages []string `yaml:"additional_languages,omitempty"` + EnglishOnlyCategories ArrayString `yaml:"english_only_categories,omitempty"` + NonEnglishOnlyCategories ArrayString `yaml:"non_english_only_categories,omitempty"` + AdditionalLanguages ArrayString `yaml:"additional_languages,omitempty"` FileStorage string `yaml:"filestorage,omitempty"` StorageLink string `yaml:"storage_link,omitempty"` CacheLink string `yaml:"cache_link,omitempty"` @@ -198,3 +199,14 @@ type SearchConfig struct { ElasticsearchIndex string `yaml:"es_index,omitempty"` ElasticsearchType string `yaml:"es_type,omitempty"` } + +type ArrayString []string + +func (ar ArrayString) Contains(str string) bool { + for _, s := range ar { + if s == str { + return true + } + } + return false +} diff --git a/controllers/torrent/router.go b/controllers/torrent/router.go index 126654b8..3cb584ef 100644 --- a/controllers/torrent/router.go +++ b/controllers/torrent/router.go @@ -9,6 +9,8 @@ func init() { { torrentRoutes.GET("/", TorrentEditUserPanel) torrentRoutes.POST("/", TorrentPostEditUserPanel) + torrentRoutes.GET("/tag", ViewFormTag) + torrentRoutes.POST("/tag", ViewFormTag) torrentRoutes.GET("/delete", TorrentDeleteUserPanel) } torrentViewRoutes := router.Get().Group("/view") diff --git a/controllers/torrent/tag.go b/controllers/torrent/tag.go index f1a87f6f..7ee60fd3 100644 --- a/controllers/torrent/tag.go +++ b/controllers/torrent/tag.go @@ -1,8 +1,17 @@ package torrentController import ( + "errors" + + "fmt" + "net/http" + "strconv" + + "github.com/NyaaPantsu/nyaa/controllers/router" "github.com/NyaaPantsu/nyaa/models" "github.com/NyaaPantsu/nyaa/models/tags" + "github.com/NyaaPantsu/nyaa/models/torrents" + "github.com/NyaaPantsu/nyaa/templates" msg "github.com/NyaaPantsu/nyaa/utils/messages" "github.com/NyaaPantsu/nyaa/utils/validator" "github.com/NyaaPantsu/nyaa/utils/validator/tags" @@ -16,6 +25,12 @@ func postTag(c *gin.Context, torrent *models.Torrent, user *models.User) { c.Bind(tagForm) validator.ValidateForm(tagForm, messages) + // We check that the tag type sent is one enabled in config.yml + if !tagsValidator.CheckTagType(tagForm.Type) { + messages.ErrorT(errors.New("wrong_tag_type")) + return + } + for _, tag := range user.Tags { if tag.Tag == tagForm.Tag { return // already a tag by the user, don't add one more @@ -25,3 +40,45 @@ func postTag(c *gin.Context, torrent *models.Torrent, user *models.User) { tags.Create(tagForm.Tag, tagForm.Type, torrent, user) // Add a tag to the db tags.Filter(tagForm.Tag, tagForm.Type, torrent.ID) // Check if we have a tag reaching the maximum weight, if yes, deletes every tag and add only the one accepted } + +func ViewFormTag(c *gin.Context) { + messages := msg.GetMessages(c) + user := router.GetUser(c) + id, _ := strconv.ParseInt(c.Query("id"), 10, 32) + // Retrieve the torrent + torrent, err := torrents.FindByID(uint(id)) + + // If torrent not found, display 404 + if err != nil { + c.Status(http.StatusNotFound) + return + } + + // We load tags for user and torrents + user.LoadTags(torrent) + torrent.LoadTags() + + // We add a tag if posted + if c.PostForm("tag") != "" && user.ID > 0 { + postTag(c, torrent, user) + if !messages.HasErrors() { + if _, ok := c.GetQuery("json"); ok { + c.JSON(http.StatusOK, struct { + Ok bool + }{true}) + return + } + c.Redirect(http.StatusSeeOther, fmt.Sprintf("/view/%d", id)) + } + if _, ok := c.GetQuery("json"); ok { + c.JSON(http.StatusOK, struct { + Ok bool + }{false}) + return + } + } + tagForm := &tagsValidator.CreateForm{} + c.Bind(tagForm) + + templates.Form(c, "/site/torrents/tag.jet.html", tagForm) +} diff --git a/templates/site/torrents/edit.jet.html b/templates/site/torrents/edit.jet.html index 943cfe0b..820d81e4 100644 --- a/templates/site/torrents/edit.jet.html +++ b/templates/site/torrents/edit.jet.html @@ -10,12 +10,12 @@ {{ yield csrf_field() }}