Skip to content

Improve typing of normalizeRecipients #4122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/chat/__tests__/narrowsReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ describe('narrowsReducer', () => {

const message = {
id: 1,
type: 'private',
display_recipient: [{ email: '[email protected]' }],
flags: [],
};
Expand Down
9 changes: 0 additions & 9 deletions src/utils/__tests__/recipient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ describe('normalizeRecipients', () => {

expect(normalized).toEqual(expectedResult);
});

test('on a string input, returns same string', () => {
const recipients = '[email protected]';
const expectedResult = '[email protected]';

const normalized = normalizeRecipients(recipients);

expect(normalized).toEqual(expectedResult);
});
});

describe('normalizeRecipientsSansMe', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/narrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ export const isSearchNarrow = (narrow?: Narrow): boolean =>
/** (For search narrows, just returns false.) */
export const isMessageInNarrow = (message: Message, narrow: Narrow, ownEmail: string): boolean => {
const matchRecipients = (emails: string[]) => {
if (message.type !== 'private') {
return false;
}
const normalizedRecipients = normalizeRecipients(message.display_recipient);
const normalizedNarrow = [...emails, ownEmail].sort().join(',');
return normalizedRecipients === ownEmail || normalizedRecipients === normalizedNarrow;
Expand Down
29 changes: 20 additions & 9 deletions src/utils/recipient.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@ import type { PmRecipientUser, Message, Outbox, User } from '../types';
const filterRecipients = (recipients: PmRecipientUser[], ownUserId: number): PmRecipientUser[] =>
recipients.length === 1 ? recipients : recipients.filter(r => r.id !== ownUserId);

// TODO types: this union is confusing
export const normalizeRecipients = (recipients: $ReadOnlyArray<{ email: string, ... }> | string) =>
!Array.isArray(recipients)
? recipients
: recipients
.map(s => s.email.trim())
.filter(x => x.length > 0)
.sort()
.join(',');
/**
* "Hash" a set of recipients, represented as an unsorted list, into a string.
* (A "recipient" is usually either a `User` or a `PmRecipientUser`.)
*
* Considered as a map from sets of users to strings, this function is
* injective. Do not rely on any other property of its output.
*/
// Unfortunately we do exactly that in a lot of places.
//
// TODO: fix that. Then, possibly, make this function nontrivial to invert, so
// that we can't accidentally do that again.
export const normalizeRecipients = (recipients: $ReadOnlyArray<{ email: string, ... }>): string =>
recipients
// The purpose of the `trim` and `filter` below are obscure; they've been
// present without explanation since antiquity (commit a2419662d4,
// 0.7.1~251). As of 2020-05, they're believed to be superfluous.
.map(s => s.email.trim())
.filter(x => x.length > 0)
.sort()
.join(',');

export const normalizeRecipientsSansMe = (
recipients: $ReadOnlyArray<{ email: string, ... }>,
Expand Down