2017-07-28 20:43:22 +02:00
package torrentController
import (
2017-07-28 23:02:37 +02:00
"errors"
"fmt"
"net/http"
"strconv"
"github.com/NyaaPantsu/nyaa/controllers/router"
2017-07-28 20:43:22 +02:00
"github.com/NyaaPantsu/nyaa/models"
2017-07-30 03:29:09 +02:00
"github.com/NyaaPantsu/nyaa/models/tag"
2017-07-28 23:02:37 +02:00
"github.com/NyaaPantsu/nyaa/models/torrents"
"github.com/NyaaPantsu/nyaa/templates"
2017-08-01 14:47:27 +02:00
"github.com/NyaaPantsu/nyaa/utils/api"
2017-07-30 03:29:09 +02:00
"github.com/NyaaPantsu/nyaa/utils/log"
2017-07-28 20:43:22 +02:00
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/validator"
2017-07-31 03:23:21 +02:00
"github.com/NyaaPantsu/nyaa/utils/validator/tag"
2017-07-28 20:43:22 +02:00
"github.com/gin-gonic/gin"
)
2017-07-31 03:15:37 +02:00
// postTag is a function used by controllers to post a tag
2017-08-01 14:47:27 +02:00
func postTag ( c * gin . Context , torrent * models . Torrent , user * models . User ) * models . Tag {
2017-07-28 20:43:22 +02:00
messages := msg . GetMessages ( c )
tagForm := & tagsValidator . CreateForm { }
c . Bind ( tagForm )
validator . ValidateForm ( tagForm , messages )
2017-07-28 23:02:37 +02:00
// 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" ) )
2017-08-01 14:47:27 +02:00
return nil
2017-07-28 23:02:37 +02:00
}
2017-07-30 03:29:09 +02:00
if user . Tags . Contains ( models . Tag { Tag : tagForm . Tag , Type : tagForm . Type } ) {
log . Info ( "User has already tagged the type for the torrent" )
2017-08-01 14:47:27 +02:00
return nil
2017-07-28 20:43:22 +02:00
}
2017-08-01 14:47:27 +02:00
tag , _ := 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
return tag
2017-07-28 20:43:22 +02:00
}
2017-07-28 23:02:37 +02:00
2017-07-31 03:15:37 +02:00
// ViewFormTag is a controller displaying a form to add a tag to a torrent
2017-07-28 23:02:37 +02:00
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 {
2017-08-01 14:47:27 +02:00
tag := postTag ( c , torrent , user )
2017-07-28 23:02:37 +02:00
if ! messages . HasErrors ( ) {
if _ , ok := c . GetQuery ( "json" ) ; ok {
2017-08-01 14:47:27 +02:00
apiUtils . ResponseHandler ( c , tag )
2017-07-28 23:02:37 +02:00
return
}
c . Redirect ( http . StatusSeeOther , fmt . Sprintf ( "/view/%d" , id ) )
}
if _ , ok := c . GetQuery ( "json" ) ; ok {
2017-08-01 14:47:27 +02:00
apiUtils . ResponseHandler ( c )
2017-07-28 23:02:37 +02:00
return
}
}
tagForm := & tagsValidator . CreateForm { }
c . Bind ( tagForm )
templates . Form ( c , "/site/torrents/tag.jet.html" , tagForm )
}
2017-07-30 03:29:09 +02:00
2017-07-31 03:15:37 +02:00
// AddTag is a controller to add a
func AddTag ( 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 ( )
if c . Query ( "tag" ) != "" && user . ID > 0 {
tagForm := & tagsValidator . CreateForm { c . Query ( "tag" ) , c . Query ( "type" ) }
validator . ValidateForm ( tagForm , messages )
if ! messages . HasErrors ( ) {
2017-08-01 14:47:27 +02:00
tag := postTag ( c , torrent , user )
2017-07-31 03:15:37 +02:00
if ! messages . HasErrors ( ) {
if _ , ok := c . GetQuery ( "json" ) ; ok {
2017-08-01 14:47:27 +02:00
apiUtils . ResponseHandler ( c , tag )
2017-07-31 03:15:37 +02:00
return
}
}
}
}
if _ , ok := c . GetQuery ( "json" ) ; ok {
2017-08-01 14:47:27 +02:00
apiUtils . ResponseHandler ( c )
2017-07-31 03:15:37 +02:00
return
}
c . Redirect ( http . StatusSeeOther , fmt . Sprintf ( "/view/%d" , id ) )
}
// DeleteTag is a controller to delete a user tag
2017-07-30 03:29:09 +02:00
func DeleteTag ( 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 ( )
2017-07-31 03:15:37 +02:00
if c . Query ( "tag" ) != "" && user . ID > 0 {
tagForm := & tagsValidator . CreateForm { c . Query ( "tag" ) , c . Query ( "type" ) }
2017-07-30 03:29:09 +02:00
validator . ValidateForm ( tagForm , messages )
if ! messages . HasErrors ( ) {
for _ , tag := range user . Tags {
if tag . Tag == tagForm . Tag && tag . Type == tagForm . Type {
_ , err := tag . Delete ( )
if err != nil {
log . CheckError ( err )
break
}
if _ , ok := c . GetQuery ( "json" ) ; ok {
2017-08-01 14:47:27 +02:00
apiUtils . ResponseHandler ( c , tag )
2017-07-30 03:29:09 +02:00
return
}
break
}
}
}
}
if _ , ok := c . GetQuery ( "json" ) ; ok {
2017-08-01 14:47:27 +02:00
apiUtils . ResponseHandler ( c )
2017-07-30 03:29:09 +02:00
return
}
c . Redirect ( http . StatusSeeOther , fmt . Sprintf ( "/view/%d" , id ) )
}