2017-07-29 15:20:57 +02:00
package tags
import (
2017-07-31 03:15:37 +02:00
"fmt"
2017-07-29 15:20:57 +02:00
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/models/users"
"github.com/NyaaPantsu/nyaa/utils/log"
)
2017-08-01 00:05:30 +02:00
// Filter check if a tag type has reached the maximal votes and removes the other tag of the same type
2017-08-01 23:38:13 +02:00
func Filter ( tag string , tagType string , torrent * models . Torrent ) bool {
if torrent . ID == 0 || tagType == "" || tag == "" {
2017-07-30 03:29:09 +02:00
return false
}
2017-07-29 15:20:57 +02:00
tagSum := models . Tag { }
2017-08-01 23:38:13 +02:00
if err := models . ORM . Select ( "torrent_id, tag, type, accepted, SUM(weight) as total" ) . Where ( "torrent_id = ? AND tag = ? AND type = ?" , torrent . ID , tag , tagType ) . Group ( "type, tag" ) . Find ( & tagSum ) . Error ; err == nil {
2017-07-31 03:15:37 +02:00
fmt . Println ( tagSum )
2017-07-29 15:20:57 +02:00
if tagSum . Total > config . Get ( ) . Torrents . Tags . MaxWeight {
2017-08-01 23:38:13 +02:00
tags , err := FindAll ( tagType , torrent . ID )
2017-07-29 15:20:57 +02:00
if err != nil {
return false
}
for _ , toDelete := range tags {
user , _ , err := users . FindRawByID ( toDelete . UserID )
if err != nil {
log . CheckErrorWithMessage ( err , "USER_NOT_FOUND: Couldn't update pantsu points!" )
}
2017-07-31 18:29:43 +02:00
if user . ID > 0 {
if toDelete . Tag == tag {
user . IncreasePantsu ( )
} else {
user . DecreasePantsu ( )
}
user . Update ( )
2017-07-29 15:20:57 +02:00
}
toDelete . Delete ( )
}
2017-08-01 23:38:13 +02:00
/ * err := DeleteAllType ( tagType , torrent . ID ) // We can also delete them in batch
2017-07-29 15:20:57 +02:00
log . CheckError ( err ) * /
tagSum . Accepted = true
tagSum . UserID = 0 // System ID
tagSum . Weight = config . Get ( ) . Torrents . Tags . MaxWeight // Overriden to the maximal value
models . ORM . Save ( & tagSum ) // We only add back the tag accepted
2017-08-01 23:38:13 +02:00
callbackOnType ( & tagSum , torrent )
2017-07-29 15:20:57 +02:00
return true
}
}
return false
}
2017-08-01 00:05:30 +02:00
2017-08-01 23:38:13 +02:00
func callbackOnType ( tag * models . Tag , torrent * models . Torrent ) {
2017-08-01 00:05:30 +02:00
switch tag . Type {
case "anidbid" , "vndbid" :
2017-08-01 23:38:13 +02:00
if tag . Accepted && tag . TorrentID > 0 && torrent . ID > 0 {
torrent . DbID = tag . Tag
torrent . Update ( false )
2017-08-01 00:05:30 +02:00
}
}
}