Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Display uploader name with link to profile page

Cette révision appartient à :
sfan5 2017-05-09 12:35:46 +02:00
Parent a913bead67
révision fb07da18ec
4 fichiers modifiés avec 24 ajouts et 7 suppressions

Voir le fichier

@ -4,7 +4,6 @@ import (
"github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/util" "github.com/ewhal/nyaa/util"
"html"
"html/template" "html/template"
"strconv" "strconv"
"strings" "strings"
@ -34,7 +33,7 @@ type Torrents struct {
Description string `gorm:"column:description"` Description string `gorm:"column:description"`
WebsiteLink string `gorm:"column:website_link"` WebsiteLink string `gorm:"column:website_link"`
Uploader *User `gorm:"ForeignKey:uploader"` Uploader *User `gorm:"ForeignKey:UploaderId"`
OldComments []OldComment `gorm:"ForeignKey:torrent_id"` OldComments []OldComment `gorm:"ForeignKey:torrent_id"`
Comments []Comment `gorm:"ForeignKey:torrent_id"` Comments []Comment `gorm:"ForeignKey:torrent_id"`
} }
@ -66,6 +65,7 @@ type TorrentsJson struct {
Category string `json:"category"` Category string `json:"category"`
Downloads int `json:"downloads"` Downloads int `json:"downloads"`
UploaderId uint `json:"uploader_id"` UploaderId uint `json:"uploader_id"`
UploaderName template.HTML `json:"uploader_name"`
WebsiteLink template.URL `json:"website_link"` WebsiteLink template.URL `json:"website_link"`
Magnet template.URL `json:"magnet"` Magnet template.URL `json:"magnet"`
} }
@ -79,12 +79,15 @@ func (t *Torrents) ToJson() TorrentsJson {
commentsJson = append(commentsJson, CommentsJson{Username: c.Username, Content: template.HTML(c.Content), Date: c.Date}) commentsJson = append(commentsJson, CommentsJson{Username: c.Username, Content: template.HTML(c.Content), Date: c.Date})
} }
for _, c := range t.Comments { for _, c := range t.Comments {
commentsJson = append(commentsJson, CommentsJson{Username: c.User.Username, Content: util.MarkdownToHTML(c.Content), Date: c.CreatedAt}) commentsJson = append(commentsJson, CommentsJson{Username: c.User.Username, Content: util.MarkdownToHTML(c.Content), Date: c.CreatedAt})
} }
uploader := ""
if t.Uploader != nil {
uploader = t.Uploader.Username
}
res := TorrentsJson{ res := TorrentsJson{
Id: strconv.FormatUint(uint64(t.Id), 10), Id: strconv.FormatUint(uint64(t.Id), 10),
Name: html.UnescapeString(t.Name), Name: t.Name,
Status: t.Status, Status: t.Status,
Hash: t.Hash, Hash: t.Hash,
Date: t.Date.Format(time.RFC3339), Date: t.Date.Format(time.RFC3339),
@ -95,6 +98,7 @@ func (t *Torrents) ToJson() TorrentsJson {
Category: strconv.Itoa(t.Category), Category: strconv.Itoa(t.Category),
Downloads: t.Downloads, Downloads: t.Downloads,
UploaderId: t.UploaderId, UploaderId: t.UploaderId,
UploaderName: util.SafeText(uploader),
WebsiteLink: util.Safe(t.WebsiteLink), WebsiteLink: util.Safe(t.WebsiteLink),
Magnet: util.Safe(magnet)} Magnet: util.Safe(magnet)}

Voir le fichier

@ -58,7 +58,10 @@ func GetTorrentById(id string) (model.Torrents, error) {
if tmp.Find(&torrent).RecordNotFound() { if tmp.Find(&torrent).RecordNotFound() {
return torrent, errors.New("Article is not found.") return torrent, errors.New("Article is not found.")
} }
// .Preload("Comments.User") doesn't work // GORM relly likes not doing its job correctly
// (or maybe I'm just retarded)
torrent.Uploader = new(model.User)
db.ORM.Where("user_id = ?", torrent.UploaderId).Find(torrent.Uploader)
for i := range torrent.Comments { for i := range torrent.Comments {
torrent.Comments[i].User = new(model.User) torrent.Comments[i].User = new(model.User)
db.ORM.Where("user_id = ?", torrent.Comments[i].UserId).Find(torrent.Comments[i].User) db.ORM.Where("user_id = ?", torrent.Comments[i].UserId).Find(torrent.Comments[i].User)

Voir le fichier

@ -26,6 +26,9 @@
<td>{{T "size"}}</td> <td>{{T "size"}}</td>
<td>{{.Filesize}}</td> <td>{{.Filesize}}</td>
</tr> </tr>
<tr>
<td>Uploader</td>
<td><a href="{{$.URL.Parse (printf "/user/%d/-" .UploaderId) }}">{{.UploaderName}}</a></td>
{{if ne .WebsiteLink ""}} {{if ne .WebsiteLink ""}}
<tr> <tr>
<td>{{T "Link"}}</td> <td>{{T "Link"}}</td>

Voir le fichier

@ -1,7 +1,14 @@
package util package util
import "html/template" import (
"html"
"html/template"
)
func Safe(s string) template.URL { func Safe(s string) template.URL {
return template.URL(s) return template.URL(s)
} }
func SafeText(s string) template.HTML {
return template.HTML(html.EscapeString(s))
}