From d0b3801596fa15ad86ed4146d5267ee39bc14254 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Bodas Date: Thu, 22 Jul 2021 11:30:06 +0530 Subject: [PATCH] ui_init: Fix "Error: Unknown user_id in get_by_user_id: 0". For spectators (logged view), we send user_id=0 via page_params. The people module does not know about this user ID, and so throws the exception. Earlier `people.get_by_user_id` was not called on page load, but only when determining settings permissions with `settings_data.user_has_permission`. But 231c536cad3d9c33cd32fb2b12af54d037fad959 made it so that that function is always called, so we need to handle the spectator case explicitly. Co-authored-by: Gaurav Pandey --- frontend_tests/node_tests/settings_data.js | 7 +++++++ static/js/settings_data.js | 5 +++++ static/js/ui_init.js | 13 ++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/frontend_tests/node_tests/settings_data.js b/frontend_tests/node_tests/settings_data.js index 36a03320e6..3a062dde7f 100644 --- a/frontend_tests/node_tests/settings_data.js +++ b/frontend_tests/node_tests/settings_data.js @@ -147,6 +147,13 @@ function test_policy(label, policy, validation_func) { page_params.is_guest = false; assert.equal(validation_func(), true); + page_params.is_spectator = true; + page_params[policy] = settings_config.common_policy_values.by_members.code; + assert.equal(validation_func(), false); + + page_params.is_spectator = false; + assert.equal(validation_func(), true); + page_params[policy] = settings_config.common_policy_values.by_full_members.code; page_params.user_id = 30; isaac.date_joined = new Date(Date.now()); diff --git a/static/js/settings_data.js b/static/js/settings_data.js index 0d56f30fb6..e710b6a232 100644 --- a/static/js/settings_data.js +++ b/static/js/settings_data.js @@ -3,6 +3,7 @@ import * as settings_config from "./settings_config"; let user_join_date; export function initialize(current_user_join_date) { + // We keep the `user_join_date` as the present day's date if the user is a spectator user_join_date = current_user_join_date; } @@ -105,6 +106,10 @@ function user_has_permission(policy_value) { return true; } + if (page_params.is_spectator) { + return false; + } + if (page_params.is_guest) { return false; } diff --git a/static/js/ui_init.js b/static/js/ui_init.js index 37770a4c27..a3a2645aea 100644 --- a/static/js/ui_init.js +++ b/static/js/ui_init.js @@ -499,7 +499,18 @@ export function initialize_everything() { popover_menus.initialize(); people.initialize(page_params.user_id, people_params); - settings_data.initialize(people.get_by_user_id(page_params.user_id).date_joined); + + let date_joined; + if (!page_params.is_spectator) { + const user = people.get_by_user_id(page_params.user_id); + date_joined = user.date_joined; + } else { + // Spectators don't have an account, so we just prevent their + // date_joined is now. + date_joined = new Date(); + } + + settings_data.initialize(date_joined); // These components must be initialized early, because other // modules' initialization has not been audited for whether they