Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
channel: Simplify password change tracking.
Aman Agrawalcc8cb5432eb9
diff --git a/frontend_tests/node_tests/channel.js b/frontend_tests/node_tests/channel.jsindex 89fb4fb6cba..40934996ce6 100644--- a/frontend_tests/node_tests/channel.js+++ b/frontend_tests/node_tests/channel.js@@ -19,15 +19,6 @@ const xhr_401 = {responseText: '{"msg": "Use cannnot access XYZ"}',};-const xhr_password_changes = new WeakMap();-xhr_password_changes.set(xhr_401, 0);--const setup = mock_esm("../../static/js/setup", {- password_change_in_progress: false,- password_changes: 0,- xhr_password_changes,-});-let login_to_access_shown = false;mock_esm("../../static/js/spectators", {login_to_access: () => {@@ -44,7 +35,7 @@ set_global("window", {const reload_state = zrequire("reload_state");const channel = zrequire("channel");-const default_stub_xhr = "default-stub-xhr";+const default_stub_xhr = {"default-stub-xhr": 0};const $ = mock_jquery({});@@ -176,7 +167,7 @@ test("normal_post", () => {let orig_success_called;let orig_error_called;- const stub_xhr = "stub-xhr-normal-post";+ const stub_xhr = {"stub-xhr-normal-post": 0};test_with_mock_ajax({xhr: stub_xhr,@@ -273,12 +264,12 @@ test("authentication_error_401_password_change_in_progress", () => {// password_change_in_progress = truecheck_ajax_options(options) {page_params.is_spectator = true;- setup.password_change_in_progress = true;+ channel.set_password_change_in_progress(true);options.simulate_error();assert.ok(!login_to_access_shown);- setup.password_change_in_progress = false;+ channel.set_password_change_in_progress(false);page_params.is_spectator = false;login_to_access_shown = false;},@@ -372,7 +363,7 @@ test("retry", () => {test("too_many_pending", () => {channel.clear_for_tests();$.ajax = () => {- const xhr = "stub";+ const xhr = {stub: 0};return xhr;};diff --git a/static/js/channel.js b/static/js/channel.jsindex 04750045022..11ace4eb4df 100644--- a/static/js/channel.js+++ b/static/js/channel.js@@ -3,9 +3,19 @@ import $ from "jquery";import * as blueslip from "./blueslip";import {page_params} from "./page_params";import * as reload_state from "./reload_state";-import * as setup from "./setup";import * as spectators from "./spectators";+let password_change_in_progress = false;+export let password_changes = 0;++export function set_password_change_in_progress(value) {+ password_change_in_progress = value;+ if (!value) {+ password_changes += 1;+ }+}+export const xhr_password_changes = new WeakMap();+const pending_requests = [];export function clear_for_tests() {@@ -56,10 +66,7 @@ function call(args, idempotent) {}if (xhr.status === 401) {- if (- setup.password_change_in_progress ||- setup.xhr_password_changes.get(xhr) !== setup.password_changes- ) {+ if (password_change_in_progress || xhr.password_changes !== password_changes) {// The backend for handling password change API requests// will replace the user's session; this results in a// brief race where any API request will fail with a 401@@ -135,6 +142,13 @@ function call(args, idempotent) {const jqXHR = $.ajax(args);add_pending_request(jqXHR);++ // Remember the number of completed password changes when the+ // request was initiated. This allows us to detect race+ // situations where a password change occurred before we got a+ // response that failed due to the ongoing password change.+ jqXHR.password_changes = password_changes;+return jqXHR;}diff --git a/static/js/settings_account.js b/static/js/settings_account.jsindex e9326e711aa..15c81a41fd7 100644--- a/static/js/settings_account.js+++ b/static/js/settings_account.js@@ -21,7 +21,6 @@ import * as pill_typeahead from "./pill_typeahead";import * as settings_bots from "./settings_bots";import * as settings_data from "./settings_data";import * as settings_ui from "./settings_ui";-import * as setup from "./setup";import * as ui_report from "./ui_report";import * as user_pill from "./user_pill";import * as user_profile from "./user_profile";@@ -443,14 +442,14 @@ export function set_up() {}}- setup.set_password_change_in_progress(true);+ channel.set_password_change_in_progress(true);const opts = {success_continuation() {- setup.set_password_change_in_progress(false);+ channel.set_password_change_in_progress(false);overlays.close_modal("#change_password_modal");},error_continuation() {- setup.set_password_change_in_progress(false);+ channel.set_password_change_in_progress(false);},error_msg_element: change_password_error,failure_msg_html: null,diff --git a/static/js/setup.js b/static/js/setup.jsindex 7ccf4bb8632..9d45334f207 100644--- a/static/js/setup.js+++ b/static/js/setup.js@@ -6,17 +6,6 @@ import {page_params} from "./page_params";import * as util from "./util";// Miscellaneous early setup.-export let password_change_in_progress = false;-export let password_changes = 0;-export const xhr_password_changes = new WeakMap();--export function set_password_change_in_progress(value) {- password_change_in_progress = value;- if (!value) {- password_changes += 1;- }-}-$(() => {if (util.is_mobile()) {// Disable the tutorial; it's ugly on mobile.@@ -47,14 +36,6 @@ $(() => {return this.outerWidth(...args) || 0;};- // Remember the number of completed password changes when the- // request was initiated. This allows us to detect race- // situations where a password change occurred before we got a- // response that failed due to the ongoing password change.- $(document).ajaxSend((event, xhr) => {- xhr_password_changes.set(xhr, password_changes);- });-$.fn.expectOne = function () {if (blueslip && this.length !== 1) {blueslip.error("Expected one element in jQuery set, " + this.length + " found");