Skip to content

Commit

Permalink
narrow [nfc]: Combine 1:1 and group PMs as a single case in caseNarrow.
Browse files Browse the repository at this point in the history
Most call sites get simpler, because they were already doing the
same thing in the two cases.
  • Loading branch information
gnprice committed Dec 8, 2020
1 parent f22a2bd commit d284584
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 48 deletions.
3 changes: 1 addition & 2 deletions src/chat/narrowsSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ export const isNarrowValid: Selector<boolean, Narrow> = createSelector(
{
stream: streamName => streams.find(s => s.name === streamName) !== undefined,
topic: streamName => streams.find(s => s.name === streamName) !== undefined,
pm: email => allUsersByEmail.get(email) !== undefined,
groupPm: emails => emails.every(email => allUsersByEmail.get(email) !== undefined),
pm: emails => emails.every(email => allUsersByEmail.get(email) !== undefined),
},
() => true,
),
Expand Down
17 changes: 7 additions & 10 deletions src/outbox/outboxActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,13 @@ const extractTypeToAndSubjectFromNarrow = (
narrow: Narrow,
allUsersByEmail: Map<string, UserOrBot>,
ownUser: UserOrBot,
): DataFromNarrow => {
const forPm = emails => ({
type: 'private',
display_recipient: mapEmailsToUsers(emails, allUsersByEmail, ownUser),
subject: '',
});
return caseNarrowPartial(narrow, {
pm: email => forPm([email]),
groupPm: forPm,
): DataFromNarrow =>
caseNarrowPartial(narrow, {
pm: emails => ({
type: 'private',
display_recipient: mapEmailsToUsers(emails, allUsersByEmail, ownUser),
subject: '',
}),

// TODO: we shouldn't ever be passing a whole-stream narrow here;
// ensure we don't, then remove this case
Expand All @@ -142,7 +140,6 @@ const extractTypeToAndSubjectFromNarrow = (
subject: topic,
}),
});
};

