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 (
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
"github.com/ewhal/nyaa/service/user/permission"
"github.com/nicksnyder/go-i18n/i18n"
2017-05-05 06:07:45 +02:00
"html/template"
"log"
"math"
"net/url"
"strconv"
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-05 16:39:15 +02:00
var FuncMap = template . FuncMap {
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-05 17:14:10 +02:00
"genRouteWithQuery" : func ( name string , currentUrl * url . URL , params ... string ) template . HTML {
url , err := Router . Get ( name ) . URL ( params ... )
if err == nil {
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
return template . HTML ( template . HTMLEscapeString ( url . String ( ) + "?" + currentUrl . RawQuery ) ) // TODO: Review application of character escaping
2017-05-05 17:14:10 +02:00
}
return "error"
} ,
2017-05-07 01:20:13 +02:00
"genNav" : func ( nav Navigation , currentUrl * url . URL , pagesSelectable int ) template . HTML {
2017-05-10 16:43:50 +02:00
var ret = ""
if ( nav . TotalItem > 0 ) {
2017-05-05 06:07:45 +02:00
maxPages := math . Ceil ( float64 ( nav . TotalItem ) / float64 ( nav . MaxItemPerPage ) )
2017-05-05 03:53:38 +02:00
2017-05-05 06:07:45 +02:00
if nav . CurrentPage - 1 > 0 {
2017-05-05 16:39:15 +02:00
url , _ := Router . Get ( nav . Route ) . URL ( "page" , "1" )
2017-05-05 06:07:45 +02:00
ret = ret + "<li><a id=\"page-prev\" href=\"" + url . String ( ) + "?" + currentUrl . RawQuery + "\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"
}
startValue := 1
2017-05-10 03:15:29 +02:00
if nav . CurrentPage > pagesSelectable / 2 {
2017-05-05 06:07:45 +02:00
startValue = ( int ( math . Min ( ( float64 ( nav . CurrentPage ) + math . Floor ( float64 ( pagesSelectable ) / 2 ) ) , maxPages ) ) - pagesSelectable + 1 )
}
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
endValue := ( startValue + pagesSelectable - 1 )
2017-05-05 06:07:45 +02:00
if endValue > int ( maxPages ) {
endValue = int ( maxPages )
}
log . Println ( nav . TotalItem )
for i := startValue ; i <= endValue ; i ++ {
pageNum := strconv . Itoa ( i )
2017-05-05 16:39:15 +02:00
url , _ := Router . Get ( nav . Route ) . URL ( "page" , pageNum )
2017-05-05 06:07:45 +02:00
ret = ret + "<li"
if i == nav . CurrentPage {
ret = ret + " class=\"active\""
}
2017-05-05 03:53:38 +02:00
2017-05-05 06:07:45 +02:00
ret = ret + "><a href=\"" + url . String ( ) + "?" + currentUrl . RawQuery + "\">" + strconv . Itoa ( i ) + "</a></li>"
}
if nav . CurrentPage < int ( maxPages ) {
2017-05-05 16:39:15 +02:00
url , _ := Router . Get ( nav . Route ) . URL ( "page" , strconv . Itoa ( nav . CurrentPage + 1 ) )
2017-05-05 06:07:45 +02:00
ret = ret + "<li><a id=\"page-next\" href=\"" + url . String ( ) + "?" + currentUrl . RawQuery + "\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"
}
2017-05-10 16:43:50 +02:00
}
2017-05-05 06:07:45 +02:00
return template . HTML ( ret )
} ,
2017-05-07 02:32:32 +02:00
"T" : i18n . IdentityTfunc ,
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 ,
"GetRole" : userPermission . GetRole ,
"IsFollower" : userPermission . IsFollower ,
2017-05-10 22:03:57 +02:00
"NoEncode" : func ( str string ) template . HTML {
return template . HTML ( str )
} ,
2017-05-05 06:07:45 +02:00
}