Renaming works then?
Cette révision appartient à :
Parent
a8bc3887ba
révision
ea43de8abb
4 fichiers modifiés avec 83 ajouts et 0 suppressions
19
models/tag/create.go
Fichier normal
19
models/tag/create.go
Fichier normal
|
@ -0,0 +1,19 @@
|
||||||
|
package tags
|
||||||
|
|
||||||
|
import "github.com/NyaaPantsu/nyaa/models"
|
||||||
|
|
||||||
|
func Create(tag string, tagType string, torrent *models.Torrent, user *models.User) (*models.Tag, error) {
|
||||||
|
newTag := &models.Tag{
|
||||||
|
Tag: tag,
|
||||||
|
Type: tagType,
|
||||||
|
TorrentID: torrent.ID,
|
||||||
|
UserID: user.ID,
|
||||||
|
Weight: user.Pantsu,
|
||||||
|
Accepted: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := models.ORM.Create(newTag).Error; err != nil {
|
||||||
|
return newTag, err
|
||||||
|
}
|
||||||
|
return newTag, nil
|
||||||
|
}
|
12
models/tag/delete.go
Fichier normal
12
models/tag/delete.go
Fichier normal
|
@ -0,0 +1,12 @@
|
||||||
|
package tags
|
||||||
|
|
||||||
|
import "github.com/NyaaPantsu/nyaa/models"
|
||||||
|
|
||||||
|
// DeleteAllType : Deletes all tag from a tag type and torrent ID
|
||||||
|
func DeleteAllType(tagType string, torrentID uint) error {
|
||||||
|
if err := models.ORM.Model(&models.Tag{}).Where("torrent_id = ? AND type = ?", torrentID, tagType).Delete(&models.Tag{}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
11
models/tag/find.go
Fichier normal
11
models/tag/find.go
Fichier normal
|
@ -0,0 +1,11 @@
|
||||||
|
package tags
|
||||||
|
|
||||||
|
import "github.com/NyaaPantsu/nyaa/models"
|
||||||
|
|
||||||
|
func FindAll(tagType string, torrentID uint) ([]models.Tag, error) {
|
||||||
|
tags := []models.Tag{}
|
||||||
|
if err := models.ORM.Where("type = ? AND torrent_id = ?", tagType, torrentID).Find(&tags).Error; err != nil {
|
||||||
|
return tags, err
|
||||||
|
}
|
||||||
|
return tags, nil
|
||||||
|
}
|
41
models/tag/helpers.go
Fichier normal
41
models/tag/helpers.go
Fichier normal
|
@ -0,0 +1,41 @@
|
||||||
|
package tags
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/NyaaPantsu/nyaa/config"
|
||||||
|
"github.com/NyaaPantsu/nyaa/models"
|
||||||
|
"github.com/NyaaPantsu/nyaa/models/users"
|
||||||
|
"github.com/NyaaPantsu/nyaa/utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Filter(tag string, tagType string, torrentID uint) bool {
|
||||||
|
tagSum := models.Tag{}
|
||||||
|
if err := models.ORM.Select("tag, type, accepted, SUM(weight) as total").Where("torrent_id = ? AND tag = ? AND type = ?", torrentID, tag, tagType).Group("type, tag").Find(&tagSum).Error; err == nil {
|
||||||
|
if tagSum.Total > config.Get().Torrents.Tags.MaxWeight {
|
||||||
|
tags, err := FindAll(tagType, torrentID)
|
||||||
|
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!")
|
||||||
|
}
|
||||||
|
if toDelete.Tag == tag {
|
||||||
|
user.IncreasePantsu()
|
||||||
|
} else {
|
||||||
|
user.DecreasePantsu()
|
||||||
|
}
|
||||||
|
user.Update()
|
||||||
|
toDelete.Delete()
|
||||||
|
}
|
||||||
|
/* err := DeleteAllType(tagType, torrentID) // We can also delete them in batch
|
||||||
|
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
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
Référencer dans un nouveau ticket