Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Start mod system

Cette révision appartient à :
tomleb 2017-05-10 00:20:45 -04:00
Parent bb38b7a3ec
révision 1ddd4c12f8
8 fichiers modifiés avec 167 ajouts et 4 suppressions

Voir le fichier

@ -30,7 +30,7 @@ func GormInit(conf *config.Config) (*gorm.DB, error) {
// TODO: Enable Gorm initialization for non-development builds
if config.Environment == "DEVELOPMENT" {
db.LogMode(true)
db.AutoMigrate(&model.Torrent{}, &model.UserFollows{}, &model.User{}, &model.Comment{}, &model.OldComment{})
db.AutoMigrate(&model.Torrent{}, &model.UserFollows{}, &model.User{}, &model.Comment{}, &model.OldComment{}, &model.TorrentReport{})
}
return db, nil

Voir le fichier

@ -40,6 +40,17 @@ type Torrent struct {
Comments []Comment `gorm:"ForeignKey:torrent_id"`
}
// TODO Add field to specify kind of reports
// TODO Add CreatedAt field
// INFO 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:"description"`
Torrent Torrent `gorm:"ForeignKey:Id"`
User User `gorm:"ForeignKey:Id"`
}
/* We need a JSON object instead of a Gorm structure because magnet URLs are
not in the database and have to be generated dynamically */
@ -74,6 +85,20 @@ type TorrentJSON struct {
TorrentLink template.URL `json:"torrent"`
}
type TorrentReportJson struct {
ID uint `json:"id"`
Description string `json:"description"`
Torrent Torrent `json:"torrent"`
User User `json:"user"`
}
/* Model Conversion to Json */
func (report *TorrentReport) ToJson() TorrentReportJson {
json := TorrentReportJson{report.ID, report.Description, report.Torrent, report.User}
return json
}
// ToJSON converts a model.Torrent to its equivalent JSON structure
func (t *Torrent) ToJSON() TorrentJSON {
magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...)
@ -126,3 +151,11 @@ func TorrentsToJSON(t []Torrent) []TorrentJSON { // TODO: Convert to singular ve
}
return json
}
func TorrentReportsToJSON(reports []TorrentReport) []TorrentReportJson {
json := make([]TorrentReportJson, len(reports))
for i := range reports {
json[i] = reports[i].ToJson()
}
return json
}

Voir le fichier

