Skip to content

Commit

Permalink
recipient [nfc]: Simplify pmUnreadsKeyFromMessage a bit.
Browse files Browse the repository at this point in the history
[This is a cherry-pick of d8f4512, which was reverted in 084d726;
 see preceding commit.]
  • Loading branch information
gnprice authored and rk-for-zulip committed May 1, 2020
1 parent bb578d0 commit 9d6bb8b
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/utils/recipient.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,22 @@ export const pmUnreadsKeyFromMessage = (message: Message, ownUserId?: number): s
if (message.type !== 'private') {
throw new Error('pmUnreadsKeyFromMessage: expected PM, got stream message');
}
// This includes all users in the thread; see `Message#display_recipient`.
const recipients: PmRecipientUser[] = message.display_recipient;
// This includes all users in the thread; see `Message#display_recipient`.
const userIds = recipients.map(r => r.id);

if (recipients.length === 1) {
if (userIds.length === 1) {
// Self-PM.
return recipients[0].id.toString();
} else if (recipients.length === 2) {
return userIds[0].toString();
} else if (userIds.length === 2) {
// Non-self 1:1 PM. Unlike display_recipient, leave out the self user.
if (ownUserId === undefined) {
throw new Error('getRecipientsIds: got 1:1 PM, but ownUserId omitted');
}
return recipients.filter(r => r.id !== ownUserId)[0].id.toString();
return userIds.filter(userId => userId !== ownUserId)[0].toString();
} else {
// Group PM.
return recipients
.map(s => s.id)
.sort((a, b) => a - b)
.join(',');
return userIds.sort((a, b) => a - b).join(',');
}
};

Expand Down

0 comments on commit 9d6bb8b

Please sign in to comment.