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/themeToggle/themeToggle.go
kilo 1a0f8e6604 Add failsafe to prevent theme1 & theme2 being dentical at time (#1385)
* Add failsafe for theme toggle

* remove unneeded /
2017-08-22 10:49:43 +10:00

49 lignes
1,3 Kio
Go

package themeToggleController
import (
"net/http"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/utils/timeHelper"
"github.com/gin-gonic/gin"
)
// toggleThemeHandler : Controller to switch between theme1 & theme2
func toggleThemeHandler(c *gin.Context) {
theme, err := c.Cookie("theme")
if err != nil {
theme = "g"
}
theme2, err := c.Cookie("theme2")
if err != nil {
theme2 = "tomorrow"
}
if theme == theme2 {
if theme == "tomorrow" {
theme2 = "g"
}
if theme != "tomorrow" {
theme2 = "tomorrow"
}
}
//Get theme1 & theme2 value, set g.css & tomorrow.css by default
//Also check if both theme are identical which can happen at time
//Switch theme & theme2 value
http.SetCookie(c.Writer, &http.Cookie{Name: "theme", Value: theme2, Domain: getDomainName(), Path: "/", Expires: timeHelper.FewDaysLater(365)})
http.SetCookie(c.Writer, &http.Cookie{Name: "theme2", Value: theme, Domain: getDomainName(), Path: "/", Expires: timeHelper.FewDaysLater(365)})
//Redirect user to page he was beforehand
c.Redirect(http.StatusSeeOther, c.Param("redirect") + "#footer")
return
}
func getDomainName() string {
domain := config.Get().Cookies.DomainName
if config.Get().Environment == "DEVELOPMENT" {
domain = ""
}
return domain
}