Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Tags no-js works + less sql in Development mode + debug tool

Cette révision appartient à :
akuma06 2017-07-31 03:15:37 +02:00
Parent fe2c128e44
révision 6e5be0f7f1
10 fichiers modifiés avec 86 ajouts et 27 suppressions

Voir le fichier

@ -17,7 +17,7 @@ import (
func ErrorMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if config.Get().Environment == "DEVELOPMENT" {
if c.Writer.Status() >= 300 && config.Get().Environment == "DEVELOPMENT" {
messages := msg.GetMessages(c)
if messages.HasErrors() {
log.Errorf("Request has errors: %v", messages.GetAllErrors())
@ -60,14 +60,3 @@ func ScopesRequired(scopes ...string) gin.HandlerFunc {
c.Next()
}
}
func pprofHandler(handler http.HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
currentUser := router.GetUser(c)
if currentUser.HasAdmin() {
handler.ServeHTTP(c.Writer, c.Request)
} else {
templates.HttpError(c, http.StatusNotFound)
}
}
}

Voir le fichier

@ -11,6 +11,8 @@ func init() {
torrentRoutes.POST("/", TorrentPostEditUserPanel)
torrentRoutes.GET("/tag", ViewFormTag)
torrentRoutes.POST("/tag", ViewFormTag)
torrentRoutes.GET("/tag/add", AddTag)
torrentRoutes.GET("/tag/remove", DeleteTag)
torrentRoutes.GET("/delete", TorrentDeleteUserPanel)
}
torrentViewRoutes := router.Get().Group("/view")

Voir le fichier

@ -19,6 +19,7 @@ import (
"github.com/gin-gonic/gin"
)
// postTag is a function used by controllers to post a tag
func postTag(c *gin.Context, torrent *models.Torrent, user *models.User) {
messages := msg.GetMessages(c)
tagForm := &tagsValidator.CreateForm{}
@ -41,6 +42,7 @@ func postTag(c *gin.Context, torrent *models.Torrent, user *models.User) {
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
}
// ViewFormTag is a controller displaying a form to add a tag to a torrent
func ViewFormTag(c *gin.Context) {
messages := msg.GetMessages(c)
user := router.GetUser(c)
@ -83,6 +85,51 @@ func ViewFormTag(c *gin.Context) {
templates.Form(c, "/site/torrents/tag.jet.html", tagForm)
}
// 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() {
postTag(c, torrent, user)
if !messages.HasErrors() {
if _, ok := c.GetQuery("json"); ok {
c.JSON(http.StatusOK, struct {
Ok bool
}{true})
return
}
}
}
}
if _, ok := c.GetQuery("json"); ok {
c.JSON(http.StatusOK, struct {
Ok bool
}{false})
return
}
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/view/%d", id))
}
// DeleteTag is a controller to delete a user tag
func DeleteTag(c *gin.Context) {
messages := msg.GetMessages(c)
user := router.GetUser(c)
@ -100,10 +147,9 @@ func DeleteTag(c *gin.Context) {
user.LoadTags(torrent)
torrent.LoadTags()
if c.PostForm("tag") != "" && user.ID > 0 {
tagForm := &tagsValidator.CreateForm{}
if c.Query("tag") != "" && user.ID > 0 {
tagForm := &tagsValidator.CreateForm{c.Query("tag"), c.Query("type")}
c.Bind(tagForm)
validator.ValidateForm(tagForm, messages)
if !messages.HasErrors() {

Voir le fichier

@ -55,11 +55,6 @@ func ViewHandler(c *gin.Context) {
user.LoadTags(torrent)
torrent.LoadTags()
// We add a tag if posted
if c.PostForm("tag") != "" && user.ID > 0 {
postTag(c, torrent, user)
}
// Convert torrent to the JSON Model used to display a torrent
// Since many datas need to be parsed from a simple torrent model to the actual display
b := torrent.ToJSON()

Voir le fichier

@ -28,7 +28,7 @@ func (ta *Tag) Update() (int, error) {
// Delete : delete a tag based on id
func (ta *Tag) Delete() (int, error) {
if ORM.Delete(ta).Error != nil {
if ORM.Where("tag = ? AND type = ? AND torrent_id = ? AND user_id = ?", ta.Tag, ta.Type, ta.TorrentID, ta.UserID).Delete(ta).Error != nil {
return http.StatusInternalServerError, errors.New("tag_not_deleted")
}

Voir le fichier

@ -2,6 +2,7 @@ package tags
import (
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/utils/cache"
"github.com/pkg/errors"
)
@ -21,5 +22,6 @@ func Create(tag string, tagType string, torrent *models.Torrent, user *models.Us
if err := models.ORM.Create(newTag).Error; err != nil {
return newTag, err
}
cache.C.Delete(torrent.Identifier())
return newTag, nil
}

Voir le fichier

@ -1,6 +1,8 @@
package tags
import (
"fmt"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/models/users"
@ -12,7 +14,8 @@ func Filter(tag string, tagType string, torrentID uint) bool {
return false
}
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 err := models.ORM.Select("torrent_id, 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 {
fmt.Println(tagSum)
if tagSum.Total > config.Get().Torrents.Tags.MaxWeight {
tags, err := FindAll(tagType, torrentID)
if err != nil {

Voir le fichier

@ -38,6 +38,8 @@ func CreateUserFromRequest(registrationForm *userValidator.RegistrationForm) (*m
// currently unused but needs to be set:
user.APIToken, _ = crypto.GenerateRandomToken32()
user.APITokenExpiry = time.Unix(0, 0)
// set default number of PP
user.Pantsu = 1
if models.ORM.Create(user).Error != nil {
return user, errors.New("user not created")

Voir le fichier

@ -71,11 +71,7 @@
<span class="tag{{ if len(Torrent.Tags) > 5 }} big{{ end }}" title="{{ .Tag }}">
{{ T("tagtype_" + .Type) }}: {{ .Tag }}
{{ if .Accepted == false }}
<form action="{{ if User.Tags.Contains(.) }}/torrent/tag/delete?id={{Torrent.ID}}{{end}}" class="tag-form">
<input type="hidden" name="tag" value="{{ .Tag }}">
<input type="hidden" name="type" value="{{ .Type }}">
<button type="submit">{{ if User.Tags.Contains(.) }}-{{else}}+{{end}}</button>
</form>
<a href="/torrent/tag/{{ if User.Tags.Contains(.) }}remove{{else}}add{{end}}?id={{Torrent.ID}}&tag={{ .Tag }}&type={{ .Type }}" class="tag-form">{{ if User.Tags.Contains(.) }}-{{else}}+{{end}}</a>
{{ end }}
</span>
{{end}}

24
utils/debug/debug.go Fichier normal
Voir le fichier

@ -0,0 +1,24 @@
package debug
import (
"runtime"
"github.com/NyaaPantsu/nyaa/utils/log"
)
func LogCaller(parent int) {
if parent <= 0 {
parent = 1
}
parent++
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(parent, pc)
frames := runtime.CallersFrames(pc)
for {
frame, ok := frames.Next()
if !ok {
return
}
log.Infof("called from %s in %s#%d\n", frame.Func.Name(), frame.File, frame.Line)
}
}