Page MenuHomePhabricator

[web] [lib] Modify useSearchUsers to include identity search and utilize identity search on web and native
ClosedPublic

Authored by will on Feb 13 2024, 2:09 PM.
Tags
None
Referenced Files
F3480931: D11058.id37639.diff
Tue, Dec 17, 12:54 AM
F3480930: D11058.id37626.diff
Tue, Dec 17, 12:54 AM
F3480929: D11058.id37562.diff
Tue, Dec 17, 12:54 AM
F3480928: D11058.id37417.diff
Tue, Dec 17, 12:54 AM
F3480927: D11058.id37113.diff
Tue, Dec 17, 12:54 AM
F3480926: D11058.id37047.diff
Tue, Dec 17, 12:54 AM
F3480925: D11058.id37043.diff
Tue, Dec 17, 12:54 AM
F3480919: D11058.id.diff
Tue, Dec 17, 12:54 AM
Subscribers

Details

Summary

This diff modifies the pre-existing useSearchUsers hook to conditionally call identity search if available. If not, it fallback to keyserver search. The useSearchUsers function is already used by native and web and so the identity search context provider is added.

Depends on D11000

Test Plan

flow and tested both callsites on local dev environment.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

web/app.react.js
425–427 ↗(On Diff #37043)

When the comm services access token isn't set in redux, the selector returns null and so the identity search provider isn't initialized. I can add a usingCommServicesAccessToken conditional but I don't think we need it.

There might be a case where diffs that add the access token to redux are landed before usingCommServicesAccessToken is set to true, in which case we'll just have an unused websocket connection

include in web/settings/relationship/add-users-list.react.js

web/settings/relationship/add-users-list.react.js
78 ↗(On Diff #37047)

What might be beneficial is to actually check in this conditional if the identity search provider websocket connection is connected. I can rebase with this change if it makes sense to do this.

will requested review of this revision.Feb 13 2024, 2:52 PM
ashoat requested changes to this revision.Feb 14 2024, 10:17 AM
ashoat added inline comments.
web/settings/relationship/add-users-list.react.js
78 ↗(On Diff #37047)

Yes that sounds like a good idea. I think we should extract this into a cross-platform hook that:

  1. Reduce code duplication here and in D11059
  2. Doesn't automatically query both, and instead is able to query conditionally
  3. If !usingCommServicesAccessToken || !identitySearchProviderWebsocketConnection, it just queries the keyserver
  4. Otherwise, it first tries to query the identity service. If the query fails, it falls back to querying the keyserver
This revision now requires changes to proceed.Feb 14 2024, 10:17 AM

[web] [lib] Add identity search function to lib and use in web

will retitled this revision from [web] Utilize identity search on web to [web] [lib] Modify useSearchUsers to include identity search and utilize identity search on web.Feb 14 2024, 10:08 PM
will edited the summary of this revision. (Show Details)

I added the search function changes to this diff to show usage in web and abandoned D11060, D11049, D11001 which implemented a separate identity search hook.

Was unsure if I should've included native changes as well in this diff as technically if this diff was landed without changes to native, native wouldn't have the identity search provider initialized when using identity search

ashoat requested changes to this revision.Feb 15 2024, 7:18 AM
In D11058#319716, @will wrote:

Was unsure if I should've included native changes as well in this diff as technically if this diff was landed without changes to native, native wouldn't have the identity search provider initialized when using identity search

Not sure I follow what you're saying here. How would native be "using identity search" without D11059?

lib/shared/search-utils.js
385 ↗(On Diff #37113)

Let's rename this to callLegacyAshoatKeyserverSearchUsers

386 ↗(On Diff #37113)

Let's renamed connected to identitySearchSocketConnected

397 ↗(On Diff #37113)

Let's rename this to searchAshoatKeyserver or something similar

405 ↗(On Diff #37113)

We should still throw if we catch here, so that we dispatch a _FAILED action instead of a _SUCCESS one. You'll need to find another way to call setSearchResults in that case. See my suggestion below

409–438 ↗(On Diff #37113)

I think we can rewrite this in a cleaner way, while also considering that we should not swallow failures. How about this:

const setSearchResultsFromServer = React.useCallback(
  (userInfos: $ReadOnlyArray<GlobalAccountUserInfo>) => {
    setSearchResults(userInfos.filter(({ id }) => id !== currentUserID));
  },
  [currentUserID],
);

React.useEffect(() => {
  if (forwardLookupSearchText.length === 0) {
    setSearchResults([]);
    return;
  }
  const searchUsersPromise = (async () => {
    if (usingCommServicesAccessToken && identitySearchSocketConnected) {
      try {
        const identitySearchResult = await callIdentitySearchUsers(
          forwardLookupSearchText,
        );
        const userInfos = identitySearchResult.map(user => ({
          id: user.userID,
          username: user.username,
          avatar: null,
        }));
        setSearchResultsFromServer(userInfos);
        return;
      } catch (err) {
        console.error(err);
      }
    }
    const { userInfos: keyserverSearchResult } = await
      callLegacyAshoatKeyserverSearchUsers(
        forwardLookupSearchText,
      );
    setSearchResultsFromServer(keyserverSearchResult);
  })();
  void dispatchActionPromise(searchUsersActionTypes, searchUsersPromise);
}, [
  setSearchResultsFromServer.
  callLegacyAshoatKeyserverSearchUsers,
  callIdentitySearchUsers,
  identitySearchSocketConnected,
  dispatchActionPromise,
  forwardLookupSearchText,
]);

This also lets us skip defining searchServer entirely.

425–427 ↗(On Diff #37113)

We should always handle the simple case first to reduce the "mental RAM" necessary for the reader

This revision now requires changes to proceed.Feb 15 2024, 7:18 AM

rebase with review changes and add native

will retitled this revision from [web] [lib] Modify useSearchUsers to include identity search and utilize identity search on web to [web] [lib] Modify useSearchUsers to include identity search and utilize identity search on web and native.Feb 21 2024, 1:54 PM
will edited the summary of this revision. (Show Details)
will planned changes to this revision.Feb 21 2024, 2:06 PM
will added inline comments.
lib/shared/search-utils.js
420 ↗(On Diff #37417)

Just clarifying, do we want to throw new Error so the Dispatch can result in an ERROR if this fails or does console.error(err) suffice? Doesn't this stop execution and prevents utilizing the keyserver search as a fallback? My other thought was to set a boolean and throw on a conditional after setSearchResultsFromServer(keyserverSearchResult); runs

lib/shared/search-utils.js
420 ↗(On Diff #37417)

Got clarification from Atul. Was initially confused on whether or not the dispatch should record an error even we resorted to fallback.

From what I understand, the dispatch will always record a success as long as it receives results irregardless if it was the fallback or identity search results or not.

will requested review of this revision.Feb 21 2024, 2:22 PM
This revision is now accepted and ready to land.Feb 21 2024, 4:35 PM

init IdentitySearchProvider on native