Skip to content

Commit

Permalink
More permissive username search
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Feb 14, 2024
1 parent 89525d3 commit 4a41e87
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 31 deletions.
3 changes: 0 additions & 3 deletions .storybook/preview.tsx
Expand Up @@ -111,9 +111,6 @@ window.SignalContext = {
platform: '',
release: '',
},
usernames: {
hash: input => Buffer.from(input),
} as any,
config: {} as any,

getHourCyclePreference: () => HourCyclePreference.UnknownPreference,
Expand Down
2 changes: 0 additions & 2 deletions test/setup-test-node.js
Expand Up @@ -5,7 +5,6 @@

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const { usernames } = require('@signalapp/libsignal-client');

const { Crypto } = require('../ts/context/Crypto');
const { setEnvironment, Environment } = require('../ts/environment');
Expand All @@ -23,7 +22,6 @@ global.window = {
performance,
SignalContext: {
crypto: new Crypto(),
usernames,
log: {
info: (...args) => console.log(...args),
warn: (...args) => console.warn(...args),
Expand Down
16 changes: 15 additions & 1 deletion ts/components/LeftPane.stories.tsx
Expand Up @@ -660,6 +660,7 @@ export function ComposeNoResults(): JSX.Element {
uuidFetchState: {},
regionCode: 'US',
searchTerm: '',
username: undefined,
},
})}
/>
Expand All @@ -677,6 +678,7 @@ export function ComposeSomeContactsNoSearchTerm(): JSX.Element {
uuidFetchState: {},
regionCode: 'US',
searchTerm: '',
username: undefined,
},
})}
/>
Expand All @@ -694,6 +696,7 @@ export function ComposeSomeContactsWithASearchTerm(): JSX.Element {
uuidFetchState: {},
regionCode: 'US',
searchTerm: 'ar',
username: undefined,
},
})}
/>
Expand All @@ -711,6 +714,7 @@ export function ComposeSomeGroupsNoSearchTerm(): JSX.Element {
uuidFetchState: {},
regionCode: 'US',
searchTerm: '',
username: undefined,
},
})}
/>
Expand All @@ -728,6 +732,7 @@ export function ComposeSomeGroupsWithSearchTerm(): JSX.Element {
uuidFetchState: {},
regionCode: 'US',
searchTerm: 'ar',
username: undefined,
},
})}
/>
Expand All @@ -745,6 +750,7 @@ export function ComposeSearchIsValidUsername(): JSX.Element {
uuidFetchState: {},
regionCode: 'US',
searchTerm: 'someone',
username: 'someone',
},
})}
/>
Expand All @@ -764,6 +770,7 @@ export function ComposeSearchIsValidUsernameFetchingUsername(): JSX.Element {
},
regionCode: 'US',
searchTerm: 'someone',
username: 'someone',
},
})}
/>
Expand All @@ -781,6 +788,7 @@ export function ComposeSearchIsValidPhoneNumber(): JSX.Element {
uuidFetchState: {},
regionCode: 'US',
searchTerm: '2125555454',
username: undefined,
},
})}
/>
Expand All @@ -800,6 +808,7 @@ export function ComposeSearchIsValidPhoneNumberFetchingPhoneNumber(): JSX.Elemen
},
regionCode: 'US',
searchTerm: '(212)5555454',
username: undefined,
},
})}
/>
Expand All @@ -817,6 +826,7 @@ export function ComposeAllKindsOfResultsNoSearchTerm(): JSX.Element {
uuidFetchState: {},
regionCode: 'US',
searchTerm: '',
username: undefined,
},
})}
/>
Expand All @@ -834,6 +844,7 @@ export function ComposeAllKindsOfResultsWithASearchTerm(): JSX.Element {
uuidFetchState: {},
regionCode: 'US',
searchTerm: 'someone',
username: 'someone',
},
})}
/>
Expand Down Expand Up @@ -912,6 +923,7 @@ export function ChooseGroupMembersPartialPhoneNumber(): JSX.Element {
ourE164: undefined,
ourUsername: undefined,
searchTerm: '+1(212) 555',
username: undefined,
regionCode: 'US',
selectedContacts: [],
},
Expand All @@ -937,6 +949,7 @@ export function ChooseGroupMembersValidPhoneNumber(): JSX.Element {
searchTerm: '+1(212) 555 5454',
regionCode: 'US',
selectedContacts: [],
username: undefined,
},
})}
/>
Expand All @@ -957,9 +970,10 @@ export function ChooseGroupMembersUsername(): JSX.Element {
isShowingMaximumGroupSizeModal: false,
ourE164: undefined,
ourUsername: undefined,
searchTerm: '@signal',
searchTerm: 'signal.01',
regionCode: 'US',
selectedContacts: [],
username: 'signal.01',
},
})}
/>
Expand Down
Expand Up @@ -70,6 +70,7 @@ const createProps = (
i18n={i18n}
lookupConversationWithoutServiceId={lookupConversationWithoutServiceId}
showUserNotFoundModal={action('showUserNotFoundModal')}
username={undefined}
/>
);
},
Expand Down
Expand Up @@ -12,7 +12,6 @@ import { omit } from 'lodash';
import type { ListRowProps } from 'react-virtualized';

