DEV: Reduce text-size cookie duration (#19445)

Following the same behavior as the theme/color-scheme cookies, now the
cookie expires in 1 year and automatically extends on every user visit.
This commit is contained in:
Jan Cernik 2022-12-14 07:02:09 -03:00 committed by GitHub
parent ff5a0bec89
commit ca1d76f197
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import { extendThemeCookie } from "discourse/lib/theme-selector"; import { extendThemeCookie } from "discourse/lib/theme-selector";
import { extendColorSchemeCookies } from "discourse/lib/color-scheme-picker"; import { extendColorSchemeCookies } from "discourse/lib/color-scheme-picker";
import { extendTextSizeCookie } from "discourse/models/user";
import { later } from "@ember/runloop"; import { later } from "@ember/runloop";
import { isTesting } from "discourse-common/config/environment"; import { isTesting } from "discourse-common/config/environment";
@ -13,6 +14,7 @@ export default {
later(() => { later(() => {
extendThemeCookie(); extendThemeCookie();
extendColorSchemeCookies(); extendColorSchemeCookies();
extendTextSizeCookie();
}, DELAY); }, DELAY);
}, },
}; };

View File

@ -58,6 +58,19 @@ export const SECOND_FACTOR_METHODS = {
SECURITY_KEY: 3, SECURITY_KEY: 3,
}; };
const TEXT_SIZE_COOKIE_NAME = "text_size";
const COOKIE_EXPIRY_DAYS = 365;
export function extendTextSizeCookie() {
const currentValue = cookie(TEXT_SIZE_COOKIE_NAME);
if (currentValue) {
cookie(TEXT_SIZE_COOKIE_NAME, currentValue, {
path: "/",
expires: COOKIE_EXPIRY_DAYS,
});
}
}
const isForever = (dt) => moment().diff(dt, "years") < -100; const isForever = (dt) => moment().diff(dt, "years") < -100;
let userFields = [ let userFields = [
@ -1053,8 +1066,8 @@ const User = RestModel.extend({
@discourseComputed("user_option.text_size_seq", "user_option.text_size") @discourseComputed("user_option.text_size_seq", "user_option.text_size")
currentTextSize(serverSeq, serverSize) { currentTextSize(serverSeq, serverSize) {
if (cookie("text_size")) { if (cookie(TEXT_SIZE_COOKIE_NAME)) {
const [cookieSize, cookieSeq] = cookie("text_size").split("|"); const [cookieSize, cookieSeq] = cookie(TEXT_SIZE_COOKIE_NAME).split("|");
if (cookieSeq >= serverSeq) { if (cookieSeq >= serverSeq) {
return cookieSize; return cookieSize;
} }
@ -1065,12 +1078,12 @@ const User = RestModel.extend({
updateTextSizeCookie(newSize) { updateTextSizeCookie(newSize) {
if (newSize) { if (newSize) {
const seq = this.get("user_option.text_size_seq"); const seq = this.get("user_option.text_size_seq");
cookie("text_size", `${newSize}|${seq}`, { cookie(TEXT_SIZE_COOKIE_NAME, `${newSize}|${seq}`, {
path: "/", path: "/",
expires: 9999, expires: COOKIE_EXPIRY_DAYS,
}); });
} else { } else {
removeCookie("text_size", { path: "/", expires: 1 }); removeCookie(TEXT_SIZE_COOKIE_NAME, { path: "/" });
} }
}, },