Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Make comments work

Cette révision appartient à :
sfan5 2017-05-08 20:07:25 +02:00
Parent a3d13f768a
révision 5d36bba34d
3 fichiers modifiés avec 17 ajouts et 41 suppressions

Voir le fichier

@ -12,8 +12,8 @@ type Comment struct {
CreatedAt time.Time `gorm:"column:created_at"` CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"` UpdatedAt time.Time `gorm:"column:updated_at"`
Torrent *Torrents `gorm:"ForeignKey:TorrentId"` Torrent *Torrents `gorm:"ForeignKey:torrent_id"`
User *User `gorm:"ForeignKey:UserId"` User *User `gorm:"ForeignKey:user_id"`
} }
type OldComment struct { type OldComment struct {
@ -22,10 +22,10 @@ type OldComment struct {
Content string `gorm:"column:content"` Content string `gorm:"column:content"`
Date time.Time `gorm:"column:date"` Date time.Time `gorm:"column:date"`
Torrent *Torrents `gorm:"ForeignKey:TorrentId"` Torrent *Torrents `gorm:"ForeignKey:torrent_id"`
} }
func (c OldComment) TableName() string { func (c OldComment) TableName() string {
// cba to renamed this in the db // cba to rename this in the db
return "comments_old" return "comments_old"
} }

Voir le fichier

@ -35,9 +35,9 @@ 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:UploaderId"` Uploader *User `gorm:"ForeignKey:uploader"`
OldComments []OldComment `gorm:"ForeignKey:Id"` OldComments []OldComment `gorm:"ForeignKey:torrent_id"`
Comments []Comment `gorm:"ForeignKey:Id"` Comments []Comment `gorm:"ForeignKey:torrent_id"`
} }
/* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically /* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically
@ -52,19 +52,10 @@ type ApiResultJson struct {
TotalRecordCount int `json:"totalRecordCount"` TotalRecordCount int `json:"totalRecordCount"`
} }
type OldCommentsJson struct {
C template.HTML `json:"c"`
Us string `json:"us"`
Un string `json:"un"`
UI int `json:"ui"`
T int `json:"t"`
Av string `json:"av"`
ID string `json:"id"`
}
type CommentsJson struct { type CommentsJson struct {
Content template.HTML `json:"content"`
Username string `json:"username"` Username string `json:"username"`
Content template.HTML `json:"content"`
Date time.Time `json:"date"`
} }
type TorrentsJson struct { type TorrentsJson struct {
@ -85,27 +76,13 @@ type TorrentsJson struct {
func (t *Torrents) ToJson() TorrentsJson { func (t *Torrents) ToJson() TorrentsJson {
magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...) magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...)
//offset := 0
var commentsJson []CommentsJson var commentsJson []CommentsJson
/*if len(t.OldComments) != 0 { for _, c := range t.OldComments {
b := []OldCommentsJson{} commentsJson = append(commentsJson, CommentsJson{Username: c.Username, Content: template.HTML(c.Content), Date: c.Date})
err := json.Unmarshal([]byte(t.OldComments), &b)
if err == nil {
commentsJson = make([]CommentsJson, len(t.Comments)+len(b))
offset = len(b)
for i, commentJson := range b {
commentsJson[i] = CommentsJson{Content: commentJson.C,
Username: commentJson.Un}
} }
} else { for _, c := range t.Comments {
commentsJson = make([]CommentsJson, len(t.Comments)) commentsJson = append(commentsJson, CommentsJson{Username: (*c.User).Username, Content: template.HTML(c.Content), Date: c.CreatedAt})
} }
} else {
commentsJson = make([]CommentsJson, len(t.Comments))
}
for i, comment := range t.Comments {
commentsJson[i+offset] = CommentsJson{Content: template.HTML(comment.Content), Username: comment.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: html.UnescapeString(t.Name),

Voir le fichier

@ -45,7 +45,7 @@ func GetFeeds() []model.Feed {
func GetTorrentById(id string) (model.Torrents, error) { func GetTorrentById(id string) (model.Torrents, error) {
var torrent model.Torrents var torrent model.Torrents
if db.ORM.Where("torrent_id = ?", id).Preload("Comments").Find(&torrent).RecordNotFound() { if db.ORM.Where("torrent_id = ?", id).Preload("Comments").Preload("OldComments").Find(&torrent).RecordNotFound() {
return torrent, errors.New("Article is not found.") return torrent, errors.New("Article is not found.")
} }
@ -55,11 +55,10 @@ func GetTorrentById(id string) (model.Torrents, error) {
func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) ([]model.Torrents, int) { func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) ([]model.Torrents, int) {
var torrents []model.Torrents var torrents []model.Torrents
var count int var count int
conditions := "torrent_hash IS NOT NULL" // filter out broken entries conditions := ""
if strings.HasPrefix(orderBy, "filesize") { if strings.HasPrefix(orderBy, "filesize") {
// torrents w/ NULL filesize fuck up the sorting on postgres // torrents w/ NULL filesize fuck up the sorting on postgres
// TODO: fix this properly conditions = "filesize IS NOT NULL"
conditions += " AND filesize IS NOT NULL"
} }
var params []interface{} var params []interface{}