set cookie expiration to 10 months, refresh every month. #104

This commit is contained in:
Ryan Stafford 2024-07-09 02:30:15 +00:00
parent 0d0648cdba
commit b1fcb48897
2 changed files with 67 additions and 45 deletions

111
routes.go
View file

@ -16,6 +16,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
@ -312,6 +313,9 @@ func Initialize(Host string, r *http.Request) (State, error) {
state.Session = &sess state.Session = &sess
} }
} }
if age, err := strconv.Atoi(getCookie(r, "CookieAge")); err == nil {
state.CookieAge = age
}
state.Listing = getCookie(r, "DefaultListingType") state.Listing = getCookie(r, "DefaultListingType")
state.Sort = getCookie(r, "DefaultSortType") state.Sort = getCookie(r, "DefaultSortType")
state.CommentSort = getCookie(r, "DefaultCommentSortType") state.CommentSort = getCookie(r, "DefaultCommentSortType")
@ -508,6 +512,9 @@ func GetIcon(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
} }
func GetFrontpage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func GetFrontpage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
state, err := Initialize(ps.ByName("host"), r) state, err := Initialize(ps.ByName("host"), r)
if state.CookieAge != int(time.Now().Month()) {
setCookies(w, &state)
}
if err != nil { if err != nil {
Render(w, "index.html", state) Render(w, "index.html", state)
return return
@ -791,7 +798,7 @@ func setCookie(w http.ResponseWriter, host string, name string, value string) {
cookie := http.Cookie{ cookie := http.Cookie{
Name: name, Name: name,
Value: value, Value: value,
MaxAge: 86400 * 30, MaxAge: 86400 * 30 * 10,
HttpOnly: true, HttpOnly: true,
SameSite: http.SameSiteNoneMode, SameSite: http.SameSiteNoneMode,
Secure: true, Secure: true,
@ -810,6 +817,49 @@ func deleteCookie(w http.ResponseWriter, host string, name string) {
} }
http.SetCookie(w, &cookie) http.SetCookie(w, &cookie)
} }
func setCookies(w http.ResponseWriter, state *State) {
env := make(map[string]string)
env["HideThumbnails"] = "HIDE_THUMBNAILS"
env["CollapseMedia"] = "COLLAPSE_MEDIA"
env["LinksInNewWindow"] = "LINKS_IN_NEW_WINDOW"
env["DefaultSortType"] = "SORT"
env["DefaultListingType"] = "LISTING"
env["DefaultCommentSortType"] = "COMMENT_SORT"
bools := make(map[string]bool)
bools["ShowNSFW"] = state.ShowNSFW
bools["HideInstanceNames"] = state.HideInstanceNames
bools["HideThumbnails"] = state.HideThumbnails
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) != "" {
setCookie(w, "", k, "0")
} else {
deleteCookie(w, "", k)
}
}
strings := make(map[string]string)
strings["DefaultSortType"] = state.Sort
strings["DefaultListingType"] = state.Listing
strings["DefaultCommentSortType"] = state.CommentSort
for k, v := range strings {
setCookie(w, "", k, v)
}
if state.Dark == nil {
deleteCookie(w, "", "Dark")
} else if *state.Dark {
setCookie(w, "", "Dark", "1")
} else {
setCookie(w, "", "Dark", "0")
}
if state.Session != nil {
setCookie(w, state.Host, "user", state.Session.UserName+":"+strconv.Itoa(state.Session.UserID))
setCookie(w, state.Host, "jwt", state.Client.Token)
}
setCookie(w, state.Host, "CookieAge", strconv.Itoa(int(time.Now().Month())))
}
func Settings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func Settings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
state, err := Initialize(ps.ByName("host"), r) state, err := Initialize(ps.ByName("host"), r)
if err != nil { if err != nil {
@ -819,62 +869,33 @@ func Settings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
state.GetSite() state.GetSite()
switch r.Method { switch r.Method {
case "POST": case "POST":
for _, name := range []string{"DefaultSortType", "DefaultListingType", "DefaultCommentSortType"} {
deleteCookie(w, state.Host, name)
setCookie(w, "", name, r.FormValue(name))
}
dark := true dark := true
switch r.FormValue("Dark") { switch r.FormValue("Dark") {
case "1": case "1":
setCookie(w, "", "Dark", "1")
state.Dark = &dark state.Dark = &dark
case "0": case "0":
setCookie(w, "", "Dark", "0")
dark = false dark = false
state.Dark = &dark state.Dark = &dark
default: default:
deleteCookie(w, "", "Dark")
state.Dark = nil state.Dark = nil
} }
if r.FormValue("shownsfw") != "" { bools := make(map[string]*bool)
setCookie(w, "", "ShowNSFW", "1") bools["shownsfw"] = &state.ShowNSFW
state.ShowNSFW = true bools["hideInstanceNames"] = &state.HideInstanceNames
} else { bools["hideThumbnails"] = &state.HideThumbnails
deleteCookie(w, state.Host, "ShowNSFW") bools["collapseMedia"] = &state.CollapseMedia
deleteCookie(w, "", "ShowNSFW") bools["linksInNewWindow"] = &state.LinksInNewWindow
state.ShowNSFW = false for k, v := range bools {
*v = r.FormValue(k) != ""
} }
if r.FormValue("hideInstanceNames") != "" { strings := make(map[string]*string)
setCookie(w, "", "HideInstanceNames", "1") strings["DefaultListingType"] = &state.Listing
state.HideInstanceNames = true strings["DefaultSortType"] = &state.Sort
} else { strings["DefaultCommentSortType"] = &state.CommentSort
deleteCookie(w, "", "HideInstanceNames") for k, v := range strings {
state.HideInstanceNames = false *v = r.FormValue(k)
} }
if r.FormValue("hideThumbnails") != "" { setCookies(w, &state)
setCookie(w, "", "HideThumbnails", "1")
state.HideInstanceNames = true
} else {
setCookie(w, "", "HideThumbnails", "0")
state.HideInstanceNames = false
}
if r.FormValue("collapseMedia") != "" {
setCookie(w, "", "CollapseMedia", "1")
state.CollapseMedia = true
} else {
setCookie(w, "", "CollapseMedia", "0")
state.CollapseMedia = false
}
if r.FormValue("linksInNewWindow") != "" {
setCookie(w, "", "LinksInNewWindow", "1")
state.LinksInNewWindow = true
} else {
setCookie(w, "", "LinksInNewWindow", "0")
state.LinksInNewWindow = false
}
state.Listing = r.FormValue("DefaultListingType")
state.Sort = r.FormValue("DefaultSortType")
state.CommentSort = r.FormValue("DefaultCommentSortType")
// TODO save user settings // TODO save user settings
// TODO fetch user settings // TODO fetch user settings
//case "GET": //case "GET":

View file

@ -94,6 +94,7 @@ type State struct {
UserName string UserName string
User *lemmy.GetPersonDetailsResponse User *lemmy.GetPersonDetailsResponse
Now int64 Now int64
CookieAge int
XHR bool XHR bool
Op string Op string
Site *lemmy.GetSiteResponse Site *lemmy.GetSiteResponse