From 187922d51c23c9a838cba82c53097a381fe4ad84 Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Mon, 2 May 2022 15:40:23 +0400 Subject: [PATCH] FEATURE: introduce a sitewide setting for disabling suggesting weekends in time pickers (#16563) --- .../app/components/future-date-input.js | 6 ++- .../app/components/time-shortcut-picker.js | 6 ++- .../discourse/app/lib/time-shortcut.js | 7 +++- .../tests/acceptance/topic-edit-timer-test.js | 1 + .../user-preferences-notifications-test.js | 1 + .../components/time-shortcut-picker-test.js | 1 + .../tests/unit/lib/time-shortcut-test.js | 42 +++++++++++++++++-- config/locales/server.en.yml | 1 + config/site_settings.yml | 4 ++ 9 files changed, 62 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/future-date-input.js b/app/assets/javascripts/discourse/app/components/future-date-input.js index 6c325c758a0..2278fb79cb1 100644 --- a/app/assets/javascripts/discourse/app/components/future-date-input.js +++ b/app/assets/javascripts/discourse/app/components/future-date-input.js @@ -69,7 +69,11 @@ export default Component.extend({ shortcuts.push(shortcutsFactory.now()); } - shortcuts = hideDynamicTimeShortcuts(shortcuts, this.userTimezone); + shortcuts = hideDynamicTimeShortcuts( + shortcuts, + this.userTimezone, + this.siteSettings + ); return shortcuts.map((s) => { return { diff --git a/app/assets/javascripts/discourse/app/components/time-shortcut-picker.js b/app/assets/javascripts/discourse/app/components/time-shortcut-picker.js index c4b3e67e7a7..07627e149ee 100644 --- a/app/assets/javascripts/discourse/app/components/time-shortcut-picker.js +++ b/app/assets/javascripts/discourse/app/components/time-shortcut-picker.js @@ -182,7 +182,11 @@ export default Component.extend({ } else { options = defaultTimeShortcuts(userTimezone); } - options = hideDynamicTimeShortcuts(options, userTimezone); + options = hideDynamicTimeShortcuts( + options, + userTimezone, + this.siteSettings + ); let specialOptions = specialShortcutOptions(); if (this.lastCustomDate && this.lastCustomTime) { diff --git a/app/assets/javascripts/discourse/app/lib/time-shortcut.js b/app/assets/javascripts/discourse/app/lib/time-shortcut.js index 05558f3a8a8..1ecd26dfdf2 100644 --- a/app/assets/javascripts/discourse/app/lib/time-shortcut.js +++ b/app/assets/javascripts/discourse/app/lib/time-shortcut.js @@ -244,7 +244,11 @@ export function timeShortcuts(timezone) { }; } -export function hideDynamicTimeShortcuts(shortcuts, timezone) { +export function hideDynamicTimeShortcuts( + shortcuts, + timezone, + siteSettings = {} +) { const shortcutsToHide = new Set(); const _now = now(timezone); if (_now.hour() >= LATER_TODAY_CUTOFF_HOUR) { @@ -256,6 +260,7 @@ export function hideDynamicTimeShortcuts(shortcuts, timezone) { } if ( + !siteSettings.suggest_weekends_in_date_pickers || _now.day() === MOMENT_FRIDAY || _now.day() === MOMENT_SATURDAY || _now.day() === MOMENT_SUNDAY diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-edit-timer-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-edit-timer-test.js index ed49efa101e..983a3d522f6 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/topic-edit-timer-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/topic-edit-timer-test.js @@ -346,6 +346,7 @@ acceptance("Topic - Edit timer", function (needs) { }); test("Shows correct time frame options", async function (assert) { + this.siteSettings.suggest_weekends_in_date_pickers = true; updateCurrentUser({ moderator: true }); await visit("/t/internationalization-localization"); diff --git a/app/assets/javascripts/discourse/tests/acceptance/user-preferences-notifications-test.js b/app/assets/javascripts/discourse/tests/acceptance/user-preferences-notifications-test.js index 248c308c3e9..01da030999e 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/user-preferences-notifications-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/user-preferences-notifications-test.js @@ -125,6 +125,7 @@ acceptance("User Notifications - Users - Ignore User", function (needs) { }); test("Shows correct timeframe options", async function (assert) { + this.siteSettings.suggest_weekends_in_date_pickers = true; await visit("/u/eviltrout/preferences/users"); await click("div.user-notifications div div button"); diff --git a/app/assets/javascripts/discourse/tests/integration/components/time-shortcut-picker-test.js b/app/assets/javascripts/discourse/tests/integration/components/time-shortcut-picker-test.js index 443e8866694..7d5aeeeed9f 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/time-shortcut-picker-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/time-shortcut-picker-test.js @@ -38,6 +38,7 @@ discourseModule( template, beforeEach() { + this.siteSettings.suggest_weekends_in_date_pickers = true; const tuesday = "2100-06-08T08:00:00"; this.clock = fakeTime(tuesday, this.currentUser._timezone, true); }, diff --git a/app/assets/javascripts/discourse/tests/unit/lib/time-shortcut-test.js b/app/assets/javascripts/discourse/tests/unit/lib/time-shortcut-test.js index 1d8b5e1ff0c..8edc420a26b 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/time-shortcut-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/time-shortcut-test.js @@ -57,36 +57,70 @@ module( }); test("hides 'This Weekend' on Fridays, Saturdays and Sundays", function (assert) { + const siteSettings = { suggest_weekends_in_date_pickers: true }; const timezone = moment.tz.guess(); const shortcuts = defaultTimeShortcuts(timezone); this.clock = fakeTime("2100-04-22 18:00:00", timezone, true); // Thursday - let result = hideDynamicTimeShortcuts(shortcuts, timezone).mapBy("id"); + let result = hideDynamicTimeShortcuts( + shortcuts, + timezone, + siteSettings + ).mapBy("id"); assert.ok( result.includes("this_weekend"), "shows this_weekend on Thursdays" ); this.clock = fakeTime("2100-04-23 18:00:00", timezone, true); // Friday - result = hideDynamicTimeShortcuts(shortcuts, timezone).mapBy("id"); + result = hideDynamicTimeShortcuts( + shortcuts, + timezone, + siteSettings + ).mapBy("id"); assert.notOk( result.includes("this_weekend"), "doesn't show this_weekend on Fridays" ); this.clock = fakeTime("2100-04-24 18:00:00", timezone, true); // Saturday - result = hideDynamicTimeShortcuts(shortcuts, timezone).mapBy("id"); + result = hideDynamicTimeShortcuts( + shortcuts, + timezone, + siteSettings + ).mapBy("id"); assert.notOk( result.includes("this_weekend"), "doesn't show this_weekend on Saturdays" ); this.clock = fakeTime("2100-04-25 18:00:00", timezone, true); // Sunday - result = hideDynamicTimeShortcuts(shortcuts, timezone).mapBy("id"); + result = hideDynamicTimeShortcuts( + shortcuts, + timezone, + siteSettings + ).mapBy("id"); assert.notOk( result.includes("this_weekend"), "doesn't show this_weekend on Sundays" ); }); + + test("hides 'This Weekend' when disabled in site settings", function (assert) { + const siteSettings = { suggest_weekends_in_date_pickers: false }; + const timezone = moment.tz.guess(); + const shortcuts = defaultTimeShortcuts(timezone); + + this.clock = fakeTime("2100-04-19 18:00:00", timezone, true); // Monday + let result = hideDynamicTimeShortcuts( + shortcuts, + timezone, + siteSettings + ).mapBy("id"); + assert.notOk( + result.includes("this_weekend"), + "shows this_weekend on Thursdays" + ); + }); } ); diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 778bb3d35f2..80c37af5b14 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2357,6 +2357,7 @@ en: allow_changing_staged_user_tracking: "Allow a staged user's category and tag notification preferences to be changed by an admin user." use_email_for_username_and_name_suggestions: "Use the first part of email addresses for username and name suggestions. Note that this makes it easier for the public to guess full user email addresses (because a large proportion of people share common services like `gmail.com`)." use_name_for_username_suggestions: "Use a user's full name when suggesting usernames." + suggest_weekends_in_date_pickers: "Include weekends (Saturday and Sunday) in date picker suggestions (disable this if you use Discourse only on weekdays, Monday through Friday)." errors: invalid_css_color: "Invalid color. Enter a color name or hex value." diff --git a/config/site_settings.yml b/config/site_settings.yml index 752e1712ae5..9f34f609d20 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -2399,6 +2399,10 @@ uncategorized: default: false hidden: true + suggest_weekends_in_date_pickers: + client: true + default: true + user_preferences: default_email_digest_frequency: enum: "DigestEmailSiteSetting"