From 7bb008cb195a30504c383b55ae00182c3840110e Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sat, 29 Jul 2017 18:42:02 +0200 Subject: [PATCH] Fix for userID = 0 was current user --- controllers/torrent/delete.go | 2 +- controllers/torrent/edit.go | 2 +- models/user.go | 32 ++++++++++++++++++- .../layouts/partials/menu/profile.jet.html | 2 +- templates/template.go | 2 +- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/controllers/torrent/delete.go b/controllers/torrent/delete.go index 67328e6b..ec1bc838 100644 --- a/controllers/torrent/delete.go +++ b/controllers/torrent/delete.go @@ -18,7 +18,7 @@ func TorrentDeleteUserPanel(c *gin.Context) { id, _ := strconv.ParseInt(c.Query("id"), 10, 32) currentUser := router.GetUser(c) torrent, _ := torrents.FindByID(uint(id)) - if currentUser.CurrentOrAdmin(torrent.UploaderID) { + if currentUser.CurrentOrAdmin(torrent.UploaderID) && torrent.ID > 0 { _, _, err := torrent.Delete(false) if err == nil { if torrent.Uploader == nil { diff --git a/controllers/torrent/edit.go b/controllers/torrent/edit.go index 4d7abad2..005f61c2 100644 --- a/controllers/torrent/edit.go +++ b/controllers/torrent/edit.go @@ -20,7 +20,7 @@ func TorrentEditUserPanel(c *gin.Context) { id, _ := strconv.ParseInt(c.Query("id"), 10, 32) torrent, _ := torrents.FindByID(uint(id)) currentUser := router.GetUser(c) - if currentUser.CurrentOrAdmin(torrent.UploaderID) { + if currentUser.CurrentOrAdmin(torrent.UploaderID) && torrent.ID > 0 { uploadForm := torrentValidator.TorrentRequest{} uploadForm.Name = torrent.Name uploadForm.Category = strconv.Itoa(torrent.Category) + "_" + strconv.Itoa(torrent.SubCategory) diff --git a/models/user.go b/models/user.go index 47c7726b..2a0656bc 100644 --- a/models/user.go +++ b/models/user.go @@ -12,6 +12,8 @@ import ( "errors" + "math" + "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/utils/crypto" ) @@ -45,6 +47,7 @@ type User struct { Mascot string `gorm:"column:mascot"` MascotURL string `gorm:"column:mascot_url"` UserSettings string `gorm:"column:settings"` + Pantsu float64 `gorm:"column:pantsu"` // TODO: move this to PublicUser Followers []User // Don't work `gorm:"foreignkey:user_id;associationforeignkey:follower_id;many2many:user_follows"` @@ -56,6 +59,7 @@ type User struct { UnreadNotifications int `gorm:"-"` // We don't want to loop every notifications when accessing user unread notif Settings UserSettings `gorm:"-"` // We don't want to load settings everytime, stock it as a string, parse it when needed + Tags []Tag `gorm:"-"` // We load tags only when viewing a torrent } // UserJSON : User model conversion in JSON @@ -97,7 +101,7 @@ func (u User) Size() (s int) { 6*2 + // string pointers 4*3 + //time.Time 3*2 + // arrays - // string arrays + // string arrays len(u.Username) + len(u.Password) + len(u.Email) + len(u.APIToken) + len(u.MD5) + len(u.Language) + len(u.Theme) s *= 8 @@ -150,6 +154,9 @@ func (u *User) HasAdmin() bool { // CurrentOrAdmin check that user has admin permission or user is the current user. func (u *User) CurrentOrAdmin(userID uint) bool { + if userID == 0 { + return false + } log.Debugf("user.ID == userID %d %d %s", u.ID, userID, u.ID == userID) return (u.IsModerator() || u.ID == userID) } @@ -390,3 +397,26 @@ func (u *User) Filter() *User { u.Torrents = torrents return u } + +// IncreasePantsu is a function that uses the formula to increase the Pantsu points of a user +func (u *User) IncreasePantsu() { + if u.Pantsu <= 0 { + u.Pantsu = 1 // Pantsu points should never be less or equal to 0. This would trigger a division by 0 + } + u.Pantsu = u.Pantsu * (1 + 1/(math.Pow(math.Log(u.Pantsu+1), 5))) // First votes substancially increases the vote, further it increase slowly +} + +// DecreasePantsu is a function that uses the formula to decrease the Pantsu points of a user +func (u *User) DecreasePantsu() { + u.Pantsu = 0.8 * u.Pantsu // You lose 20% of your pantsu points each wrong vote +} + +func (u *User) LoadTags(torrent *Torrent) { + if u.ID == 0 { + return + } + if err := ORM.Where("torrent_id = ? AND user_id = ?", torrent.ID, u.ID).Find(&u.Tags).Error; err != nil { + log.CheckErrorWithMessage(err, "LOAD_TAGS_ERROR: Couldn't load tags!") + return + } +} diff --git a/templates/layouts/partials/menu/profile.jet.html b/templates/layouts/partials/menu/profile.jet.html index 12de4de7..3859b912 100644 --- a/templates/layouts/partials/menu/profile.jet.html +++ b/templates/layouts/partials/menu/profile.jet.html @@ -51,7 +51,7 @@ {{ T("my_notifications")}}
{{end}} - {{if User.CurrentOrAdmin(UserProfile.ID) }} + {{if UserProfile.ID > 0 && User.CurrentOrAdmin(UserProfile.ID) }} {{ T("settings")}} diff --git a/templates/template.go b/templates/template.go index 83b9ff7e..fcffc624 100644 --- a/templates/template.go +++ b/templates/template.go @@ -164,7 +164,7 @@ func userProfileBase(c *gin.Context, templateName string, userProfile *models.Us query.Set("limit", "20") c.Request.URL.RawQuery = query.Encode() nbTorrents := 0 - if currentUser.CurrentOrAdmin(userProfile.ID) { + if userProfile.ID > 0 && currentUser.CurrentOrAdmin(userProfile.ID) { _, userProfile.Torrents, nbTorrents, _ = search.ByQuery(c, 1, true, true, false, false) } else { _, userProfile.Torrents, nbTorrents, _ = search.ByQuery(c, 1, true, true, false, true)