From 22672cd64ae40b123a7228a5fee3370af4ef818c Mon Sep 17 00:00:00 2001 From: Greg Price Date: Thu, 9 Nov 2023 17:06:43 -0800 Subject: [PATCH] user [nfc]: Document the new preference for tryGetUserForId over throwing --- src/users/userSelectors.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/users/userSelectors.js b/src/users/userSelectors.js index 38d0cfd7df5..42d2a0028b0 100644 --- a/src/users/userSelectors.js +++ b/src/users/userSelectors.js @@ -125,7 +125,7 @@ export const getOwnUser = (state: PerAccountState): User => { * * See `getUserForId` for a version which only ever returns a real user, * throwing if none. That makes it a bit simpler to use in contexts where - * we assume the relevant user must exist, which is true of most of the app. + * we assume the relevant user must exist. */ export const tryGetUserForId = (state: PerAccountState, userId: UserId): UserOrBot | null => getAllUsersById(state).get(userId) ?? null; @@ -136,9 +136,13 @@ export const tryGetUserForId = (state: PerAccountState, userId: UserId): UserOrB * This works for any user in this Zulip org/realm, including deactivated * users and cross-realm bots. See `getAllUsers` for details. * - * Throws if no such user exists. + * Throws if no such user exists in our data. This is therefore only + * appropriate when we can know the given user must be one we know about, + * which is uncommon. (For example, even if we're looking at a message the + * user sent: we might be a guest with limited access to users, and the + * other user might no longer be in the stream so not be in our data.) * - * See `tryGetUserForId` for a non-throwing version. + * Generally use `tryGetUserForId` instead. */ export const getUserForId = (state: PerAccountState, userId: UserId): UserOrBot => { const user = tryGetUserForId(state, userId);