Skip to content

Commit

Permalink
Add support to getNarrowFromNotificationData for group narrows
Browse files Browse the repository at this point in the history
For this to work we need to parse a string of comma delimited
numbers of the recipients. This data is not yet passed to the
function; we keep the function flexible and functional by making
the parameter of `usersById` optional.`
  • Loading branch information
borisyankov committed May 1, 2018
1 parent 02088d9 commit 343dd7d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export type User = {
isBot: boolean,
};

export type UserIdMap = {
[userId: string]: User,
};

export type PresenceAggregated = {
client: string,
status: UserStatus,
Expand Down
19 changes: 18 additions & 1 deletion src/utils/__tests__/notificationsCommon-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getNarrowFromNotificationData } from '../notificationsCommon';
import { homeNarrow, topicNarrow, privateNarrow } from '../narrow';
import { homeNarrow, topicNarrow, privateNarrow, groupNarrow } from '../narrow';

describe('getNarrowFromNotificationData', () => {
test('unknown notification data returns home narrow', () => {
Expand All @@ -26,4 +26,21 @@ describe('getNarrowFromNotificationData', () => {
const narrow = getNarrowFromNotificationData(notification);
expect(narrow).toEqual(privateNarrow('mark@example.com'));
});

test('on notification for a group message returns a group narrow', () => {
const notification = {
recipient_type: 'private',
pm_users: '1,2,4',
};
const usersById = {
'1': { email: 'me@example.com' },
'2': { email: 'mark@example.com' },
'4': { email: 'john@example.com' },
};
const expectedNarrow = groupNarrow(['me@example.com', 'mark@example.com', 'john@example.com']);

const narrow = getNarrowFromNotificationData(notification, usersById);

expect(narrow).toEqual(expectedNarrow);
});
});
34 changes: 25 additions & 9 deletions src/utils/notificationsCommon.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
/* @flow */
import { homeNarrow, topicNarrow, privateNarrow } from '../utils/narrow';
import type { Notification } from '../types';

export const getNarrowFromNotificationData = (data: Notification) =>
!data || !data.recipient_type
? homeNarrow
: data.recipient_type === 'stream'
? topicNarrow(data.stream, data.topic)
: privateNarrow(data.sender_email);
import type { Notification, NotificationGroup, UserIdMap } from '../types';
import { homeNarrow, topicNarrow, privateNarrow, groupNarrow } from '../utils/narrow';

const getGroupNarrowFromNotificationData = (data: NotificationGroup, usersById: UserIdMap = {}) => {
const userIds = data.pm_users.split(',');
const userEmails = userIds.map(x => usersById[x].email);
return groupNarrow(userEmails);
};

export const getNarrowFromNotificationData = (data: Notification, usersById: UserIdMap = {}) => {
if (!data || !data.recipient_type) {
return homeNarrow;
}

if (data.recipient_type === 'stream') {
return topicNarrow(data.stream, data.topic);
}

// $FlowFixMe
if (!data.pm_users) {
return privateNarrow(data.sender_email);
}

return getGroupNarrowFromNotificationData(data, usersById);
};

0 comments on commit 343dd7d

Please sign in to comment.