2017-07-16 17:20:35 +02:00
package templates
2017-06-28 13:42:38 +02:00
import (
"net/http"
"path"
2017-07-20 20:21:57 +02:00
"strconv"
2017-06-28 13:42:38 +02:00
"github.com/NyaaPantsu/nyaa/config"
2017-06-29 13:15:23 +02:00
"github.com/NyaaPantsu/nyaa/models"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/filelist"
2017-07-20 20:21:57 +02:00
msg "github.com/NyaaPantsu/nyaa/utils/messages"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
2017-07-20 20:21:57 +02:00
"github.com/NyaaPantsu/nyaa/utils/search"
2017-06-28 13:42:38 +02:00
"github.com/gin-gonic/gin"
"github.com/justinas/nosurf"
"fmt"
"github.com/CloudyKit/jet"
2017-07-16 17:20:35 +02:00
"github.com/NyaaPantsu/nyaa/utils/cookies"
2017-07-02 23:53:23 +02:00
"github.com/NyaaPantsu/nyaa/utils/validator/user"
2017-06-28 13:42:38 +02:00
)
// TemplateDir : Variable to the template directory
const TemplateDir = "./templates" // FIXME: Need to be a constant!
// ModeratorDir : Variable to the admin template sub directory
const ModeratorDir = "admin"
// SiteDir : Variable pointing to the site page templates
const SiteDir = "site"
// ErrorsDir : Variable pointing to the errors page templates
const ErrorsDir = "errors"
// View : Jet Template Renderer
var View = jet . NewHTMLSet ( "./templates" )
func init ( ) {
2017-07-10 14:11:05 +02:00
if config . Get ( ) . Environment == "DEVELOPMENT" {
2017-06-28 13:42:38 +02:00
View . SetDevelopmentMode ( true )
fmt . Println ( "Template Live Update enabled" )
}
}
2017-07-19 21:33:07 +02:00
// Commonvariables return a jet.VarMap variable containing the necessary variables to run index layouts
2017-07-16 17:20:35 +02:00
func Commonvariables ( c * gin . Context ) jet . VarMap {
2017-07-04 01:15:43 +02:00
token := nosurf . Token ( c . Request )
2017-07-20 20:21:57 +02:00
messages := msg . GetMessages ( c )
2017-07-16 17:20:35 +02:00
user , _ , _ := cookies . CurrentUser ( c )
2017-07-16 00:51:13 +02:00
variables := templateFunctions ( make ( jet . VarMap ) )
2017-07-16 17:20:35 +02:00
variables . Set ( "Navigation" , NewNavigation ( ) )
variables . Set ( "Search" , NewSearchForm ( c ) )
2017-07-16 00:51:13 +02:00
variables . Set ( "T" , publicSettings . GetTfuncFromRequest ( c ) )
variables . Set ( "Theme" , publicSettings . GetThemeFromRequest ( c ) )
variables . Set ( "Mascot" , publicSettings . GetMascotFromRequest ( c ) )
2017-07-24 07:03:43 +02:00
variables . Set ( "MascotURL" , publicSettings . GetMascotURLFromRequest ( c ) )
2017-07-16 17:20:35 +02:00
variables . Set ( "User" , user )
2017-07-16 00:51:13 +02:00
variables . Set ( "URL" , c . Request . URL )
variables . Set ( "CsrfToken" , token )
variables . Set ( "Config" , config . Get ( ) )
2017-07-20 20:21:57 +02:00
variables . Set ( "Infos" , messages . GetAllInfos ( ) )
variables . Set ( "Errors" , messages . GetAllErrors ( ) )
2017-07-16 00:51:13 +02:00
return variables
2017-06-28 13:42:38 +02:00
}
2017-07-19 21:33:07 +02:00
// NewPanelSearchForm : Helper that creates a search form without items/page field
2017-06-28 13:42:38 +02:00
// these need to be used when the templateVariables don't include `navigation`
2017-07-16 17:20:35 +02:00
func NewPanelSearchForm ( c * gin . Context ) SearchForm {
form := NewSearchForm ( c )
2017-06-28 13:42:38 +02:00
form . ShowItemsPerPage = false
return form
}
2017-07-19 21:33:07 +02:00
// NewPanelCommonvariables return a jet.VarMap variable containing the necessary variables to run index admin layouts
2017-07-16 17:20:35 +02:00
func NewPanelCommonvariables ( c * gin . Context ) jet . VarMap {
common := Commonvariables ( c )
common . Set ( "Search" , NewPanelSearchForm ( c ) )
2017-06-28 13:42:38 +02:00
return common
}
2017-07-19 21:33:07 +02:00
// Render is a function rendering a template
2017-07-16 17:20:35 +02:00
func Render ( c * gin . Context , templateName string , variables jet . VarMap ) {
2017-06-28 13:42:38 +02:00
t , err := View . GetTemplate ( templateName )
if err != nil {
c . AbortWithError ( http . StatusInternalServerError , err )
return
}
2017-07-16 00:51:13 +02:00
if err = t . Execute ( c . Writer , variables , nil ) ; err != nil {
2017-06-28 13:42:38 +02:00
c . AbortWithError ( http . StatusInternalServerError , err )
return
}
}
2017-07-19 21:33:07 +02:00
// HttpError render an error template
2017-07-16 17:20:35 +02:00
func HttpError ( c * gin . Context , errorCode int ) {
2017-07-04 01:15:43 +02:00
switch errorCode {
case http . StatusNotFound :
2017-07-16 17:20:35 +02:00
Static ( c , path . Join ( ErrorsDir , "404.jet.html" ) )
2017-07-04 01:15:43 +02:00
c . AbortWithStatus ( errorCode )
return
case http . StatusBadRequest :
2017-07-16 17:20:35 +02:00
Static ( c , path . Join ( ErrorsDir , "400.jet.html" ) )
2017-07-04 01:15:43 +02:00
c . AbortWithStatus ( errorCode )
return
case http . StatusInternalServerError :
2017-07-16 17:20:35 +02:00
Static ( c , path . Join ( ErrorsDir , "500.jet.html" ) )
2017-07-04 01:15:43 +02:00
c . AbortWithStatus ( errorCode )
2017-06-28 13:42:38 +02:00
return
}
}
2017-07-19 21:33:07 +02:00
// Static render static templates
2017-07-16 17:20:35 +02:00
func Static ( c * gin . Context , templateName string ) {
2017-07-16 00:51:13 +02:00
var variables jet . VarMap
2017-06-28 13:42:38 +02:00
if isAdminTemplate ( templateName ) {
2017-07-16 17:20:35 +02:00
variables = NewPanelCommonvariables ( c )
2017-06-28 13:42:38 +02:00
} else {
2017-07-16 17:20:35 +02:00
variables = Commonvariables ( c )
2017-06-28 13:42:38 +02:00
}
2017-07-16 17:20:35 +02:00
Render ( c , templateName , variables )
2017-06-28 13:42:38 +02:00
}
2017-07-19 21:33:07 +02:00
// ModelList render list models templates
2017-07-16 17:20:35 +02:00
func ModelList ( c * gin . Context , templateName string , models interface { } , nav Navigation , search SearchForm ) {
2017-07-16 00:51:13 +02:00
var variables jet . VarMap
2017-06-28 13:42:38 +02:00
if isAdminTemplate ( templateName ) {
2017-07-16 17:20:35 +02:00
variables = NewPanelCommonvariables ( c )
2017-06-28 13:42:38 +02:00
} else {
2017-07-16 17:20:35 +02:00
variables = Commonvariables ( c )
2017-06-28 13:42:38 +02:00
}
2017-07-16 00:51:13 +02:00
variables . Set ( "Models" , models )
variables . Set ( "Navigation" , nav )
variables . Set ( "Search" , search )
2017-07-16 17:20:35 +02:00
Render ( c , templateName , variables )
2017-06-28 13:42:38 +02:00
}
2017-07-19 21:33:07 +02:00
// Form render a template form
2017-07-16 17:20:35 +02:00
func Form ( c * gin . Context , templateName string , form interface { } ) {
2017-07-16 00:51:13 +02:00
var variables jet . VarMap
2017-06-28 13:42:38 +02:00
if isAdminTemplate ( templateName ) {
2017-07-16 17:20:35 +02:00
variables = NewPanelCommonvariables ( c )
2017-06-28 13:42:38 +02:00
} else {
2017-07-16 17:20:35 +02:00
variables = Commonvariables ( c )
2017-06-28 13:42:38 +02:00
}
2017-07-16 00:51:13 +02:00
variables . Set ( "Form" , form )
2017-07-16 17:20:35 +02:00
Render ( c , templateName , variables )
2017-06-28 13:42:38 +02:00
}
2017-07-19 21:33:07 +02:00
// Torrent render a torrent view template
2017-07-16 17:20:35 +02:00
func Torrent ( c * gin . Context , torrent models . TorrentJSON , rootFolder * filelist . FileListFolder , captchaID string ) {
variables := Commonvariables ( c )
2017-07-16 00:51:13 +02:00
variables . Set ( "Torrent" , torrent )
variables . Set ( "RootFolder" , rootFolder )
variables . Set ( "CaptchaID" , captchaID )
2017-07-20 20:21:57 +02:00
Render ( c , path . Join ( SiteDir , "torrents" , "view.jet.html" ) , variables )
}
// userProfilBase render the base for user profile
func userProfileBase ( c * gin . Context , templateName string , userProfile * models . User , variables jet . VarMap ) {
currentUser , _ , _ := cookies . CurrentUser ( c )
query := c . Request . URL . Query ( )
query . Set ( "userID" , strconv . Itoa ( int ( userProfile . ID ) ) )
query . Set ( "limit" , "20" )
c . Request . URL . RawQuery = query . Encode ( )
nbTorrents := 0
if currentUser . CurrentOrAdmin ( userProfile . ID ) {
_ , userProfile . Torrents , nbTorrents , _ = search . ByQuery ( c , 1 , true , true , false , false )
} else {
_ , userProfile . Torrents , nbTorrents , _ = search . ByQuery ( c , 1 , true , true , false , true )
}
variables . Set ( "UserProfile" , userProfile )
variables . Set ( "NbTorrents" , nbTorrents )
Render ( c , path . Join ( SiteDir , "user" , templateName ) , variables )
2017-06-28 13:42:38 +02:00
}
2017-07-19 21:33:07 +02:00
// UserProfileEdit render a form to edit a profile
2017-07-16 17:20:35 +02:00
func UserProfileEdit ( c * gin . Context , userProfile * models . User , userForm userValidator . UserForm , languages publicSettings . Languages ) {
variables := Commonvariables ( c )
2017-07-16 00:51:13 +02:00
variables . Set ( "UserForm" , userForm )
variables . Set ( "Languages" , languages )
2017-07-20 20:21:57 +02:00
userProfileBase ( c , "edit.jet.html" , userProfile , variables )
2017-06-28 13:42:38 +02:00
}
2017-07-19 21:33:07 +02:00
// UserProfile render a user profile
2017-07-20 13:34:41 +02:00
func UserProfile ( c * gin . Context , userProfile * models . User ) {
2017-07-20 20:21:57 +02:00
userProfileBase ( c , "torrents.jet.html" , userProfile , Commonvariables ( c ) )
2017-06-28 13:42:38 +02:00
}
2017-07-19 21:33:07 +02:00
// UserProfileNotifications render a user profile notifications
2017-07-16 17:20:35 +02:00
func UserProfileNotifications ( c * gin . Context , userProfile * models . User ) {
2017-07-20 20:21:57 +02:00
userProfileBase ( c , "notifications.jet.html" , userProfile , Commonvariables ( c ) )
2017-06-28 13:42:38 +02:00
}
2017-07-19 21:33:07 +02:00
// DatabaseDump render the list of database dumps template
2017-07-16 17:20:35 +02:00
func DatabaseDump ( c * gin . Context , listDumps [ ] models . DatabaseDumpJSON , GPGLink string ) {
variables := Commonvariables ( c )
2017-07-16 00:51:13 +02:00
variables . Set ( "ListDumps" , listDumps )
variables . Set ( "GPGLink" , GPGLink )
2017-07-20 20:21:57 +02:00
Render ( c , path . Join ( SiteDir , "database" , "dumps.jet.html" ) , variables )
2017-06-28 13:42:38 +02:00
}
2017-07-19 21:33:07 +02:00
// PanelAdmin render the panel admin template index
2017-07-16 17:20:35 +02:00
func PanelAdmin ( c * gin . Context , torrent [ ] models . Torrent , reports [ ] models . TorrentReportJSON , users [ ] models . User , comments [ ] models . Comment ) {
variables := NewPanelCommonvariables ( c )
2017-07-16 00:51:13 +02:00
variables . Set ( "Torrents" , torrent )
variables . Set ( "TorrentReports" , reports )
variables . Set ( "Users" , users )
variables . Set ( "Comments" , comments )
2017-07-16 17:20:35 +02:00
Render ( c , path . Join ( ModeratorDir , "index.jet.html" ) , variables )
2017-06-28 13:42:38 +02:00
}
func isAdminTemplate ( templateName string ) bool {
if templateName != "" && len ( templateName ) > len ( ModeratorDir ) {
return templateName [ : 5 ] == ModeratorDir
}
return false
}