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 ) )
2017-09-30 14:50:54 +02:00
variables . Set ( "Theme" , publicSettings . GetThemeFromRequest ( c ) )
variables . Set ( "DarkTheme" , publicSettings . GetDarkThemeFromRequest ( c ) )
2017-08-30 07:21:45 +02:00
variables . Set ( "AltColors" , publicSettings . GetAltColorsFromRequest ( c ) )
2017-09-11 19:45:39 +02:00
variables . Set ( "OldNav" , publicSettings . GetOldNavFromRequest ( c ) )
2017-07-16 00:51:13 +02:00
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 )
2017-09-06 07:55:00 +02:00
variables . Set ( "EUCookieLaw" , publicSettings . GetEUCookieFromRequest ( c ) )
2017-07-16 00:51:13 +02:00
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 ) ) )
2017-10-04 15:46:06 +02:00
query . Set ( "limit" , "15" )
2017-07-20 20:21:57 +02:00
c . Request . URL . RawQuery = query . Encode ( )
nbTorrents := 0
2017-07-29 18:42:02 +02:00
if userProfile . ID > 0 && currentUser . CurrentOrAdmin ( userProfile . ID ) {
Tag Search + Tests + Search slight refactor [DONE] (#1342)
* Tag Search + Tests + Search slight refactor
First commit improving search. Different struct have their own file with their tests. This way of separating struct by files is inspired by the go packages I've seen so far.
Added new behaviour as discussed in #1334
* fix fallback to ES
* Added some comments to explain PG fallback + log err moved
* Refactored search
Nearly fully covered
WhereParams struct has disappeared for Query struct instead
In DB model, we use an interface implementing Query struct methods
* 1rst Refactor of Tags (WTF already?!)
Prepare Tags for the refactored system. Now there will be descriptive tags for a particular release (ecchi, BDSM, ....) and typed tags.
Typed tags are tags relevant to all torrents and can be limited to some input value. For example, video quality is a typed tag limited to some values (hd, full hd, sd, ...). In the same way, anidbid is also a typed tag but doesn't have default values.
Furthermore, the location storage of tags have changed, now accepted descriptive tags are stored in the torrents table in the column "tags" and they are separated by commas.
In the opposite, accepted typed tags can have have their own column in the torrents table. For example, anidbid, vndbid will populate the column DbID when accepted. On the other hand, videoquality will populate the same way as descriptive tags.
This behaviour depends on the callbackOnType function in tag/helpers.go
* fix for modtools :')
* Added anidb, vndb, dlsite & vmdb id fields in torrent model.
Tags don't have an accepted field anymore.
Accepted Tags are in torrent.AcceptedTags and non-accepted ones in torrrent.Tags.
New Helper + New Changelog for translation string.
* New upload/edit form for torrent tags.
Now the inputs are dynamically generated by the helper tag_form.
No more modal window in those form, only inputs.
Support of tags in API
New translation string for the link to the modal on torrent view.
More comments in the functions for tags
* Improving how config for tags work. Adding a test on them with understandable messages.
Config for tags have now a Field attribute which is linked to the Torrent model. For example anidbid tag type has now a AnidbID field in config which is the name of the field in torrent model (AnidbID). Every new tag type need to have a field attribute with its counterpart in torrent Model.
Fixing some errors
* Fix compile error + Tests Errors
* Improve performance by caching the list of tags with an index
Adding/removing tags works/tested
New translation strings
TODO: test/fix adding tag on upload/edit
* Mini fix to display video quality
+ tags works/tested on modo edit
* Fix editing tags on modpanel
* Edit tags works
* Add translation string
* Add search backend for tags.
?tags=xxx,eee,ddd
?anidb=21
?vndb=23
?vgmdb=24
?vq=full_hd
* Fix Ajax tag Removal&Add
* Added form for descriptive tags
* Forgot to add the link between database and form for descriptive tags.
* Adding the increase/decrease pantsu for descriptive tags
* Fix #1370
* When you actually forgot to commit files after having forgotten commits
2017-08-22 03:48:10 +02:00
_ , userProfile . Torrents , nbTorrents , _ = search . ByQuery ( c , 1 , true , false , false )
2017-07-20 20:21:57 +02:00
} else {
Tag Search + Tests + Search slight refactor [DONE] (#1342)
* Tag Search + Tests + Search slight refactor
First commit improving search. Different struct have their own file with their tests. This way of separating struct by files is inspired by the go packages I've seen so far.
Added new behaviour as discussed in #1334
* fix fallback to ES
* Added some comments to explain PG fallback + log err moved
* Refactored search
Nearly fully covered
WhereParams struct has disappeared for Query struct instead
In DB model, we use an interface implementing Query struct methods
* 1rst Refactor of Tags (WTF already?!)
Prepare Tags for the refactored system. Now there will be descriptive tags for a particular release (ecchi, BDSM, ....) and typed tags.
Typed tags are tags relevant to all torrents and can be limited to some input value. For example, video quality is a typed tag limited to some values (hd, full hd, sd, ...). In the same way, anidbid is also a typed tag but doesn't have default values.
Furthermore, the location storage of tags have changed, now accepted descriptive tags are stored in the torrents table in the column "tags" and they are separated by commas.
In the opposite, accepted typed tags can have have their own column in the torrents table. For example, anidbid, vndbid will populate the column DbID when accepted. On the other hand, videoquality will populate the same way as descriptive tags.
This behaviour depends on the callbackOnType function in tag/helpers.go
* fix for modtools :')
* Added anidb, vndb, dlsite & vmdb id fields in torrent model.
Tags don't have an accepted field anymore.
Accepted Tags are in torrent.AcceptedTags and non-accepted ones in torrrent.Tags.
New Helper + New Changelog for translation string.
* New upload/edit form for torrent tags.
Now the inputs are dynamically generated by the helper tag_form.
No more modal window in those form, only inputs.
Support of tags in API
New translation string for the link to the modal on torrent view.
More comments in the functions for tags
* Improving how config for tags work. Adding a test on them with understandable messages.
Config for tags have now a Field attribute which is linked to the Torrent model. For example anidbid tag type has now a AnidbID field in config which is the name of the field in torrent model (AnidbID). Every new tag type need to have a field attribute with its counterpart in torrent Model.
Fixing some errors
* Fix compile error + Tests Errors
* Improve performance by caching the list of tags with an index
Adding/removing tags works/tested
New translation strings
TODO: test/fix adding tag on upload/edit
* Mini fix to display video quality
+ tags works/tested on modo edit
* Fix editing tags on modpanel
* Edit tags works
* Add translation string
* Add search backend for tags.
?tags=xxx,eee,ddd
?anidb=21
?vndb=23
?vgmdb=24
?vq=full_hd
* Fix Ajax tag Removal&Add
* Added form for descriptive tags
* Forgot to add the link between database and form for descriptive tags.
* Adding the increase/decrease pantsu for descriptive tags
* Fix #1370
* When you actually forgot to commit files after having forgotten commits
2017-08-22 03:48:10 +02:00
_ , userProfile . Torrents , nbTorrents , _ = search . ByQuery ( c , 1 , true , false , true )
2017-07-20 20:21:57 +02:00
}
2017-10-31 01:34:23 +01:00
var uploadedSize int64
for _ , torrent := range userProfile . Torrents {
2017-10-31 01:38:47 +01:00
uploadedSize += torrent . Filesize
2017-10-31 01:34:23 +01:00
}
2017-07-20 20:21:57 +02:00
variables . Set ( "UserProfile" , userProfile )
2017-10-31 01:34:23 +01:00
variables . Set ( "NbTorrents" , [ ] int64 { int64 ( nbTorrents ) , uploadedSize } )
2017-07-20 20:21:57 +02:00
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
}