const getContentPreview = (content: string, state: GlobalState): string => {
try {
Expand Down
8 changes: 6 additions & 2 deletions src/title/Title.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ export default class Title extends PureComponent<Props> {
allPrivate: () => <TitleSpecial code="private" color={color} />,
stream: () => <TitleStream narrow={narrow} color={color} />,
topic: () => <TitleStream narrow={narrow} color={color} />,
pm: email => <TitlePrivate email={email} color={color} />,
groupPm: () => <TitleGroup narrow={narrow} />,
pm: emails =>
emails.length === 1 ? (
<TitlePrivate email={emails[0]} color={color} />
) : (
<TitleGroup narrow={narrow} />
),
search: () => null,
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/users/usersActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ export const sendTypingStart = (narrow: Narrow) => async (

const usersByEmail = getAllUsersByEmail(getState());
const recipientIds = caseNarrowPartial(narrow, {
pm: email => [email],
groupPm: emails => emails,
pm: emails => emails,
}).map(email => {
const user = usersByEmail.get(email);
if (!user) {
Expand Down
58 changes: 27 additions & 31 deletions src/utils/narrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ export const SEARCH_NARROW = (query: string): Narrow => [

type NarrowCases<T> = {|
home: () => T,
pm: (email: string) => T,
groupPm: (emails: string[]) => T,
pm: (emails: string[]) => T,
starred: () => T,
mentioned: () => T,
allPrivate: () => T,
Expand All @@ -147,12 +146,9 @@ export function caseNarrow<T>(narrow: Narrow, cases: NarrowCases<T>): T {
case 0: return cases.home();
case 1:
switch (narrow[0].operator) {
case 'pm-with':
if (narrow[0].operand.indexOf(',') < 0) {
return cases.pm(narrow[0].operand);
} else { /* eslint-disable-line */
case 'pm-with': {
const emails = narrow[0].operand.split(',');
return cases.groupPm(emails);
return cases.pm(emails);
}
case 'is':
switch (narrow[0].operand) {
Expand Down Expand Up @@ -180,7 +176,6 @@ export function caseNarrowPartial<T>(narrow: Narrow, cases: $Shape<NarrowCases<T
({
home: err('home'),
pm: err('PM'),
groupPm: err('group PM'),
starred: err('starred'),
mentioned: err('mentions'),
allPrivate: err('all-private'),
Expand All @@ -204,7 +199,6 @@ export function caseNarrowDefault<T>(
({
home: defaultCase,
pm: defaultCase,
groupPm: defaultCase,
starred: defaultCase,
mentioned: defaultCase,
allPrivate: defaultCase,
Expand All @@ -221,10 +215,10 @@ export const isHomeNarrow = (narrow?: Narrow): boolean =>
!!narrow && caseNarrowDefault(narrow, { home: () => true }, () => false);

export const is1to1PmNarrow = (narrow?: Narrow): boolean =>
!!narrow && caseNarrowDefault(narrow, { pm: () => true }, () => false);
!!narrow && caseNarrowDefault(narrow, { pm: emails => emails.length === 1 }, () => false);

export const isGroupPmNarrow = (narrow?: Narrow): boolean =>
!!narrow && caseNarrowDefault(narrow, { groupPm: () => true }, () => false);
!!narrow && caseNarrowDefault(narrow, { pm: emails => emails.length > 1 }, () => false);

/**
* The recipients' emails if a group PM narrow; else error.
Expand All @@ -233,10 +227,17 @@ export const isGroupPmNarrow = (narrow?: Narrow): boolean =>
* to use caseNarrow.
*/
export const emailsOfGroupPmNarrow = (narrow: Narrow): string[] =>
caseNarrowPartial(narrow, { groupPm: emails => emails });
caseNarrowPartial(narrow, {
pm: emails => {
if (emails.length === 1) {
throw new Error('emailsOfGroupPmNarrow: got 1:1 narrow');
}
return emails;
},
});

export const isPmNarrow = (narrow?: Narrow): boolean =>
!!narrow && caseNarrowDefault(narrow, { pm: () => true, groupPm: () => true }, () => false);
!!narrow && caseNarrowDefault(narrow, { pm: () => true }, () => false);

export const isSpecialNarrow = (narrow?: Narrow): boolean =>
!!narrow
Expand Down Expand Up @@ -277,39 +278,34 @@ export const isMessageInNarrow = (
flags: $ReadOnlyArray<string>,
narrow: Narrow,
ownEmail: string,
): boolean => {
const matchPmRecipients = (emails: string[]) => {
if (message.type !== 'private') {
return false;
}
const recipients = recipientsOfPrivateMessage(message);
const narrowAsRecipients = emails.map(email => ({ email }));
return (
normalizeRecipientsSansMe(recipients, ownEmail)
=== normalizeRecipientsSansMe(narrowAsRecipients, ownEmail)
);
};

return caseNarrow(narrow, {
): boolean =>
caseNarrow(narrow, {
home: () => true,
stream: name => message.type === 'stream' && name === streamNameOfStreamMessage(message),
topic: (streamName, topic) =>
message.type === 'stream'
&& streamName === streamNameOfStreamMessage(message)
&& topic === message.subject,
pm: email => matchPmRecipients([email]),
groupPm: matchPmRecipients,
pm: emails => {
if (message.type !== 'private') {
return false;
}
const recipients = recipientsOfPrivateMessage(message);
const narrowAsRecipients = emails.map(email => ({ email }));
return (
normalizeRecipientsSansMe(recipients, ownEmail)
=== normalizeRecipientsSansMe(narrowAsRecipients, ownEmail)
);
},
starred: () => flags.includes('starred'),
mentioned: () => flags.includes('mentioned') || flags.includes('wildcard_mentioned'),
allPrivate: () => message.type === 'private',
search: () => false,
});
};

export const canSendToNarrow = (narrow: Narrow): boolean =>
caseNarrow(narrow, {
pm: () => true,
groupPm: () => true,
stream: () => true,
topic: () => true,
home: () => false,
Expand Down
1 change: 0 additions & 1 deletion src/webview/html/messageHeaderAsHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export default (
topic: () => 'none',

pm: () => 'none',
groupPm: () => 'none',

home: () => 'full',
starred: () => 'full',
Expand Down

0 comments on commit d284584

Please sign in to comment.