Skip to content

Commit

Permalink
narrow [nfc]: Add another checked-input constructor, pmNarrowFromUsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnprice committed Dec 12, 2020
1 parent e4dfdb1 commit bed2f2a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/notification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import NotificationsIOS from 'react-native-notifications';

import type { Notification } from './types';
import type { Auth, Dispatch, Identity, Narrow, UserOrBot } from '../types';
import { topicNarrow, pmNarrowFromEmail, pmNarrowFromEmails } from '../utils/narrow';
import { topicNarrow, pmNarrowFromEmail, pmNarrowFromUsers } from '../utils/narrow';
import type { JSONable, JSONableDict } from '../utils/jsonable';
import * as api from '../api';
import * as logging from '../utils/logging';
Expand Down Expand Up @@ -107,7 +107,7 @@ export const getNarrowFromNotificationData = (

const ids = data.pm_users.split(',').map(s => parseInt(s, 10));
const users = pmKeyRecipientsFromIds(ids, allUsersById, ownUserId);
return users && pmNarrowFromEmails(users.map(u => u.email));
return users === null ? null : pmNarrowFromUsers(users);
};

const getInitialNotification = async (): Promise<Notification | null> => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/internalLinks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow strict-local */
import { addBreadcrumb } from '@sentry/react-native';
import type { Narrow, Stream, UserOrBot } from '../types';
import { topicNarrow, streamNarrow, pmNarrowFromEmails, specialNarrow } from './narrow';
import { topicNarrow, streamNarrow, specialNarrow, pmNarrowFromUsers } from './narrow';
import { pmKeyRecipientsFromIds } from './recipient';
import { isUrlOnRealm } from './url';

Expand Down Expand Up @@ -130,7 +130,7 @@ export const getNarrowFromLink = (
case 'pm': {
const ids = parsePmOperand(paths[1]);
const users = pmKeyRecipientsFromIds(ids, allUsersById, ownUserId);
return users && pmNarrowFromEmails(users.map(u => u.email));
return users === null ? null : pmNarrowFromUsers(users);
}
case 'topic':
return topicNarrow(parseStreamOperand(paths[1], streamsById), parseTopicOperand(paths[3]));
Expand Down
19 changes: 16 additions & 3 deletions src/utils/narrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
recipientsOfPrivateMessage,
streamNameOfStreamMessage,
type PmKeyRecipients,
type PmKeyUsers,
} from './recipient';

export const isSameNarrow = (narrow1: Narrow, narrow2: Narrow): boolean =>
Expand Down Expand Up @@ -68,15 +69,15 @@ const pmNarrowByString = (emails: string): Narrow => [
// * OK, email: PmConversationList < PmConversationCard: the data comes
// from `getRecentConversations`, which filters and sorts by email
// * OK, email: PmConversationList < UnreadCards: ditto
//
// Known call stacks using the validating helpers -- still listed here
// because sorting can still vary, for now:
// * OK, unsorted: getNarrowFromLink. Though there's basically a bug in
// the webapp, where the URL that appears in the location bar for a
// group PM conversation excludes self -- so it's unusable if you try
// to give someone else in it a link to a particular message, say.
// * Good: getNarrowFromNotificationData: filters, and starts from
// notification's pm_users, which is sorted.
//
// Known call stacks using the validating helpers -- still listed here
// because sorting can still vary, for now:
// * Good: messageHeaderAsHtml: comes from pmKeyRecipientsFromMessage,
// which filters and sorts by ID
// * Good: getNarrowForReply: also pmKeyRecipientsFromMessage
Expand All @@ -92,10 +93,22 @@ export const pmNarrowFromEmail = (email: string): Narrow => pmNarrowFromEmails([
* The argument's type guarantees that it comes from
* `pmKeyRecipientsFromMessage` or one of its related functions. This
* ensures that we've properly either removed the self user, or not.
*
* See also `pmNarrowFromUsers`, which does the same thing with a slightly
* different form of input.
*/
export const pmNarrowFromRecipients = (recipients: PmKeyRecipients): Narrow =>
pmNarrowFromEmails(recipients.map(r => r.email));

/**
* A PM narrow, either 1:1 or group.
*
* This is just like `pmNarrowFromRecipients`, but taking a slightly
* different form of input. Use whichever one is more convenient.
*/
export const pmNarrowFromUsers = (recipients: PmKeyUsers): Narrow =>
pmNarrowFromEmails(recipients.map(r => r.email));

export const specialNarrow = (operand: string): Narrow => [
{
operator: 'is',
Expand Down
12 changes: 11 additions & 1 deletion src/utils/recipient.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export const recipientsOfPrivateMessage = (
*/
export opaque type PmKeyRecipients: $ReadOnlyArray<PmRecipientUser> = $ReadOnlyArray<PmRecipientUser>;

/**
* A set of users identifying a PM conversation, as per pmKeyRecipientsFromMessage.
*
* This is just like `PmKeyRecipients` but with a different selection of
* details about the users. See there for discussion.
*
* See also `pmNarrowFromUsers`, which requires a value of this type.
*/
export opaque type PmKeyUsers: $ReadOnlyArray<UserOrBot> = $ReadOnlyArray<UserOrBot>;

// Filter a list of PM recipients in the quirky way that we do.
//
// Specifically: all users, except the self-user, except if it's the
Expand Down Expand Up @@ -212,7 +222,7 @@ export const pmKeyRecipientsFromIds = (
userIds: number[],
allUsersById: Map<number, UserOrBot>,
ownUserId: number,
): UserOrBot[] | null => {
): PmKeyUsers | null => {
const users = [];
for (const id of userIds) {
if (id === ownUserId && userIds.length > 1) {
Expand Down

0 comments on commit bed2f2a

Please sign in to comment.