Skip to content

Commit

Permalink
dont set default cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
rystaf committed Jul 10, 2024
1 parent 313265a commit 9775db7
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,17 @@ func RegReplace(input string, match string, replace string) string {
return re.ReplaceAllString(input, replace)
}

func getenv(key, fallback string) string {
func getenv(key string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
switch key {
case "SORT", "COMMENT_SORT":
return "Hot"
case "LISTING":
return "All"
default:
return ""
}
}
return value
}
Expand Down Expand Up @@ -343,13 +350,13 @@ func Initialize(Host string, r *http.Request) (State, error) {
}
state.ParseQuery(r.URL.RawQuery)
if state.Sort == "" {
state.Sort = getenv("SORT", "Hot")
state.Sort = getenv("SORT")
}
if state.CommentSort == "" {
state.CommentSort = getenv("COMMENT_SORT", "Hot")
state.CommentSort = getenv("COMMENT_SORT")
}
if state.Listing == "" || state.Session == nil && state.Listing == "Subscribed" {
state.Listing = getenv("LISTING", "All")
state.Listing = getenv("LISTING")
}
if linksInNewWindow := getCookie(r, "LinksInNewWindow"); linksInNewWindow != "" {
state.LinksInNewWindow = linksInNewWindow != "0"
Expand Down Expand Up @@ -832,10 +839,12 @@ func setCookies(w http.ResponseWriter, state *State) {
bools["CollapseMedia"] = state.CollapseMedia
bools["LinksInNewWindow"] = state.LinksInNewWindow
for k, v := range bools {
if v {
setCookie(w, "", k, "1")
} else if e, ok := env[k]; ok && os.Getenv(e) != "" {
e, ok := env[k]
ev := (ok && os.Getenv(e) != "")
if !v && ev {
setCookie(w, "", k, "0")
} else if v && !ev {
setCookie(w, "", k, "1")
} else {
deleteCookie(w, "", k)
}
Expand All @@ -845,7 +854,11 @@ func setCookies(w http.ResponseWriter, state *State) {
strings["DefaultListingType"] = state.Listing
strings["DefaultCommentSortType"] = state.CommentSort
for k, v := range strings {
setCookie(w, "", k, v)
if e, ok := env[k]; ok && getenv(e) == v {
deleteCookie(w, "", k)
} else {
setCookie(w, "", k, v)
}
}
if state.Dark == nil {
deleteCookie(w, "", "Dark")
Expand Down

0 comments on commit 9775db7

Please sign in to comment.