summaryrefslogtreecommitdiffstats
path: root/model/report.go
blob: abac1e649e3d5d5c625953a8bb021412fdf34680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package model

import (
	"time"

	"github.com/NyaaPantsu/nyaa/config"
)

// User can be null (anonymous reports)
// FIXME  can't preload field Torrents for model.TorrentReport
type TorrentReport struct {
	ID          uint   `gorm:"column:torrent_report_id;primary_key"`
	Description string `gorm:"column:type"`
	TorrentID   uint   `gorm:"column:torrent_id"`
	UserID      uint   `gorm:"column:user_id"`

	CreatedAt time.Time `gorm:"column:created_at"`

	Torrent *Torrent `gorm:"AssociationForeignKey:TorrentID;ForeignKey:torrent_id"`
	User    *User    `gorm:"AssociationForeignKey:UserID;ForeignKey:user_id"`
}

func (r TorrentReport) TableName() string {
	return config.ReportsTableName
}

type TorrentReportJson struct {
	ID          uint        `json:"id"`
	Description string      `json:"description"`
	Torrent     TorrentJSON `json:"torrent"`
	User        UserJSON    `json:"user"`
}

/* Model Conversion to Json */

func getReportDescription(d string) string {
	if d == "illegal" {
		return "Illegal content"
	} else if d == "spam" {
		return "Spam / Garbage"
	} else if d == "wrongcat" {
		return "Wrong category"
	} else if d == "dup" {
		return "Duplicate / Deprecated"
	}
	return "???"
}

func (report *TorrentReport) ToJson() TorrentReportJson {
	var t TorrentJSON = TorrentJSON{}
	if report.Torrent != nil { // FIXME: report.Torrent should never be nil
		t = report.Torrent.ToJSON()
	}
	var u UserJSON = UserJSON{}
	if report.User != nil {
		u = report.User.ToJSON()
	}
	json := TorrentReportJson{report.ID, getReportDescription(report.Description), t, u}
	return json
}

func TorrentReportsToJSON(reports []TorrentReport) []TorrentReportJson {
	json := make([]TorrentReportJson, len(reports))
	for i := range reports {
		json[i] = reports[i].ToJson()
	}
	return json
}