Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/controllers/settings/settings.go
kilo 870b72f1e9 "No comments" message, avatar in comments, classic theme improvements, some other changes, Old Navigation (#1545)
* Update view.jet.html

* Update en-us.all.json

* Update view.jet.html

* Update CHANGELOG.md

* fix travis

* Update classic.css

* Update view.jet.html

* Update main.css

* Update classic.css

* Update classic.css

* Update classic.css

* add link to user page & avatar in comment

* stylisation for avatar in main.css

* Update classic.css

* Fix wrongly placed ::before

* Add rule list to terms of service

* import rules on register

* fix refine-container-2's inputs

* Update base.jet.html

* Update en-us.all.json

* Update CHANGELOG.md

* GenNav changes to constantly put HTML for nav arrows regardless of page

* css changes for website nav etc etc

* add OldNav global variable

* Add OldNav to test.go

* Update publicSettings.go

* change OldNav's value type

* Old navigation in settings

* add OldNav in user variables

* add oldNav input handler in settings.go

* Change OldNav's default value into false

* Create OldNav.jet.html

* Update search.jet.html

* remove character that had nothing to do here

* fix wrong variable name

* fix worng variable name and travis

* Update classic.css

* Add sort order & type to old nav

* add toString() function in test

* add toString() function

* translation string for oldnav setting

* Use translation string in settings.jet.html

* fix few html errors

* ditto

* travis fix test

* remove useless charset

* remove useless things

* add spaces before attributes

* attempt at fixing travis 2

* fix wrong variable name

* Update classic.css

* fix travis plsss
2017-09-11 19:45:39 +02:00

83 lignes
3 Kio
Go

package settingsController
import (
"net/http"
"net/url"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/controllers/router"
"github.com/NyaaPantsu/nyaa/templates"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
"github.com/NyaaPantsu/nyaa/utils/timeHelper"
"github.com/gin-gonic/gin"
)
// SeePublicSettingsHandler : Controller to view the languages and themes
func SeePublicSettingsHandler(c *gin.Context) {
_, Tlang := publicSettings.GetTfuncAndLanguageFromRequest(c)
availableLanguages := publicSettings.GetAvailableLanguages()
languagesJSON := templates.LanguagesJSONResponse{Tlang.Tag, availableLanguages}
contentType := c.Request.Header.Get("Content-Type")
if contentType == "application/json" {
c.Header("Content-Type", "application/json")
c.JSON(http.StatusOK, languagesJSON)
} else {
templates.Form(c, "site/user/public/settings.jet.html", languagesJSON)
}
}
// ChangePublicSettingsHandler : Controller for changing the current language and theme
func ChangePublicSettingsHandler(c *gin.Context) {
theme := c.PostForm("theme")
lang := c.PostForm("language")
mascot := c.PostForm("mascot")
mascotURL := c.PostForm("mascot_url")
altColors := c.PostForm("altColors")
oldNav := c.PostForm("oldNav")
messages := msg.GetMessages(c)
availableLanguages := publicSettings.GetAvailableLanguages()
if !availableLanguages.Exist(lang) {
messages.AddErrorT("errors", "language_not_available")
}
// FIXME Are the settings actually sanitized?
// Limit the mascot URL, so base64-encoded images aren't valid
if len(mascotURL) > 256 {
messages.AddErrorT("errors", "mascot_url_too_long")
}
_, err := url.Parse(mascotURL)
if err != nil {
messages.AddErrorTf("errors", "mascor_url_parse_error", err.Error())
}
// If logged in, update user settings.
user := router.GetUser(c)
if user.ID > 0 {
user.Language = lang
user.Theme = theme
user.Mascot = mascot
user.MascotURL = mascotURL
user.AltColors = altColors
user.OldNav = oldNav
user.UpdateRaw()
}
// Set cookie with http and not gin for expires (maxage not supported in <IE8)
http.SetCookie(c.Writer, &http.Cookie{Name: "lang", Value: lang, Domain: getDomainName(), Path: "/", Expires: timeHelper.FewDaysLater(365)})
http.SetCookie(c.Writer, &http.Cookie{Name: "theme", Value: theme, Domain: getDomainName(), Path: "/", Expires: timeHelper.FewDaysLater(365)})
http.SetCookie(c.Writer, &http.Cookie{Name: "mascot", Value: mascot, Domain: getDomainName(), Path: "/", Expires: timeHelper.FewDaysLater(365)})
http.SetCookie(c.Writer, &http.Cookie{Name: "mascot_url", Value: mascotURL, Domain: getDomainName(), Path: "/", Expires: timeHelper.FewDaysLater(365)})
http.SetCookie(c.Writer, &http.Cookie{Name: "oldNav", Value: oldNav, Domain: getDomainName(), Path: "/", Expires: timeHelper.FewDaysLater(365)})
c.Redirect(http.StatusSeeOther, "/")
}
func getDomainName() string {
domain := config.Get().Cookies.DomainName
if config.Get().Environment == "DEVELOPMENT" {
domain = ""
}
return domain
}