@ -39,7 +39,6 @@ func init() {
gzipUserFollowHandler := handlers.CompressHandler(http.HandlerFunc(UserFollowHandler))
gzipUserProfileFormHandler := handlers.CompressHandler(http.HandlerFunc(UserProfileFormHandler))
gzipIndexModPanel := handlers.CompressHandler(http.HandlerFunc(IndexModPanel))
gzipTorrentsListPanel := handlers.CompressHandler(http.HandlerFunc(TorrentsListPanel))
gzipUsersListPanel := handlers.CompressHandler(http.HandlerFunc(UsersListPanel))
@ -49,7 +48,10 @@ func init() {
gzipCommentDeleteModPanel := handlers.CompressHandler(http.HandlerFunc(CommentDeleteModPanel))
gzipTorrentDeleteModPanel := handlers.CompressHandler(http.HandlerFunc(TorrentDeleteModPanel))
gzipGetTorrentReportHandler := handlers.CompressHandler(http.HandlerFunc(GetTorrentReportHandler))
gzipTorrentReportCreateHandler := handlers.CompressHandler(http.HandlerFunc(CreateTorrentReportHandler))
gzipTorrentReportDeleteHandler := handlers.CompressHandler(http.HandlerFunc(DeleteTorrentReportHandler))
gzipTorrentDeleteHandler := handlers.CompressHandler(http.HandlerFunc(DeleteTorrentHandler))
Router = mux.NewRouter()
@ -93,5 +95,11 @@ func init() {
Router.PathPrefix("/captcha").Methods("GET").HandlerFunc(captcha.ServeFiles)
Router.Handle("/report/create", gzipTorrentReportCreateHandler).Name("torrent_report_create").Methods("POST")
// TODO Allow only moderators to access /moderation/*
Router.Handle("/moderation/report/delete", gzipTorrentReportDeleteHandler).Name("torrent_report_delete").Methods("POST")
Router.Handle("/moderation/torrent/delete", gzipTorrentDeleteHandler).Name("torrent_delete").Methods("POST")
Router.Handle("/moderation/report", gzipGetTorrentReportHandler ).Name("torrent_report").Methods("GET")
Router.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
}

Voir le fichier

@ -7,7 +7,7 @@ import (
var TemplateDir = "templates"
var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate *template.Template
var torrentReportTemplate, homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate *template.Template
type templateLoader struct {
templ **template.Template
@ -18,6 +18,11 @@ type templateLoader struct {
// ReloadTemplates reloads templates on runtime
func ReloadTemplates() {
templs := []templateLoader{
templateLoader{
templ: &torrentReportTemplate,
name: "torrent_report",
file: "torrent_report.html",
},
templateLoader{
templ: &homeTemplate,
name: "home",

72
router/torrentReportHandler.go Fichier normal
Voir le fichier

@ -0,0 +1,72 @@
package router
import (
"net/http"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/moderation"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util/modelHelper"
)
func SanitizeTorrentReport(torrentReport *model.TorrentReport) {
// TODO unescape html ?
return
}
func IsValidTorrentReport() bool {
// TODO Validate, see if user_id already reported, see if torrent exists
return true
}
// TODO Only allow moderators for each action in this file
func CreateTorrentReportHandler(w http.ResponseWriter, r *http.Request) {
var torrentReport model.TorrentReport
var err error
modelHelper.BindValueForm(&torrentReport, r)
if IsValidTorrentReport() {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
SanitizeTorrentReport(&torrentReport)
_, err = moderationService.CreateTorrentReport(torrentReport)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func DeleteTorrentReportHandler(w http.ResponseWriter, r *http.Request) {
// TODO Figure out how to get torrent report id from form
var id int
var err error
_, err = moderationService.DeleteTorrentReport(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func GetTorrentReportHandler(w http.ResponseWriter, r *http.Request) {
torrentReports, err := moderationService.GetTorrentReports()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = torrentReportTemplate.ExecuteTemplate(w, "torrent_report.html", model.TorrentReportsToJSON(torrentReports))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func DeleteTorrentHandler(w http.ResponseWriter, r *http.Request) {
// TODO Figure out how to get torrent report id from form
var err error
var id string
_, err = torrentService.DeleteTorrent(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

Voir le fichier

@ -0,0 +1,38 @@
package moderationService
import (
"errors"
"net/http"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
)
// Return torrentReport in case we did modified it (ie: CreatedAt field)
func CreateTorrentReport(torrentReport model.TorrentReport) (model.TorrentReport, error) {
if db.ORM.Create(&torrentReport).Error != nil {
return torrentReport, errors.New("TorrentReport was not created")
}
return torrentReport, nil
}
func DeleteTorrentReport(id int) (int, error) {
var torrentReport model.TorrentReport
if db.ORM.First(&torrentReport, id).RecordNotFound() {
return http.StatusNotFound, errors.New("Trying to delete a torrent report that does not exists.")
}
if db.ORM.Delete(&torrentReport).Error != nil {
return http.StatusInternalServerError, errors.New("User is not deleted.")
}
return http.StatusOK, nil
}
// TODO Add WhereParams to filter the torrent reports (ie: searching description)
// TODO Use limit, offset
func GetTorrentReports() ([]model.TorrentReport, error) {
var torrentReports []model.TorrentReport
if db.ORM.Preload("User").Preload("Torrents").Find(&torrentReports).Error != nil {
return nil, errors.New("Problem finding all torrent reports.")
}
return torrentReports, nil
}

Voir le fichier

@ -5,6 +5,7 @@ import (
"strconv"
"strings"
"net/http"
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"

6
templates/torrent_report.html Fichier exécutable
Voir le fichier

@ -0,0 +1,6 @@
<!DOCTYPE html>
<html>
{{ range .TorrentReport }}
{{ .ID }} {{ .Description }} {{ .User }}
{{end}}
</html>