import type { LocalizerType, ThemeType } from '../../../../types/Util';
import { getUsernameFromSearch } from '../../../../types/Username';
import { strictAssert, assertDev } from '../../../../util/assert';
import { refMerger } from '../../../../util/refMerger';
import { useRestoreFocus } from '../../../../hooks/useRestoreFocus';
Expand Down Expand Up @@ -59,6 +58,7 @@ export type StatePropsType = {
ourUsername: string | undefined;
searchTerm: string;
selectedContacts: ReadonlyArray<ConversationType>;
username: string | undefined;

confirmAdds: () => void;
onClose: () => void;
Expand Down Expand Up @@ -96,11 +96,10 @@ export function ChooseGroupMembersModal({
toggleSelectedContact,
lookupConversationWithoutServiceId,
showUserNotFoundModal,
username,
}: PropsType): JSX.Element {
const [focusRef] = useRestoreFocus();

const username = getUsernameFromSearch(searchTerm);

const isUsernameChecked = selectedContacts.some(
contact => contact.username === username
);
Expand Down
Expand Up @@ -126,6 +126,7 @@ const createProps = (
ourE164={undefined}
ourUsername={undefined}
showUserNotFoundModal={action('showUserNotFoundModal')}
username={undefined}
/>
);
},
Expand Down
4 changes: 2 additions & 2 deletions ts/components/leftPane/LeftPaneChooseGroupMembersHelper.tsx
Expand Up @@ -18,7 +18,6 @@ import {
} from '../AddGroupMemberErrorDialog';
import { Button } from '../Button';
import type { LocalizerType } from '../../types/Util';
import { getUsernameFromSearch } from '../../types/Username';
import type { ParsedE164Type } from '../../util/libphonenumberInstance';
import { parseAndFormatPhoneNumber } from '../../util/libphonenumberInstance';
import type { UUIDFetchStateType } from '../../util/uuidFetchState';
Expand All @@ -38,6 +37,7 @@ export type LeftPaneChooseGroupMembersPropsType = {
ourUsername: string | undefined;
searchTerm: string;
regionCode: string | undefined;
username: string | undefined;
selectedContacts: Array<ConversationType>;
};

Expand Down Expand Up @@ -80,6 +80,7 @@ export class LeftPaneChooseGroupMembersHelper extends LeftPaneHelper<LeftPaneCho
regionCode,
selectedContacts,
uuidFetchState,
username,
}: Readonly<LeftPaneChooseGroupMembersPropsType>) {
super();

Expand All @@ -93,7 +94,6 @@ export class LeftPaneChooseGroupMembersHelper extends LeftPaneHelper<LeftPaneCho
isShowingRecommendedGroupSizeModal;
this.searchTerm = searchTerm;

const username = getUsernameFromSearch(searchTerm);
const isUsernameVisible =
username !== undefined &&
username !== ourUsername &&
Expand Down
5 changes: 2 additions & 3 deletions ts/components/leftPane/LeftPaneComposeHelper.tsx
Expand Up @@ -12,7 +12,6 @@ import { SearchInput } from '../SearchInput';
import type { LocalizerType } from '../../types/Util';
import type { ParsedE164Type } from '../../util/libphonenumberInstance';
import { parseAndFormatPhoneNumber } from '../../util/libphonenumberInstance';
import { getUsernameFromSearch } from '../../types/Username';
import type { UUIDFetchStateType } from '../../util/uuidFetchState';
import {
isFetchingByUsername,
Expand All @@ -27,6 +26,7 @@ export type LeftPaneComposePropsType = {
regionCode: string | undefined;
searchTerm: string;
uuidFetchState: UUIDFetchStateType;
username: string | undefined;
};

enum TopButtons {
Expand Down Expand Up @@ -57,6 +57,7 @@ export class LeftPaneComposeHelper extends LeftPaneHelper<LeftPaneComposePropsTy
regionCode,
searchTerm,
uuidFetchState,
username,
}: Readonly<LeftPaneComposePropsType>) {
super();

Expand All @@ -65,8 +66,6 @@ export class LeftPaneComposeHelper extends LeftPaneHelper<LeftPaneComposePropsTy
this.searchTerm = searchTerm;
this.uuidFetchState = uuidFetchState;

const username = getUsernameFromSearch(this.searchTerm);

this.username = username;
this.isUsernameVisible =
Boolean(username) &&
Expand Down
5 changes: 3 additions & 2 deletions ts/components/leftPane/LeftPaneFindByUsernameHelper.tsx
Expand Up @@ -9,7 +9,6 @@ import type { Row } from '../ConversationList';
import { RowType } from '../ConversationList';
import { SearchInput } from '../SearchInput';
import type { LocalizerType } from '../../types/Util';
import { getUsernameFromSearch } from '../../types/Username';
import type { ShowConversationType } from '../../state/ducks/conversations';
import type { UUIDFetchStateType } from '../../util/uuidFetchState';
import { isFetchingByUsername } from '../../util/uuidFetchState';
Expand All @@ -21,6 +20,7 @@ import { Button } from '../Button';
export type LeftPaneFindByUsernamePropsType = {
searchTerm: string;
uuidFetchState: UUIDFetchStateType;
username: string | undefined;
};

type DoLookupActionsType = Readonly<{
Expand All @@ -39,13 +39,14 @@ export class LeftPaneFindByUsernameHelper extends LeftPaneHelper<LeftPaneFindByU
constructor({
searchTerm,
uuidFetchState,
username,
}: Readonly<LeftPaneFindByUsernamePropsType>) {
super();

this.searchTerm = searchTerm;
this.uuidFetchState = uuidFetchState;

this.username = getUsernameFromSearch(this.searchTerm);
this.username = username;
}

override getHeaderContents({
Expand Down
4 changes: 4 additions & 0 deletions ts/state/smart/ChooseGroupMembersModal.tsx
Expand Up @@ -7,6 +7,7 @@ import type { StateType } from '../reducer';
import { mapDispatchToProps } from '../actions';
import { strictAssert } from '../../util/assert';
import { lookupConversationWithoutServiceId } from '../../util/lookupConversationWithoutServiceId';
import { getUsernameFromSearch } from '../../util/Username';

import type { StatePropsType } from '../../components/conversation/conversation-details/AddGroupMembersModal/ChooseGroupMembersModal';
import { ChooseGroupMembersModal } from '../../components/conversation/conversation-details/AddGroupMembersModal/ChooseGroupMembersModal';
Expand Down Expand Up @@ -47,6 +48,8 @@ const mapStateToProps = (
return convo;
});

const { searchTerm } = props;

return {
...props,
regionCode: getRegionCode(state),
Expand All @@ -58,6 +61,7 @@ const mapStateToProps = (
ourUsername: getMe(state).username,
selectedContacts,
lookupConversationWithoutServiceId,
username: getUsernameFromSearch(searchTerm),
};
};

Expand Down
10 changes: 10 additions & 0 deletions ts/state/smart/LeftPane.tsx
Expand Up @@ -14,6 +14,7 @@ import { missingCaseError } from '../../util/missingCaseError';
import { lookupConversationWithoutServiceId } from '../../util/lookupConversationWithoutServiceId';
import { isDone as isRegistrationDone } from '../../util/registration';
import { getCountryDataForLocale } from '../../util/getCountryData';
import { getUsernameFromSearch } from '../../util/Username';
import { LeftPaneMode } from '../../types/leftPane';

import { ComposerStep, OneTimeModalState } from '../ducks/conversationsEnums';
Expand Down Expand Up @@ -182,12 +183,18 @@ const getModeSpecificProps = (
regionCode: getRegionCode(state),
searchTerm: getComposerConversationSearchTerm(state),
uuidFetchState: getComposerUUIDFetchState(state),
username: getUsernameFromSearch(
getComposerConversationSearchTerm(state)
),
};
case ComposerStep.FindByUsername:
return {
mode: LeftPaneMode.FindByUsername,
searchTerm: getComposerConversationSearchTerm(state),
uuidFetchState: getComposerUUIDFetchState(state),
username: getUsernameFromSearch(
getComposerConversationSearchTerm(state)
),
};
case ComposerStep.FindByPhoneNumber:
return {
Expand Down Expand Up @@ -215,6 +222,9 @@ const getModeSpecificProps = (
searchTerm: getComposerConversationSearchTerm(state),
selectedContacts: getComposeSelectedContacts(state),
uuidFetchState: getComposerUUIDFetchState(state),
username: getUsernameFromSearch(
getComposerConversationSearchTerm(state)
),
};
case ComposerStep.SetGroupMetadata:
return {
Expand Down
Expand Up @@ -3,15 +3,20 @@

import { assert } from 'chai';

import * as Username from '../../types/Username';
import * as Username from '../../util/Username';

describe('Username', () => {
describe('getUsernameFromSearch', () => {
const { getUsernameFromSearch } = Username;

it('matches invalid username searches', () => {
assert.isUndefined(getUsernameFromSearch('use'));
assert.isUndefined(getUsernameFromSearch('username9012345678901234567'));
assert.isUndefined(getUsernameFromSearch('us'));
assert.isUndefined(getUsernameFromSearch('123'));
});

it('matches partial username searches without discriminator', () => {
assert.strictEqual(getUsernameFromSearch('use'), 'use');
assert.strictEqual(getUsernameFromSearch('use.'), 'use.');
});

it('matches valid username searches', () => {
Expand Down

0 comments on commit 4a41e87

Please sign in to comment.