2017-05-05 16:39:15 +02:00
package router
2017-05-05 03:53:38 +02:00
2017-05-05 06:07:45 +02:00
import (
"html/template"
"math"
"net/url"
2017-06-17 04:24:30 +02:00
"sort"
2017-05-05 06:07:45 +02:00
"strconv"
2017-05-17 16:05:42 +02:00
"time"
2017-05-14 21:45:50 +02:00
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/config"
2017-05-30 00:28:21 +02:00
"github.com/NyaaPantsu/nyaa/model"
2017-06-15 04:44:46 +02:00
"github.com/NyaaPantsu/nyaa/service/activity"
"github.com/NyaaPantsu/nyaa/service/torrent"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/service/user/permission"
2017-05-21 14:34:32 +02:00
"github.com/NyaaPantsu/nyaa/util"
2017-05-22 18:25:04 +02:00
"github.com/NyaaPantsu/nyaa/util/categories"
2017-05-24 16:13:56 +02:00
"github.com/NyaaPantsu/nyaa/util/filelist"
2017-05-27 19:08:47 +02:00
"github.com/NyaaPantsu/nyaa/util/publicSettings"
2017-06-12 01:14:26 +02:00
"github.com/NyaaPantsu/nyaa/util/torrentLanguages"
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
)
2017-05-05 03:53:38 +02:00
2017-05-21 04:10:31 +02:00
type captchaData struct {
CaptchaID string
2017-05-27 19:08:47 +02:00
T publicSettings . TemplateTfunc
2017-05-21 04:10:31 +02:00
}
2017-05-25 21:54:58 +02:00
// FuncMap : Functions accessible in templates by {{ $.Function }}
2017-05-05 16:39:15 +02:00
var FuncMap = template . FuncMap {
2017-05-17 13:16:40 +02:00
"inc" : func ( i int ) int {
return i + 1
} ,
2017-05-05 06:07:45 +02:00
"min" : math . Min ,
"genRoute" : func ( name string , params ... string ) string {
2017-05-05 16:39:15 +02:00
url , err := Router . Get ( name ) . URL ( params ... )
2017-05-05 06:07:45 +02:00
if err == nil {
return url . String ( )
}
return "error"
} ,
2017-05-12 12:08:38 +02:00
"genRouteWithQuery" : func ( name string , currentUrl * url . URL , params ... string ) template . URL {
2017-05-05 17:14:10 +02:00
url , err := Router . Get ( name ) . URL ( params ... )
if err == nil {
2017-05-12 12:08:38 +02:00
return template . URL ( url . String ( ) + "?" + currentUrl . RawQuery )
2017-05-05 17:14:10 +02:00
}
return "error"
} ,
2017-05-13 00:31:27 +02:00
"genViewTorrentRoute" : func ( torrent_id uint ) string {
// Helper for when you have an uint while genRoute("view_torrent", ...) takes a string
// FIXME better solution?
s := strconv . FormatUint ( uint64 ( torrent_id ) , 10 )
url , err := Router . Get ( "view_torrent" ) . URL ( "id" , s )
if err == nil {
return url . String ( )
}
return "error"
} ,
2017-05-13 18:53:54 +02:00
"genSearchWithOrdering" : func ( currentUrl url . URL , sortBy string ) template . URL {
values := currentUrl . Query ( )
2017-05-20 01:10:16 +02:00
order := false //Default is DESC
sort := "2" //Default is Date (Actually ID, but Date is the same thing)
2017-05-18 01:03:11 +02:00
2017-05-13 18:53:54 +02:00
if _ , ok := values [ "order" ] ; ok {
order , _ = strconv . ParseBool ( values [ "order" ] [ 0 ] )
}
2017-05-18 01:03:11 +02:00
if _ , ok := values [ "sort" ] ; ok {
sort = values [ "sort" ] [ 0 ]
}
2017-05-20 01:10:16 +02:00
2017-05-18 01:03:11 +02:00
if sort == sortBy {
order = ! order //Flip order by repeat-clicking
} else {
order = false //Default to descending when sorting by something new
}
2017-05-13 18:53:54 +02:00
values . Set ( "sort" , sortBy )
values . Set ( "order" , strconv . FormatBool ( order ) )
2017-05-14 14:44:39 +02:00
url , _ := Router . Get ( "search" ) . URL ( )
2017-05-14 15:26:25 +02:00
url . RawQuery = values . Encode ( )
2017-05-14 14:30:09 +02:00
2017-05-14 14:44:39 +02:00
return template . URL ( url . String ( ) )
2017-05-13 18:53:54 +02:00
} ,
2017-05-15 02:01:04 +02:00
"genSortArrows" : func ( currentUrl url . URL , sortBy string ) template . HTML {
values := currentUrl . Query ( )
leftclass := "sortarrowdim"
rightclass := "sortarrowdim"
2017-05-18 01:03:11 +02:00
order := false
sort := "2"
2017-05-15 02:01:04 +02:00
if _ , ok := values [ "order" ] ; ok {
2017-05-18 01:03:11 +02:00
order , _ = strconv . ParseBool ( values [ "order" ] [ 0 ] )
}
if _ , ok := values [ "sort" ] ; ok {
sort = values [ "sort" ] [ 0 ]
}
if sort == sortBy {
if order {
rightclass = ""
} else {
leftclass = ""
2017-05-15 02:01:04 +02:00
}
}
2017-05-15 11:08:17 +02:00
arrows := "<span class=\"sortarrowleft " + leftclass + "\">▼</span><span class=\"" + rightclass + "\">▲</span>"
2017-05-15 02:01:04 +02:00
return template . HTML ( arrows )
} ,
2017-05-25 21:54:58 +02:00
"genNav" : func ( nav navigation , currentUrl * url . URL , pagesSelectable int ) template . HTML {
2017-05-10 16:43:50 +02:00
var ret = ""
2017-05-14 14:30:09 +02:00
if nav . TotalItem > 0 {
maxPages := math . Ceil ( float64 ( nav . TotalItem ) / float64 ( nav . MaxItemPerPage ) )
2017-05-05 03:53:38 +02:00
2017-05-14 14:30:09 +02:00
if nav . CurrentPage - 1 > 0 {
url , _ := Router . Get ( nav . Route ) . URL ( "page" , "1" )
2017-05-27 09:16:43 +02:00
ret = ret + "<a id=\"page-prev\" href=\"" + url . String ( ) + "?" + currentUrl . RawQuery + "\" aria-label=\"Previous\"><li><span aria-hidden=\"true\">«</span></li></a>"
2017-05-14 14:30:09 +02:00
}
startValue := 1
if nav . CurrentPage > pagesSelectable / 2 {
startValue = ( int ( math . Min ( ( float64 ( nav . CurrentPage ) + math . Floor ( float64 ( pagesSelectable ) / 2 ) ) , maxPages ) ) - pagesSelectable + 1 )
2017-05-05 06:07:45 +02:00
}
2017-05-20 16:26:22 +02:00
if startValue < 1 {
startValue = 1
}
2017-05-14 14:30:09 +02:00
endValue := ( startValue + pagesSelectable - 1 )
if endValue > int ( maxPages ) {
endValue = int ( maxPages )
}
for i := startValue ; i <= endValue ; i ++ {
pageNum := strconv . Itoa ( i )
url , _ := Router . Get ( nav . Route ) . URL ( "page" , pageNum )
2017-06-06 22:38:43 +02:00
ret = ret + "<a aria-label=\"Page " + strconv . Itoa ( i ) + "\" href=\"" + url . String ( ) + "?" + currentUrl . RawQuery + "\">" + "<li"
2017-05-14 14:30:09 +02:00
if i == nav . CurrentPage {
ret = ret + " class=\"active\""
}
2017-05-27 09:16:43 +02:00
ret = ret + ">" + strconv . Itoa ( i ) + "</li></a>"
2017-05-14 14:30:09 +02:00
}
if nav . CurrentPage < int ( maxPages ) {
url , _ := Router . Get ( nav . Route ) . URL ( "page" , strconv . Itoa ( nav . CurrentPage + 1 ) )
2017-05-27 09:16:43 +02:00
ret = ret + "<a id=\"page-next\" href=\"" + url . String ( ) + "?" + currentUrl . RawQuery + "\" aria-label=\"Next\"><li><span aria-hidden=\"true\">»</span></li></a>"
2017-05-14 14:30:09 +02:00
}
2017-05-25 21:54:58 +02:00
itemsThisPageStart := nav . MaxItemPerPage * ( nav . CurrentPage - 1 ) + 1
2017-05-24 16:14:24 +02:00
itemsThisPageEnd := nav . MaxItemPerPage * nav . CurrentPage
if nav . TotalItem < itemsThisPageEnd {
itemsThisPageEnd = nav . TotalItem
}
ret = ret + "<p>" + strconv . Itoa ( itemsThisPageStart ) + "-" + strconv . Itoa ( itemsThisPageEnd ) + "/" + strconv . Itoa ( nav . TotalItem ) + "</p>"
2017-05-10 16:43:50 +02:00
}
2017-05-05 06:07:45 +02:00
return template . HTML ( ret )
} ,
2017-05-20 12:45:27 +02:00
"Sukebei" : config . IsSukebei ,
2017-05-27 19:08:47 +02:00
"getDefaultLanguage" : publicSettings . GetDefaultLanguage ,
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
"getAvatar" : func ( hash string , size int ) string {
return "https://www.gravatar.com/avatar/" + hash + "?s=" + strconv . Itoa ( size )
2017-05-09 03:36:48 +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
"CurrentOrAdmin" : userPermission . CurrentOrAdmin ,
2017-05-09 03:36:48 +02:00
"CurrentUserIdentical" : userPermission . CurrentUserIdentical ,
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
"HasAdmin" : userPermission . HasAdmin ,
2017-05-12 11:58:22 +02:00
"NeedsCaptcha" : userPermission . NeedsCaptcha ,
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
"GetRole" : userPermission . GetRole ,
"IsFollower" : userPermission . IsFollower ,
2017-06-21 03:58:54 +02:00
"DisplayTorrent" : func ( t model . Torrent , u model . User ) bool {
return ( ( ! t . Hidden && t . Status != 0 ) || userPermission . CurrentOrAdmin ( & u , t . UploaderID ) )
} ,
2017-05-10 22:03:57 +02:00
"NoEncode" : func ( str string ) template . HTML {
return template . HTML ( str )
} ,
2017-05-11 03:00:57 +02:00
"calcWidthSeed" : func ( seed uint32 , leech uint32 ) float64 {
2017-05-14 14:30:09 +02:00
return float64 ( float64 ( seed ) / ( float64 ( seed ) + float64 ( leech ) ) ) * 100
2017-05-11 03:00:57 +02:00
} ,
"calcWidthLeech" : func ( seed uint32 , leech uint32 ) float64 {
2017-05-14 14:30:09 +02:00
return float64 ( float64 ( leech ) / ( float64 ( seed ) + float64 ( leech ) ) ) * 100
2017-05-11 03:00:57 +02:00
} ,
2017-05-17 16:05:42 +02:00
"formatDateRFC" : func ( t time . Time ) string {
// because time.* isn't available in templates...
return t . Format ( time . RFC3339 )
} ,
2017-06-03 03:45:24 +02:00
"GetHostname" : util . GetHostname ,
2017-06-17 04:24:30 +02:00
"GetCategories" : func ( keepParent bool , keepChild bool ) map [ string ] string {
return categories . GetCategoriesSelect ( keepParent , keepChild )
} ,
"GetCategory" : func ( category string , keepParent bool ) ( categoryRet map [ string ] string ) {
cat := categories . GetCategoriesSelect ( true , true )
var keys [ ] string
for name := range cat {
keys = append ( keys , name )
}
sort . Strings ( keys )
found := false
categoryRet = make ( map [ string ] string )
for _ , key := range keys {
if cat [ key ] == category + "_" {
found = true
if keepParent {
categoryRet [ key ] = cat [ key ]
}
} else if len ( cat [ key ] ) <= 2 && len ( categoryRet ) > 0 {
break
} else if found {
categoryRet [ key ] = cat [ key ]
}
}
return
2017-05-24 09:11:13 +02:00
} ,
"CategoryName" : func ( category string , sub_category string ) string {
2017-05-22 18:25:04 +02:00
s := category + "_" + sub_category
if category , ok := categories . GetCategories ( ) [ s ] ; ok {
return category
}
2017-05-25 21:54:58 +02:00
return ""
} ,
2017-06-12 01:14:26 +02:00
"GetTorrentLanguages" : torrentLanguages . GetTorrentLanguages ,
"LanguageName" : func ( code string , T publicSettings . TemplateTfunc ) template . HTML {
if code == "other" || code == "multiple" {
return T ( "language_" + code + "_name" )
}
if ! torrentLanguages . LanguageExists ( code ) {
return T ( "unknown" )
}
return T ( "language_" + code + "_name" )
} ,
"FlagCode" : func ( languageCode string ) string {
if languageCode == "other" || languageCode == "multiple" {
return languageCode
}
return torrentLanguages . FlagFromLanguage ( languageCode )
} ,
2017-05-27 19:08:47 +02:00
"fileSize" : func ( filesize int64 , T publicSettings . TemplateTfunc ) template . HTML {
2017-05-26 03:53:18 +02:00
if filesize == 0 {
return T ( "unknown" )
}
return template . HTML ( util . FormatFilesize ( filesize ) )
} ,
2017-05-27 19:08:47 +02:00
"makeCaptchaData" : func ( captchaID string , T publicSettings . TemplateTfunc ) captchaData {
2017-05-21 04:10:31 +02:00
return captchaData { captchaID , T }
} ,
2017-05-24 09:11:13 +02:00
"DefaultUserSettings" : func ( s string ) bool {
2017-05-31 04:21:57 +02:00
return config . Conf . Users . DefaultUserSettings [ s ]
2017-05-22 00:22:42 +02:00
} ,
2017-05-27 19:08:47 +02:00
"makeTreeViewData" : func ( f * filelist . FileListFolder , nestLevel int , T publicSettings . TemplateTfunc , identifierChain string ) interface { } {
2017-05-30 00:28:21 +02:00
return struct {
Folder * filelist . FileListFolder
NestLevel int
T publicSettings . TemplateTfunc
2017-05-26 03:53:18 +02:00
IdentifierChain string
2017-05-30 00:28:21 +02:00
} { f , nestLevel , T , identifierChain }
} ,
"lastID" : func ( currentUrl url . URL , torrents [ ] model . TorrentJSON ) int {
values := currentUrl . Query ( )
order := false
sort := "2"
if _ , ok := values [ "order" ] ; ok {
order , _ = strconv . ParseBool ( values [ "order" ] [ 0 ] )
}
if _ , ok := values [ "sort" ] ; ok {
sort = values [ "sort" ] [ 0 ]
}
lastID := 0
if sort == "2" || sort == "" {
if order {
lastID = int ( torrents [ len ( torrents ) - 1 ] . ID )
2017-05-31 21:07:56 +02:00
} else if len ( torrents ) > 0 {
2017-05-30 00:28:21 +02:00
lastID = int ( torrents [ 0 ] . ID )
}
}
return lastID
2017-05-24 16:13:56 +02:00
} ,
2017-06-04 02:28:33 +02:00
"getReportDescription" : func ( d string , T publicSettings . TemplateTfunc ) 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 string ( T ( d ) )
} ,
2017-06-06 00:06:52 +02:00
"genUploaderLink" : func ( uploaderID uint , uploaderName template . HTML , torrentHidden bool ) template . HTML {
2017-06-15 04:44:46 +02:00
uploaderID , username := torrentService . HideTorrentUser ( uploaderID , string ( uploaderName ) , torrentHidden )
2017-06-06 00:06:04 +02:00
if uploaderID == 0 {
2017-06-15 04:44:46 +02:00
return template . HTML ( username )
2017-06-06 00:06:04 +02:00
}
2017-06-15 04:44:46 +02:00
url , err := Router . Get ( "user_profile" ) . URL ( "id" , strconv . Itoa ( int ( uploaderID ) ) , "username" , username )
2017-06-06 00:06:04 +02:00
if err != nil {
return "error"
}
2017-06-15 04:44:46 +02:00
return template . HTML ( "<a href=\"" + url . String ( ) + "\">" + username + "</a>" )
} ,
"genActivityContent" : func ( a model . Activity , T publicSettings . TemplateTfunc ) template . HTML {
return activity . ToLocale ( & a , T )
2017-06-06 00:06:04 +02:00
} ,
2017-05-05 06:07:45 +02:00
}