FIX: Improve user timezone saving (#9230)

Based on issues identified in https://meta.discourse.org/t/improved-bookmarks-with-reminders/144542/20

* Implement the resolvedTimezone() function on the user model where we return the user's timezone if it has been set, or we guess it using moment and save it to the user using an update call if it has not yet been set. This covers the cases of users who do not log out/in often who will not get their timezone set via login. This also makes sure the guess + save is done in a non-obtrusive way not on every page -- only when it is needed.

* Before if a user's timezone was blank when they visited their profile page we were autofilling the dropdown with the guessed timezone from moment. However this was confusing as it would appear you have that timezone saved in the DB when you really didn't. Now we do not autofill the dropdown and added a button to automatically guess the current timezone to make everything more explicit.
This commit is contained in:
Martin Brennan
2020-03-24 11:39:09 +10:00
committed by GitHub
parent 4b8acce92b
commit b8b29e79ad
11 changed files with 109 additions and 25 deletions
@@ -1,10 +1,11 @@
import { currentUser } from "helpers/qunit-helpers";
import { logIn } from "helpers/qunit-helpers";
import User from "discourse/models/user";
let BookmarkController;
moduleFor("controller:bookmark", {
beforeEach() {
Discourse.currentUser = currentUser();
BookmarkController = this.subject({ currentUser: Discourse.currentUser });
logIn();
BookmarkController = this.subject({ currentUser: User.current() });
},
afterEach() {
@@ -119,7 +120,7 @@ QUnit.test(
function(assert) {
let dt = moment.tz(
"2019-12-11T11:37:16",
BookmarkController.currentUser.timezone
BookmarkController.currentUser.resolvedTimezone()
);
assert.equal(
@@ -181,14 +182,23 @@ QUnit.test(
}
);
QUnit.test(
"userHasTimezoneSet updates true/false based on whether the current user timezone is set globally",
function(assert) {
Discourse.currentUser.timezone = null;
BookmarkController.onShow();
assert.equal(BookmarkController.userHasTimezoneSet, false);
Discourse.currentUser.timezone = "Australia/Brisbane";
BookmarkController.onShow();
assert.equal(BookmarkController.userHasTimezoneSet, true);
}
);
QUnit.test("user timezone updates when the modal is shown", function(assert) {
User.current().changeTimezone(null);
let stub = sandbox.stub(moment.tz, "guess").returns("Europe/Moscow");
BookmarkController.onShow();
assert.equal(BookmarkController.userHasTimezoneSet, true);
assert.equal(
BookmarkController.userTimezone,
"Europe/Moscow",
"the user does not have their timezone set and a timezone is guessed"
);
User.current().changeTimezone("Australia/Brisbane");
BookmarkController.onShow();
assert.equal(BookmarkController.userHasTimezoneSet, true);
assert.equal(
BookmarkController.userTimezone,
"Australia/Brisbane",
"the user does their timezone set"
);
stub.restore();
});