Skip to content
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

Remove redundant, stringly-typed fields from PmConversationData #4116

Closed
Closed
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
recent convs: Add users field to PmConversationData.
PmConversationData has two distinct and terrible ways of passing down
the list of users. Add a third way (presumably less terrible), which
will replace both of them.
rk-for-zulip committed May 20, 2020
commit 75bf3883ebb4cd1fe0d582bf96dfd9faa30ed63c
Original file line number Diff line number Diff line change
@@ -72,24 +72,28 @@ describe('getRecentConversations', () => {
ids: '0',
recipients: me.email,
msgId: 4,
users: [me],
unread: 1,
},
{
ids: '1',
recipients: john.email,
msgId: 3,
users: [me, john],
unread: 2,
},
{
ids: '2',
recipients: mark.email,
users: [me, mark],
msgId: 2,
unread: 1,
},
{
ids: '0,1,2',
recipients: [john.email, mark.email].join(','),
msgId: 0,
users: [me, john, mark],
unread: 1,
},
];
@@ -148,24 +152,28 @@ describe('getRecentConversations', () => {
ids: '0',
recipients: me.email,
msgId: 6,
users: [me],
unread: 1,
},
{
ids: '0,1,2',
recipients: [john.email, mark.email].join(','),
msgId: 5,
users: [me, john, mark],
unread: 1,
},
{
ids: '1',
recipients: john.email,
msgId: 4,
users: [me, john],
unread: 2,
},
{
ids: '2',
recipients: mark.email,
msgId: 3,
users: [me, mark],
unread: 1,
},
];
24 changes: 22 additions & 2 deletions src/pm-conversations/pmConversationsSelectors.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
/* @flow strict-local */
import { createSelector } from 'reselect';

import type { Message, PmConversationData, Selector, User } from '../types';
import type {
Message,
PmConversationData,
PmRecipientUser,
Selector,
User,
UserOrBot,
} from '../types';
import { getPrivateMessages } from '../message/messageSelectors';
import { getOwnUser } from '../users/userSelectors';
import { getOwnUser, getAllUsersById } from '../users/userSelectors';
import { getUnreadByPms, getUnreadByHuddles } from '../unread/unreadSelectors';
import { normalizeRecipientsSansMe, pmUnreadsKeyFromMessage } from '../utils/recipient';

export const getRecentConversations: Selector<PmConversationData[]> = createSelector(
getOwnUser,
getPrivateMessages,
getAllUsersById,
getUnreadByPms,
getUnreadByHuddles,
(
ownUser: User,
messages: Message[],
usersById: Map<number, UserOrBot>,
unreadPms: { [number]: number },
unreadHuddles: { [string]: number },
): PmConversationData[] => {
const getUser = (id: number): UserOrBot => {
const val = usersById.get(id);
if (val === undefined) {
throw new Error('getRecentConversations: unknown user ID');
}
return val;
};

const items = messages.map(msg => ({
ids: pmUnreadsKeyFromMessage(msg, ownUser.user_id),
recipients: normalizeRecipientsSansMe(msg.display_recipient, ownUser.email),
users: (msg.display_recipient: PmRecipientUser[])
.map(s => getUser(s.id))
.sort((a, b) => a.user_id - b.user_id),
msgId: msg.id,
}));

13 changes: 12 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,15 @@
import type { IntlShape } from 'react-intl';
import type { DangerouslyImpreciseStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet';

import type { Auth, Topic, Message, Reaction, ReactionType, Narrow } from './api/apiTypes';
import type {
Auth,
Topic,
Message,
Reaction,
ReactionType,
Narrow,
UserOrBot,
} from './api/apiTypes';
import type { AppStyles } from './styles/theme';
import type { ZulipVersion } from './utils/zulipVersion';

@@ -341,4 +349,7 @@ export type PmConversationData = {|

/** The count of unread messages in this conversation. */
unread: number,

/** List of all recipients (including the self-user), sorted by user ID. */
users: UserOrBot[],
|};