Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0

Fix for userID = 0 was current user

Cette révision appartient à :
akuma06 2017-07-29 18:42:02 +02:00
Parent ef9588b6cd
révision 7bb008cb19
5 fichiers modifiés avec 35 ajouts et 5 suppressions

Voir le fichier

@ -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 {

Voir le fichier

@ -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)

Voir le fichier

@ -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
}
}

Voir le fichier

@ -51,7 +51,7 @@
<a class="form-input" href="/notifications">{{ T("my_notifications")}}</a>
<br/>
{{end}}
{{if User.CurrentOrAdmin(UserProfile.ID) }}
{{if UserProfile.ID > 0 && User.CurrentOrAdmin(UserProfile.ID) }}
<a class="form-input" href="/user/{{UserProfile.ID}}/{{UserProfile.Username}}/edit">
{{ T("settings")}}
</a>

Voir le fichier

@ -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)