Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0

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 à :
sfan5 2017-05-20 16:26:22 +02:00 révisé par ewhal
Parent f61bff5fac
révision 4c24318cb3
8 fichiers modifiés avec 42 ajouts et 29 suppressions

Voir le fichier

@ -14,16 +14,18 @@ const (
// that was copied from the original Nyaa
LastOldTorrentID = 923000
TorrentsTableName = "torrents"
ReportsTableName = "torrent_reports"
CommentsTableName = "comments"
UploadsOldTableName = "user_uploads_old"
FilesTableName = "files"
// for sukebei:
//LastOldTorrentID = 2303945
//TorrentsTableName = "sukebei_torrents"
//ReportsTableName = "sukebei_torrent_reports"
//CommentsTableName = "sukebei_comments"
//UploadsOldTableName = "sukebei_user_uploads_old"
// FIXME: files table needs to be seperate too
//FilesTableName = "sukebei_files"
)
func IsSukebei() bool {

Voir le fichier

@ -1,6 +1,7 @@
package model
import (
"github.com/NyaaPantsu/nyaa/config"
"github.com/zeebo/bencode"
)
@ -12,6 +13,10 @@ type File struct {
Filesize int64 `gorm:"column:filesize"`
}
func (f File) TableName() string {
return config.FilesTableName
}
// Returns the total size of memory allocated for this struct
func (f File) Size() int {
return (2 + len(f.BencodedPath) + 1) * 8;

Voir le fichier

@ -2,11 +2,11 @@ package model
import (
"time"
"github.com/NyaaPantsu/nyaa/config"
)
// TODO Add field to specify kind of reports
// TODO Add CreatedAt field
// INFO User can be null (anonymous reports)
// 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"`
@ -20,6 +20,10 @@ type TorrentReport struct {
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"`
@ -43,9 +47,8 @@ func getReportDescription(d string) string {
}
func (report *TorrentReport) ToJson() TorrentReportJson {
// FIXME: report.Torrent and report.User should never be nil
var t TorrentJSON = TorrentJSON{}
if report.Torrent != nil {
if report.Torrent != nil { // FIXME: report.Torrent should never be nil
t = report.Torrent.ToJSON()
}
var u UserJSON = UserJSON{}

Voir le fichier

@ -2,7 +2,7 @@ package router
import (
"html/template"
"log"
//"log"
"math"
"net/url"
"strconv"
@ -109,11 +109,14 @@ var FuncMap = template.FuncMap{
if nav.CurrentPage > pagesSelectable/2 {
startValue = (int(math.Min((float64(nav.CurrentPage)+math.Floor(float64(pagesSelectable)/2)), maxPages)) - pagesSelectable + 1)
}
if startValue < 1 {
startValue = 1
}
endValue := (startValue + pagesSelectable - 1)
if endValue > int(maxPages) {
endValue = int(maxPages)
}
log.Println(nav.TotalItem)
//log.Println(nav.TotalItem)
for i := startValue; i <= endValue; i++ {
pageNum := strconv.Itoa(i)
url, _ := Router.Get(nav.Route).URL("page", pageNum)

Voir le fichier

@ -6,7 +6,6 @@ import (
"strconv"
"time"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/service/captcha"
@ -59,7 +58,7 @@ func UploadPostHandler(w http.ResponseWriter, r *http.Request) {
}
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 {
// add to db and redirect
torrent := model.Torrent{
@ -72,7 +71,7 @@ func UploadPostHandler(w http.ResponseWriter, r *http.Request) {
Filesize: uploadForm.Filesize,
Description: uploadForm.Description,
UploaderID: user.ID}
db.ORM.Table(config.TorrentsTableName).Create(&torrent)
db.ORM.Create(&torrent)
// add filelist to files db, if we have one
if len(uploadForm.FileList) > 0 {

Voir le fichier

@ -1,7 +1,6 @@
package commentService
import (
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
)
@ -9,7 +8,7 @@ import (
func GetAllComments(limit int, offset int, conditions string, values ...interface{}) ([]model.Comment, int) {
var comments []model.Comment
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)
return comments, nbComments
}

Voir le fichier

@ -54,15 +54,18 @@ func GetTorrentById(id string) (torrent model.Torrent, err error) {
return
}
tmp := db.ORM.Table(config.TorrentsTableName).Where("torrent_id = ?", id).Preload("Comments").Preload("FileList")
err = tmp.Error
if err != nil {
return
tmp := db.ORM.Where("torrent_id = ?", id).Preload("Comments")
if id_int > config.LastOldTorrentID {
tmp = tmp.Preload("FileList")
}
if id_int <= config.LastOldTorrentID && !config.IsSukebei() {
// only preload old comments if they could actually exist
tmp = tmp.Preload("OldComments")
}
err = tmp.Error
if err != nil {
return
}
if tmp.Find(&torrent).RecordNotFound() {
err = errors.New("Article is not found.")
return
@ -74,7 +77,7 @@ func GetTorrentById(id string) (torrent model.Torrent, err error) {
torrent.OldUploader = ""
if torrent.ID <= config.LastOldTorrentID && torrent.UploaderID == 0 {
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
}
}
@ -92,7 +95,7 @@ func GetTorrentById(id string) (torrent model.Torrent, err error) {
// won't fetch user or comments
func GetRawTorrentById(id uint) (torrent model.Torrent, err error) {
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.")
}
return
@ -128,7 +131,7 @@ func getTorrentsOrderBy(parameters *serviceBase.WhereParams, orderBy string, lim
conditions := strings.Join(conditionArray, " AND ")
if countAll {
// 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 {
return
}
@ -189,18 +192,18 @@ func GetAllTorrentsDB() ([]model.Torrent, int, error) {
func DeleteTorrent(id string) (int, error) {
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.")
}
if db.ORM.Table(config.TorrentsTableName).Delete(&torrent).Error != nil {
return http.StatusInternalServerError, errors.New("Torrent is not deleted.")
if db.ORM.Delete(&torrent).Error != nil {
return http.StatusInternalServerError, errors.New("Torrent was not deleted.")
}
return http.StatusOK, nil
}
func UpdateTorrent(torrent model.Torrent) (int, error) {
if db.ORM.Table(config.TorrentsTableName).Save(torrent).Error != nil {
return http.StatusInternalServerError, errors.New("Torrent is not updated.")
if db.ORM.Save(torrent).Error != nil {
return http.StatusInternalServerError, errors.New("Torrent was not updated.")
}
return http.StatusOK, nil

Voir le fichier

@ -7,7 +7,6 @@ import (
"strconv"
"time"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
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 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")
}
currentUser, err := CurrentUser(r)
@ -268,7 +267,7 @@ func RetrieveUserByUsername(username string) (*model.PublicUser, string, int, er
func RetrieveOldUploadsByUsername(username string) ([]uint, error) {
var ret []uint
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 {
return ret, err
}