forked from NyaaPantsu/nyaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
219 lines (192 loc) · 7.16 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package templates
import (
"net/http"
"path"
"strconv"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/utils/filelist"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
"github.com/NyaaPantsu/nyaa/utils/search"
"github.com/gin-gonic/gin"
"github.com/justinas/nosurf"
"fmt"
"github.com/CloudyKit/jet"
"github.com/NyaaPantsu/nyaa/utils/cookies"
"github.com/NyaaPantsu/nyaa/utils/validator/user"
)
// 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() {
if config.Get().Environment == "DEVELOPMENT" {
View.SetDevelopmentMode(true)
fmt.Println("Template Live Update enabled")
}
}
// Commonvariables return a jet.VarMap variable containing the necessary variables to run index layouts
func Commonvariables(c *gin.Context) jet.VarMap {
token := nosurf.Token(c.Request)
messages := msg.GetMessages(c)
user, _, _ := cookies.CurrentUser(c)
variables := templateFunctions(make(jet.VarMap))
variables.Set("Navigation", NewNavigation())
variables.Set("Search", NewSearchForm(c))
variables.Set("T", publicSettings.GetTfuncFromRequest(c))
variables.Set("Theme", publicSettings.GetThemeFromRequest(c))
variables.Set("Mascot", publicSettings.GetMascotFromRequest(c))
variables.Set("MascotURL", publicSettings.GetMascotUrlFromRequest(c))
variables.Set("User", user)
variables.Set("URL", c.Request.URL)
variables.Set("CsrfToken", token)
variables.Set("Config", config.Get())
variables.Set("Infos", messages.GetAllInfos())
variables.Set("Errors", messages.GetAllErrors())
return variables
}
// NewPanelSearchForm : Helper that creates a search form without items/page field
// these need to be used when the templateVariables don't include `navigation`
func NewPanelSearchForm(c *gin.Context) SearchForm {
form := NewSearchForm(c)
form.ShowItemsPerPage = false
return form
}
// NewPanelCommonvariables return a jet.VarMap variable containing the necessary variables to run index admin layouts
func NewPanelCommonvariables(c *gin.Context) jet.VarMap {
common := Commonvariables(c)
common.Set("Search", NewPanelSearchForm(c))
return common
}
// Render is a function rendering a template
func Render(c *gin.Context, templateName string, variables jet.VarMap) {
t, err := View.GetTemplate(templateName)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if err = t.Execute(c.Writer, variables, nil); err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
// HttpError render an error template
func HttpError(c *gin.Context, errorCode int) {
switch errorCode {
case http.StatusNotFound:
Static(c, path.Join(ErrorsDir, "404.jet.html"))
c.AbortWithStatus(errorCode)
return
case http.StatusBadRequest:
Static(c, path.Join(ErrorsDir, "400.jet.html"))
c.AbortWithStatus(errorCode)
return
case http.StatusInternalServerError:
Static(c, path.Join(ErrorsDir, "500.jet.html"))
c.AbortWithStatus(errorCode)
return
}
}
// Static render static templates
func Static(c *gin.Context, templateName string) {
var variables jet.VarMap
if isAdminTemplate(templateName) {
variables = NewPanelCommonvariables(c)
} else {
variables = Commonvariables(c)
}
Render(c, templateName, variables)
}
// ModelList render list models templates
func ModelList(c *gin.Context, templateName string, models interface{}, nav Navigation, search SearchForm) {
var variables jet.VarMap
if isAdminTemplate(templateName) {
variables = NewPanelCommonvariables(c)
} else {
variables = Commonvariables(c)
}
variables.Set("Models", models)
variables.Set("Navigation", nav)
variables.Set("Search", search)
Render(c, templateName, variables)
}
// Form render a template form
func Form(c *gin.Context, templateName string, form interface{}) {
var variables jet.VarMap
if isAdminTemplate(templateName) {
variables = NewPanelCommonvariables(c)
} else {
variables = Commonvariables(c)
}
variables.Set("Form", form)
Render(c, templateName, variables)
}
// Torrent render a torrent view template
func Torrent(c *gin.Context, torrent models.TorrentJSON, rootFolder *filelist.FileListFolder, captchaID string) {
variables := Commonvariables(c)
variables.Set("Torrent", torrent)
variables.Set("RootFolder", rootFolder)
variables.Set("CaptchaID", captchaID)
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)
}
// UserProfileEdit render a form to edit a profile
func UserProfileEdit(c *gin.Context, userProfile *models.User, userForm userValidator.UserForm, languages publicSettings.Languages) {
variables := Commonvariables(c)
variables.Set("UserForm", userForm)
variables.Set("Languages", languages)
userProfileBase(c, "edit.jet.html", userProfile, variables)
}
// UserProfile render a user profile
func UserProfile(c *gin.Context, userProfile *models.User) {
userProfileBase(c, "torrents.jet.html", userProfile, Commonvariables(c))
}
// UserProfileNotifications render a user profile notifications
func UserProfileNotifications(c *gin.Context, userProfile *models.User) {
userProfileBase(c, "notifications.jet.html", userProfile, Commonvariables(c))
}
// DatabaseDump render the list of database dumps template
func DatabaseDump(c *gin.Context, listDumps []models.DatabaseDumpJSON, GPGLink string) {
variables := Commonvariables(c)
variables.Set("ListDumps", listDumps)
variables.Set("GPGLink", GPGLink)
Render(c, path.Join(SiteDir, "database", "dumps.jet.html"), variables)
}
// PanelAdmin render the panel admin template index
func PanelAdmin(c *gin.Context, torrent []models.Torrent, reports []models.TorrentReportJSON, users []models.User, comments []models.Comment) {
variables := NewPanelCommonvariables(c)
variables.Set("Torrents", torrent)
variables.Set("TorrentReports", reports)
variables.Set("Users", users)
variables.Set("Comments", comments)
Render(c, path.Join(ModeratorDir, "index.jet.html"), variables)
}
func isAdminTemplate(templateName string) bool {
if templateName != "" && len(templateName) > len(ModeratorDir) {
return templateName[:5] == ModeratorDir
}
return false
}