2017-05-05 14:20:51 +02:00
package model
import (
"github.com/ewhal/nyaa/config"
2017-05-06 04:18:24 +02:00
"github.com/ewhal/nyaa/util"
2017-05-05 14:20:51 +02:00
2017-05-09 16:42:12 +02:00
"fmt"
2017-05-05 14:20:51 +02:00
"html/template"
"strconv"
"strings"
"time"
)
type Feed struct {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
ID int
2017-05-05 14:20:51 +02:00
Name string
Hash string
Magnet string
Timestamp string
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
type Torrent struct {
ID uint ` gorm:"column:torrent_id;primary_key" `
Name string ` gorm:"column:torrent_name" `
Hash string ` gorm:"column:torrent_hash" `
Category int ` gorm:"column:category" `
SubCategory int ` gorm:"column:sub_category" `
Status int ` gorm:"column:status" `
Date time . Time ` gorm:"column:date" `
UploaderID uint ` gorm:"column:uploader" `
Downloads int ` gorm:"column:downloads" `
Stardom int ` gorm:"column:stardom" `
Filesize int64 ` gorm:"column:filesize" `
Description string ` gorm:"column:description" `
WebsiteLink string ` gorm:"column:website_link" `
2017-05-10 10:47:06 +02:00
DeletedAt * time . Time
2017-05-08 19:26:29 +02:00
2017-05-09 12:35:46 +02:00
Uploader * User ` gorm:"ForeignKey:UploaderId" `
2017-05-10 13:32:45 +02:00
OldUploader string ` gorm:"-" ` // ???????
2017-05-09 01:56:57 +02:00
OldComments [ ] OldComment ` gorm:"ForeignKey:torrent_id" `
Comments [ ] Comment ` gorm:"ForeignKey:torrent_id" `
2017-05-10 10:27:17 +02:00
}
// Returns the total size of memory recursively allocated for this struct
2017-05-10 13:09:23 +02:00
// FIXME: doesn't go have sizeof or something nicer for this?
2017-05-10 10:47:06 +02:00
func ( t Torrent ) Size ( ) ( s int ) {
s += 8 + // ints
2 * 3 + // time.Time
2 + // pointers
4 * 2 + // string pointers
2017-05-10 10:27:17 +02:00
// string array sizes
len ( t . Name ) + len ( t . Hash ) + len ( t . Description ) + len ( t . WebsiteLink ) +
2017-05-10 10:47:06 +02:00
2 * 2 // array pointers
2017-05-10 10:27:17 +02:00
s *= 8 // Assume 64 bit OS
2017-05-10 11:32:49 +02:00
if t . Uploader != nil {
s += t . Uploader . Size ( )
}
2017-05-10 10:27:17 +02:00
for _ , c := range t . OldComments {
s += c . Size ( )
}
for _ , c := range t . Comments {
s += c . Size ( )
}
2017-05-08 19:26:29 +02:00
2017-05-10 10:27:17 +02:00
return
2017-05-10 11:08:02 +02:00
}
2017-05-10 06:20:45 +02:00
// 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 {
2017-05-10 11:08:02 +02:00
ID uint ` gorm:"column:torrent_report_id;primary_key" `
Description string ` gorm:"column:type" `
TorrentID uint
UserID uint
Torrent Torrent ` gorm:"AssociationForeignKey:TorrentID;ForeignKey:ID" `
User User ` gorm:"AssociationForeignKey:UserID;ForeignKey:ID" `
2017-05-05 14:20:51 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
/ * We need a JSON object instead of a Gorm structure because magnet URLs are
not in the database and have to be generated dynamically * /
2017-05-05 14:20:51 +02:00
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
type ApiResultJSON struct {
Torrents [ ] TorrentJSON ` json:"torrents" `
QueryRecordCount int ` json:"queryRecordCount" `
TotalRecordCount int ` json:"totalRecordCount" `
2017-05-05 14:20:51 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
type CommentJSON struct {
2017-05-08 04:06:11 +02:00
Username string ` json:"username" `
2017-05-08 20:07:25 +02:00
Content template . HTML ` json:"content" `
Date time . Time ` json:"date" `
2017-05-08 04:06:11 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
type TorrentJSON struct {
ID string ` json:"id" `
Name string ` json:"name" `
Status int ` json:"status" `
Hash string ` json:"hash" `
Date string ` json:"date" `
Filesize string ` json:"filesize" `
Description template . HTML ` json:"description" `
Comments [ ] CommentJSON ` json:"comments" `
SubCategory string ` json:"sub_category" `
Category string ` json:"category" `
Downloads int ` json:"downloads" `
UploaderID uint ` json:"uploader_id" `
UploaderName template . HTML ` json:"uploader_name" `
2017-05-10 13:32:45 +02:00
OldUploader template . HTML ` json:"uploader_old" `
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
WebsiteLink template . URL ` json:"website_link" `
Magnet template . URL ` json:"magnet" `
TorrentLink template . URL ` json:"torrent" `
2017-05-05 14:20:51 +02:00
}
2017-05-10 06:20:45 +02:00
type TorrentReportJson struct {
2017-05-10 11:08:02 +02:00
ID uint ` json:"id" `
Description string ` json:"description" `
Torrent TorrentJSON ` json:"torrent" `
2017-05-10 09:57:55 +02:00
User string
2017-05-10 06:20:45 +02:00
}
/* Model Conversion to Json */
func ( report * TorrentReport ) ToJson ( ) TorrentReportJson {
2017-05-10 09:57:55 +02:00
json := TorrentReportJson { report . ID , report . Description , report . Torrent . ToJSON ( ) , report . User . Username }
2017-05-10 06:20:45 +02:00
return json
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
// ToJSON converts a model.Torrent to its equivalent JSON structure
func ( t * Torrent ) ToJSON ( ) TorrentJSON {
2017-05-07 03:10:35 +02:00
magnet := util . InfoHashToMagnet ( strings . TrimSpace ( t . Hash ) , t . Name , config . Trackers ... )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
commentsJSON := make ( [ ] CommentJSON , 0 , len ( t . OldComments ) + len ( t . Comments ) )
2017-05-08 20:07:25 +02:00
for _ , c := range t . OldComments {
2017-05-10 13:34:25 +02:00
commentsJSON = append ( commentsJSON , CommentJSON { Username : c . Username , Content : template . HTML ( c . Content ) , Date : c . Date } )
2017-05-08 20:07:25 +02:00
}
for _ , c := range t . Comments {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
commentsJSON = append ( commentsJSON , CommentJSON { Username : c . User . Username , Content : util . MarkdownToHTML ( c . Content ) , Date : c . CreatedAt } )
2017-05-08 04:06:11 +02:00
}
2017-05-09 12:35:46 +02:00
uploader := ""
if t . Uploader != nil {
uploader = t . Uploader . Username
}
2017-05-09 16:42:12 +02:00
torrentlink := ""
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if t . ID <= config . LastOldTorrentID && len ( config . TorrentCacheLink ) > 0 {
2017-05-09 16:42:12 +02:00
torrentlink = fmt . Sprintf ( config . TorrentCacheLink , t . Hash )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
} else if t . ID > config . LastOldTorrentID && len ( config . TorrentStorageLink ) > 0 {
torrentlink = fmt . Sprintf ( config . TorrentStorageLink , t . Hash ) // TODO: Fix as part of configuration changes
2017-05-08 04:06:11 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
res := TorrentJSON {
ID : strconv . FormatUint ( uint64 ( t . ID ) , 10 ) ,
2017-05-09 12:35:46 +02:00
Name : t . Name ,
2017-05-05 14:20:51 +02:00
Status : t . Status ,
Hash : t . Hash ,
2017-05-08 19:26:29 +02:00
Date : t . Date . Format ( time . RFC3339 ) ,
2017-05-07 13:51:59 +02:00
Filesize : util . FormatFilesize2 ( t . Filesize ) ,
2017-05-09 09:28:29 +02:00
Description : util . MarkdownToHTML ( t . Description ) ,
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
Comments : commentsJSON ,
SubCategory : strconv . Itoa ( t . SubCategory ) ,
2017-05-06 23:46:53 +02:00
Category : strconv . Itoa ( t . Category ) ,
2017-05-09 08:15:56 +02:00
Downloads : t . Downloads ,
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
UploaderID : t . UploaderID ,
2017-05-09 12:35:46 +02:00
UploaderName : util . SafeText ( uploader ) ,
2017-05-10 13:32:45 +02:00
OldUploader : util . SafeText ( t . OldUploader ) ,
2017-05-09 08:15:56 +02:00
WebsiteLink : util . Safe ( t . WebsiteLink ) ,
2017-05-09 16:42:12 +02:00
Magnet : util . Safe ( magnet ) ,
TorrentLink : util . Safe ( torrentlink ) }
2017-05-05 14:20:51 +02:00
return res
}
/* Complete the functions when necessary... */
2017-05-09 01:56:57 +02:00
// Map Torrents to TorrentsToJSON without reallocations
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
func TorrentsToJSON ( t [ ] Torrent ) [ ] TorrentJSON { // TODO: Convert to singular version
json := make ( [ ] TorrentJSON , len ( t ) )
2017-05-09 01:56:57 +02:00
for i := range t {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
json [ i ] = t [ i ] . ToJSON ( )
2017-05-09 01:56:57 +02:00
}
return json
}
2017-05-10 06:20:45 +02:00
func TorrentReportsToJSON ( reports [ ] TorrentReport ) [ ] TorrentReportJson {
json := make ( [ ] TorrentReportJson , len ( reports ) )
for i := range reports {
json [ i ] = reports [ i ] . ToJson ( )
}
return json
}