More sukebei fixes (#649)
* Remove useless .Table() from db usage This is handled via TableName() already * Optimization: omit file-list fetching for old torrents * Use seperate tables for reports & files on sukebei * Fix invalid pages in nav if <5 pages total
Cette révision appartient à :
Parent
f61bff5fac
révision
4c24318cb3
8 fichiers modifiés avec 42 ajouts et 29 suppressions
|
@ -14,16 +14,18 @@ const (
|
||||||
// that was copied from the original Nyaa
|
// that was copied from the original Nyaa
|
||||||
LastOldTorrentID = 923000
|
LastOldTorrentID = 923000
|
||||||
TorrentsTableName = "torrents"
|
TorrentsTableName = "torrents"
|
||||||
|
ReportsTableName = "torrent_reports"
|
||||||
CommentsTableName = "comments"
|
CommentsTableName = "comments"
|
||||||
UploadsOldTableName = "user_uploads_old"
|
UploadsOldTableName = "user_uploads_old"
|
||||||
|
FilesTableName = "files"
|
||||||
|
|
||||||
// for sukebei:
|
// for sukebei:
|
||||||
//LastOldTorrentID = 2303945
|
//LastOldTorrentID = 2303945
|
||||||
//TorrentsTableName = "sukebei_torrents"
|
//TorrentsTableName = "sukebei_torrents"
|
||||||
|
//ReportsTableName = "sukebei_torrent_reports"
|
||||||
//CommentsTableName = "sukebei_comments"
|
//CommentsTableName = "sukebei_comments"
|
||||||
//UploadsOldTableName = "sukebei_user_uploads_old"
|
//UploadsOldTableName = "sukebei_user_uploads_old"
|
||||||
|
//FilesTableName = "sukebei_files"
|
||||||
// FIXME: files table needs to be seperate too
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsSukebei() bool {
|
func IsSukebei() bool {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/NyaaPantsu/nyaa/config"
|
||||||
"github.com/zeebo/bencode"
|
"github.com/zeebo/bencode"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,6 +13,10 @@ type File struct {
|
||||||
Filesize int64 `gorm:"column:filesize"`
|
Filesize int64 `gorm:"column:filesize"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f File) TableName() string {
|
||||||
|
return config.FilesTableName
|
||||||
|
}
|
||||||
|
|
||||||
// Returns the total size of memory allocated for this struct
|
// Returns the total size of memory allocated for this struct
|
||||||
func (f File) Size() int {
|
func (f File) Size() int {
|
||||||
return (2 + len(f.BencodedPath) + 1) * 8;
|
return (2 + len(f.BencodedPath) + 1) * 8;
|
||||||
|
|
|
@ -2,11 +2,11 @@ package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/NyaaPantsu/nyaa/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO Add field to specify kind of reports
|
// User can be null (anonymous reports)
|
||||||
// TODO Add CreatedAt field
|
|
||||||
// INFO User can be null (anonymous reports)
|
|
||||||
// FIXME can't preload field Torrents for model.TorrentReport
|
// FIXME can't preload field Torrents for model.TorrentReport
|
||||||
type TorrentReport struct {
|
type TorrentReport struct {
|
||||||
ID uint `gorm:"column:torrent_report_id;primary_key"`
|
ID uint `gorm:"column:torrent_report_id;primary_key"`
|
||||||
|
@ -20,6 +20,10 @@ type TorrentReport struct {
|
||||||
User *User `gorm:"AssociationForeignKey:UserID;ForeignKey:user_id"`
|
User *User `gorm:"AssociationForeignKey:UserID;ForeignKey:user_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r TorrentReport) TableName() string {
|
||||||
|
return config.ReportsTableName
|
||||||
|
}
|
||||||
|
|
||||||
type TorrentReportJson struct {
|
type TorrentReportJson struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
@ -43,9 +47,8 @@ func getReportDescription(d string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (report *TorrentReport) ToJson() TorrentReportJson {
|
func (report *TorrentReport) ToJson() TorrentReportJson {
|
||||||
// FIXME: report.Torrent and report.User should never be nil
|
|
||||||
var t TorrentJSON = TorrentJSON{}
|
var t TorrentJSON = TorrentJSON{}
|
||||||
if report.Torrent != nil {
|
if report.Torrent != nil { // FIXME: report.Torrent should never be nil
|
||||||
t = report.Torrent.ToJSON()
|
t = report.Torrent.ToJSON()
|
||||||
}
|
}
|
||||||
var u UserJSON = UserJSON{}
|
var u UserJSON = UserJSON{}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
//"log"
|
||||||
"math"
|
"math"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -109,11 +109,14 @@ var FuncMap = template.FuncMap{
|
||||||
if nav.CurrentPage > pagesSelectable/2 {
|
if nav.CurrentPage > pagesSelectable/2 {
|
||||||
startValue = (int(math.Min((float64(nav.CurrentPage)+math.Floor(float64(pagesSelectable)/2)), maxPages)) - pagesSelectable + 1)
|
startValue = (int(math.Min((float64(nav.CurrentPage)+math.Floor(float64(pagesSelectable)/2)), maxPages)) - pagesSelectable + 1)
|
||||||
}
|
}
|
||||||
|
if startValue < 1 {
|
||||||
|
startValue = 1
|
||||||
|
}
|
||||||
endValue := (startValue + pagesSelectable - 1)
|
endValue := (startValue + pagesSelectable - 1)
|
||||||
if endValue > int(maxPages) {
|
if endValue > int(maxPages) {
|
||||||
endValue = int(maxPages)
|
endValue = int(maxPages)
|
||||||
}
|
}
|
||||||
log.Println(nav.TotalItem)
|
//log.Println(nav.TotalItem)
|
||||||
for i := startValue; i <= endValue; i++ {
|
for i := startValue; i <= endValue; i++ {
|
||||||
pageNum := strconv.Itoa(i)
|
pageNum := strconv.Itoa(i)
|
||||||
url, _ := Router.Get(nav.Route).URL("page", pageNum)
|
url, _ := Router.Get(nav.Route).URL("page", pageNum)
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/NyaaPantsu/nyaa/config"
|
|
||||||
"github.com/NyaaPantsu/nyaa/db"
|
"github.com/NyaaPantsu/nyaa/db"
|
||||||
"github.com/NyaaPantsu/nyaa/model"
|
"github.com/NyaaPantsu/nyaa/model"
|
||||||
"github.com/NyaaPantsu/nyaa/service/captcha"
|
"github.com/NyaaPantsu/nyaa/service/captcha"
|
||||||
|
@ -59,7 +58,7 @@ func UploadPostHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var sameTorrents int
|
var sameTorrents int
|
||||||
db.ORM.Model(&model.Torrent{}).Table(config.TorrentsTableName).Where("torrent_hash = ?", uploadForm.Infohash).Count(&sameTorrents)
|
db.ORM.Model(&model.Torrent{}).Where("torrent_hash = ?", uploadForm.Infohash).Count(&sameTorrents)
|
||||||
if sameTorrents == 0 {
|
if sameTorrents == 0 {
|
||||||
// add to db and redirect
|
// add to db and redirect
|
||||||
torrent := model.Torrent{
|
torrent := model.Torrent{
|
||||||
|
@ -72,7 +71,7 @@ func UploadPostHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
Filesize: uploadForm.Filesize,
|
Filesize: uploadForm.Filesize,
|
||||||
Description: uploadForm.Description,
|
Description: uploadForm.Description,
|
||||||
UploaderID: user.ID}
|
UploaderID: user.ID}
|
||||||
db.ORM.Table(config.TorrentsTableName).Create(&torrent)
|
db.ORM.Create(&torrent)
|
||||||
|
|
||||||
// add filelist to files db, if we have one
|
// add filelist to files db, if we have one
|
||||||
if len(uploadForm.FileList) > 0 {
|
if len(uploadForm.FileList) > 0 {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package commentService
|
package commentService
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/NyaaPantsu/nyaa/config"
|
|
||||||
"github.com/NyaaPantsu/nyaa/db"
|
"github.com/NyaaPantsu/nyaa/db"
|
||||||
"github.com/NyaaPantsu/nyaa/model"
|
"github.com/NyaaPantsu/nyaa/model"
|
||||||
)
|
)
|
||||||
|
@ -9,7 +8,7 @@ import (
|
||||||
func GetAllComments(limit int, offset int, conditions string, values ...interface{}) ([]model.Comment, int) {
|
func GetAllComments(limit int, offset int, conditions string, values ...interface{}) ([]model.Comment, int) {
|
||||||
var comments []model.Comment
|
var comments []model.Comment
|
||||||
var nbComments int
|
var nbComments int
|
||||||
db.ORM.Table(config.CommentsTableName).Model(&comments).Where(conditions, values...).Count(&nbComments)
|
db.ORM.Model(&comments).Where(conditions, values...).Count(&nbComments)
|
||||||
db.ORM.Preload("User").Limit(limit).Offset(offset).Where(conditions, values...).Find(&comments)
|
db.ORM.Preload("User").Limit(limit).Offset(offset).Where(conditions, values...).Find(&comments)
|
||||||
return comments, nbComments
|
return comments, nbComments
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,15 +54,18 @@ func GetTorrentById(id string) (torrent model.Torrent, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp := db.ORM.Table(config.TorrentsTableName).Where("torrent_id = ?", id).Preload("Comments").Preload("FileList")
|
tmp := db.ORM.Where("torrent_id = ?", id).Preload("Comments")
|
||||||
err = tmp.Error
|
if id_int > config.LastOldTorrentID {
|
||||||
if err != nil {
|
tmp = tmp.Preload("FileList")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if id_int <= config.LastOldTorrentID && !config.IsSukebei() {
|
if id_int <= config.LastOldTorrentID && !config.IsSukebei() {
|
||||||
// only preload old comments if they could actually exist
|
// only preload old comments if they could actually exist
|
||||||
tmp = tmp.Preload("OldComments")
|
tmp = tmp.Preload("OldComments")
|
||||||
}
|
}
|
||||||
|
err = tmp.Error
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
if tmp.Find(&torrent).RecordNotFound() {
|
if tmp.Find(&torrent).RecordNotFound() {
|
||||||
err = errors.New("Article is not found.")
|
err = errors.New("Article is not found.")
|
||||||
return
|
return
|
||||||
|
@ -74,7 +77,7 @@ func GetTorrentById(id string) (torrent model.Torrent, err error) {
|
||||||
torrent.OldUploader = ""
|
torrent.OldUploader = ""
|
||||||
if torrent.ID <= config.LastOldTorrentID && torrent.UploaderID == 0 {
|
if torrent.ID <= config.LastOldTorrentID && torrent.UploaderID == 0 {
|
||||||
var tmp model.UserUploadsOld
|
var tmp model.UserUploadsOld
|
||||||
if !db.ORM.Table(config.UploadsOldTableName).Where("torrent_id = ?", torrent.ID).Find(&tmp).RecordNotFound() {
|
if !db.ORM.Where("torrent_id = ?", torrent.ID).Find(&tmp).RecordNotFound() {
|
||||||
torrent.OldUploader = tmp.Username
|
torrent.OldUploader = tmp.Username
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,7 +95,7 @@ func GetTorrentById(id string) (torrent model.Torrent, err error) {
|
||||||
// won't fetch user or comments
|
// won't fetch user or comments
|
||||||
func GetRawTorrentById(id uint) (torrent model.Torrent, err error) {
|
func GetRawTorrentById(id uint) (torrent model.Torrent, err error) {
|
||||||
err = nil
|
err = nil
|
||||||
if db.ORM.Table(config.TorrentsTableName).Where("torrent_id = ?", id).Find(&torrent).RecordNotFound() {
|
if db.ORM.Where("torrent_id = ?", id).Find(&torrent).RecordNotFound() {
|
||||||
err = errors.New("Article is not found.")
|
err = errors.New("Article is not found.")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -128,7 +131,7 @@ func getTorrentsOrderBy(parameters *serviceBase.WhereParams, orderBy string, lim
|
||||||
conditions := strings.Join(conditionArray, " AND ")
|
conditions := strings.Join(conditionArray, " AND ")
|
||||||
if countAll {
|
if countAll {
|
||||||
// FIXME: `deleted_at IS NULL` is duplicate in here because GORM handles this for us
|
// FIXME: `deleted_at IS NULL` is duplicate in here because GORM handles this for us
|
||||||
err = db.ORM.Model(&torrents).Table(config.TorrentsTableName).Where(conditions, params...).Count(&count).Error
|
err = db.ORM.Model(&torrents).Where(conditions, params...).Count(&count).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -189,18 +192,18 @@ func GetAllTorrentsDB() ([]model.Torrent, int, error) {
|
||||||
|
|
||||||
func DeleteTorrent(id string) (int, error) {
|
func DeleteTorrent(id string) (int, error) {
|
||||||
var torrent model.Torrent
|
var torrent model.Torrent
|
||||||
if db.ORM.Table(config.TorrentsTableName).First(&torrent, id).RecordNotFound() {
|
if db.ORM.First(&torrent, id).RecordNotFound() {
|
||||||
return http.StatusNotFound, errors.New("Torrent is not found.")
|
return http.StatusNotFound, errors.New("Torrent is not found.")
|
||||||
}
|
}
|
||||||
if db.ORM.Table(config.TorrentsTableName).Delete(&torrent).Error != nil {
|
if db.ORM.Delete(&torrent).Error != nil {
|
||||||
return http.StatusInternalServerError, errors.New("Torrent is not deleted.")
|
return http.StatusInternalServerError, errors.New("Torrent was not deleted.")
|
||||||
}
|
}
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateTorrent(torrent model.Torrent) (int, error) {
|
func UpdateTorrent(torrent model.Torrent) (int, error) {
|
||||||
if db.ORM.Table(config.TorrentsTableName).Save(torrent).Error != nil {
|
if db.ORM.Save(torrent).Error != nil {
|
||||||
return http.StatusInternalServerError, errors.New("Torrent is not updated.")
|
return http.StatusInternalServerError, errors.New("Torrent was not updated.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/NyaaPantsu/nyaa/config"
|
|
||||||
"github.com/NyaaPantsu/nyaa/db"
|
"github.com/NyaaPantsu/nyaa/db"
|
||||||
"github.com/NyaaPantsu/nyaa/model"
|
"github.com/NyaaPantsu/nyaa/model"
|
||||||
formStruct "github.com/NyaaPantsu/nyaa/service/user/form"
|
formStruct "github.com/NyaaPantsu/nyaa/service/user/form"
|
||||||
|
@ -129,7 +128,7 @@ func RetrieveUser(r *http.Request, id string) (*model.PublicUser, bool, uint, in
|
||||||
var currentUserID uint
|
var currentUserID uint
|
||||||
var isAuthor bool
|
var isAuthor bool
|
||||||
|
|
||||||
if db.ORM.Table(config.TorrentsTableName).First(&user, id).RecordNotFound() {
|
if db.ORM.First(&user, id).RecordNotFound() {
|
||||||
return nil, isAuthor, currentUserID, http.StatusNotFound, errors.New("user not found")
|
return nil, isAuthor, currentUserID, http.StatusNotFound, errors.New("user not found")
|
||||||
}
|
}
|
||||||
currentUser, err := CurrentUser(r)
|
currentUser, err := CurrentUser(r)
|
||||||
|
@ -268,7 +267,7 @@ func RetrieveUserByUsername(username string) (*model.PublicUser, string, int, er
|
||||||
func RetrieveOldUploadsByUsername(username string) ([]uint, error) {
|
func RetrieveOldUploadsByUsername(username string) ([]uint, error) {
|
||||||
var ret []uint
|
var ret []uint
|
||||||
var tmp []*model.UserUploadsOld
|
var tmp []*model.UserUploadsOld
|
||||||
err := db.ORM.Table(config.UploadsOldTableName).Where("username = ?", username).Find(&tmp).Error
|
err := db.ORM.Where("username = ?", username).Find(&tmp).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
Référencer dans un nouveau ticket